From: Urberg, J. <ju...@ve...> - 2002-07-15 13:02:55
|
I have a first shot at a JBoss MBean to set up Hibernate. You might want to modify it a bit so it works with the configure functionality. To compile, you need jboss.jar and jmxri.jar in your class path. Jar it up and put in %JBOSS_DIST%/lib/ext along with the hibernate jars. Add the following to jboss.jmcl and your good to go: <mbean code="hibernate.HibernateIntegration" name="DefaultDomain:service=Hibernate,name={your factory name here}"> <attribute name="MappingFile">../conf/catalina/mapping.hbm.xml</attribute> <attribute name="Datasource">{your datasource}</attribute> <attribute name="JndiName">{your factory name here}</attribute> <attribute name="UserName"></attribute> <attribute name="Password"></attribute> <attribute name="ShowSql">false</attribute> <attribute name="UseOuterJoin">true</attribute> </mbean> HibernateIntegrationMBean.java ------------------------------- import org.jboss.util.ServiceMBean; /** * Interface definition for JMX Bean that manages Hibernate */ public interface HibernateIntegrationMBean extends ServiceMBean { public String getMappingFile(); public void setMappingFile(String aMappingFile); public String getDatasource(); public void setDatasource(String aDatasource); public boolean getShowSql(); public void setShowSql(boolean aFlag); public String getJndiName(); public void setJndiName(String aJndiName); public boolean getUseOuterJoin(); public void setUseOuterJoin(boolean aFlag); public String getUserName(); public void setUserName(String aUserName); public String getPassword(); public void setPassword(String aPassword); } HibernateIntegration.java ------------------------- import java.io.Serializable; import java.sql.Connection; import java.sql.SQLException; import java.util.Properties; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.Name; import javax.naming.NameNotFoundException; import javax.naming.NamingException; import org.jboss.util.ServiceMBeanSupport; import cirrus.hibernate.Databinder; import cirrus.hibernate.Datastore; import cirrus.hibernate.Hibernate; import cirrus.hibernate.HibernateException; import cirrus.hibernate.Session; import cirrus.hibernate.SessionFactory; /** * Implementation of Hibernate mbean. Creates a Hibernate Session Factory and * binds it to the specified JNDI name. */ public class HibernateIntegration extends ServiceMBeanSupport implements HibernateIntegrationMBean, SessionFactory, Serializable { protected String mappingFile; protected String datasource; protected String jndiName; protected String userName; protected String password; protected boolean showSql; protected boolean useOuterJoin; protected SessionFactory factory; /** * The name of the current bean * @return String */ public String getName() { return "Hibernate Session Factory (" + jndiName + ")"; } /** * The Hibernate mapping file * @return String */ public String getMappingFile() { return mappingFile; } /** * The Hibernate mapping file * @param String */ public void setMappingFile(String aMappingFile) { mappingFile = aMappingFile; } /** * The JNDI name of the datasource to use in this session factory * @return String */ public String getDatasource() { return datasource; } /** * The JNDI name of the datasource to use in this session factory * @param String */ public void setDatasource(String aDatasource) { datasource = aDatasource; } /** * Show the SQL in the server log * @return boolean */ public boolean getShowSql() { return showSql; } /** * Show the SQL in the server log * @param boolean */ public void setShowSql(boolean aFlag) { showSql = aFlag; } /** * The JNDI name to bind the session factory to * @return String */ public String getJndiName() { return jndiName; } /** * The JNDI name to bind the session factory to * @param String */ public void setJndiName(String aJndiName) { jndiName = aJndiName; } /** * This datasource supports outer joins * @return boolean */ public boolean getUseOuterJoin() { return useOuterJoin; } /** * This datasource supports outer joins * @param boolean */ public void setUseOuterJoin(boolean aFlag) { useOuterJoin = aFlag; } /** * Log into the database with this name * @return String */ public String getUserName() { return userName; } /** * Log into the database with this name * @param String */ public void setUserName(String aUserName) { userName = aUserName; } /** * Log into the database with this password * @return String */ public String getPassword() { return password; } /** * Log into the database with this password * @param String */ public void setPassword(String aPassword) { password = aPassword; } /** * Create the SessionFactory and bind to the jndi name on startup */ public void startService() throws Exception { Datastore ds = Hibernate.createDatastore().storeFile(mappingFile); Properties props = new Properties(); props.put("hibernate.datasource", datasource); props.put("hibernate.username", userName); props.put("hibernate.password", password); props.put("hibernate.show_sql", String.valueOf(showSql)); props.put("hibernate.use_outer_join", String.valueOf(useOuterJoin)); factory = ds.buildSessionFactory(props); bind(new InitialContext(), "java:/" + jndiName, this); log.info("Hibernate Session Factory bound to java:/" + jndiName); } /** * Unbind the Session factory from JNDI */ public void stopService() { try { new InitialContext().unbind("java:/" + jndiName); } catch (NamingException e) {} } /** * @see cirrus.hibernate.SessionFactory#openDatabinder() */ public Databinder openDatabinder() throws HibernateException { return factory.openDatabinder(); } /** * @see cirrus.hibernate.SessionFactory#openSession() */ public Session openSession() throws SQLException { return factory.openSession(); } /** * @see cirrus.hibernate.SessionFactory#openSession(Connection) */ public Session openSession(Connection connection) { return factory.openSession(connection); } /** * Bind val to name in ctx, and make sure that all intermediate contexts exist * @param Context * @param Name * @param Object * @throws NamingException */ protected void bind(Context ctx, String name, Object val) throws NamingException { Name n = ctx.getNameParser("").parse(name); while (n.size() > 1) { String ctxName = n.get(0); try { ctx = (Context)ctx.lookup(ctxName); } catch (NameNotFoundException e) { ctx = ctx.createSubcontext(ctxName); } n = n.getSuffix(1); } ctx.bind(n.get(0), val); } } |