FileDocCategorySizeDatePackage
MetadataEntityListener.javaAPI DocGlassfish v2 API17696Thu Jul 19 11:51:58 BST 2007oracle.toplink.essentials.internal.ejb.cmp3.metadata.listeners

MetadataEntityListener

public class MetadataEntityListener extends DescriptorEventAdapter
A MetadataEntityListener and is placed on the owning entity's descriptor. Callback methods from an EntityListener require a signature on the method. Namely, they must have an Object parameter.
author
Guy Pelletier
since
TopLink 10.1.3/EJB 3.0 Preview

Fields Summary
public static final String
POST_BUILD
public static final String
POST_CLONE
public static final String
POST_DELETE
public static final String
POST_INSERT
public static final String
POST_REFRESH
public static final String
POST_UPDATE
public static final String
PRE_PERSIST
public static final String
PRE_REMOVE
public static final String
PRE_UPDATE_WITH_CHANGES
private Object
m_listener
private Class
m_entityClass
private Hashtable
m_methods
private Hashtable
m_overriddenEvents
private static Hashtable
m_eventStrings
Constructors Summary
protected MetadataEntityListener()
INTERNAL: The default constructor should not be used.

    
                  
      
        m_methods = new Hashtable<String, Method>();
        
        // Remember which events are overridden in subclasses.
        m_overriddenEvents = new Hashtable<Integer, Boolean>();
        
        // For quick look up of equivalent event strings from event codes.
        if (m_eventStrings == null) {
            m_eventStrings = new Hashtable<Integer, String>();   
            m_eventStrings.put(new Integer(DescriptorEventManager.PostBuildEvent), POST_BUILD);
            m_eventStrings.put(new Integer(DescriptorEventManager.PostCloneEvent), POST_CLONE);
            m_eventStrings.put(new Integer(DescriptorEventManager.PostDeleteEvent), POST_DELETE);
            m_eventStrings.put(new Integer(DescriptorEventManager.PostInsertEvent), POST_INSERT);
            m_eventStrings.put(new Integer(DescriptorEventManager.PostRefreshEvent), POST_REFRESH);
            m_eventStrings.put(new Integer(DescriptorEventManager.PostUpdateEvent), POST_UPDATE);
            m_eventStrings.put(new Integer(DescriptorEventManager.PrePersistEvent), PRE_PERSIST);
            m_eventStrings.put(new Integer(DescriptorEventManager.PreRemoveEvent), PRE_REMOVE);
            m_eventStrings.put(new Integer(DescriptorEventManager.PreUpdateWithChangesEvent), PRE_UPDATE_WITH_CHANGES);
        }    	
    
protected MetadataEntityListener(Class entityClass)
INTERNAL:

    	this();
        m_entityClass = entityClass;
    
public MetadataEntityListener(Class listenerClass, Class entityClass)
INTERNAL:

 
        this(entityClass);
        
        try {
            m_listener = listenerClass.newInstance();
        } catch (Exception ex) {
            ValidationException.errorInstantiatingClass(listenerClass, ex);
        }
    
Methods Summary
public voidaddEventMethod(java.lang.String event, java.lang.reflect.Method method)
INTERNAL:

        validateMethod(method);
        
        if (m_methods.containsKey(event)) {
            throw ValidationException.multipleLifecycleCallbackMethodsForSameLifecycleEvent(getListenerClass(), method, getEventMethod(event));
        } else {
            m_methods.put(event, method);
        }
    
public java.lang.ClassgetEntityClass()
INTERNAL:

        return m_entityClass;
    
protected java.lang.reflect.MethodgetEventMethod(int eventCode)
INTERNAL:

        String eventString = m_eventStrings.get(eventCode);
        
        if (eventString != null) {
            return getEventMethod(eventString);
        } else {
            return null;
        }
    
protected java.lang.reflect.MethodgetEventMethod(java.lang.String event)
INTERNAL:

        return m_methods.get(event);
    
protected java.lang.StringgetEventMethodName(java.lang.String eventName)
INTERNAL:

        Method method = getEventMethod(eventName);
        
        if (method != null) {
            return method.getName();
        } else {
            return null;
        }
    
public java.lang.ClassgetListenerClass()
INTERNAL:

        return m_listener.getClass();    
    
public java.util.HashtablegetMethods()
INTERNAL:

        return m_methods;    
    
public java.lang.StringgetPostBuildMethodName()
INTERNAL:

        return getEventMethodName(POST_BUILD);
    
public java.lang.StringgetPostCloneMethodName()
INTERNAL:

        return getEventMethodName(POST_CLONE);
    
public java.lang.StringgetPostDeleteMethodName()
INTERNAL:

        return getEventMethodName(POST_DELETE);
    
