01: package org.concern.controller.jmx;
02:
03: import org.apache.commons.logging.Log;
04: import org.apache.commons.logging.LogFactory;
05: import org.hibernate.jmx.HibernateService;
06: import org.hibernate.cfg.Configuration;
07: import org.hibernate.cfg.Environment;
08: import org.hibernate.util.NamingHelper;
09: import org.hibernate.HibernateException;
10: import org.hibernate.tool.hbm2ddl.SchemaExport;
11: import org.hibernate.tool.hbm2ddl.SchemaUpdate;
12:
13: import javax.naming.*;
14:
15: /**
16: * @author holger
17: */
18: public class ExposeMappingHibernateService extends HibernateService
19: implements ExposeMappingHibernateServiceMBean {
20:
21: private static Log log = LogFactory
22: .getLog(ExposeMappingHibernateService.class.getName());
23: String boundName;
24: String currentSessionContextClass;
25:
26: protected Configuration configuration;
27: private int defaultBatchFetchSize;
28:
29: public ExposeMappingHibernateService() {
30: }
31:
32: public void start() throws HibernateException {
33: boundName = getJndiName();
34: try {
35: if (currentSessionContextClass != null)
36: super .setProperty(
37: Environment.CURRENT_SESSION_CONTEXT_CLASS,
38: currentSessionContextClass);
39:
40: if (defaultBatchFetchSize != -1)
41: super .setProperty(Environment.DEFAULT_BATCH_FETCH_SIZE,
42: "" + defaultBatchFetchSize);
43:
44: configuration = buildConfiguration();
45: log.info("starting service at JNDI name: " + boundName);
46: configuration.buildSessionFactory();
47: } catch (HibernateException he) {
48: log
49: .info("Could not build SessionFactory using the MBean classpath - will try again using client classpath: "
50: + he.getMessage());
51: log.debug("Error was", he);
52: }
53: try {
54: Context ctx = NamingHelper
55: .getInitialContext(getProperties());
56: NamingHelper.bind(ctx, boundName + "Configuration",
57: configuration);
58: } catch (NamingException e) {
59: e.printStackTrace();
60: }
61: }
62:
63: public String getCurrentSessionContextClass() {
64: return currentSessionContextClass;
65: }
66:
67: public void setCurrentSessionContextClass(
68: String currentSessionContextClass) {
69: this .currentSessionContextClass = currentSessionContextClass;
70: }
71:
72: public int getDefaultBatchFetchSize() {
73: return defaultBatchFetchSize;
74: }
75:
76: public void setDefaultBatchFetchSize(int defaultBatchFetchSize) {
77: this .defaultBatchFetchSize = defaultBatchFetchSize;
78: }
79:
80: public void updateSchema() {
81: new SchemaUpdate(buildConfiguration()).execute(false, true);
82: }
83: }
|