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