public java.lang.StringgetPostInsertMethodName()
INTERNAL:

        return getEventMethodName(POST_INSERT);
    
public java.lang.StringgetPostRefreshMethodName()
INTERNAL:

        return getEventMethodName(POST_REFRESH);
    
public java.lang.StringgetPostUpdateMethodName()
INTERNAL:

        return getEventMethodName(POST_UPDATE);
    
public java.lang.StringgetPrePersistMethodName()
INTERNAL:

        return getEventMethodName(PRE_PERSIST);
    
public java.lang.StringgetPreRemoveMethodName()
INTERNAL:

        return getEventMethodName(PRE_REMOVE);
    
public java.lang.StringgetPreUpdateWithChangesMethodName()
INTERNAL:

        return getEventMethodName(PRE_UPDATE_WITH_CHANGES);
    
public booleanhasCallbackMethods()
INTERNAL:

        return m_methods.size() > 0;
    
public booleanhasOverriddenEventMethod(java.lang.reflect.Method eventMethod, int eventCode)
INTERNAL:

        return hasOverriddenEventMethod(getEventMethod(eventCode), eventMethod);
    
protected booleanhasOverriddenEventMethod(java.lang.reflect.Method eventMethod1, java.lang.reflect.Method eventMethod2)
INTERNAL:

        return (eventMethod1 != null && eventMethod1.getName().equals(eventMethod2.getName()));
    
public booleanhasOverriddenEventMethod(java.lang.reflect.Method eventMethod, java.lang.String eventCode)
INTERNAL:

        return hasOverriddenEventMethod(getEventMethod(eventCode), eventMethod);
    
public voidinitializeCallbackMethods(java.lang.ClassLoader classLoader)
INTERNAL: Sub-classes should implement where necessary.

protected voidinvokeMethod(java.lang.reflect.Method method, java.lang.Object onObject, java.lang.Object[] objectList, oracle.toplink.essentials.descriptors.DescriptorEvent event)
INTERNAL:

        //if (method != null && ! isSessionPostBuildEvent(event)) {
        if (method != null) {
            try {
                if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
                    try {
                        AccessController.doPrivileged(new PrivilegedMethodInvoker(method, onObject, objectList));
                    } catch (PrivilegedActionException exception) {
                        Exception throwableException = exception.getException();
                        if (throwableException instanceof IllegalAccessException) {
                            throw ValidationException.invalidCallbackMethod(onObject.getClass(), method.toString());
                        } else {
                            Throwable cause = throwableException.getCause();
                            
                            if (cause instanceof RuntimeException) {
                                throw (RuntimeException) cause;    
                            } else {
                                throw (Error) cause;
                            }                         }
                    }
                } else {
                    PrivilegedAccessHelper.invokeMethod(method, onObject, objectList);
                }
            } catch (IllegalAccessException exception) {
                // WIP throw a better exception string.
                throw ValidationException.invalidCallbackMethod(onObject.getClass(), method.toString());
            } catch (InvocationTargetException e) {
                Throwable cause = e.getCause();
                
                if (cause instanceof RuntimeException) {
                    throw (RuntimeException) cause;    
                } else {
                    throw (Error) cause;
                }
            }
        }
    
protected voidinvokeMethod(java.lang.String event, oracle.toplink.essentials.descriptors.DescriptorEvent descriptorEvent)
INTERNAL:

        Object[] objectList = { descriptorEvent.getSource() };
        invokeMethod(getEventMethod(event), m_listener, objectList, descriptorEvent);
    
public booleanisEntityClassListener()
INTERNAL:

        return false;
    
public booleanisOverriddenEvent(oracle.toplink.essentials.descriptors.DescriptorEvent event, java.util.Vector eventManagers)
INTERNAL: Return true is listener has a lifecycle callback method that is overridden in a subclass.

        int eventCode = event.getEventCode();
        if (! m_overriddenEvents.containsKey(eventCode)) {
            m_overriddenEvents.put(eventCode, false); // default
            
            Method eventMethod = getEventMethod(eventCode);
        
            if (eventMethod != null) {
                for (DescriptorEventManager eventManager : (Vector<DescriptorEventManager>) eventManagers) {
                    MetadataEntityListener childListener = (MetadataEntityListener) eventManager.getEntityEventListener();
                    
                    // We can't override ourself ...
                    if (childListener == this) {
                        break;
                    } else {
                        if (childListener.hasOverriddenEventMethod(eventMethod, eventCode)) {
                            m_overriddenEvents.put(eventCode, true);
                            break; // stop looking
                        }
                    }
                }
            }
        }
        
        return m_overriddenEvents.get(eventCode);
    
