Search in sources :

Example 6 with MetaData

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

the class WebListenerAnnotation method apply.

/**
     * @see DiscoveredAnnotation#apply()
     */
public void apply() {
    Class<? extends java.util.EventListener> clazz = (Class<? extends EventListener>) getTargetClass();
    if (clazz == null) {
        LOG.warn(_className + " cannot be loaded");
        return;
    }
    try {
        if (ServletContextListener.class.isAssignableFrom(clazz) || ServletContextAttributeListener.class.isAssignableFrom(clazz) || ServletRequestListener.class.isAssignableFrom(clazz) || ServletRequestAttributeListener.class.isAssignableFrom(clazz) || HttpSessionListener.class.isAssignableFrom(clazz) || HttpSessionAttributeListener.class.isAssignableFrom(clazz) || HttpSessionIdListener.class.isAssignableFrom(clazz)) {
            java.util.EventListener listener = (java.util.EventListener) _context.getServletContext().createInstance(clazz);
            MetaData metaData = _context.getMetaData();
            if (metaData.getOrigin(clazz.getName() + ".listener") == Origin.NotSet) {
                ListenerHolder h = _context.getServletHandler().newListenerHolder(new Source(Source.Origin.ANNOTATION, clazz.getName()));
                h.setListener(listener);
                _context.getServletHandler().addListener(h);
                _context.addEventListener(listener);
            }
        } else
            LOG.warn(clazz.getName() + " does not implement one of the servlet listener interfaces");
    } catch (Exception e) {
        LOG.warn(e);
    }
}
Also used : HttpSessionListener(javax.servlet.http.HttpSessionListener) ServletRequestListener(javax.servlet.ServletRequestListener) ListenerHolder(org.eclipse.jetty.servlet.ListenerHolder) Source(org.eclipse.jetty.servlet.Source) EventListener(java.util.EventListener) MetaData(org.eclipse.jetty.webapp.MetaData) EventListener(java.util.EventListener) HttpSessionIdListener(javax.servlet.http.HttpSessionIdListener)

Example 7 with MetaData

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

the class MultiPartConfigAnnotationHandler method doHandle.

/** 
     * @see org.eclipse.jetty.annotations.AnnotationIntrospector.AbstractIntrospectableAnnotationHandler#doHandle(java.lang.Class)
     */
public void doHandle(Class clazz) {
    if (!Servlet.class.isAssignableFrom(clazz))
        return;
    MultipartConfig multi = (MultipartConfig) clazz.getAnnotation(MultipartConfig.class);
    if (multi == null)
        return;
    MetaData metaData = _context.getMetaData();
    //TODO: The MultipartConfigElement needs to be set on the ServletHolder's Registration.
    //How to identify the correct Servlet?  If the Servlet has no WebServlet annotation on it, does it mean that this MultipartConfig
    //annotation applies to all declared instances in web.xml/programmatically?
    //Assuming TRUE for now.
    ServletHolder holder = getServletHolderForClass(clazz);
    if (holder != null) {
        Descriptor d = metaData.getOriginDescriptor(holder.getName() + ".servlet.multipart-config");
        //let the annotation override it
        if (d == null) {
            metaData.setOrigin(holder.getName() + ".servlet.multipart-config", multi, clazz);
            holder.getRegistration().setMultipartConfig(new MultipartConfigElement(multi));
        }
    }
}
Also used : MultipartConfigElement(javax.servlet.MultipartConfigElement) MultipartConfig(javax.servlet.annotation.MultipartConfig) MetaData(org.eclipse.jetty.webapp.MetaData) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) Servlet(javax.servlet.Servlet) Descriptor(org.eclipse.jetty.webapp.Descriptor)

Example 8 with MetaData

use of org.eclipse.jetty.webapp.MetaData 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 9 with MetaData

use of org.eclipse.jetty.webapp.MetaData 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 10 with MetaData

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

the class ResourceAnnotationHandler method handleField.

