Search in sources :

Example 1 with PostConstructCallback

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

the class PostConstructAnnotationHandler method doHandle.

public void doHandle(Class clazz) {
    //Check that the PostConstruct is on a class that we're interested in
    if (supportsPostConstruct(clazz)) {
        Method[] methods = clazz.getDeclaredMethods();
        for (int i = 0; i < methods.length; i++) {
            Method m = (Method) methods[i];
            if (m.isAnnotationPresent(PostConstruct.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 post-construct then all post-constructs
                //in fragments must be ignored. Otherwise, they are additive.
                MetaData metaData = _context.getMetaData();
                Origin origin = metaData.getOrigin("post-construct");
                if (origin != null && (origin == Origin.WebXml || origin == Origin.WebDefaults || origin == Origin.WebOverride))
                    return;
                PostConstructCallback callback = new PostConstructCallback();
                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) PostConstructCallback(org.eclipse.jetty.plus.annotation.PostConstructCallback) Method(java.lang.reflect.Method)

Example 2 with PostConstructCallback

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

the class PlusDescriptorProcessor method visitPostConstruct.

/**
     * If web.xml has at least 1 post-construct, then all post-constructs in fragments
     * are ignored. Otherwise, post-constructs from fragments are merged.
     * post-construct is the name of a class and method to call after all
     * resources have been setup but before the class is put into use
     * 
     * @param context the context 
     * @param descriptor the descriptor 
     * @param node the xml node
     */
public void visitPostConstruct(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");
        return;
    }
    if (methodName == null || methodName.equals("")) {
        LOG.warn("No lifecycle-callback-method specified for class " + className);
        return;
    }
    //ServletSpec 3.0 p80 If web.xml declares a post-construct then all post-constructs
    //in fragments must be ignored. Otherwise, they are additive.
    Origin o = context.getMetaData().getOrigin("post-construct");
    switch(o) {
        case NotSet:
            {
                //No post-constructs have been declared previously.
                context.getMetaData().setOrigin("post-construct", descriptor);
                try {
                    Class<?> clazz = context.loadClass(className);
                    LifeCycleCallback callback = new PostConstructCallback();
                    callback.setTarget(clazz, methodName);
                    ((LifeCycleCallbackCollection) context.getAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION)).add(callback);
                } catch (ClassNotFoundException e) {
                    LOG.warn("Couldn't load post-construct target class " + className);
                }
                break;
            }
        case WebXml:
        case WebDefaults:
        case WebOverride:
            {
                //to add to it
                if (!(descriptor instanceof FragmentDescriptor)) {
                    try {
                        Class<?> clazz = context.loadClass(className);
                        LifeCycleCallback callback = new PostConstructCallback();
                        callback.setTarget(clazz, methodName);
                        ((LifeCycleCallbackCollection) context.getAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION)).add(callback);
                    } catch (ClassNotFoundException e) {
                        LOG.warn("Couldn't load post-construct target class " + className);
                    }
                }
                break;
            }
        case WebFragment:
            {
                //A web-fragment first declared a post-construct. Allow all other web-fragments to merge in their post-constructs
                try {
                    Class<?> clazz = context.loadClass(className);
                    LifeCycleCallback callback = new PostConstructCallback();
                    callback.setTarget(clazz, methodName);
                    ((LifeCycleCallbackCollection) context.getAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION)).add(callback);
                } catch (ClassNotFoundException e) {
                    LOG.warn("Couldn't load post-construct target class " + className);
                }
                break;
            }
    }
}
Also used : Origin(org.eclipse.jetty.webapp.Origin) PostConstructCallback(org.eclipse.jetty.plus.annotation.PostConstructCallback) LifeCycleCallback(org.eclipse.jetty.plus.annotation.LifeCycleCallback) FragmentDescriptor(org.eclipse.jetty.webapp.FragmentDescriptor)

Aggregations

PostConstructCallback (org.eclipse.jetty.plus.annotation.PostConstructCallback)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