KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > jmx > HibernateService


1 //$Id: HibernateService.java,v 1.7 2005/03/17 10:48:03 turin42 Exp $
2
package org.hibernate.jmx;
3
4 import java.util.Properties JavaDoc;
5 import java.util.Map JavaDoc;
6
7 import javax.naming.InitialContext JavaDoc;
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 /**
21  * Implementation of <tt>HibernateServiceMBean</tt>. Creates a
22  * <tt>SessionFactory</tt> and binds it to the specified JNDI name.<br>
23  * <br>
24  * All mapping documents are loaded as resources by the MBean.
25  * @see HibernateServiceMBean
26  * @see org.hibernate.SessionFactory
27  * @author John Urberg, Gavin King
28  */

29 public class HibernateService extends ExternalSessionFactoryConfig implements HibernateServiceMBean {
30
31     private static final Log log = LogFactory.getLog(HibernateServiceMBean.class);
32
33     private String JavaDoc boundName;
34     private Properties JavaDoc properties = new Properties JavaDoc();
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 JavaDoc context = NamingHelper.getInitialContext( buildProperties() );
53             ( (SessionFactory) context.lookup(boundName) ).close();
54             //context.unbind(boundName);
55
}
56         catch (Exception JavaDoc 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 JavaDoc getExtraProperties() {
68         return properties;
69     }
70
71     public String JavaDoc getTransactionStrategy() {
72         return getProperty(Environment.TRANSACTION_STRATEGY);
73     }
74
75     public void setTransactionStrategy(String JavaDoc txnStrategy) {
76         setProperty(Environment.TRANSACTION_STRATEGY, txnStrategy);
77     }
78
79     public String JavaDoc getUserTransactionName() {
80         return getProperty(Environment.USER_TRANSACTION);
81     }
82
83     public void setUserTransactionName(String JavaDoc utName) {
84         setProperty(Environment.USER_TRANSACTION, utName);
85     }
86
87     public String JavaDoc getTransactionManagerLookupStrategy() {
88         return getProperty(Environment.TRANSACTION_MANAGER_STRATEGY);
89     }
90
91     public void setTransactionManagerLookupStrategy(String JavaDoc lkpStrategy) {
92         setProperty(Environment.TRANSACTION_MANAGER_STRATEGY, lkpStrategy);
93     }
94
95     public String JavaDoc getPropertyList() {
96         return buildProperties().toString();
97     }
98
99     public String JavaDoc getProperty(String JavaDoc property) {
100         return properties.getProperty(property);
101     }
102
103     public void setProperty(String JavaDoc property, String JavaDoc 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 JavaDoc getName() {
114         return getProperty(Environment.SESSION_FACTORY_NAME);
115     }
116
117     public String JavaDoc getDatasource() {
118         return getProperty(Environment.DATASOURCE);
119     }
120
121     public void setDatasource(String JavaDoc datasource) {
122         setProperty(Environment.DATASOURCE, datasource);
123     }
124
125     public String JavaDoc getJndiName() {
126         return getProperty(Environment.SESSION_FACTORY_NAME);
127     }
128
129     public void setJndiName(String JavaDoc jndiName) {
130         setProperty(Environment.SESSION_FACTORY_NAME, jndiName);
131     }
132
133     public String JavaDoc getUserName() {
134         return getProperty(Environment.USER);
135     }
136
137     public void setUserName(String JavaDoc userName) {
138         setProperty(Environment.USER, userName);
139     }
140
141     public String JavaDoc getPassword() {
142         return getProperty(Environment.PASS);
143     }
144
145     public void setPassword(String JavaDoc password) {
146         setProperty(Environment.PASS, password);
147     }
148
149     public void setFlushBeforeCompletionEnabled(String JavaDoc enabled) {
150         setProperty(Environment.FLUSH_BEFORE_COMPLETION, enabled);
151     }
152
153     public String JavaDoc getFlushBeforeCompletionEnabled() {
154         return getProperty(Environment.FLUSH_BEFORE_COMPLETION);
155     }
156
157     public void setAutoCloseSessionEnabled(String JavaDoc enabled) {
158         setProperty(Environment.AUTO_CLOSE_SESSION, enabled);
159     }
160
161     public String JavaDoc getAutoCloseSessionEnabled() {
162         return getProperty(Environment.AUTO_CLOSE_SESSION);
163     }
164
165     public Properties JavaDoc getProperties() {
166         return buildProperties();
167     }
168 }
169
Popular Tags