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);
}
}
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));
}
}
}
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);
}
}
}
}
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);
}
}
}
}
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);
}
}
}
}
Aggregations