Search in sources :

Example 6 with FragmentDescriptor

use of org.eclipse.jetty.webapp.FragmentDescriptor in project jetty.project by eclipse.

the class PlusDescriptorProcessor method visitResourceEnvRef.

/**
     * Common Annotations Spec section 2.3:
     * <p>
     * resource-env-ref is for:
     * <ul>
     * <li>javax.transaction.UserTransaction</li>
     * <li>javax.resource.cci.InteractionSpec</li>
     * <li>anything else that is not a connection factory</li>
     * </ul>
     * 
     * @param context the context 
     * @param descriptor the descriptor 
     * @param node the xml node
     * @throws Exception if unable to load classes, or bind jndi entries
     */
public void visitResourceEnvRef(WebAppContext context, Descriptor descriptor, XmlParser.Node node) throws Exception {
    String jndiName = node.getString("resource-env-ref-name", false, true);
    String type = node.getString("resource-env-ref-type", false, true);
    Origin o = context.getMetaData().getOrigin("resource-env-ref." + jndiName);
    switch(o) {
        case NotSet:
            {
                //First declaration of resource-env-ref with this jndiName
                //JavaEE Spec sec 5.7.1.3 says the resource-env-ref-type
                //is mandatory, but the schema says it is optional!
                Class<?> typeClass = TypeUtil.fromName(type);
                if (typeClass == null)
                    typeClass = context.loadClass(type);
                addInjections(context, descriptor, node, jndiName, typeClass);
                bindResourceEnvRef(context, jndiName, typeClass);
                break;
            }
        case WebXml:
        case WebDefaults:
        case WebOverride:
            {
                //Only allow other web-default, web.xml, web-override to change it.
                if (!(descriptor instanceof FragmentDescriptor)) {
                    //We're processing web-defaults, web.xml or web-override. Any of them can
                    //set or change the resource-env-ref.
                    context.getMetaData().setOrigin("resource-env-ref." + jndiName, descriptor);
                    Class<?> typeClass = TypeUtil.fromName(type);
                    if (typeClass == null)
                        typeClass = context.loadClass(type);
                    addInjections(context, descriptor, node, jndiName, typeClass);
                    bindResourceEnvRef(context, jndiName, typeClass);
                } else {
                    //We're processing a web-fragment. It can only contribute injections if the
                    //there haven't been any injections declared yet, or they weren't declared in a WebXml file.
                    Descriptor d = context.getMetaData().getOriginDescriptor("resource-env-ref." + jndiName + ".injection");
                    if (d == null || d instanceof FragmentDescriptor) {
                        Class<?> typeClass = TypeUtil.fromName(type);
                        if (typeClass == null)
                            typeClass = context.loadClass(type);
                        addInjections(context, descriptor, node, jndiName, typeClass);
                    }
                }
                break;
            }
        case WebFragment:
            {
                Descriptor otherFragment = context.getMetaData().getOriginDescriptor("resource-env-ref." + jndiName);
                XmlParser.Node otherFragmentRoot = otherFragment.getRoot();
                Iterator<Object> iter = otherFragmentRoot.iterator();
                XmlParser.Node otherNode = null;
                while (iter.hasNext() && otherNode == null) {
                    Object obj = iter.next();
                    if (!(obj instanceof XmlParser.Node))
                        continue;
                    XmlParser.Node n = (XmlParser.Node) obj;
                    if ("resource-env-ref".equals(n.getTag()) && jndiName.equals(n.getString("resource-env-ref-name", false, true)))
                        otherNode = n;
                }
                if (otherNode != null) {
                    //declarations of the resource-ref must be the same in both fragment descriptors
                    String otherType = otherNode.getString("resource-env-ref-type", false, true);
                    //types must be the same
                    type = (type == null ? "" : type);
                    otherType = (otherType == null ? "" : otherType);
                    //ServletSpec p.75. No declaration of resource-ref in web xml, but different in multiple web-fragments. Error.
                    if (!type.equals(otherType))
                        throw new IllegalStateException("Conflicting resource-env-ref " + jndiName + " in " + descriptor.getResource());
                    //same in multiple web-fragments, merge the injections
                    addInjections(context, descriptor, node, jndiName, TypeUtil.fromName(type));
                } else
                    throw new IllegalStateException("resource-env-ref." + jndiName + " not found in declaring descriptor " + otherFragment);
            }
    }
}
Also used : Origin(org.eclipse.jetty.webapp.Origin) XmlParser(org.eclipse.jetty.xml.XmlParser) Iterator(java.util.Iterator) FragmentDescriptor(org.eclipse.jetty.webapp.FragmentDescriptor) Descriptor(org.eclipse.jetty.webapp.Descriptor) FragmentDescriptor(org.eclipse.jetty.webapp.FragmentDescriptor)

