Author: steve.ebersole(a)jboss.com
Date: 2010-07-07 16:00:04 -0400 (Wed, 07 Jul 2010)
New Revision: 19911
Added:
core/trunk/core/src/main/java/org/hibernate/jmx/
core/trunk/core/src/main/java/org/hibernate/jmx/HibernateService.java
core/trunk/core/src/main/java/org/hibernate/jmx/HibernateServiceMBean.java
core/trunk/core/src/main/java/org/hibernate/jmx/SessionFactoryStub.java
core/trunk/core/src/main/java/org/hibernate/jmx/StatisticsService.java
core/trunk/core/src/main/java/org/hibernate/jmx/StatisticsServiceMBean.java
core/trunk/core/src/main/java/org/hibernate/jmx/package.html
core/trunk/core/src/test/java/org/hibernate/jmx/
core/trunk/core/src/test/java/org/hibernate/jmx/Entity.hbm.xml
core/trunk/core/src/test/java/org/hibernate/jmx/Entity.java
core/trunk/core/src/test/java/org/hibernate/jmx/TrivialTest.java
Removed:
core/trunk/jmx/
Modified:
core/trunk/pom.xml
Log:
HHH-5358 - Merge jmx module back into core
Copied: core/trunk/core/src/main/java/org/hibernate/jmx/HibernateService.java (from rev
19909, core/trunk/jmx/src/main/java/org/hibernate/jmx/HibernateService.java)
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/jmx/HibernateService.java
(rev 0)
+++ core/trunk/core/src/main/java/org/hibernate/jmx/HibernateService.java 2010-07-07
20:00:04 UTC (rev 19911)
@@ -0,0 +1,168 @@
+//$Id: HibernateService.java 6100 2005-03-17 10:48:03Z turin42 $
+package org.hibernate.jmx;
+
+import java.util.Properties;
+import java.util.Map;
+
+import javax.naming.InitialContext;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.hibernate.HibernateException;
+import org.hibernate.SessionFactory;
+import org.hibernate.cfg.Environment;
+import org.hibernate.tool.hbm2ddl.SchemaExport;
+import org.hibernate.util.NamingHelper;
+import org.hibernate.util.ExternalSessionFactoryConfig;
+
+
+/**
+ * Implementation of <tt>HibernateServiceMBean</tt>. Creates a
+ * <tt>SessionFactory</tt> and binds it to the specified JNDI
name.<br>
+ * <br>
+ * All mapping documents are loaded as resources by the MBean.
+ * @see HibernateServiceMBean
+ * @see org.hibernate.SessionFactory
+ * @author John Urberg, Gavin King
+ */
+public class HibernateService extends ExternalSessionFactoryConfig implements
HibernateServiceMBean {
+
+ private static final Logger log = LoggerFactory.getLogger(HibernateServiceMBean.class);
+
+ private String boundName;
+ private Properties properties = new Properties();
+
+
+ public void start() throws HibernateException {
+ boundName = getJndiName();
+ try {
+ buildSessionFactory();
+ }
+ catch (HibernateException he) {
+ log.info( "Could not build SessionFactory using the MBean classpath - will try
again using client classpath: " + he.getMessage() );
+ log.debug("Error was", he);
+ new SessionFactoryStub(this);
+ }
+ }
+
+ public void stop() {
+ log.info("stopping service");
+ try {
+ InitialContext context = NamingHelper.getInitialContext( buildProperties() );
+ ( (SessionFactory) context.lookup(boundName) ).close();
+ //context.unbind(boundName);
+ }
+ catch (Exception e) {
+ log.warn("exception while stopping service", e);
+ }
+ }
+
+ SessionFactory buildSessionFactory() throws HibernateException {
+ log.info( "starting service at JNDI name: " + boundName );
+ log.info( "service properties: " + properties );
+ return buildConfiguration().buildSessionFactory();
+ }
+
+ protected Map getExtraProperties() {
+ return properties;
+ }
+
+ public String getTransactionStrategy() {
+ return getProperty(Environment.TRANSACTION_STRATEGY);
+ }
+
+ public void setTransactionStrategy(String txnStrategy) {
+ setProperty(Environment.TRANSACTION_STRATEGY, txnStrategy);
+ }
+
+ public String getUserTransactionName() {
+ return getProperty(Environment.USER_TRANSACTION);
+ }
+
+ public void setUserTransactionName(String utName) {
+ setProperty(Environment.USER_TRANSACTION, utName);
+ }
+
+ public String getTransactionManagerLookupStrategy() {
+ return getProperty(Environment.TRANSACTION_MANAGER_STRATEGY);
+ }
+
+ public void setTransactionManagerLookupStrategy(String lkpStrategy) {
+ setProperty(Environment.TRANSACTION_MANAGER_STRATEGY, lkpStrategy);
+ }
+
+ public String getPropertyList() {
+ return buildProperties().toString();
+ }
+
+ public String getProperty(String property) {
+ return properties.getProperty(property);
+ }
+
+ public void setProperty(String property, String value) {
+ properties.setProperty(property, value);
+ }
+
+ public void dropSchema() {
+ new SchemaExport( buildConfiguration() ).drop(false, true);
+ }
+
+ public void createSchema() {
+ new SchemaExport( buildConfiguration() ).create(false, true);
+ } public String getName() {
+ return getProperty(Environment.SESSION_FACTORY_NAME);
+ }
+
+ public String getDatasource() {
+ return getProperty(Environment.DATASOURCE);
+ }
+
+ public void setDatasource(String datasource) {
+ setProperty(Environment.DATASOURCE, datasource);
+ }
+
+ public String getJndiName() {
+ return getProperty(Environment.SESSION_FACTORY_NAME);
+ }
+
+ public void setJndiName(String jndiName) {
+ setProperty(Environment.SESSION_FACTORY_NAME, jndiName);
+ }
+
+ public String getUserName() {
+ return getProperty(Environment.USER);
+ }
+
+ public void setUserName(String userName) {
+ setProperty(Environment.USER, userName);
+ }
+
+ public String getPassword() {
+ return getProperty(Environment.PASS);
+ }
+
+ public void setPassword(String password) {
+ setProperty(Environment.PASS, password);
+ }
+
+ public void setFlushBeforeCompletionEnabled(String enabled) {
+ setProperty(Environment.FLUSH_BEFORE_COMPLETION, enabled);
+ }
+
+ public String getFlushBeforeCompletionEnabled() {
+ return getProperty(Environment.FLUSH_BEFORE_COMPLETION);
+ }
+
+ public void setAutoCloseSessionEnabled(String enabled) {
+ setProperty(Environment.AUTO_CLOSE_SESSION, enabled);
+ }
+
+ public String getAutoCloseSessionEnabled() {
+ return getProperty(Environment.AUTO_CLOSE_SESSION);
+ }
+
+ public Properties getProperties() {
+ return buildProperties();
+ }
+}
Copied: core/trunk/core/src/main/java/org/hibernate/jmx/HibernateServiceMBean.java (from
rev 19909, core/trunk/jmx/src/main/java/org/hibernate/jmx/HibernateServiceMBean.java)
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/jmx/HibernateServiceMBean.java
(rev 0)
+++ core/trunk/core/src/main/java/org/hibernate/jmx/HibernateServiceMBean.java 2010-07-07
20:00:04 UTC (rev 19911)
@@ -0,0 +1,333 @@
+//$Id: HibernateServiceMBean.java 10860 2006-11-22 00:02:55Z steve.ebersole(a)jboss.com $
+package org.hibernate.jmx;
+
+import org.hibernate.HibernateException;
+
+/**
+ * Hibernate JMX Management API
+ * @see HibernateService
+ * @author John Urberg, Gavin King
+ */
+public interface HibernateServiceMBean {
+
+ /**
+ * The Hibernate mapping files (might be overridden by subclasses
+ * that want to specify the mapping files by some other mechanism)
+ * @return String
+ */
+ public String getMapResources();
+ /**
+ * Specify the Hibernate mapping files
+ * @param mappingFiles
+ */
+ public void setMapResources(String mappingFiles);
+ /**
+ * Add a mapping file
+ * @param mapResource
+ */
+ public void addMapResource(String mapResource);
+
+ /**
+ * Set a property
+ * @param property the property name
+ * @param value the property value
+ */
+ public void setProperty(String property, String value);
+
+ /**
+ * Get a property
+ * @param property the property name
+ * @return the property value
+ */
+ public String getProperty(String property);
+
+ /**
+ * Display the properties
+ * @return a list of property names and values
+ */
+ public String getPropertyList();
+
+ /**
+ * The JNDI name of the datasource to use in this <tt>SessionFactory</tt>
+ * @return String
+ */
+ public String getDatasource();
+ /**
+ * Set the JNDI name of the datasource to use in this
<tt>SessionFactory</tt>
+ * @param datasource
+ */
+ public void setDatasource(String datasource);
+
+ /**
+ * Log into the database with this name
+ * @return String
+ */
+ public String getUserName();
+ /**
+ * Log into the database with this name
+ * @param userName
+ */
+ public void setUserName(String userName);
+
+ /**
+ * Log into the database with this password
+ * @return String
+ */
+ public String getPassword();
+ /**
+ * Log into the database with this password
+ * @param password
+ */
+ public void setPassword(String password);
+
+ /**
+ * The JNDI name of the dialect class to use in this
<tt>SessionFactory</tt>
+ * @return String
+ */
+ public String getDialect();
+ /**
+ * The name of the dialect class to use in this <tt>SessionFactory</tt>
+ * @param dialect fully qualified class name of <tt>Dialect</tt> subclass
+ * @see org.hibernate.dialect.Dialect
+ */
+ public void setDialect(String dialect);
+
+ /**
+ * The JNDI name to bind to the <tt>SessionFactory</tt>
+ * @return String
+ */
+ public String getJndiName();
+ /**
+ * The JNDI name to bind to the <tt>SessionFactory</tt>
+ * @param jndiName
+ */
+ public void setJndiName(String jndiName);
+
+ /**
+ * The fully qualified class name of the Hibernate
<tt>TransactionFactory</tt> implementation
+ * @return the class name
+ * @see org.hibernate.transaction.TransactionFactory
+ */
+ public String getTransactionStrategy();
+
+ /**
+ * Set the fully qualified class name of the Hibernate
<tt>TransactionFactory</tt> implementation
+ * @param txnStrategy the class name
+ * @see org.hibernate.transaction.TransactionFactory
+ */
+ public void setTransactionStrategy(String txnStrategy);
+
+ /**
+ * The JNDI name of the JTA UserTransaction object (used only be
<tt>JTATransaction</tt>).
+ * @return the JNDI name
+ * @see org.hibernate.transaction.JTATransaction
+ */
+ public String getUserTransactionName();
+ /**
+ * Set the JNDI name of the JTA UserTransaction object (used only by
<tt>JTATransaction</tt>).
+ * @param utName the JNDI name
+ * @see org.hibernate.transaction.JTATransaction
+ */
+ public void setUserTransactionName(String utName);
+
+ /**
+ * Get the strategy for obtaining the JTA <tt>TransactionManager</tt>
+ * @return the class name
+ * @see org.hibernate.transaction.TransactionManagerLookup
+ */
+ public String getTransactionManagerLookupStrategy();
+ /**
+ * Set the strategy for obtaining the JTA <tt>TransactionManager</tt>
+ * @param lkpStrategy the class name
+ * @see org.hibernate.transaction.TransactionManagerLookup
+ */
+ public void setTransactionManagerLookupStrategy(String lkpStrategy);
+
+ /**
+ * Is SQL logging enabled?
+ */
+ public String getShowSqlEnabled();
+ /**
+ * Enable logging of SQL to console
+ */
+ public void setShowSqlEnabled(String showSql);
+ /**
+ * Get the maximum outer join fetch depth
+ */
+ public String getMaximumFetchDepth();
+ /**
+ * Set the maximum outer join fetch depth
+ */
+ public void setMaximumFetchDepth(String fetchDepth);
+ /**
+ * Get the maximum JDBC batch size
+ */
+ public String getJdbcBatchSize();
+ /**
+ * Set the maximum JDBC batch size
+ */
+ public void setJdbcBatchSize(String batchSize);
+ /**
+ * Get the JDBC fetch size
+ */
+ public String getJdbcFetchSize();
+ /**
+ * Set the JDBC fetch size
+ */
+ public void setJdbcFetchSize(String fetchSize);
+ /**
+ * Get the query language substitutions
+ */
+ public String getQuerySubstitutions();
+ /**
+ * Set the query language substitutions
+ */
+ public void setQuerySubstitutions(String querySubstitutions);
+ /**
+ * Get the default schema
+ */
+ public String getDefaultSchema();
+ /**
+ * Set the default schema
+ */
+ public void setDefaultSchema(String schema);
+ /**
+ * Get the default catalog
+ */
+ public String getDefaultCatalog();
+ /**
+ * Set the default catalog
+ */
+ public void setDefaultCatalog(String catalog);
+ /**
+ * Is use of scrollable resultsets enabled?
+ */
+ public String getJdbcScrollableResultSetEnabled();
+ /**
+ * Enable or disable the use of scrollable resultsets
+ */
+ public void setJdbcScrollableResultSetEnabled(String enabled);
+ /**
+ * Is use of JDBC3 <tt>getGeneratedKeys()</tt> enabled?
+ */
+ public String getGetGeneratedKeysEnabled();
+ /**
+ * Enable or disable the use <tt>getGeneratedKeys()</tt>
+ */
+ public void setGetGeneratedKeysEnabled(String enabled);
+ /**
+ * Get the second-level cache provider class name
+ */
+ public String getCacheProviderClass();
+ /**
+ * Set the second-level cache provider class name
+ */
+ public void setCacheProviderClass(String providerClassName);
+ /**
+ * For cache providers which support this setting, get the
+ * provider's specific configuration resource.
+ */
+ public String getCacheProviderConfig();
+ /**
+ * For cache providers which support this setting, specify the
+ * provider's specific configuration resource.
+ */
+ public void setCacheProviderConfig(String cacheProviderConfig);
+ /**
+ * Is the query cache enabled?
+ */
+ public String getQueryCacheEnabled();
+ /**
+ * Enable or disable the query cache
+ */
+ public void setQueryCacheEnabled(String enabled);
+ /**
+ * Is the second-level cache enabled?
+ */
+ public String getSecondLevelCacheEnabled();
+ /**
+ * Enable or disable the second-level cache
+ */
+ public void setSecondLevelCacheEnabled(String enabled);
+ /**
+ * Get the cache region prefix
+ */
+ public String getCacheRegionPrefix();
+ /**
+ * Set the cache region prefix
+ */
+ public void setCacheRegionPrefix(String prefix);
+ /**
+ * Is the second-level cache optimized for miminal puts?
+ */
+ public String getMinimalPutsEnabled();
+ /**
+ * Enable or disable optimization of second-level cache
+ * for minimal puts
+ */
+ public void setMinimalPutsEnabled(String enabled);
+ /**
+ * Are SQL comments enabled?
+ */
+ public String getCommentsEnabled();
+ /**
+ * Enable or disable the inclusion of comments in
+ * generated SQL
+ */
+ public void setCommentsEnabled(String enabled);
+ /**
+ * Is JDBC batch update for versioned entities enabled?
+ */
+ public String getBatchVersionedDataEnabled();
+ /**
+ * Enable or disable the use of batch updates for
+ * versioned entities
+ */
+ public void setBatchVersionedDataEnabled(String enabled);
+
+ /**
+ * Enable automatic flushing of the Session when JTA transaction ends.
+ */
+ public void setFlushBeforeCompletionEnabled(String enabled);
+ /**
+ * Is automatic Session flusing enabled?
+ */
+ public String getFlushBeforeCompletionEnabled();
+
+ /**
+ * Enable automatic closing of Session when JTA transaction ends.
+ */
+ public void setAutoCloseSessionEnabled(String enabled);
+ /**
+ * Is automatic Session closing enabled?
+ */
+ public String getAutoCloseSessionEnabled();
+
+ /**
+ * Export the <tt>CREATE</tt> DDL to the database
+ * @throws HibernateException
+ */
+ public void createSchema() throws HibernateException;
+ /**
+ * Export the <tt>DROP</tt> DDL to the database
+ * @throws HibernateException
+ */
+ public void dropSchema() throws HibernateException;
+
+
+ /**
+ * Create the <tt>SessionFactory</tt> and bind to the jndi name on startup
+ */
+ public void start() throws HibernateException;
+ /**
+ * Unbind the <tt>SessionFactory</tt> or stub from JNDI
+ */
+ public void stop();
+
+}
+
+
+
+
+
+
Copied: core/trunk/core/src/main/java/org/hibernate/jmx/SessionFactoryStub.java (from rev
19909, core/trunk/jmx/src/main/java/org/hibernate/jmx/SessionFactoryStub.java)
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/jmx/SessionFactoryStub.java
(rev 0)
+++ core/trunk/core/src/main/java/org/hibernate/jmx/SessionFactoryStub.java 2010-07-07
20:00:04 UTC (rev 19911)
@@ -0,0 +1,215 @@
+//$Id: SessionFactoryStub.java 8754 2005-12-05 23:36:59Z steveebersole $
+package org.hibernate.jmx;
+
+import java.io.InvalidObjectException;
+import java.io.ObjectStreamException;
+import java.io.Serializable;
+import java.sql.Connection;
+import java.util.Map;
+import java.util.Set;
+
+import javax.naming.NamingException;
+import javax.naming.Reference;
+import javax.naming.StringRefAddr;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.hibernate.AssertionFailure;
+import org.hibernate.HibernateException;
+import org.hibernate.Interceptor;
+import org.hibernate.SessionFactory;
+import org.hibernate.StatelessSession;
+import org.hibernate.Cache;
+import org.hibernate.TypeHelper;
+import org.hibernate.engine.FilterDefinition;
+import org.hibernate.id.IdentifierGenerator;
+import org.hibernate.id.UUIDHexGenerator;
+import org.hibernate.impl.SessionFactoryObjectFactory;
+import org.hibernate.metadata.ClassMetadata;
+import org.hibernate.metadata.CollectionMetadata;
+import org.hibernate.stat.Statistics;
+
+/**
+ * A flyweight for <tt>SessionFactory</tt>. If the MBean itself does not
+ * have classpath to the persistent classes, then a stub will be registered
+ * with JNDI and the actual <tt>SessionFactoryImpl</tt> built upon first
+ * access.
+ * @author Gavin King
+ */
+public class SessionFactoryStub implements SessionFactory {
+
+ private static final Logger log = LoggerFactory.getLogger(SessionFactoryStub.class);
+
+ private static final IdentifierGenerator UUID_GENERATOR = new UUIDHexGenerator();
+
+ private transient SessionFactory impl;
+ private transient HibernateService service;
+ private String uuid;
+ private String name;
+
+ SessionFactoryStub(HibernateService service) {
+ this.service = service;
+ this.name = service.getJndiName();
+ try {
+ uuid = (String) UUID_GENERATOR.generate(null, null);
+ }
+ catch (Exception e) {
+ throw new AssertionFailure("Could not generate UUID");
+ }
+
+ SessionFactoryObjectFactory.addInstance( uuid, name, this, service.getProperties() );
+ }
+
+ public org.hibernate.classic.Session openSession(Connection connection, Interceptor
interceptor) {
+ return getImpl().openSession(connection, interceptor);
+ }
+
+ public org.hibernate.classic.Session openSession(Interceptor interceptor) throws
HibernateException {
+ return getImpl().openSession(interceptor);
+ }
+
+ public org.hibernate.classic.Session openSession() throws HibernateException {
+ return getImpl().openSession();
+ }
+
+ public org.hibernate.classic.Session openSession(Connection conn) {
+ return getImpl().openSession(conn);
+ }
+
+ public org.hibernate.classic.Session getCurrentSession() {
+ return getImpl().getCurrentSession();
+ }
+
+ private synchronized SessionFactory getImpl() {
+ if (impl==null) impl = service.buildSessionFactory();
+ return impl;
+ }
+
+ //readResolveObject
+ private Object readResolve() throws ObjectStreamException {
+ // look for the instance by uuid
+ Object result = SessionFactoryObjectFactory.getInstance(uuid);
+ if (result==null) {
+ // in case we were deserialized in a different JVM, look for an instance with the same
name
+ // (alternatively we could do an actual JNDI lookup here....)
+ result = SessionFactoryObjectFactory.getNamedInstance(name);
+ if (result==null) {
+ throw new InvalidObjectException("Could not find a stub SessionFactory named:
" + name);
+ }
+ else {
+ log.debug("resolved stub SessionFactory by name");
+ }
+ }
+ else {
+ log.debug("resolved stub SessionFactory by uid");
+ }
+ return result;
+ }
+
+ /**
+ * @see javax.naming.Referenceable#getReference()
+ */
+ public Reference getReference() throws NamingException {
+ return new Reference(
+ SessionFactoryStub.class.getName(),
+ new StringRefAddr("uuid", uuid),
+ SessionFactoryObjectFactory.class.getName(),
+ null
+ );
+ }
+
+ public ClassMetadata getClassMetadata(Class persistentClass) throws HibernateException
{
+ return getImpl().getClassMetadata(persistentClass);
+ }
+
+ public ClassMetadata getClassMetadata(String entityName)
+ throws HibernateException {
+ return getImpl().getClassMetadata(entityName);
+ }
+
+ public CollectionMetadata getCollectionMetadata(String roleName) throws
HibernateException {
+ return getImpl().getCollectionMetadata(roleName);
+ }
+
+ public Map getAllClassMetadata() throws HibernateException {
+ return getImpl().getAllClassMetadata();
+ }
+
+ public Map getAllCollectionMetadata() throws HibernateException {
+ return getImpl().getAllCollectionMetadata();
+ }
+
+ public void close() throws HibernateException {
+ }
+
+ public boolean isClosed() {
+ return false;
+ }
+
+ public Cache getCache() {
+ return getImpl().getCache();
+ }
+
+ public void evict(Class persistentClass, Serializable id)
+ throws HibernateException {
+ getImpl().evict(persistentClass, id);
+ }
+
+ public void evict(Class persistentClass) throws HibernateException {
+ getImpl().evict(persistentClass);
+ }
+
+ public void evictEntity(String entityName, Serializable id)
+ throws HibernateException {
+ getImpl().evictEntity(entityName, id);
+ }
+
+ public void evictEntity(String entityName) throws HibernateException {
+ getImpl().evictEntity(entityName);
+ }
+
+ public void evictCollection(String roleName, Serializable id)
+ throws HibernateException {
+ getImpl().evictCollection(roleName, id);
+ }
+
+ public void evictCollection(String roleName) throws HibernateException {
+ getImpl().evictCollection(roleName);
+ }
+
+ public void evictQueries() throws HibernateException {
+ getImpl().evictQueries();
+ }
+
+ public void evictQueries(String cacheRegion) throws HibernateException {
+ getImpl().evictQueries(cacheRegion);
+ }
+
+ public Statistics getStatistics() {
+ return getImpl().getStatistics();
+ }
+
+ public StatelessSession openStatelessSession() {
+ return getImpl().openStatelessSession();
+ }
+
+ public StatelessSession openStatelessSession(Connection conn) {
+ return getImpl().openStatelessSession(conn);
+ }
+
+ public Set getDefinedFilterNames() {
+ return getImpl().getDefinedFilterNames();
+ }
+
+ public FilterDefinition getFilterDefinition(String filterName) throws HibernateException
{
+ return getImpl().getFilterDefinition( filterName );
+ }
+
+ public boolean containsFetchProfileDefinition(String name) {
+ return getImpl().containsFetchProfileDefinition( name );
+ }
+
+ public TypeHelper getTypeHelper() {
+ return getImpl().getTypeHelper();
+ }
+}
Copied: core/trunk/core/src/main/java/org/hibernate/jmx/StatisticsService.java (from rev
19909, core/trunk/jmx/src/main/java/org/hibernate/jmx/StatisticsService.java)
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/jmx/StatisticsService.java
(rev 0)
+++ core/trunk/core/src/main/java/org/hibernate/jmx/StatisticsService.java 2010-07-07
20:00:04 UTC (rev 19911)
@@ -0,0 +1,317 @@
+//$Id: StatisticsService.java 8262 2005-09-30 07:48:53Z oneovthafew $
+package org.hibernate.jmx;
+
+import javax.naming.InitialContext;
+import javax.naming.NameNotFoundException;
+import javax.naming.NamingException;
+import javax.naming.Reference;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.hibernate.SessionFactory;
+import org.hibernate.impl.SessionFactoryObjectFactory;
+import org.hibernate.stat.CollectionStatistics;
+import org.hibernate.stat.EntityStatistics;
+import org.hibernate.stat.QueryStatistics;
+import org.hibernate.stat.SecondLevelCacheStatistics;
+import org.hibernate.stat.Statistics;
+import org.hibernate.stat.StatisticsImpl;
+
+/**
+ * JMX service for Hibernate statistics<br>
+ * <br>
+ * Register this MBean in your JMX server for a specific session factory
+ * <pre>
+ * //build the ObjectName you want
+ * Hashtable tb = new Hashtable();
+ * tb.put("type", "statistics");
+ * tb.put("sessionFactory", "myFinancialApp");
+ * ObjectName on = new ObjectName("hibernate", tb);
+ * StatisticsService stats = new StatisticsService();
+ * stats.setSessionFactory(sessionFactory);
+ * server.registerMBean(stats, on);
+ * </pre>
+ * And call the MBean the way you want<br>
+ * <br>
+ * Register this MBean in your JMX server with no specific session factory
+ * <pre>
+ * //build the ObjectName you want
+ * Hashtable tb = new Hashtable();
+ * tb.put("type", "statistics");
+ * tb.put("sessionFactory", "myFinancialApp");
+ * ObjectName on = new ObjectName("hibernate", tb);
+ * StatisticsService stats = new StatisticsService();
+ * server.registerMBean(stats, on);
+ * </pre>
+ * And call the MBean by providing the <code>SessionFactoryJNDIName</code>
first.
+ * Then the session factory will be retrieved from JNDI and the statistics
+ * loaded.
+ *
+ * @author Emmanuel Bernard
+ */
+public class StatisticsService implements StatisticsServiceMBean {
+
+ //TODO: We probably should have a StatisticsNotPublishedException, to make it clean
+
+ SessionFactory sf;
+ String sfJNDIName;
+ Logger log = LoggerFactory.getLogger(StatisticsService.class);
+ Statistics stats = new StatisticsImpl();
+
+ /**
+ * @see StatisticsServiceMBean#setSessionFactoryJNDIName(java.lang.String)
+ */
+ public void setSessionFactoryJNDIName(String sfJNDIName) {
+ this.sfJNDIName = sfJNDIName;
+ try {
+ Object obj = new InitialContext().lookup(sfJNDIName);
+ if (obj instanceof Reference) {
+ Reference ref = (Reference) obj;
+ setSessionFactory( (SessionFactory) SessionFactoryObjectFactory.getInstance( (String)
ref.get(0).getContent() ) );
+ }
+ else {
+ setSessionFactory( (SessionFactory) obj );
+ }
+ }
+ catch (NameNotFoundException e) {
+ log.error("No session factory with JNDI name " + sfJNDIName, e);
+ setSessionFactory(null);
+ }
+ catch (NamingException e) {
+ log.error("Error while accessing session factory with JNDI name " +
sfJNDIName, e);
+ setSessionFactory(null);
+ }
+ catch (ClassCastException e) {
+ log.error("JNDI name " + sfJNDIName + " does not handle a session
factory reference", e);
+ setSessionFactory(null);
+ }
+ }
+
+ /**
+ * Useful to init this MBean wo a JNDI session factory name
+ *
+ * @param sf session factory to register
+ */
+ public void setSessionFactory(SessionFactory sf) {
+ this.sf = sf;
+ if (sf == null) {
+ stats = new StatisticsImpl();
+ }
+ else {
+ stats = sf.getStatistics();
+ }
+
+ }
+ /**
+ * @see StatisticsServiceMBean#clear()
+ */
+ public void clear() {
+ stats.clear();
+ }
+ /**
+ * @see StatisticsServiceMBean#getEntityStatistics(java.lang.String)
+ */
+ public EntityStatistics getEntityStatistics(String entityName) {
+ return stats.getEntityStatistics(entityName);
+ }
+ /**
+ * @see StatisticsServiceMBean#getCollectionStatistics(java.lang.String)
+ */
+ public CollectionStatistics getCollectionStatistics(String role) {
+ return stats.getCollectionStatistics(role);
+ }
+ /**
+ * @see StatisticsServiceMBean#getSecondLevelCacheStatistics(java.lang.String)
+ */
+ public SecondLevelCacheStatistics getSecondLevelCacheStatistics(String regionName) {
+ return stats.getSecondLevelCacheStatistics(regionName);
+ }
+ /**
+ * @see StatisticsServiceMBean#getQueryStatistics(java.lang.String)
+ */
+ public QueryStatistics getQueryStatistics(String hql) {
+ return stats.getQueryStatistics(hql);
+ }
+ /**
+ * @see StatisticsServiceMBean#getEntityDeleteCount()
+ */
+ public long getEntityDeleteCount() {
+ return stats.getEntityDeleteCount();
+ }
+ /**
+ * @see StatisticsServiceMBean#getEntityInsertCount()
+ */
+ public long getEntityInsertCount() {
+ return stats.getEntityInsertCount();
+ }
+ /**
+ * @see StatisticsServiceMBean#getEntityLoadCount()
+ */
+ public long getEntityLoadCount() {
+ return stats.getEntityLoadCount();
+ }
+ /**
+ * @see StatisticsServiceMBean#getEntityFetchCount()
+ */
+ public long getEntityFetchCount() {
+ return stats.getEntityFetchCount();
+ }
+ /**
+ * @see StatisticsServiceMBean#getEntityUpdateCount()
+ */
+ public long getEntityUpdateCount() {
+ return stats.getEntityUpdateCount();
+ }
+ /**
+ * @see StatisticsServiceMBean#getQueryExecutionCount()
+ */
+ public long getQueryExecutionCount() {
+ return stats.getQueryExecutionCount();
+ }
+ public long getQueryCacheHitCount() {
+ return stats.getQueryCacheHitCount();
+ }
+ public long getQueryExecutionMaxTime() {
+ return stats.getQueryExecutionMaxTime();
+ }
+ public long getQueryCacheMissCount() {
+ return stats.getQueryCacheMissCount();
+ }
+ public long getQueryCachePutCount() {
+ return stats.getQueryCachePutCount();
+ }
+ /**
+ * @see StatisticsServiceMBean#getFlushCount()
+ */
+ public long getFlushCount() {
+ return stats.getFlushCount();
+ }
+ /**
+ * @see StatisticsServiceMBean#getConnectCount()
+ */
+ public long getConnectCount() {
+ return stats.getConnectCount();
+ }
+ /**
+ * @see StatisticsServiceMBean#getSecondLevelCacheHitCount()
+ */
+ public long getSecondLevelCacheHitCount() {
+ return stats.getSecondLevelCacheHitCount();
+ }
+ /**
+ * @see StatisticsServiceMBean#getSecondLevelCacheMissCount()
+ */
+ public long getSecondLevelCacheMissCount() {
+ return stats.getSecondLevelCacheMissCount();
+ }
+ /**
+ * @see StatisticsServiceMBean#getSecondLevelCachePutCount()
+ */
+ public long getSecondLevelCachePutCount() {
+ return stats.getSecondLevelCachePutCount();
+ }
+ /**
+ * @see StatisticsServiceMBean#getSessionCloseCount()
+ */
+ public long getSessionCloseCount() {
+ return stats.getSessionCloseCount();
+ }
+ /**
+ * @see StatisticsServiceMBean#getSessionOpenCount()
+ */
+ public long getSessionOpenCount() {
+ return stats.getSessionOpenCount();
+ }
+ /**
+ * @see StatisticsServiceMBean#getCollectionLoadCount()
+ */
+ public long getCollectionLoadCount() {
+ return stats.getCollectionLoadCount();
+ }
+ /**
+ * @see StatisticsServiceMBean#getCollectionFetchCount()
+ */
+ public long getCollectionFetchCount() {
+ return stats.getCollectionFetchCount();
+ }
+ /**
+ * @see StatisticsServiceMBean#getCollectionUpdateCount()
+ */
+ public long getCollectionUpdateCount() {
+ return stats.getCollectionUpdateCount();
+ }
+ /**
+ * @see StatisticsServiceMBean#getCollectionRemoveCount()
+ */
+ public long getCollectionRemoveCount() {
+ return stats.getCollectionRemoveCount();
+ }
+ /**
+ * @see StatisticsServiceMBean#getCollectionRecreateCount()
+ */
+ public long getCollectionRecreateCount() {
+ return stats.getCollectionRecreateCount();
+ }
+ /**
+ * @see StatisticsServiceMBean#getStartTime()
+ */
+ public long getStartTime() {
+ return stats.getStartTime();
+ }
+
+ /**
+ * @see StatisticsServiceMBean#isStatisticsEnabled()
+ */
+ public boolean isStatisticsEnabled() {
+ return stats.isStatisticsEnabled();
+ }
+
+ /**
+ * @see StatisticsServiceMBean#setStatisticsEnabled(boolean)
+ */
+ public void setStatisticsEnabled(boolean enable) {
+ stats.setStatisticsEnabled(enable);
+ }
+
+ public void logSummary() {
+ stats.logSummary();
+ }
+
+ public String[] getCollectionRoleNames() {
+ return stats.getCollectionRoleNames();
+ }
+
+ public String[] getEntityNames() {
+ return stats.getEntityNames();
+ }
+
+ public String[] getQueries() {
+ return stats.getQueries();
+ }
+
+ public String[] getSecondLevelCacheRegionNames() {
+ return stats.getSecondLevelCacheRegionNames();
+ }
+
+ public long getSuccessfulTransactionCount() {
+ return stats.getSuccessfulTransactionCount();
+ }
+ public long getTransactionCount() {
+ return stats.getTransactionCount();
+ }
+
+ public long getCloseStatementCount() {
+ return stats.getCloseStatementCount();
+ }
+ public long getPrepareStatementCount() {
+ return stats.getPrepareStatementCount();
+ }
+
+ public long getOptimisticFailureCount() {
+ return stats.getOptimisticFailureCount();
+ }
+
+ public String getQueryExecutionMaxTimeQueryString() {
+ return stats.getQueryExecutionMaxTimeQueryString();
+ }
+}
Copied: core/trunk/core/src/main/java/org/hibernate/jmx/StatisticsServiceMBean.java (from
rev 19909, core/trunk/jmx/src/main/java/org/hibernate/jmx/StatisticsServiceMBean.java)
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/jmx/StatisticsServiceMBean.java
(rev 0)
+++ core/trunk/core/src/main/java/org/hibernate/jmx/StatisticsServiceMBean.java 2010-07-07
20:00:04 UTC (rev 19911)
@@ -0,0 +1,19 @@
+//$Id: StatisticsServiceMBean.java 4332 2004-08-15 12:55:28Z oneovthafew $
+package org.hibernate.jmx;
+
+import org.hibernate.stat.Statistics;
+
+/**
+ * MBean exposing Session Factory statistics
+ *
+ * @see org.hibernate.stat.Statistics
+ * @author Emmanuel Bernard
+ */
+public interface StatisticsServiceMBean extends Statistics {
+ /**
+ * Publish the statistics of a session factory bound to
+ * the default JNDI context
+ * @param sfJNDIName session factory jndi name
+ */
+ public abstract void setSessionFactoryJNDIName(String sfJNDIName);
+}
\ No newline at end of file
Copied: core/trunk/core/src/main/java/org/hibernate/jmx/package.html (from rev 19909,
core/trunk/jmx/src/main/java/org/hibernate/jmx/package.html)
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/jmx/package.html
(rev 0)
+++ core/trunk/core/src/main/java/org/hibernate/jmx/package.html 2010-07-07 20:00:04 UTC
(rev 19911)
@@ -0,0 +1,14 @@
+<html>
+<head></head>
+<body>
+<p>
+ This package exposes a Hibernate instance via JMX.
+</p>
+<p>
+ <tt>HibernateService</tt> allows configuration and management
+ of the Hibernate runtime. <tt>StatisticsService</tt>
+ reports information that might be useful for performance
+ tuning.
+</p>
+</body>
+</html>
Property changes on: core/trunk/core/src/main/java/org/hibernate/jmx/package.html
___________________________________________________________________
Name: svn:executable
+ *
Copied: core/trunk/core/src/test/java/org/hibernate/jmx/Entity.hbm.xml (from rev 19909,
core/trunk/jmx/src/test/java/org/hibernate/jmx/Entity.hbm.xml)
===================================================================
--- core/trunk/core/src/test/java/org/hibernate/jmx/Entity.hbm.xml
(rev 0)
+++ core/trunk/core/src/test/java/org/hibernate/jmx/Entity.hbm.xml 2010-07-07 20:00:04 UTC
(rev 19911)
@@ -0,0 +1,15 @@
+<?xml version="1.0"?>
+<!DOCTYPE hibernate-mapping PUBLIC
+ "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
+ "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
+
+<hibernate-mapping>
+
+ <class name="org.hibernate.jmx.Entity">
+ <id name="id" type="long" column="id_">
+ <generator class="assigned"/>
+ </id>
+ <property name="name"/>
+ </class>
+
+</hibernate-mapping>
Copied: core/trunk/core/src/test/java/org/hibernate/jmx/Entity.java (from rev 19909,
core/trunk/jmx/src/test/java/org/hibernate/jmx/Entity.java)
===================================================================
--- core/trunk/core/src/test/java/org/hibernate/jmx/Entity.java
(rev 0)
+++ core/trunk/core/src/test/java/org/hibernate/jmx/Entity.java 2010-07-07 20:00:04 UTC
(rev 19911)
@@ -0,0 +1,27 @@
+package org.hibernate.jmx;
+
+/**
+ * {@inheritDoc}
+ *
+ * @author Steve Ebersole
+ */
+public class Entity {
+ private Long id;
+ private String name;
+
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+}
Copied: core/trunk/core/src/test/java/org/hibernate/jmx/TrivialTest.java (from rev 19909,
core/trunk/jmx/src/test/java/org/hibernate/jmx/TrivialTest.java)
===================================================================
--- core/trunk/core/src/test/java/org/hibernate/jmx/TrivialTest.java
(rev 0)
+++ core/trunk/core/src/test/java/org/hibernate/jmx/TrivialTest.java 2010-07-07 20:00:04
UTC (rev 19911)
@@ -0,0 +1,30 @@
+package org.hibernate.jmx;
+
+/**
+ * Test copied over from o.h.t.legacy.FooBarTest
+ *
+ * @author Steve Ebersole
+ */
+public class TrivialTest {
+ public void testService() throws Exception {
+ HibernateService hs = new HibernateService();
+ hs.setJndiName( "SessionFactory" );
+ hs.setMapResources( "org/hibernate/jmx/Entity.hbm.xml" );
+ hs.setShowSqlEnabled( "true" );
+ hs.start();
+ hs.stop();
+ hs.setProperty( "foo", "bar" );
+ hs.start();
+ hs.stop();
+ try {
+ hs.setMapResources( "non-existent" );
+ hs.start();
+ }
+ catch( Throwable t ) {
+ // expected behavior
+ }
+ finally {
+ hs.stop();
+ }
+ }
+}
Modified: core/trunk/pom.xml
===================================================================
--- core/trunk/pom.xml 2010-07-07 19:39:23 UTC (rev 19910)
+++ core/trunk/pom.xml 2010-07-07 20:00:04 UTC (rev 19911)
@@ -30,7 +30,6 @@
<module>connection-proxool</module>
<module>annotations</module>
<module>envers</module>
- <module>jmx</module>
<module>jdbc3-testing</module>
<module>cache-infinispan</module>
<!--