1 package org.hibernate.jmx; 3 4 import java.util.Properties ; 5 import java.util.Map ; 6 7 import javax.naming.InitialContext ; 8 9 import org.apache.commons.logging.Log; 10 import org.apache.commons.logging.LogFactory; 11 12 import org.hibernate.HibernateException; 13 import org.hibernate.SessionFactory; 14 import org.hibernate.cfg.Environment; 15 import org.hibernate.tool.hbm2ddl.SchemaExport; 16 import org.hibernate.util.NamingHelper; 17 import org.hibernate.util.ExternalSessionFactoryConfig; 18 19 20 29 public class HibernateService extends ExternalSessionFactoryConfig implements HibernateServiceMBean { 30 31 private static final Log log = LogFactory.getLog(HibernateServiceMBean.class); 32 33 private String boundName; 34 private Properties properties = new Properties (); 35 36 37 public void start() throws HibernateException { 38 boundName = getJndiName(); 39 try { 40 buildSessionFactory(); 41 } 42 catch (HibernateException he) { 43 log.info( "Could not build SessionFactory using the MBean classpath - will try again using client classpath: " + he.getMessage() ); 44 log.debug("Error was", he); 45 new SessionFactoryStub(this); 46 } 47 } 48 49 public void stop() { 50 log.info("stopping service"); 51 try { 52 InitialContext context = NamingHelper.getInitialContext( buildProperties() ); 53 ( (SessionFactory) context.lookup(boundName) ).close(); 54 } 56 catch (Exception e) { 57 log.warn("exception while stopping service", e); 58 } 59 } 60 61 SessionFactory buildSessionFactory() throws HibernateException { 62 log.info( "starting service at JNDI name: " + boundName ); 63 log.info( "service properties: " + properties ); 64 return buildConfiguration().buildSessionFactory(); 65 } 66 67 protected Map getExtraProperties() { 68 return properties; 69 } 70 71 public String getTransactionStrategy() { 72 return getProperty(Environment.TRANSACTION_STRATEGY); 73 } 74 75 public void setTransactionStrategy(String txnStrategy) { 76 setProperty(Environment.TRANSACTION_STRATEGY, txnStrategy); 77 } 78 79 public String getUserTransactionName() { 80 return getProperty(Environment.USER_TRANSACTION); 81 } 82 83 public void setUserTransactionName(String utName) { 84 setProperty(Environment.USER_TRANSACTION, utName); 85 } 86 87 public String getTransactionManagerLookupStrategy() { 88 return getProperty(Environment.TRANSACTION_MANAGER_STRATEGY); 89 } 90 91 public void setTransactionManagerLookupStrategy(String lkpStrategy) { 92 setProperty(Environment.TRANSACTION_MANAGER_STRATEGY, lkpStrategy); 93 } 94 95 public String getPropertyList() { 96 return buildProperties().toString(); 97 } 98 99 public String getProperty(String property) { 100 return properties.getProperty(property); 101 } 102 103 public void setProperty(String property, String value) { 104 properties.setProperty(property, value); 105 } 106 107 public void dropSchema() { 108 new SchemaExport( buildConfiguration() ).drop(false, true); 109 } 110 111 public void createSchema() { 112 new SchemaExport( buildConfiguration() ).create(false, true); 113 } public String getName() { 114 return getProperty(Environment.SESSION_FACTORY_NAME); 115 } 116 117 public String getDatasource() { 118 return getProperty(Environment.DATASOURCE); 119 } 120 121 public void setDatasource(String datasource) { 122 setProperty(Environment.DATASOURCE, datasource); 123 } 124 125 public String getJndiName() { 126 return getProperty(Environment.SESSION_FACTORY_NAME); 127 } 128 129 public void setJndiName(String jndiName) { 130 setProperty(Environment.SESSION_FACTORY_NAME, jndiName); 131 } 132 133 public String getUserName() { 134 return getProperty(Environment.USER); 135 } 136 137 public void setUserName(String userName) { 138 setProperty(Environment.USER, userName); 139 } 140 141 public String getPassword() { 142 return getProperty(Environment.PASS); 143 } 144 145 public void setPassword(String password) { 146 setProperty(Environment.PASS, password); 147 } 148 149 public void setFlushBeforeCompletionEnabled(String enabled) { 150 setProperty(Environment.FLUSH_BEFORE_COMPLETION, enabled); 151 } 152 153 public String getFlushBeforeCompletionEnabled() { 154 return getProperty(Environment.FLUSH_BEFORE_COMPLETION); 155 } 156 157 public void setAutoCloseSessionEnabled(String enabled) { 158 setProperty(Environment.AUTO_CLOSE_SESSION, enabled); 159 } 160 161 public String getAutoCloseSessionEnabled() { 162 return getProperty(Environment.AUTO_CLOSE_SESSION); 163 } 164 165 public Properties getProperties() { 166 return buildProperties(); 167 } 168 } 169 | Popular Tags |