use of javax.jdo.listener.InstanceLifecycleListener in project datanucleus-api-jdo by datanucleus.
the class JDOPersistenceManagerFactory method processLifecycleListenersFromProperties.
/**
* Convenience method to extract lifecycle listeners that are specified by way of persistence properties.
* @param props Persistence props.
*/
protected void processLifecycleListenersFromProperties(Map props) {
if (props != null) {
// Process any lifecycle listeners defined in persistent properties
Iterator<Map.Entry> propsIter = props.entrySet().iterator();
while (propsIter.hasNext()) {
Map.Entry entry = propsIter.next();
String key = (String) entry.getKey();
if (key.startsWith(Constants.PROPERTY_INSTANCE_LIFECYCLE_LISTENER)) {
String listenerClsName = key.substring(45);
String listenerClasses = (String) entry.getValue();
ClassLoaderResolver clr = nucleusContext.getClassLoaderResolver(null);
Class listenerCls = null;
try {
listenerCls = clr.classForName(listenerClsName);
} catch (ClassNotResolvedException cnre) {
throw new JDOUserException(Localiser.msg("012022", listenerClsName));
}
InstanceLifecycleListener listener = null;
// Find method getInstance()
Method method = ClassUtils.getMethodForClass(listenerCls, "getInstance", null);
if (method != null) {
// Create instance via getInstance()
try {
listener = (InstanceLifecycleListener) method.invoke(null);
} catch (Exception e) {
throw new JDOUserException(Localiser.msg("012021", listenerClsName), e);
}
} else {
// Try default constructor
try {
listener = (InstanceLifecycleListener) listenerCls.newInstance();
} catch (Exception e) {
throw new JDOUserException(Localiser.msg("012020", listenerClsName), e);
}
}
Class[] classes = null;
if (!StringUtils.isWhitespace(listenerClasses)) {
String[] classNames = StringUtils.split(listenerClasses, ",");
classes = new Class[classNames.length];
for (int i = 0; i < classNames.length; i++) {
classes[i] = clr.classForName(classNames[i]);
}
}
addInstanceLifecycleListener(listener, classes);
}
}
}
}
use of javax.jdo.listener.InstanceLifecycleListener in project datanucleus-api-jdo by datanucleus.
the class JDOCallbackHandler method addListener.
/**
* Adds a new listener to this handler.
* @param listener the listener instance
* @param classes the persistent classes which events are fired for the listener
*/
public void addListener(Object listener, Class[] classes) {
if (listener == null) {
return;
}
InstanceLifecycleListener jdoListener = (InstanceLifecycleListener) listener;
LifecycleListenerForClass entry;
if (listeners.containsKey(jdoListener)) {
entry = listeners.get(jdoListener).mergeClasses(classes);
} else {
entry = new LifecycleListenerForClass(jdoListener, classes);
}
listeners.put(jdoListener, entry);
listenersWorkingCopy = null;
}
Aggregations