public void handleField(Class<?> clazz, Field field) {
    Resource resource = (Resource) field.getAnnotation(Resource.class);
    if (resource != null) {
        //JavaEE Spec 5.2.3: Field cannot be static
        if (Modifier.isStatic(field.getModifiers())) {
            LOG.warn("Skipping Resource annotation on " + clazz.getName() + "." + field.getName() + ": cannot be static");
            return;
        }
        //JavaEE Spec 5.2.3: Field cannot be final
        if (Modifier.isFinal(field.getModifiers())) {
            LOG.warn("Skipping Resource annotation on " + clazz.getName() + "." + field.getName() + ": cannot be final");
            return;
        }
        //work out default name
        String name = clazz.getCanonicalName() + "/" + field.getName();
        //allow @Resource name= to override the field name
        name = (resource.name() != null && !resource.name().trim().equals("") ? resource.name() : name);
        String mappedName = (resource.mappedName() != null && !resource.mappedName().trim().equals("") ? resource.mappedName() : null);
        //get the type of the Field
        Class<?> type = field.getType();
        //Servlet Spec 3.0 p. 76
        //If a descriptor has specified at least 1 injection target for this
        //resource, then it overrides this annotation
        MetaData metaData = _context.getMetaData();
        if (metaData.getOriginDescriptor("resource-ref." + name + ".injection") != null) {
            //it overrides this annotation
            return;
        }
        //No injections for this resource in any descriptors, so we can add it
        //Does the injection already exist?
        InjectionCollection injections = (InjectionCollection) _context.getAttribute(InjectionCollection.INJECTION_COLLECTION);
        if (injections == null) {
            injections = new InjectionCollection();
            _context.setAttribute(InjectionCollection.INJECTION_COLLECTION, injections);
        }
        Injection injection = injections.getInjection(name, clazz, field);
        if (injection == null) {
            //No injection has been specified, add it
            try {
                boolean bound = org.eclipse.jetty.plus.jndi.NamingEntryUtil.bindToENC(_context, name, mappedName);
                if (!bound)
                    bound = org.eclipse.jetty.plus.jndi.NamingEntryUtil.bindToENC(_context.getServer(), name, mappedName);
                if (!bound)
                    bound = org.eclipse.jetty.plus.jndi.NamingEntryUtil.bindToENC(null, name, mappedName);
                if (!bound) {
                    //see if there is an env-entry value been bound
                    try {
                        InitialContext ic = new InitialContext();
                        String nameInEnvironment = (mappedName != null ? mappedName : name);
                        ic.lookup("java:comp/env/" + nameInEnvironment);
                        bound = true;
                    } catch (NameNotFoundException e) {
                        bound = false;
                    }
                }
                //Check there is a JNDI entry for this annotation
                if (bound) {
                    LOG.debug("Bound " + (mappedName == null ? name : mappedName) + " as " + name);
                    //   Make the Injection for it if the binding succeeded
                    injection = new Injection();
                    injection.setTarget(clazz, field, type);
                    injection.setJndiName(name);
                    injection.setMappingName(mappedName);
                    injections.add(injection);
                    //TODO - an @Resource is equivalent to a resource-ref, resource-env-ref, message-destination
                    metaData.setOrigin("resource-ref." + name + ".injection", resource, clazz);
                } else if (!isEnvEntryType(type)) {
                    throw new IllegalStateException("No resource at " + (mappedName == null ? name : mappedName));
                }
            } catch (NamingException e) {
                // JavaEE Spec. sec 5.4.1.3
                if (!isEnvEntryType(type))
                    throw new IllegalStateException(e);
            }
        }
    }
}
Also used : InjectionCollection(org.eclipse.jetty.plus.annotation.InjectionCollection) NameNotFoundException(javax.naming.NameNotFoundException) MetaData(org.eclipse.jetty.webapp.MetaData) Resource(javax.annotation.Resource) NamingException(javax.naming.NamingException) Injection(org.eclipse.jetty.plus.annotation.Injection) InitialContext(javax.naming.InitialContext)

Aggregations

MetaData (org.eclipse.jetty.webapp.MetaData)11 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)4 InitialContext (javax.naming.InitialContext)3 Servlet (javax.servlet.Servlet)3 LifeCycleCallbackCollection (org.eclipse.jetty.plus.annotation.LifeCycleCallbackCollection)3 Source (org.eclipse.jetty.servlet.Source)3 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 EventListener (java.util.EventListener)2 Resource (javax.annotation.Resource)2 NameNotFoundException (javax.naming.NameNotFoundException)2 NamingException (javax.naming.NamingException)2 WebInitParam (javax.servlet.annotation.WebInitParam)2 Injection (org.eclipse.jetty.plus.annotation.Injection)2 InjectionCollection (org.eclipse.jetty.plus.annotation.InjectionCollection)2 FilterHolder (org.eclipse.jetty.servlet.FilterHolder)2 FilterMapping (org.eclipse.jetty.servlet.FilterMapping)2 ServletMapping (org.eclipse.jetty.servlet.ServletMapping)2 Descriptor (org.eclipse.jetty.webapp.Descriptor)2 Origin (org.eclipse.jetty.webapp.Origin)2