View Javadoc

1   /**
2    *    Copyright 2011 meltmedia
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *        http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.xchain.namespaces.hibernate;
17  
18  import javax.xml.namespace.QName;
19  
20  import org.apache.commons.jxpath.JXPathContext;
21  import org.hibernate.Session;
22  import org.hibernate.Transaction;
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  import org.xchain.annotations.Attribute;
26  import org.xchain.annotations.AttributeType;
27  import org.xchain.annotations.Element;
28  import org.xchain.framework.hibernate.HibernateLifecycle;
29  import org.xchain.impl.ChainImpl;
30  
31  /**
32   * @author Josh Kennedy
33   */
34  @Element(localName="debug-session")
35  public abstract class DebugSessionCommand extends ChainImpl {
36  	public static Logger log = LoggerFactory.getLogger(DebugSessionCommand.class);
37  	
38  	public boolean execute(JXPathContext context) throws Exception {
39  		try {
40  			Session session = null;
41  		    Transaction transaction = null;
42  	
43  		    String message = getMessage(context);
44  		    log.debug(message);
45  		    // get the session from the context.
46  		    QName name = getName(context);
47  		    session = HibernateLifecycle.getCurrentSession(name);
48  		    
49  		    log.debug("Getting session for {}", name.toString());
50  		    
51  		    // if we didn't find the session, then bail out.
52  		    if( session == null ) {
53  		      throw new IllegalStateException("Session not found.");
54  		    }
55  		    
56  		    // Dump Session data to log
57  		    logSession(session);
58  		    
59  		    transaction = session.getTransaction();
60  		    
61  		    if (transaction != null) {
62  		    	log.debug("Transaction Active: {}", transaction.isActive());
63  		    }
64  		}
65  		catch (Exception e) {
66  			log.error("Error inspecting Session: {}", e.getMessage(), e);
67  		}
68  	    
69  		return false;
70  	}
71  	
72  	private void logSession(Session session) {
73  		log.debug("Connected, Dirty, Open:  {}", new boolean[]{session.isConnected(), session.isDirty(), session.isOpen()});
74  		log.debug("CacheMode:  {}", session.getCacheMode().toString());
75  		log.debug("EntityMode: {}", session.getEntityMode().toString());
76  		log.debug("FlushMode:  {}", session.getFlushMode().toString());
77  	}
78  	
79  	@Attribute(localName="name",
80  	           type=AttributeType.QNAME,
81  	           defaultValue= "{http://www.xchain.org/hibernate}session-factory")
82  	public abstract QName getName(JXPathContext context);
83  	
84  	@Attribute(localName="message", type=AttributeType.ATTRIBUTE_VALUE_TEMPLATE, defaultValue="")
85  	public abstract String getMessage(JXPathContext context);
86  }