Search in sources :

Example 1 with InstanceLifecycleListener

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);
            }
        }
    }
}
Also used : ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) Method(java.lang.reflect.Method) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException) JDOUserException(javax.jdo.JDOUserException) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) InvalidObjectException(java.io.InvalidObjectException) JDOFatalUserException(javax.jdo.JDOFatalUserException) NucleusException(org.datanucleus.exceptions.NucleusException) InvocationTargetException(java.lang.reflect.InvocationTargetException) TransactionIsolationNotSupportedException(org.datanucleus.exceptions.TransactionIsolationNotSupportedException) TransactionActiveOnCloseException(org.datanucleus.exceptions.TransactionActiveOnCloseException) IOException(java.io.IOException) JDOUnsupportedOptionException(javax.jdo.JDOUnsupportedOptionException) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException) InstanceLifecycleListener(javax.jdo.listener.InstanceLifecycleListener) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 2 with InstanceLifecycleListener

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;
}
Also used : InstanceLifecycleListener(javax.jdo.listener.InstanceLifecycleListener)

Aggregations

InstanceLifecycleListener (javax.jdo.listener.InstanceLifecycleListener)2 IOException (java.io.IOException)1 InvalidObjectException (java.io.InvalidObjectException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 JDOException (javax.jdo.JDOException)1 JDOFatalUserException (javax.jdo.JDOFatalUserException)1 JDOUnsupportedOptionException (javax.jdo.JDOUnsupportedOptionException)1 JDOUserException (javax.jdo.JDOUserException)1 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)1 ClassNotResolvedException (org.datanucleus.exceptions.ClassNotResolvedException)1 NucleusException (org.datanucleus.exceptions.NucleusException)1 TransactionActiveOnCloseException (org.datanucleus.exceptions.TransactionActiveOnCloseException)1 TransactionIsolationNotSupportedException (org.datanucleus.exceptions.TransactionIsolationNotSupportedException)1