HHH-13994 Bootstrap analysis: avoid initializing unused JtaPlatform classes

This commit is contained in:
Sanne Grinovero 2020-04-25 21:35:45 +01:00
parent 49405effce
commit 922ec04422
3 changed files with 331 additions and 145 deletions

View File

@ -0,0 +1,156 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.boot.registry.selector.internal;
import java.util.Objects;
import org.hibernate.engine.transaction.jta.platform.internal.AtomikosJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.BitronixJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.BorlandEnterpriseServerJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.JBossAppServerJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.JBossStandAloneJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.JOTMJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.JOnASJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.JRun4JtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.OC4JJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.OrionJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.ResinJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.SapNetWeaverJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.SunOneJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.WebSphereExtendedJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.WebSphereJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.WebSphereLibertyJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.WeblogicJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform;
public class DefaultJtaPlatformSelector implements LazyServiceResolver<JtaPlatform> {
@Override
public Class<? extends JtaPlatform> resolve(final String name) {
Objects.requireNonNull( name);
if ( name.isEmpty() ) {
return null;
}
//Let's organize all string matches in groups by first letter:
final char n = name.charAt( 0 );
switch ( n ) {
case 'B': return caseB( name );
case 'J': return caseJ( name );
case 'W': return caseW( name );
case 'o': return caseLegacy( name, this );
default: return caseOthers( name );
}
}
private static Class<? extends JtaPlatform> caseB(final String name) {
if ( "Bitronix".equals( name ) ) {
return BitronixJtaPlatform.class;
}
if ( "Borland".equals( name ) ) {
return BorlandEnterpriseServerJtaPlatform.class;
}
return null;
}
private static Class<? extends JtaPlatform> caseJ(final String name) {
if ( "JBossAS".equals( name ) ) {
return JBossAppServerJtaPlatform.class;
}
if ( "JBossTS".equals( name ) ) {
return JBossStandAloneJtaPlatform.class;
}
if ( "JOnAS".equals( name ) ) {
return JOnASJtaPlatform.class;
}
if ( "JOTM".equals( name ) ) {
return JOTMJtaPlatform.class;
}
if ( "JRun4".equals( name ) ) {
return JRun4JtaPlatform.class;
}
return null;
}
private static Class<? extends JtaPlatform> caseW(final String name) {
if ( "Weblogic".equals( name ) ) {
return WeblogicJtaPlatform.class;
}
if ( "WebSphereLiberty".equals( name ) ) {
return WebSphereLibertyJtaPlatform.class;
}
if ( "WebSphere".equals( name ) ) {
return WebSphereJtaPlatform.class;
}
if ( "WebSphereExtended".equals( name ) ) {
return WebSphereExtendedJtaPlatform.class;
}
return null;
}
private static Class<? extends JtaPlatform> caseOthers(final String name) {
if ( "Atomikos".equals( name ) ) {
return AtomikosJtaPlatform.class;
}
if ( "OC4J".equals( name ) ) {
return OC4JJtaPlatform.class;
}
if ( "Orion".equals( name ) ) {
return OrionJtaPlatform.class;
}
if ( "Resin".equals( name ) ) {
return ResinJtaPlatform.class;
}
if ( "SapNetWeaver".equals( name ) ) {
return SapNetWeaverJtaPlatform.class;
}
if ( "SunOne".equals( name ) ) {
return SunOneJtaPlatform.class;
}
return null;
}
/**
* Special case: we have several old fully qualified classnames which need to
* be remapped to their new names for backwards compatibility reasons.
* @param name
* @param defaultJtaPlatformSelector
* @return
*/
private static Class<? extends JtaPlatform> caseLegacy(
final String name,
final DefaultJtaPlatformSelector defaultJtaPlatformSelector) {
//First, let's deal with the special cases which don't follow any recognizable pattern:
if ( name.equals( "org.hibernate.service.jta.platform.internal.BorlandEnterpriseServerJtaPlatform" ) ) {
return BorlandEnterpriseServerJtaPlatform.class;
}
if ( name.equals( "org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform" ) ) {
return JBossAppServerJtaPlatform.class;
}
if ( name.equals( "org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform" ) ) {
return JBossStandAloneJtaPlatform.class;
}
//This one shouldn't be necessary as it matches the implementation FQN, but let's translate the existing
//code faithfully.
if ( name.equals( "org.hibernate.engine.transaction.jta.platform.internal.WebSphereLibertyJtaPlatform" ) ) {
return WebSphereLibertyJtaPlatform.class;
}
//All other ones follow a pattern, beginning with the same prefix and ending with the same postfix,
//if your remove those the remaining section happens to match the short name.
final String LEGACY_PREFIX = "org.hibernate.service.jta.platform.internal.";
final String LEGACY_POSTFIX = "JtaPlatform";
//All these follow the same pattern, allowing us to use recursion into the main method:
if ( name.startsWith( LEGACY_PREFIX ) && name.endsWith( LEGACY_POSTFIX ) ) {
final String cleanName = name.substring( LEGACY_PREFIX.length(), name.length() - LEGACY_POSTFIX.length() );
return defaultJtaPlatformSelector.resolve( cleanName );
}
return null;
}
}