protected booleanisSessionPostBuildEvent(oracle.toplink.essentials.descriptors.DescriptorEvent event)
INTERNAL:

        if (((String) m_eventStrings.get(event.getEventCode())).equals(POST_BUILD)) {
            return ! event.getSession().isUnitOfWork();
        }
        
        return false;
    
protected booleannoCallbackMethodAlreadySetFor(java.lang.String event, java.lang.reflect.Method method)
INTERNAL: Checks if the user already set this call back method via XML. We need to check because the set methods on the superclass will throw an exception when multiple methods are added for the callback.

        Method cbMethod = (Method) getMethods().get(event);
        
        if (cbMethod == null) {
            return true;
        } else {
            return ! cbMethod.getName().equals(method.getName());
        }
    
public voidpostBuild(oracle.toplink.essentials.descriptors.DescriptorEvent event)
INTERNAL:

        invokeMethod(POST_BUILD, event);
    
public voidpostClone(oracle.toplink.essentials.descriptors.DescriptorEvent event)
INTERNAL:

        invokeMethod(POST_CLONE, event);
    
public voidpostDelete(oracle.toplink.essentials.descriptors.DescriptorEvent event)
INTERNAL:

        invokeMethod(POST_DELETE, event);
    
public voidpostInsert(oracle.toplink.essentials.descriptors.DescriptorEvent event)
INTERNAL:

        invokeMethod(POST_INSERT, event);
    
public voidpostRefresh(oracle.toplink.essentials.descriptors.DescriptorEvent event)
INTERNAL:

        invokeMethod(POST_REFRESH, event);
    
public voidpostUpdate(oracle.toplink.essentials.descriptors.DescriptorEvent event)
INTERNAL:

        invokeMethod(POST_UPDATE, event);
    
public voidprePersist(oracle.toplink.essentials.descriptors.DescriptorEvent event)
INTERNAL:

        invokeMethod(PRE_PERSIST, event);
    
public voidpreRemove(oracle.toplink.essentials.descriptors.DescriptorEvent event)
INTERNAL:

        invokeMethod(PRE_REMOVE, event);
    
public voidpreUpdateWithChanges(oracle.toplink.essentials.descriptors.DescriptorEvent event)
INTERNAL:

        invokeMethod(PRE_UPDATE_WITH_CHANGES, event);
    
public voidsetEntityClass(java.lang.Class entityClass)
INTERNAL:

        m_entityClass = entityClass;
    
public voidsetListener(java.lang.Object listener)
INTERNAL:

        m_listener = listener;
    
public voidsetPostBuildMethod(java.lang.reflect.Method method)
INTERNAL:

        addEventMethod(POST_BUILD, method);
    
public voidsetPostCloneMethod(java.lang.reflect.Method method)
INTERNAL:

        addEventMethod(POST_CLONE, method);
    
public voidsetPostDeleteMethod(java.lang.reflect.Method method)
INTERNAL:

        addEventMethod(POST_DELETE, method);
    
public voidsetPostInsertMethod(java.lang.reflect.Method method)
INTERNAL:

        addEventMethod(POST_INSERT, method);
    
public voidsetPostRefreshMethod(java.lang.reflect.Method method)
INTERNAL:

        addEventMethod(POST_REFRESH, method);
    
public voidsetPostUpdateMethod(java.lang.reflect.Method method)
INTERNAL:

        addEventMethod(POST_UPDATE, method);
    
public voidsetPrePersistMethod(java.lang.reflect.Method method)
INTERNAL:

        addEventMethod(PRE_PERSIST, method);
    
public voidsetPreRemoveMethod(java.lang.reflect.Method method)
INTERNAL:

        addEventMethod(PRE_REMOVE, method);
    
public voidsetPreUpdateWithChangesMethod(java.lang.reflect.Method method)
INTERNAL:

        addEventMethod(PRE_UPDATE_WITH_CHANGES, method);
    
protected voidvalidateMethod(java.lang.reflect.Method method)
INTERNAL:

        int numberOfParameters = method.getParameterTypes().length;
           
        if (numberOfParameters == 1 && method.getParameterTypes()[0].isAssignableFrom(m_entityClass)) {
            // So far so good, now check the method modifiers.
            validateMethodModifiers(method);
        } else {
            Class parameterClass = (numberOfParameters == 0) ? null : method.getParameterTypes()[0];
            throw ValidationException.invalidEntityListenerCallbackMethodArguments(m_entityClass, parameterClass, m_listener.getClass(), method.getName());
        }
    
protected voidvalidateMethodModifiers(java.lang.reflect.Method method)
INTERNAL:

        int modifiers = method.getModifiers();
        
        if (Modifier.isStatic(modifiers) || Modifier.isFinal(modifiers)) {
            throw ValidationException.invalidCallbackMethodModifier(getListenerClass(), method.getName());
        }