Search in sources :

Example 1 with PreDestroyCallback

use of org.eclipse.jetty.plus.annotation.PreDestroyCallback in project jetty.project by eclipse.

the class PreDestroyAnnotationHandler method doHandle.

public void doHandle(Class clazz) {
    //Check that the PreDestroy is on a class that we're interested in
    if (supportsPreDestroy(clazz)) {
        Method[] methods = clazz.getDeclaredMethods();
        for (int i = 0; i < methods.length; i++) {
            Method m = (Method) methods[i];
            if (m.isAnnotationPresent(PreDestroy.class)) {
                if (m.getParameterCount() != 0)
                    throw new IllegalStateException(m + " has parameters");
                if (m.getReturnType() != Void.TYPE)
                    throw new IllegalStateException(m + " is not void");
                if (m.getExceptionTypes().length != 0)
                    throw new IllegalStateException(m + " throws checked exceptions");
                if (Modifier.isStatic(m.getModifiers()))
                    throw new IllegalStateException(m + " is static");
                //ServletSpec 3.0 p80 If web.xml declares even one predestroy then all predestroys
                //in fragments must be ignored. Otherwise, they are additive.
                MetaData metaData = _context.getMetaData();
                Origin origin = metaData.getOrigin("pre-destroy");
                if (origin != null && (origin == Origin.WebXml || origin == Origin.WebDefaults || origin == Origin.WebOverride))
                    return;
                PreDestroyCallback callback = new PreDestroyCallback();
                callback.setTarget(clazz.getName(), m.getName());
                LifeCycleCallbackCollection lifecycles = (LifeCycleCallbackCollection) _context.getAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION);
                if (lifecycles == null) {
                    lifecycles = new LifeCycleCallbackCollection();
                    _context.setAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION, lifecycles);
                }
                lifecycles.add(callback);
            }
        }
    }
}
Also used : Origin(org.eclipse.jetty.webapp.Origin) LifeCycleCallbackCollection(org.eclipse.jetty.plus.annotation.LifeCycleCallbackCollection) MetaData(org.eclipse.jetty.webapp.MetaData) PreDestroyCallback(org.eclipse.jetty.plus.annotation.PreDestroyCallback) Method(java.lang.reflect.Method)

Example 2 with PreDestroyCallback

use of org.eclipse.jetty.plus.annotation.PreDestroyCallback in project jetty.project by eclipse.

the class PlusDescriptorProcessor method visitPreDestroy.

/**
     *
     * pre-destroy is the name of a class and method to call just as
     * the instance is being destroyed
     * 
     * @param context the context 
     * @param descriptor the descriptor 
     * @param node the xml node
     */
public void visitPreDestroy(WebAppContext context, Descriptor descriptor, XmlParser.Node node) {
    String className = node.getString("lifecycle-callback-class", false, true);
    String methodName = node.getString("lifecycle-callback-method", false, true);
    if (className == null || className.equals("")) {
        LOG.warn("No lifecycle-callback-class specified for pre-destroy");
        return;
    }
    if (methodName == null || methodName.equals("")) {
        LOG.warn("No lifecycle-callback-method specified for pre-destroy class " + className);
        return;
    }
    Origin o = context.getMetaData().getOrigin("pre-destroy");
    switch(o) {
        case NotSet:
            {
                //No pre-destroys have been declared previously. Record this descriptor
                //as the first declarer.
                context.getMetaData().setOrigin("pre-destroy", descriptor);
                try {
                    Class<?> clazz = context.loadClass(className);
                    LifeCycleCallback callback = new PreDestroyCallback();
                    callback.setTarget(clazz, methodName);
                    ((LifeCycleCallbackCollection) context.getAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION)).add(callback);
                } catch (ClassNotFoundException e) {
                    LOG.warn("Couldn't load pre-destory target class " + className);
                }
                break;
            }
        case WebXml:
        case WebDefaults:
        case WebOverride:
            {
                //(not web-fragments) to add to them.
                if (!(descriptor instanceof FragmentDescriptor)) {
                    try {
                        Class<?> clazz = context.loadClass(className);
                        LifeCycleCallback callback = new PreDestroyCallback();
                        callback.setTarget(clazz, methodName);
                        ((LifeCycleCallbackCollection) context.getAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION)).add(callback);
                    } catch (ClassNotFoundException e) {
                        LOG.warn("Couldn't load pre-destory target class " + className);
                    }
                }
                break;
            }
        case WebFragment:
            {
                //No pre-destroys in web xml, so allow all fragments to merge their pre-destroys.
                try {
                    Class<?> clazz = context.loadClass(className);
                    LifeCycleCallback callback = new PreDestroyCallback();
                    callback.setTarget(clazz, methodName);
                    ((LifeCycleCallbackCollection) context.getAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION)).add(callback);
                } catch (ClassNotFoundException e) {
                    LOG.warn("Couldn't load pre-destory target class " + className);
                }
                break;
            }
    }
}
Also used : Origin(org.eclipse.jetty.webapp.Origin) PreDestroyCallback(org.eclipse.jetty.plus.annotation.PreDestroyCallback) LifeCycleCallback(org.eclipse.jetty.plus.annotation.LifeCycleCallback) FragmentDescriptor(org.eclipse.jetty.webapp.FragmentDescriptor)

Aggregations

PreDestroyCallback (org.eclipse.jetty.plus.annotation.PreDestroyCallback)2 Origin (org.eclipse.jetty.webapp.Origin)2 Method (java.lang.reflect.Method)1 LifeCycleCallback (org.eclipse.jetty.plus.annotation.LifeCycleCallback)1 LifeCycleCallbackCollection (org.eclipse.jetty.plus.annotation.LifeCycleCallbackCollection)1 FragmentDescriptor (org.eclipse.jetty.webapp.FragmentDescriptor)1 MetaData (org.eclipse.jetty.webapp.MetaData)1