Skip to content

Commit

Permalink
No-reflection initialization of DefaultHazelcastInstanceFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
pivovarit committed Jun 23, 2020
1 parent b5d8110 commit ca99591
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.Properties;

import static com.hazelcast.hibernate.CacheEnvironment.getCacheCleanup;
import static java.lang.Class.forName;

/**
* Abstract superclass of Hazelcast based {@link RegionFactory} implementations
Expand Down Expand Up @@ -81,20 +82,26 @@ public long nextTimestamp() {
public void start(final SessionFactoryOptions options, final Properties properties) throws CacheException {
log.info("Starting up " + getClass().getSimpleName());
if (instance == null || !instance.getLifecycleService().isRunning()) {
String defaultFactory = DefaultHazelcastInstanceFactory.class.getName();
String factoryName = properties.getProperty(CacheEnvironment.HAZELCAST_FACTORY, defaultFactory);
instanceLoader = resolveInstanceLoader(properties);
instance = instanceLoader.loadInstance();
}

cleanupService = new CleanupService(instance.getName(), getCacheCleanup(properties));
}

private IHazelcastInstanceLoader resolveInstanceLoader(Properties properties) {
String factoryName = properties.getProperty(CacheEnvironment.HAZELCAST_FACTORY);
if (factoryName != null) {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
try {
Class<IHazelcastInstanceFactory> factory =
(Class<IHazelcastInstanceFactory>) Class.forName(factoryName, true, cl);
instanceLoader = factory.newInstance().createInstanceLoader(properties);
Class<IHazelcastInstanceFactory> factory = (Class<IHazelcastInstanceFactory>) forName(factoryName, true, cl);
return factory.newInstance().createInstanceLoader(properties);
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
throw new CacheException("Failed to set up hazelcast instance factory", e);
}
instance = instanceLoader.loadInstance();
} else {
return new DefaultHazelcastInstanceFactory().createInstanceLoader(properties);
}

cleanupService = new CleanupService(instance.getName(), getCacheCleanup(properties));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.Properties;

import static com.hazelcast.hibernate.CacheEnvironment.getCacheCleanup;
import static java.lang.Class.forName;

/**
* Abstract superclass of Hazelcast based {@link RegionFactory} implementations
Expand Down Expand Up @@ -81,20 +82,26 @@ public long nextTimestamp() {
public void start(final SessionFactoryOptions options, final Properties properties) throws CacheException {
log.info("Starting up " + getClass().getSimpleName());
if (instance == null || !instance.getLifecycleService().isRunning()) {
String defaultFactory = DefaultHazelcastInstanceFactory.class.getName();
String factoryName = properties.getProperty(CacheEnvironment.HAZELCAST_FACTORY, defaultFactory);
instanceLoader = resolveInstanceLoader(properties);
instance = instanceLoader.loadInstance();
}

cleanupService = new CleanupService(instance.getName(), getCacheCleanup(properties));
}

private IHazelcastInstanceLoader resolveInstanceLoader(Properties properties) {
String factoryName = properties.getProperty(CacheEnvironment.HAZELCAST_FACTORY);
if (factoryName != null) {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
try {
Class<IHazelcastInstanceFactory> factory =
(Class<IHazelcastInstanceFactory>) Class.forName(factoryName, true, cl);
instanceLoader = factory.newInstance().createInstanceLoader(properties);
Class<IHazelcastInstanceFactory> factory = (Class<IHazelcastInstanceFactory>) forName(factoryName, true, cl);
return factory.newInstance().createInstanceLoader(properties);
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
throw new CacheException("Failed to set up hazelcast instance factory", e);
}
instance = instanceLoader.loadInstance();
} else {
return new DefaultHazelcastInstanceFactory().createInstanceLoader(properties);
}

cleanupService = new CleanupService(instance.getName(), getCacheCleanup(properties));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

import static com.hazelcast.hibernate.CacheEnvironment.HAZELCAST_FACTORY;
import static com.hazelcast.hibernate.CacheEnvironment.getCacheCleanup;
import static java.lang.Class.forName;

/**
* Simple RegionFactory implementation to return Hazelcast based local Region implementations
Expand Down Expand Up @@ -133,30 +134,30 @@ protected boolean isStarted() {
return super.isStarted() && instance.getLifecycleService().isRunning();
}

@SuppressWarnings("unchecked")
@Override
protected void prepareForUse(final SessionFactoryOptions settings, final Map configValues) {
log.info("Starting up " + getClass().getSimpleName());
if (instance == null || !instance.getLifecycleService().isRunning()) {
final String defaultFactory = DefaultHazelcastInstanceFactory.class.getName();
instanceLoader = resolveInstanceLoader(toProperties(configValues));
instance = instanceLoader.loadInstance();
}

String factoryName = (String) configValues.get(HAZELCAST_FACTORY);
if (factoryName == null) {
factoryName = defaultFactory;
}
cleanupService = new CleanupService(instance.getName(), getCacheCleanup(toProperties(configValues)));
}

final ClassLoader cl = Thread.currentThread().getContextClassLoader();
private IHazelcastInstanceLoader resolveInstanceLoader(Properties properties) {
String factoryName = properties.getProperty(CacheEnvironment.HAZELCAST_FACTORY);
if (factoryName != null) {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
try {
final Class<IHazelcastInstanceFactory> factory =
(Class<IHazelcastInstanceFactory>) Class.forName(factoryName, true, cl);
instanceLoader = factory.newInstance().createInstanceLoader(toProperties(configValues));
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
Class<IHazelcastInstanceFactory> factory = (Class<IHazelcastInstanceFactory>) forName(factoryName, true, cl);
return factory.newInstance().createInstanceLoader(properties);
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
throw new CacheException("Failed to set up hazelcast instance factory", e);
}
instance = instanceLoader.loadInstance();
} else {
return new DefaultHazelcastInstanceFactory().createInstanceLoader(properties);
}

cleanupService = new CleanupService(instance.getName(), getCacheCleanup(toProperties(configValues)));
}

@SuppressWarnings("Duplicates")
Expand Down

0 comments on commit ca99591

Please sign in to comment.