View File

@ -24,23 +24,6 @@ import org.hibernate.cache.internal.DefaultCacheKeysFactory;
import org.hibernate.cache.internal.SimpleCacheKeysFactory;
import org.hibernate.cache.spi.CacheKeysFactory;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.transaction.jta.platform.internal.AtomikosJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.BitronixJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.BorlandEnterpriseServerJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.JBossAppServerJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.JBossStandAloneJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.JOTMJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.JOnASJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.JRun4JtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.OC4JJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.OrionJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.ResinJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.SapNetWeaverJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.SunOneJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.WebSphereExtendedJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.WebSphereJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.WebSphereLibertyJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.WeblogicJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform;
import org.hibernate.hql.spi.id.MultiTableBulkIdStrategy;
import org.hibernate.hql.spi.id.global.GlobalTemporaryTableBulkIdStrategy;
@ -112,7 +95,7 @@ public class StrategySelectorBuilder {
// build the baseline...
strategySelector.registerStrategyLazily( Dialect.class, new DefaultDialectSelector() );
addJtaPlatforms( strategySelector );
strategySelector.registerStrategyLazily( JtaPlatform.class, new DefaultJtaPlatformSelector() );
addTransactionCoordinatorBuilders( strategySelector );
addMultiTableBulkIdStrategies( strategySelector );
addImplicitNamingStrategies( strategySelector );
@ -144,133 +127,6 @@ public class StrategySelectorBuilder {
}
}
private void addJtaPlatforms(StrategySelectorImpl strategySelector) {
addJtaPlatforms(
strategySelector,
AtomikosJtaPlatform.class,
"Atomikos",
"org.hibernate.service.jta.platform.internal.AtomikosJtaPlatform"
);
addJtaPlatforms(
strategySelector,
BorlandEnterpriseServerJtaPlatform.class,
"Borland",
"org.hibernate.service.jta.platform.internal.BorlandEnterpriseServerJtaPlatform"
);
addJtaPlatforms(
strategySelector,
BitronixJtaPlatform.class,
"Bitronix",
"org.hibernate.service.jta.platform.internal.BitronixJtaPlatform"
);
addJtaPlatforms(
strategySelector,
JBossAppServerJtaPlatform.class,
"JBossAS",
"org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform"
);
addJtaPlatforms(
strategySelector,
JBossStandAloneJtaPlatform.class,
"JBossTS",
"org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform"
);
addJtaPlatforms(
strategySelector,
JOnASJtaPlatform.class,
"JOnAS",
"org.hibernate.service.jta.platform.internal.JOnASJtaPlatform"
);
addJtaPlatforms(
strategySelector,
JOTMJtaPlatform.class,
"JOTM",
"org.hibernate.service.jta.platform.internal.JOTMJtaPlatform"
);
addJtaPlatforms(
strategySelector,
JRun4JtaPlatform.class,
"JRun4",
"org.hibernate.service.jta.platform.internal.JRun4JtaPlatform"
);
addJtaPlatforms(
strategySelector,
OC4JJtaPlatform.class,
"OC4J",
"org.hibernate.service.jta.platform.internal.OC4JJtaPlatform"
);
addJtaPlatforms(
strategySelector,
OrionJtaPlatform.class,
"Orion",
"org.hibernate.service.jta.platform.internal.OrionJtaPlatform"
);
addJtaPlatforms(
strategySelector,
ResinJtaPlatform.class,
"Resin",
"org.hibernate.service.jta.platform.internal.ResinJtaPlatform"
);
addJtaPlatforms(
strategySelector,
SapNetWeaverJtaPlatform.class,
"SapNetWeaver",
"org.hibernate.service.jta.platform.internal.SapNetWeaverJtaPlatform"
);
addJtaPlatforms(
strategySelector,
SunOneJtaPlatform.class,
"SunOne",
"org.hibernate.service.jta.platform.internal.SunOneJtaPlatform"
);
addJtaPlatforms(
strategySelector,
WeblogicJtaPlatform.class,
"Weblogic",
"org.hibernate.service.jta.platform.internal.WeblogicJtaPlatform"
);
addJtaPlatforms(
strategySelector,
WebSphereLibertyJtaPlatform.class,
"WebSphereLiberty",
"org.hibernate.engine.transaction.jta.platform.internal.WebSphereLibertyJtaPlatform"
);
addJtaPlatforms(
strategySelector,
WebSphereJtaPlatform.class,
"WebSphere",
"org.hibernate.service.jta.platform.internal.WebSphereJtaPlatform"
);
addJtaPlatforms(
strategySelector,
WebSphereExtendedJtaPlatform.class,
"WebSphereExtended",
"org.hibernate.service.jta.platform.internal.WebSphereExtendedJtaPlatform"
);
}
private void addJtaPlatforms(StrategySelectorImpl strategySelector, Class<? extends JtaPlatform> impl, String... names) {
for ( String name : names ) {
strategySelector.registerStrategyImplementor( JtaPlatform.class, name, impl );
}
}
private void addTransactionCoordinatorBuilders(StrategySelectorImpl strategySelector) {
strategySelector.registerStrategyImplementor(
TransactionCoordinatorBuilder.class,

View File

@ -0,0 +1,174 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.strategyselectors;
import org.hibernate.boot.registry.selector.internal.DefaultJtaPlatformSelector;
import org.hibernate.engine.transaction.jta.platform.internal.AtomikosJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.BitronixJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.BorlandEnterpriseServerJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.JBossAppServerJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.JBossStandAloneJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.JOTMJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.JOnASJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.JRun4JtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.OC4JJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.OrionJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.ResinJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.SapNetWeaverJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.SunOneJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.WebSphereExtendedJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.WebSphereJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.WebSphereLibertyJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.internal.WeblogicJtaPlatform;
import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform;
import org.junit.Assert;
import org.junit.Test;
public class JtaPlatformSelectorTest {
private final DefaultJtaPlatformSelector strategySelector = new DefaultJtaPlatformSelector();
@Test
public void verifyAllJtaPlatformResolve() {
// N.B. it might seem that there is some reduncancy, but it's not the case: the FQNs listed
// here are legacy full classnames which are being re-mapped to the right class for backwards
// compatibility reasons.
testJtaPlatformResolves(
strategySelector,
AtomikosJtaPlatform.class,
"Atomikos",
"org.hibernate.service.jta.platform.internal.AtomikosJtaPlatform"
);
testJtaPlatformResolves(
strategySelector,
BorlandEnterpriseServerJtaPlatform.class,
"Borland",
"org.hibernate.service.jta.platform.internal.BorlandEnterpriseServerJtaPlatform"
);
testJtaPlatformResolves(
strategySelector,
BitronixJtaPlatform.class,
"Bitronix",
"org.hibernate.service.jta.platform.internal.BitronixJtaPlatform"
);
testJtaPlatformResolves(
strategySelector,
JBossAppServerJtaPlatform.class,
"JBossAS",
"org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform"
);
testJtaPlatformResolves(
strategySelector,
JBossStandAloneJtaPlatform.class,
"JBossTS",
"org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform"
);
testJtaPlatformResolves(
strategySelector,
JOnASJtaPlatform.class,
"JOnAS",
"org.hibernate.service.jta.platform.internal.JOnASJtaPlatform"
);
testJtaPlatformResolves(
strategySelector,
JOTMJtaPlatform.class,
"JOTM",
"org.hibernate.service.jta.platform.internal.JOTMJtaPlatform"
);
testJtaPlatformResolves(
strategySelector,
JRun4JtaPlatform.class,
"JRun4",
"org.hibernate.service.jta.platform.internal.JRun4JtaPlatform"
);
testJtaPlatformResolves(
strategySelector,
OC4JJtaPlatform.class,
"OC4J",
"org.hibernate.service.jta.platform.internal.OC4JJtaPlatform"
);
testJtaPlatformResolves(
strategySelector,
OrionJtaPlatform.class,
"Orion",
"org.hibernate.service.jta.platform.internal.OrionJtaPlatform"
);
testJtaPlatformResolves(
strategySelector,
ResinJtaPlatform.class,
"Resin",
"org.hibernate.service.jta.platform.internal.ResinJtaPlatform"
);
testJtaPlatformResolves(
strategySelector,
SapNetWeaverJtaPlatform.class,
"SapNetWeaver",
"org.hibernate.service.jta.platform.internal.SapNetWeaverJtaPlatform"
);
testJtaPlatformResolves(
strategySelector,
SunOneJtaPlatform.class,
"SunOne",
"org.hibernate.service.jta.platform.internal.SunOneJtaPlatform"
);
testJtaPlatformResolves(
strategySelector,
WeblogicJtaPlatform.class,
"Weblogic",
"org.hibernate.service.jta.platform.internal.WeblogicJtaPlatform"
);
testJtaPlatformResolves(
strategySelector,
WebSphereLibertyJtaPlatform.class,
"WebSphereLiberty",
"org.hibernate.engine.transaction.jta.platform.internal.WebSphereLibertyJtaPlatform"
);
testJtaPlatformResolves(
strategySelector,
WebSphereJtaPlatform.class,
"WebSphere",
"org.hibernate.service.jta.platform.internal.WebSphereJtaPlatform"
);
testJtaPlatformResolves(
strategySelector,
WebSphereExtendedJtaPlatform.class,
"WebSphereExtended",
"org.hibernate.service.jta.platform.internal.WebSphereExtendedJtaPlatform"
);
}
private static void testJtaPlatformResolves(final DefaultJtaPlatformSelector strategySelector, final Class expectedType, final String shortname, final String longname) {
expectResolution(strategySelector, expectedType, shortname);
expectResolution(strategySelector, expectedType, longname);
}
private static void expectResolution(final DefaultJtaPlatformSelector strategySelector, final Class expectedType, final String name) {
Class<? extends JtaPlatform> aClass = strategySelector.resolve( name );
Assert.assertNotNull( aClass );
Assert.assertEquals( expectedType, aClass );
}
}