use of org.eclipse.jetty.webapp.Origin 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.Origin 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;
}
}
}
use of org.eclipse.jetty.webapp.Origin 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;
}
}
}
Aggregations