HHH-5951 - Strategy for deciding JtaPlatform when an explicit one not specified

This commit is contained in:
Steve Ebersole 2013-02-11 15:39:24 -06:00
parent 88c6dad9b0
commit c56bb23ef5
12 changed files with 332 additions and 37 deletions

View File

@ -282,6 +282,12 @@ public interface AvailableSettings {
*/
public static final String JTA_PLATFORM = "hibernate.transaction.jta.platform";
/**
* Names the {@link org.hibernate.engine.transaction.jta.platform.spi.JtaPlatformResolver} implementation to use.
* @since 4.3
*/
public static final String JTA_PLATFORM_RESOLVER = "hibernate.transaction.jta.platform_resolver";
/**
* The {@link org.hibernate.cache.spi.RegionFactory} implementation class
*/

View File

@ -34,7 +34,7 @@ import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatformException;
* @author Steve Ebersole
*/
public class BitronixJtaPlatform extends AbstractJtaPlatform {
private static final String TM_CLASS_NAME = "bitronix.tm.TransactionManagerServices";
public static final String TM_CLASS_NAME = "bitronix.tm.TransactionManagerServices";
@Override
protected TransactionManager locateTransactionManager() {

View File

@ -37,8 +37,8 @@ import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatformException;
* @author Steve Ebersole
*/
public class JBossStandAloneJtaPlatform extends AbstractJtaPlatform {
private static final String JBOSS_TM_CLASS_NAME = "com.arjuna.ats.jta.TransactionManager";
private static final String JBOSS_UT_CLASS_NAME = "com.arjuna.ats.jta.UserTransaction";
public static final String JBOSS_TM_CLASS_NAME = "com.arjuna.ats.jta.TransactionManager";
public static final String JBOSS_UT_CLASS_NAME = "com.arjuna.ats.jta.UserTransaction";
@Override
protected TransactionManager locateTransactionManager() {

View File

@ -36,7 +36,7 @@ import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatformException;
*/
public class JOnASJtaPlatform extends AbstractJtaPlatform {
public static final String UT_NAME = "java:comp/UserTransaction";
private static final String TM_CLASS_NAME = "org.objectweb.jonas_tm.Current";
public static final String TM_CLASS_NAME = "org.objectweb.jonas_tm.Current";
@Override
protected TransactionManager locateTransactionManager() {

View File

@ -31,6 +31,7 @@ import org.hibernate.boot.registry.StandardServiceInitiator;
import org.hibernate.boot.registry.selector.spi.StrategySelector;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform;
import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatformResolver;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.service.spi.ServiceRegistryImplementor;
@ -55,8 +56,8 @@ public class JtaPlatformInitiator implements StandardServiceInitiator<JtaPlatfor
final Object setting = configurationValues.get( AvailableSettings.JTA_PLATFORM );
final JtaPlatform platform = registry.getService( StrategySelector.class ).resolveStrategy( JtaPlatform.class, setting );
if ( platform == null ) {
LOG.debugf( " No JtaPlatform was specified, using default [%s]", NoJtaPlatform.class.getName() );
return new NoJtaPlatform();
LOG.debugf( "No JtaPlatform was specified, checking resolver" );
return registry.getService( JtaPlatformResolver.class ).resolveJtaPlatform( configurationValues, registry );
}
return platform;
}

View File

@ -0,0 +1,60 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2013, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.engine.transaction.jta.platform.internal;
import java.util.Map;
import org.jboss.logging.Logger;
import org.hibernate.boot.registry.StandardServiceInitiator;
import org.hibernate.boot.registry.selector.spi.StrategySelector;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatformResolver;
import org.hibernate.service.spi.ServiceRegistryImplementor;
/**
* @author Steve Ebersole
*/
public class JtaPlatformResolverInitiator implements StandardServiceInitiator<JtaPlatformResolver> {
public static final JtaPlatformResolverInitiator INSTANCE = new JtaPlatformResolverInitiator();
private static final Logger log = Logger.getLogger( JtaPlatformResolverInitiator.class );
@Override
public JtaPlatformResolver initiateService(Map configurationValues, ServiceRegistryImplementor registry) {
final Object setting = configurationValues.get( AvailableSettings.JTA_PLATFORM_RESOLVER );
final JtaPlatformResolver resolver = registry.getService( StrategySelector.class )
.resolveStrategy( JtaPlatformResolver.class, setting );
if ( resolver == null ) {
log.debugf( "No JtaPlatformResolver was specified, using default [%s]", StandardJtaPlatformResolver.class.getName() );
return StandardJtaPlatformResolver.INSTANCE;
}
return resolver;
}
@Override
public Class<JtaPlatformResolver> getServiceInitiated() {
return JtaPlatformResolver.class;
}
}

View File

@ -38,6 +38,8 @@ import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform;
* @author Steve Ebersole
*/
public class NoJtaPlatform implements JtaPlatform {
public static final NoJtaPlatform INSTANCE = new NoJtaPlatform();
@Override
public TransactionManager retrieveTransactionManager() {
return null;

View File

@ -0,0 +1,121 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2013, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.engine.transaction.jta.platform.internal;
import java.util.Map;
import org.jboss.logging.Logger;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.boot.registry.classloading.spi.ClassLoadingException;
import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform;
import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatformProvider;
import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatformResolver;
import org.hibernate.service.spi.ServiceRegistryImplementor;
/**
* @author Steve Ebersole
*/
public class StandardJtaPlatformResolver implements JtaPlatformResolver {
public static final StandardJtaPlatformResolver INSTANCE = new StandardJtaPlatformResolver();
private static final Logger log = Logger.getLogger( StandardJtaPlatformResolver.class );
@Override
public JtaPlatform resolveJtaPlatform(Map configurationValues, ServiceRegistryImplementor registry) {
final ClassLoaderService classLoaderService = registry.getService( ClassLoaderService.class );
// Initially look for a JtaPlatformProvider
for ( JtaPlatformProvider provider : classLoaderService.loadJavaServices( JtaPlatformProvider.class ) ) {
final JtaPlatform providedPlatform = provider.getProvidedJtaPlatform();
if ( providedPlatform!= null ) {
return providedPlatform;
}
}
// look for classes on the ClassLoader (via service) that we know indicate certain JTA impls or
// indicate running in certain environments with known JTA impls.
//
// IMPL NOTE : essentially we attempt Class lookups and use the exceptions from the class(es) not
// being found as the indicator
// JBoss ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
try {
classLoaderService.classForName( JBossStandAloneJtaPlatform.JBOSS_TM_CLASS_NAME );
classLoaderService.classForName( JBossStandAloneJtaPlatform.JBOSS_UT_CLASS_NAME );
// we know that the JBoss TS classes are available
// if neither of these look-ups resulted in an error (no such class), then JBossTM is available on
// the classpath
//
// todo : we cannot really distinguish between the need for JBossStandAloneJtaPlatform versus JBossApServerJtaPlatform
// but discussions with David led to the JtaPlatformProvider solution above, so inside JBoss AS we
// should be relying on that
return new JBossStandAloneJtaPlatform();
}
catch (ClassLoadingException ignore) {
}
// Bitronix ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
try {
classLoaderService.classForName( BitronixJtaPlatform.TM_CLASS_NAME );
return new BitronixJtaPlatform();
}
catch (ClassLoadingException ignore) {
}
// JOnAS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
try {
classLoaderService.classForName( JOnASJtaPlatform.TM_CLASS_NAME );
return new JOnASJtaPlatform();
}
catch (ClassLoadingException ignore) {
}
// JOTM ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
try {
classLoaderService.classForName( JOTMJtaPlatform.TM_CLASS_NAME );
return new JOTMJtaPlatform();
}
catch (ClassLoadingException ignore) {
}
// WebSphere ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for ( WebSphereJtaPlatform.WebSphereEnvironment webSphereEnvironment
: WebSphereJtaPlatform.WebSphereEnvironment.values() ) {
try {
Class accessClass = classLoaderService.classForName( webSphereEnvironment.getTmAccessClassName() );
return new WebSphereJtaPlatform( accessClass, webSphereEnvironment );
}
catch (ClassLoadingException ignore) {
}
}
// Finally, return the default...
log.debugf( "Could not resolve JtaPlatform, using default [%s]", NoJtaPlatform.class.getName() );
return NoJtaPlatform.INSTANCE;
}
}

View File

@ -29,7 +29,6 @@ import javax.transaction.UserTransaction;
import org.jboss.logging.Logger;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatformException;
/**
@ -39,45 +38,42 @@ import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatformException;
* @author Steve Ebersole
*/
public class WebSphereJtaPlatform extends AbstractJtaPlatform {
private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, WebSphereJtaPlatform.class.getName());
public static final String VERSION_5_UT_NAME = "java:comp/UserTransaction";
public static final String VERSION_4_UT_NAME = "jta/usertransaction";
private static final Logger log = Logger.getLogger( WebSphereJtaPlatform.class );
private final Class transactionManagerAccessClass;
private final int webSphereVersion;
private final WebSphereEnvironment webSphereEnvironment;
public WebSphereJtaPlatform() {
try {
Class clazz;
int version;
try {
clazz = Class.forName( "com.ibm.ws.Transaction.TransactionManagerFactory" );
version = 5;
LOG.debug("WebSphere 5.1");
}
catch ( Exception e ) {
try {
clazz = Class.forName( "com.ibm.ejs.jts.jta.TransactionManagerFactory" );
version = 5;
LOG.debug("WebSphere 5.0");
}
catch ( Exception e2 ) {
clazz = Class.forName( "com.ibm.ejs.jts.jta.JTSXA" );
version = 4;
LOG.debug("WebSphere 4");
}
}
Class tmAccessClass = null;
WebSphereEnvironment webSphereEnvironment = null;
transactionManagerAccessClass = clazz;
webSphereVersion = version;
for ( WebSphereEnvironment check : WebSphereEnvironment.values() ) {
try {
tmAccessClass = Class.forName( check.getTmAccessClassName() );
webSphereEnvironment = check;
log.debugf( "WebSphere version : %s", webSphereEnvironment.getWebSphereVersion() );
break;
}
catch ( Exception ignore ) {
// go on to the next iteration
}
}
catch ( Exception e ) {
throw new JtaPlatformException( "Could not locate WebSphere TransactionManager access class", e );
if ( webSphereEnvironment == null ) {
throw new JtaPlatformException( "Could not locate WebSphere TransactionManager access class" );
}
this.transactionManagerAccessClass = tmAccessClass;
this.webSphereEnvironment = webSphereEnvironment;
}
public WebSphereJtaPlatform(Class transactionManagerAccessClass, WebSphereEnvironment webSphereEnvironment) {
this.transactionManagerAccessClass = transactionManagerAccessClass;
this.webSphereEnvironment = webSphereEnvironment;
}
@Override
@SuppressWarnings("unchecked")
protected TransactionManager locateTransactionManager() {
try {
final Method method = transactionManagerAccessClass.getMethod( "getTransactionManager" );
@ -91,7 +87,36 @@ public class WebSphereJtaPlatform extends AbstractJtaPlatform {
@Override
protected UserTransaction locateUserTransaction() {
final String utName = webSphereVersion == 5 ? VERSION_5_UT_NAME : VERSION_4_UT_NAME;
final String utName = webSphereEnvironment.getUtName();
return (UserTransaction) jndiService().locate( utName );
}
public static enum WebSphereEnvironment {
WS_4_0( "4.x", "com.ibm.ejs.jts.jta.JTSXA", "jta/usertransaction" ),
WS_5_0( "5.0", "com.ibm.ejs.jts.jta.TransactionManagerFactory", "java:comp/UserTransaction" ),
WS_5_1( "5.1", "com.ibm.ws.Transaction.TransactionManagerFactory", "java:comp/UserTransaction" )
;
private final String webSphereVersion;
private final String tmAccessClassName;
private final String utName;
private WebSphereEnvironment(String webSphereVersion, String tmAccessClassName, String utName) {
this.webSphereVersion = webSphereVersion;
this.tmAccessClassName = tmAccessClassName;
this.utName = utName;
}
public String getWebSphereVersion() {
return webSphereVersion;
}
public String getTmAccessClassName() {
return tmAccessClassName;
}
public String getUtName() {
return utName;
}
}
}

View File

@ -0,0 +1,39 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2013, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.engine.transaction.jta.platform.spi;
/**
* A {@link java.util.ServiceLoader}-style provider of {@link JtaPlatform} instances. Used when an
* explicit JtaPlatform is not provided.
*
* @author Steve Ebersole
*/
public interface JtaPlatformProvider {
/**
* Retrieve the JtaPlatform provided by this environment.
*
* @return The provided JtaPlatform
*/
public JtaPlatform getProvidedJtaPlatform();
}

View File

@ -0,0 +1,39 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2013, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.engine.transaction.jta.platform.spi;
import java.util.Map;
import org.hibernate.service.Service;
import org.hibernate.service.spi.ServiceRegistryImplementor;
/**
* Service for defining how to resolve or determine the {@link JtaPlatform} to use in configurations where the user
* did not explicitly specify one.
*
* @author Steve Ebersole
*/
public interface JtaPlatformResolver extends Service {
public JtaPlatform resolveJtaPlatform(Map configurationValues, ServiceRegistryImplementor registry);
}

View File

@ -33,6 +33,7 @@ import org.hibernate.engine.jdbc.batch.internal.BatchBuilderInitiator;
import org.hibernate.engine.jdbc.dialect.internal.DatabaseInfoDialectResolverInitiator;
import org.hibernate.engine.jdbc.internal.JdbcServicesInitiator;
import org.hibernate.engine.transaction.internal.TransactionFactoryInitiator;
import org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformResolverInitiator;
import org.hibernate.id.factory.internal.MutableIdentifierGeneratorFactoryInitiator;
import org.hibernate.persister.internal.PersisterClassResolverInitiator;
import org.hibernate.persister.internal.PersisterFactoryInitiator;
@ -80,6 +81,7 @@ public class StandardServiceInitiators {
serviceInitiators.add( MutableIdentifierGeneratorFactoryInitiator.INSTANCE);
serviceInitiators.add( JtaPlatformInitiator.INSTANCE );
serviceInitiators.add( JtaPlatformResolverInitiator.INSTANCE );
serviceInitiators.add( TransactionFactoryInitiator.INSTANCE );
serviceInitiators.add( SessionFactoryServiceRegistryFactoryInitiator.INSTANCE );