package org.glassfish.grizzly.memory;
import org.glassfish.grizzly.Grizzly;
import java.util.logging.Level;
import java.util.logging.Logger;
import static org.glassfish.grizzly.memory.DefaultMemoryManagerFactory.DMMF_PROP_NAME;
class MemoryManagerInitializer {
private static final String DMM_PROP_NAME =
"org.glassfish.grizzly.DEFAULT_MEMORY_MANAGER";
private static final Logger LOGGER =
Grizzly.logger(MemoryManagerInitializer.class);
static MemoryManager initManager() {
final MemoryManager mm = initMemoryManagerViaFactory();
return (mm != null) ? mm : initMemoryManagerFallback();
}
@SuppressWarnings("unchecked")
private static MemoryManager initMemoryManagerViaFactory() {
String dmmfClassName = System.getProperty(DMMF_PROP_NAME);
if (dmmfClassName != null) {
final DefaultMemoryManagerFactory mmf = newInstance(dmmfClassName);
if (mmf != null) {
return mmf.createMemoryManager();
}
}
return null;
}
@SuppressWarnings("unchecked")
private static MemoryManager initMemoryManagerFallback() {
final String className = System.getProperty(DMM_PROP_NAME);
final MemoryManager mm = newInstance(className);
return (mm != null) ? mm : new HeapMemoryManager();
}
@SuppressWarnings("unchecked")
private static <T> T newInstance(final String className) {
if (className == null) {
return null;
}
try {
Class clazz =
Class.forName(className,
true,
MemoryManager.class.getClassLoader());
return (T) clazz.newInstance();
} catch (Exception e) {
if (LOGGER.isLoggable(Level.SEVERE)) {
LOGGER.log(Level.SEVERE,
"Unable to load or create a new instance of Class {0}. Cause: {1}",
new Object[]{className, e.getMessage()});
}
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.log(Level.FINE, e.toString(), e);
}
return null;
}
}
}