Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No-reflection initialization of DefaultHazelcastInstanceFactory #185

Merged
merged 1 commit into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -38,8 +38,8 @@
import java.util.Map;
import java.util.Properties;

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 +133,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