Example 7 with FragmentDescriptor

use of org.eclipse.jetty.webapp.FragmentDescriptor 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)

Example 8 with FragmentDescriptor

use of org.eclipse.jetty.webapp.FragmentDescriptor 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)

Example 9 with FragmentDescriptor

use of org.eclipse.jetty.webapp.FragmentDescriptor in project jetty.project by eclipse.

the class PlusDescriptorProcessorTest method setUp.

@Before
public void setUp() throws Exception {
    context = new WebAppContext();
    context.setClassLoader(new WebAppClassLoader(Thread.currentThread().getContextClassLoader(), context));
    ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(context.getClassLoader());
    Context icontext = new InitialContext();
    Context compCtx = (Context) icontext.lookup("java:comp");
    compCtx.createSubcontext("env");
    Thread.currentThread().setContextClassLoader(oldLoader);
    org.eclipse.jetty.plus.jndi.Resource ds = new org.eclipse.jetty.plus.jndi.Resource(context, "jdbc/mydatasource", new Object());
    URL webXml = Thread.currentThread().getContextClassLoader().getResource("web.xml");
    webDescriptor = new WebDescriptor(org.eclipse.jetty.util.resource.Resource.newResource(webXml));
    webDescriptor.parse();
    URL frag1Xml = Thread.currentThread().getContextClassLoader().getResource("web-fragment-1.xml");
    fragDescriptor1 = new FragmentDescriptor(org.eclipse.jetty.util.resource.Resource.newResource(frag1Xml));
    fragDescriptor1.parse();
    URL frag2Xml = Thread.currentThread().getContextClassLoader().getResource("web-fragment-2.xml");
    fragDescriptor2 = new FragmentDescriptor(org.eclipse.jetty.util.resource.Resource.newResource(frag2Xml));
    fragDescriptor2.parse();
    URL frag3Xml = Thread.currentThread().getContextClassLoader().getResource("web-fragment-3.xml");
    fragDescriptor3 = new FragmentDescriptor(org.eclipse.jetty.util.resource.Resource.newResource(frag3Xml));
    fragDescriptor3.parse();
    URL frag4Xml = Thread.currentThread().getContextClassLoader().getResource("web-fragment-4.xml");
    fragDescriptor4 = new FragmentDescriptor(org.eclipse.jetty.util.resource.Resource.newResource(frag4Xml));
    fragDescriptor4.parse();
}
Also used : InitialContext(javax.naming.InitialContext) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) Context(javax.naming.Context) WebDescriptor(org.eclipse.jetty.webapp.WebDescriptor) WebAppClassLoader(org.eclipse.jetty.webapp.WebAppClassLoader) InitialContext(javax.naming.InitialContext) URL(java.net.URL) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) WebAppClassLoader(org.eclipse.jetty.webapp.WebAppClassLoader) FragmentDescriptor(org.eclipse.jetty.webapp.FragmentDescriptor) Before(org.junit.Before)

Aggregations

FragmentDescriptor (org.eclipse.jetty.webapp.FragmentDescriptor)9 Origin (org.eclipse.jetty.webapp.Origin)6 Descriptor (org.eclipse.jetty.webapp.Descriptor)4 Iterator (java.util.Iterator)3 XmlParser (org.eclipse.jetty.xml.XmlParser)3 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 LifeCycleCallback (org.eclipse.jetty.plus.annotation.LifeCycleCallback)2 Resource (org.eclipse.jetty.util.resource.Resource)2 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)2 File (java.io.File)1 URI (java.net.URI)1 HashSet (java.util.HashSet)1 Context (javax.naming.Context)1 InitialContext (javax.naming.InitialContext)1 Handler (org.eclipse.jetty.annotations.AnnotationParser.Handler)1 PostConstructCallback (org.eclipse.jetty.plus.annotation.PostConstructCallback)1 PreDestroyCallback (org.eclipse.jetty.plus.annotation.PreDestroyCallback)1 ConcurrentHashSet (org.eclipse.jetty.util.ConcurrentHashSet)1 CounterStatistic (org.eclipse.jetty.util.statistic.CounterStatistic)1