* Issue #4644 Allow jetty-env EnvEntry to override empty env-entry Signed-off-by: Jan Bartel <janb@webtide.com>
This commit is contained in:
parent
c224b88e58
commit
a8a9dbbc08
|
@ -19,9 +19,12 @@
|
||||||
package org.eclipse.jetty.plus.webapp;
|
package org.eclipse.jetty.plus.webapp;
|
||||||
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
import javax.naming.Context;
|
import javax.naming.Context;
|
||||||
import javax.naming.InitialContext;
|
import javax.naming.InitialContext;
|
||||||
import javax.naming.NameNotFoundException;
|
import javax.naming.NameNotFoundException;
|
||||||
|
import javax.naming.NamingException;
|
||||||
|
|
||||||
import org.eclipse.jetty.jndi.NamingUtil;
|
import org.eclipse.jetty.jndi.NamingUtil;
|
||||||
import org.eclipse.jetty.plus.annotation.Injection;
|
import org.eclipse.jetty.plus.annotation.Injection;
|
||||||
|
@ -35,6 +38,7 @@ import org.eclipse.jetty.plus.jndi.EnvEntry;
|
||||||
import org.eclipse.jetty.plus.jndi.Link;
|
import org.eclipse.jetty.plus.jndi.Link;
|
||||||
import org.eclipse.jetty.plus.jndi.NamingEntry;
|
import org.eclipse.jetty.plus.jndi.NamingEntry;
|
||||||
import org.eclipse.jetty.plus.jndi.NamingEntryUtil;
|
import org.eclipse.jetty.plus.jndi.NamingEntryUtil;
|
||||||
|
import org.eclipse.jetty.util.StringUtil;
|
||||||
import org.eclipse.jetty.util.TypeUtil;
|
import org.eclipse.jetty.util.TypeUtil;
|
||||||
import org.eclipse.jetty.util.log.Log;
|
import org.eclipse.jetty.util.log.Log;
|
||||||
import org.eclipse.jetty.util.log.Logger;
|
import org.eclipse.jetty.util.log.Logger;
|
||||||
|
@ -121,14 +125,6 @@ public class PlusDescriptorProcessor extends IterativeDescriptorProcessor
|
||||||
String type = node.getString("env-entry-type", false, true);
|
String type = node.getString("env-entry-type", false, true);
|
||||||
String valueStr = node.getString("env-entry-value", false, true);
|
String valueStr = node.getString("env-entry-value", false, true);
|
||||||
|
|
||||||
//if there's no value there's no point in making a jndi entry
|
|
||||||
//nor processing injection entries
|
|
||||||
if (valueStr == null || valueStr.equals(""))
|
|
||||||
{
|
|
||||||
LOG.warn("No value for env-entry-name " + name);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Origin o = context.getMetaData().getOrigin("env-entry." + name);
|
Origin o = context.getMetaData().getOrigin("env-entry." + name);
|
||||||
switch (o)
|
switch (o)
|
||||||
{
|
{
|
||||||
|
@ -136,14 +132,7 @@ public class PlusDescriptorProcessor extends IterativeDescriptorProcessor
|
||||||
{
|
{
|
||||||
//no descriptor has configured an env-entry of this name previously
|
//no descriptor has configured an env-entry of this name previously
|
||||||
context.getMetaData().setOrigin("env-entry." + name, descriptor);
|
context.getMetaData().setOrigin("env-entry." + name, descriptor);
|
||||||
//the javaee_5.xsd says that the env-entry-type is optional
|
makeEnvEntryInjectionsAndBindings(context, descriptor, node, name, type, valueStr);
|
||||||
//if there is an <injection> element, because you can get
|
|
||||||
//type from the element, but what to do if there is more
|
|
||||||
//than one <injection> element, do you just pick the type
|
|
||||||
//of the first one?
|
|
||||||
addInjections(context, descriptor, node, name, TypeUtil.fromName(type));
|
|
||||||
Object value = TypeUtil.valueOf(type, valueStr);
|
|
||||||
bindEnvEntry(name, value);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case WebXml:
|
case WebXml:
|
||||||
|
@ -158,9 +147,7 @@ public class PlusDescriptorProcessor extends IterativeDescriptorProcessor
|
||||||
//We're processing web-defaults, web.xml or web-override. Any of them can
|
//We're processing web-defaults, web.xml or web-override. Any of them can
|
||||||
//set or change the env-entry.
|
//set or change the env-entry.
|
||||||
context.getMetaData().setOrigin("env-entry." + name, descriptor);
|
context.getMetaData().setOrigin("env-entry." + name, descriptor);
|
||||||
addInjections(context, descriptor, node, name, TypeUtil.fromName(type));
|
makeEnvEntryInjectionsAndBindings(context, descriptor, node, name, type, valueStr);
|
||||||
Object value = TypeUtil.valueOf(type, valueStr);
|
|
||||||
bindEnvEntry(name, value);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -705,6 +692,9 @@ public class PlusDescriptorProcessor extends IterativeDescriptorProcessor
|
||||||
*/
|
*/
|
||||||
public void addInjections(WebAppContext context, Descriptor descriptor, XmlParser.Node node, String jndiName, Class<?> valueClass)
|
public void addInjections(WebAppContext context, Descriptor descriptor, XmlParser.Node node, String jndiName, Class<?> valueClass)
|
||||||
{
|
{
|
||||||
|
Objects.requireNonNull(context);
|
||||||
|
Objects.requireNonNull(node);
|
||||||
|
|
||||||
Iterator<XmlParser.Node> itor = node.iterator("injection-target");
|
Iterator<XmlParser.Node> itor = node.iterator("injection-target");
|
||||||
|
|
||||||
while (itor.hasNext())
|
while (itor.hasNext())
|
||||||
|
@ -755,31 +745,59 @@ public class PlusDescriptorProcessor extends IterativeDescriptorProcessor
|
||||||
*/
|
*/
|
||||||
public void bindEnvEntry(String name, Object value) throws Exception
|
public void bindEnvEntry(String name, Object value) throws Exception
|
||||||
{
|
{
|
||||||
InitialContext ic = null;
|
InitialContext ic = new InitialContext();
|
||||||
boolean bound = false;
|
Context envCtx = (Context)ic.lookup("java:comp/env");
|
||||||
//check to see if we bound a value and an EnvEntry with this name already
|
NamingUtil.bind(envCtx, name, value);
|
||||||
//when we processed the server and the webapp's naming environment
|
}
|
||||||
//@see EnvConfiguration.bindEnvEntries()
|
|
||||||
ic = new InitialContext();
|
/**
|
||||||
|
* Make injections and any java:comp/env bindings necessary given an env-entry declaration.
|
||||||
|
* The handling of env-entries is different to other resource declarations like resource-ref, resource-env-ref etc
|
||||||
|
* because we allow the EnvEntry (@see org.eclipse.jetty.plus.jndi.EnvEntry) class that is configured externally to the webapp
|
||||||
|
* to specify a value that can override a value present in a web.xml descriptor.
|
||||||
|
*
|
||||||
|
* @param context the WebAppContext of the env-entry
|
||||||
|
* @param descriptor the web.xml, web-default.xml, web-override.xml or web-fragment.xml
|
||||||
|
* @param node the parsed xml representation of the env-entry declaration
|
||||||
|
* @param name the name field of the env-entry
|
||||||
|
* @param type the type field of the env-entry
|
||||||
|
* @param value the value field of the env-entry
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public void makeEnvEntryInjectionsAndBindings(WebAppContext context, Descriptor descriptor, XmlParser.Node node, String name, String type, String value) throws Exception
|
||||||
|
{
|
||||||
|
InitialContext ic = new InitialContext();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
NamingEntry ne = (NamingEntry)ic.lookup("java:comp/env/" + NamingEntryUtil.makeNamingEntryName(ic.getNameParser(""), name));
|
EnvEntry envEntry = (EnvEntry)ic.lookup("java:comp/env/" + NamingEntryUtil.makeNamingEntryName(ic.getNameParser(""), name));
|
||||||
if (ne != null && ne instanceof EnvEntry)
|
|
||||||
|
if (StringUtil.isEmpty(value))
|
||||||
{
|
{
|
||||||
EnvEntry ee = (EnvEntry)ne;
|
//There is an empty or missing value in the env-entry:
|
||||||
bound = ee.isOverrideWebXml();
|
//If there is an existing EnvEntry (eg from a declaration in jetty-env.xml) that is override=true, then
|
||||||
|
//we make the injection, but we can skip the rebinding becase we want to use the value already bound.
|
||||||
|
//If there isn't an existing EnvEntry then there is nothing to do: according to the spec we do not make
|
||||||
|
//an injection if the env-entry value is missing, and of course there is no value to rebind.
|
||||||
|
if (envEntry != null && envEntry.isOverrideWebXml())
|
||||||
|
addInjections(context, descriptor, node, name, TypeUtil.fromName(type));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//There is a value for the env-entry:
|
||||||
|
//According to the spec, we need to make an injection (if one is present).
|
||||||
|
//If there is an existing EnvEntry(eg from a declaration in jetty-env.xml) that is override=false, then
|
||||||
|
//we need to rebind name with the value from web.xml.
|
||||||
|
addInjections(context, descriptor, node, name, TypeUtil.fromName(type));
|
||||||
|
|
||||||
|
if (envEntry == null || !envEntry.isOverrideWebXml())
|
||||||
|
bindEnvEntry(name, TypeUtil.valueOf(type, value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (NameNotFoundException e)
|
catch (NameNotFoundException e)
|
||||||
{
|
{
|
||||||
bound = false;
|
//No matching EnvEntry has previously been bound so make the injection and do the binding with the value from web.xml
|
||||||
}
|
addInjections(context, descriptor, node, name, TypeUtil.fromName(type));
|
||||||
|
bindEnvEntry(name, TypeUtil.valueOf(type, value));
|
||||||
if (!bound)
|
|
||||||
{
|
|
||||||
//either nothing was bound or the value from web.xml should override
|
|
||||||
Context envCtx = (Context)ic.lookup("java:comp/env");
|
|
||||||
NamingUtil.bind(envCtx, name, value);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,14 @@ import java.lang.reflect.InvocationTargetException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import javax.naming.Context;
|
import javax.naming.Context;
|
||||||
import javax.naming.InitialContext;
|
import javax.naming.InitialContext;
|
||||||
|
import javax.naming.Name;
|
||||||
|
|
||||||
|
import org.eclipse.jetty.jndi.NamingUtil;
|
||||||
|
import org.eclipse.jetty.plus.annotation.Injection;
|
||||||
|
import org.eclipse.jetty.plus.annotation.InjectionCollection;
|
||||||
|
import org.eclipse.jetty.plus.jndi.EnvEntry;
|
||||||
|
import org.eclipse.jetty.plus.jndi.NamingEntryUtil;
|
||||||
|
import org.eclipse.jetty.util.IntrospectionUtil;
|
||||||
import org.eclipse.jetty.webapp.Descriptor;
|
import org.eclipse.jetty.webapp.Descriptor;
|
||||||
import org.eclipse.jetty.webapp.FragmentDescriptor;
|
import org.eclipse.jetty.webapp.FragmentDescriptor;
|
||||||
import org.eclipse.jetty.webapp.Origin;
|
import org.eclipse.jetty.webapp.Origin;
|
||||||
|
@ -39,6 +46,7 @@ import static org.hamcrest.Matchers.is;
|
||||||
import static org.hamcrest.Matchers.notNullValue;
|
import static org.hamcrest.Matchers.notNullValue;
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
import static org.junit.jupiter.api.Assertions.fail;
|
import static org.junit.jupiter.api.Assertions.fail;
|
||||||
|
@ -48,6 +56,7 @@ import static org.junit.jupiter.api.Assertions.fail;
|
||||||
*/
|
*/
|
||||||
public class PlusDescriptorProcessorTest
|
public class PlusDescriptorProcessorTest
|
||||||
{
|
{
|
||||||
|
protected static final Class<?>[] STRING_ARG = new Class[]{String.class};
|
||||||
protected WebDescriptor webDescriptor;
|
protected WebDescriptor webDescriptor;
|
||||||
protected FragmentDescriptor fragDescriptor1;
|
protected FragmentDescriptor fragDescriptor1;
|
||||||
protected FragmentDescriptor fragDescriptor2;
|
protected FragmentDescriptor fragDescriptor2;
|
||||||
|
@ -55,19 +64,95 @@ public class PlusDescriptorProcessorTest
|
||||||
protected FragmentDescriptor fragDescriptor4;
|
protected FragmentDescriptor fragDescriptor4;
|
||||||
protected WebAppContext context;
|
protected WebAppContext context;
|
||||||
|
|
||||||
|
public static class TestInjections
|
||||||
|
{
|
||||||
|
private String foo;
|
||||||
|
private String bah;
|
||||||
|
private String empty;
|
||||||
|
private String vacuum;
|
||||||
|
private String webXmlOnly;
|
||||||
|
|
||||||
|
public String getWebXmlOnly()
|
||||||
|
{
|
||||||
|
return webXmlOnly;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setWebXmlOnly(String webXmlOnly)
|
||||||
|
{
|
||||||
|
this.webXmlOnly = webXmlOnly;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getVacuum()
|
||||||
|
{
|
||||||
|
return vacuum;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setVacuum(String val)
|
||||||
|
{
|
||||||
|
vacuum = val;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getEmpty()
|
||||||
|
{
|
||||||
|
return empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEmpty(String val)
|
||||||
|
{
|
||||||
|
empty = val;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFoo(String val)
|
||||||
|
{
|
||||||
|
foo = val;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFoo()
|
||||||
|
{
|
||||||
|
return foo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getBah()
|
||||||
|
{
|
||||||
|
return bah;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBah(String val)
|
||||||
|
{
|
||||||
|
bah = val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
public void setUp() throws Exception
|
public void setUp() throws Exception
|
||||||
{
|
{
|
||||||
context = new WebAppContext();
|
context = new WebAppContext();
|
||||||
context.setClassLoader(new WebAppClassLoader(Thread.currentThread().getContextClassLoader(), context));
|
context.setClassLoader(new WebAppClassLoader(Thread.currentThread().getContextClassLoader(), context));
|
||||||
|
context.getServerClasspathPattern().add("-org.eclipse.jetty.plus.webapp."); //need visbility of the TestInjections class
|
||||||
ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
|
ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
|
||||||
Thread.currentThread().setContextClassLoader(context.getClassLoader());
|
Thread.currentThread().setContextClassLoader(context.getClassLoader());
|
||||||
Context icontext = new InitialContext();
|
Context icontext = new InitialContext();
|
||||||
Context compCtx = (Context)icontext.lookup("java:comp");
|
Context compCtx = (Context)icontext.lookup("java:comp");
|
||||||
compCtx.createSubcontext("env");
|
Context envCtx = compCtx.createSubcontext("env");
|
||||||
Thread.currentThread().setContextClassLoader(oldLoader);
|
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
org.eclipse.jetty.plus.jndi.Resource ds = new org.eclipse.jetty.plus.jndi.Resource(context, "jdbc/mydatasource", new Object());
|
org.eclipse.jetty.plus.jndi.Resource ds = new org.eclipse.jetty.plus.jndi.Resource(context, "jdbc/mydatasource", new Object());
|
||||||
|
|
||||||
|
//An EnvEntry that should override any value supplied in a web.xml file
|
||||||
|
org.eclipse.jetty.plus.jndi.EnvEntry fooStringEnvEntry = new org.eclipse.jetty.plus.jndi.EnvEntry("foo", "FOO", true);
|
||||||
|
doEnvConfiguration(envCtx, fooStringEnvEntry);
|
||||||
|
|
||||||
|
//An EnvEntry that should NOT override any value supplied in a web.xml file
|
||||||
|
org.eclipse.jetty.plus.jndi.EnvEntry bahStringEnvEntry = new org.eclipse.jetty.plus.jndi.EnvEntry("bah", "BAH", false);
|
||||||
|
doEnvConfiguration(envCtx, bahStringEnvEntry);
|
||||||
|
|
||||||
|
//An EnvEntry that will override an empty value in web.xml
|
||||||
|
org.eclipse.jetty.plus.jndi.EnvEntry emptyStringEnvEntry = new org.eclipse.jetty.plus.jndi.EnvEntry("empty", "EMPTY", true);
|
||||||
|
doEnvConfiguration(envCtx, emptyStringEnvEntry);
|
||||||
|
|
||||||
|
//An EnvEntry that will NOT override an empty value in web.xml
|
||||||
|
org.eclipse.jetty.plus.jndi.EnvEntry vacuumStringEnvEntry = new org.eclipse.jetty.plus.jndi.EnvEntry("vacuum", "VACUUM", false);
|
||||||
|
doEnvConfiguration(envCtx, vacuumStringEnvEntry);
|
||||||
|
|
||||||
URL webXml = Thread.currentThread().getContextClassLoader().getResource("web.xml");
|
URL webXml = Thread.currentThread().getContextClassLoader().getResource("web.xml");
|
||||||
webDescriptor = new WebDescriptor(org.eclipse.jetty.util.resource.Resource.newResource(webXml));
|
webDescriptor = new WebDescriptor(org.eclipse.jetty.util.resource.Resource.newResource(webXml));
|
||||||
|
@ -85,6 +170,22 @@ public class PlusDescriptorProcessorTest
|
||||||
URL frag4Xml = Thread.currentThread().getContextClassLoader().getResource("web-fragment-4.xml");
|
URL frag4Xml = Thread.currentThread().getContextClassLoader().getResource("web-fragment-4.xml");
|
||||||
fragDescriptor4 = new FragmentDescriptor(org.eclipse.jetty.util.resource.Resource.newResource(frag4Xml));
|
fragDescriptor4 = new FragmentDescriptor(org.eclipse.jetty.util.resource.Resource.newResource(frag4Xml));
|
||||||
fragDescriptor4.parse();
|
fragDescriptor4.parse();
|
||||||
|
|
||||||
|
Thread.currentThread().setContextClassLoader(oldLoader);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Do the kind of processing that EnvConfiguration would do.
|
||||||
|
*
|
||||||
|
* @param envCtx the java:comp/env context
|
||||||
|
* @param envEntry the EnvEntry
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
private void doEnvConfiguration(Context envCtx, EnvEntry envEntry) throws Exception
|
||||||
|
{
|
||||||
|
envEntry.bindToENC(envEntry.getJndiName());
|
||||||
|
Name namingEntryName = NamingEntryUtil.makeNamingEntryName(null, envEntry);
|
||||||
|
NamingUtil.bind(envCtx, namingEntryName.toString(), envEntry);
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterEach
|
@AfterEach
|
||||||
|
@ -140,6 +241,59 @@ public class PlusDescriptorProcessorTest
|
||||||
Thread.currentThread().setContextClassLoader(oldLoader);
|
Thread.currentThread().setContextClassLoader(oldLoader);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEnvEntries() throws Exception
|
||||||
|
{
|
||||||
|
ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
|
||||||
|
Thread.currentThread().setContextClassLoader(context.getClassLoader());
|
||||||
|
try
|
||||||
|
{
|
||||||
|
PlusDescriptorProcessor pdp = new PlusDescriptorProcessor();
|
||||||
|
//process web.xml
|
||||||
|
pdp.process(context, webDescriptor);
|
||||||
|
InjectionCollection injections = (InjectionCollection)context.getAttribute(InjectionCollection.INJECTION_COLLECTION);
|
||||||
|
assertNotNull(injections);
|
||||||
|
|
||||||
|
//check that there is an injection for "foo" with the value from the overriding EnvEntry of "FOO"
|
||||||
|
Injection foo = injections.getInjection("foo", TestInjections.class,
|
||||||
|
IntrospectionUtil.findMethod(TestInjections.class, "setFoo", STRING_ARG, false, true),
|
||||||
|
String.class);
|
||||||
|
assertNotNull(foo);
|
||||||
|
assertEquals("FOO", foo.lookupInjectedValue());
|
||||||
|
|
||||||
|
//check that there is an injection for "bah" with the value from web.xml of "beer"
|
||||||
|
Injection bah = injections.getInjection("bah", TestInjections.class,
|
||||||
|
IntrospectionUtil.findMethod(TestInjections.class, "setBah", STRING_ARG, false, true),
|
||||||
|
String.class);
|
||||||
|
assertNotNull(bah);
|
||||||
|
assertEquals("beer", bah.lookupInjectedValue());
|
||||||
|
|
||||||
|
//check that there is an injection for "empty" with the value from the overriding EnvEntry of "EMPTY"
|
||||||
|
Injection empty = injections.getInjection("empty", TestInjections.class,
|
||||||
|
IntrospectionUtil.findMethod(TestInjections.class, "setEmpty", STRING_ARG, false, true),
|
||||||
|
String.class);
|
||||||
|
assertNotNull(empty);
|
||||||
|
assertEquals("EMPTY", empty.lookupInjectedValue());
|
||||||
|
|
||||||
|
//check that there is NOT an injection for "vacuum"
|
||||||
|
Injection vacuum = injections.getInjection("vacuum", TestInjections.class,
|
||||||
|
IntrospectionUtil.findMethod(TestInjections.class, "setVacuum", STRING_ARG, false, true),
|
||||||
|
String.class);
|
||||||
|
assertNull(vacuum);
|
||||||
|
|
||||||
|
//check that there is an injection for "webxmlonly" with the value from web.xml of "WEBXMLONLY"
|
||||||
|
Injection webXmlOnly = injections.getInjection("webxmlonly", TestInjections.class,
|
||||||
|
IntrospectionUtil.findMethod(TestInjections.class, "setWebXmlOnly", STRING_ARG, false, true),
|
||||||
|
String.class);
|
||||||
|
assertNotNull(webXmlOnly);
|
||||||
|
assertEquals("WEBXMLONLY", webXmlOnly.lookupInjectedValue());
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
Thread.currentThread().setContextClassLoader(oldLoader);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMismatchedFragmentResourceDeclarations()
|
public void testMismatchedFragmentResourceDeclarations()
|
||||||
|
|
|
@ -1,136 +0,0 @@
|
||||||
//
|
|
||||||
// ========================================================================
|
|
||||||
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
|
|
||||||
// ------------------------------------------------------------------------
|
|
||||||
// All rights reserved. This program and the accompanying materials
|
|
||||||
// are made available under the terms of the Eclipse Public License v1.0
|
|
||||||
// and Apache License v2.0 which accompanies this distribution.
|
|
||||||
//
|
|
||||||
// The Eclipse Public License is available at
|
|
||||||
// http://www.eclipse.org/legal/epl-v10.html
|
|
||||||
//
|
|
||||||
// The Apache License v2.0 is available at
|
|
||||||
// http://www.opensource.org/licenses/apache2.0.php
|
|
||||||
//
|
|
||||||
// You may elect to redistribute this code under either of these licenses.
|
|
||||||
// ========================================================================
|
|
||||||
//
|
|
||||||
|
|
||||||
package org.eclipse.jetty.plus.webapp;
|
|
||||||
|
|
||||||
import javax.naming.Context;
|
|
||||||
import javax.naming.InitialContext;
|
|
||||||
|
|
||||||
import org.eclipse.jetty.plus.jndi.EnvEntry;
|
|
||||||
import org.eclipse.jetty.plus.jndi.NamingEntry;
|
|
||||||
import org.eclipse.jetty.plus.jndi.NamingEntryUtil;
|
|
||||||
import org.eclipse.jetty.server.Server;
|
|
||||||
import org.eclipse.jetty.webapp.MetaData;
|
|
||||||
import org.eclipse.jetty.webapp.WebAppClassLoader;
|
|
||||||
import org.eclipse.jetty.webapp.WebAppContext;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
||||||
|
|
||||||
public class TestConfiguration
|
|
||||||
{
|
|
||||||
@Test
|
|
||||||
public void testIt() throws Exception
|
|
||||||
{
|
|
||||||
ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
InitialContext ic = new InitialContext();
|
|
||||||
|
|
||||||
Server server = new Server();
|
|
||||||
|
|
||||||
WebAppContext wac = new WebAppContext();
|
|
||||||
wac.setServer(server);
|
|
||||||
wac.setClassLoader(new WebAppClassLoader(Thread.currentThread().getContextClassLoader(), wac));
|
|
||||||
|
|
||||||
MetaData metaData = new MetaData();
|
|
||||||
|
|
||||||
PlusDescriptorProcessor plusProcessor = new PlusDescriptorProcessor();
|
|
||||||
|
|
||||||
//bind some EnvEntrys at the server level
|
|
||||||
EnvEntry ee1 = new EnvEntry(server, "xxx/a", "100", true);
|
|
||||||
EnvEntry ee2 = new EnvEntry(server, "yyy/b", "200", false);
|
|
||||||
EnvEntry ee3 = new EnvEntry(server, "zzz/c", "300", false);
|
|
||||||
EnvEntry ee4 = new EnvEntry(server, "zzz/d", "400", false);
|
|
||||||
EnvEntry ee5 = new EnvEntry(server, "zzz/f", "500", true);
|
|
||||||
|
|
||||||
//bind some EnvEntrys at the webapp level
|
|
||||||
EnvEntry ee6 = new EnvEntry(wac, "xxx/a", "900", true);
|
|
||||||
EnvEntry ee7 = new EnvEntry(wac, "yyy/b", "910", true);
|
|
||||||
EnvEntry ee8 = new EnvEntry(wac, "zzz/c", "920", false);
|
|
||||||
EnvEntry ee9 = new EnvEntry(wac, "zzz/e", "930", false);
|
|
||||||
|
|
||||||
assertNotNull(NamingEntryUtil.lookupNamingEntry(server, "xxx/a"));
|
|
||||||
assertNotNull(NamingEntryUtil.lookupNamingEntry(server, "yyy/b"));
|
|
||||||
assertNotNull(NamingEntryUtil.lookupNamingEntry(server, "zzz/c"));
|
|
||||||
assertNotNull(NamingEntryUtil.lookupNamingEntry(server, "zzz/d"));
|
|
||||||
assertNotNull(NamingEntryUtil.lookupNamingEntry(wac, "xxx/a"));
|
|
||||||
assertNotNull(NamingEntryUtil.lookupNamingEntry(wac, "yyy/b"));
|
|
||||||
assertNotNull(NamingEntryUtil.lookupNamingEntry(wac, "zzz/c"));
|
|
||||||
assertNotNull(NamingEntryUtil.lookupNamingEntry(wac, "zzz/e"));
|
|
||||||
|
|
||||||
//make a new env configuration
|
|
||||||
EnvConfiguration envConfig = new EnvConfiguration();
|
|
||||||
|
|
||||||
Thread.currentThread().setContextClassLoader(wac.getClassLoader());
|
|
||||||
MetaData metadata = new MetaData();
|
|
||||||
envConfig.preConfigure(wac);
|
|
||||||
envConfig.configure(wac);
|
|
||||||
envConfig.bindEnvEntries(wac);
|
|
||||||
|
|
||||||
String val = (String)ic.lookup("java:comp/env/xxx/a");
|
|
||||||
assertEquals("900", val); //webapp naming overrides server
|
|
||||||
val = (String)ic.lookup("java:comp/env/yyy/b");
|
|
||||||
assertEquals("910", val);//webapp overrides server
|
|
||||||
val = (String)ic.lookup("java:comp/env/zzz/c");
|
|
||||||
assertEquals("920", val);//webapp overrides server
|
|
||||||
val = (String)ic.lookup("java:comp/env/zzz/d");
|
|
||||||
assertEquals("400", val);//from server naming
|
|
||||||
val = (String)ic.lookup("java:comp/env/zzz/e");
|
|
||||||
assertEquals("930", val);//from webapp naming
|
|
||||||
|
|
||||||
NamingEntry ne = (NamingEntry)ic.lookup("java:comp/env/" + NamingEntry.__contextName + "/xxx/a");
|
|
||||||
assertNotNull(ne);
|
|
||||||
ne = (NamingEntry)ic.lookup("java:comp/env/" + NamingEntry.__contextName + "/yyy/b");
|
|
||||||
assertNotNull(ne);
|
|
||||||
ne = (NamingEntry)ic.lookup("java:comp/env/" + NamingEntry.__contextName + "/zzz/c");
|
|
||||||
assertNotNull(ne);
|
|
||||||
ne = (NamingEntry)ic.lookup("java:comp/env/" + NamingEntry.__contextName + "/zzz/d");
|
|
||||||
assertNotNull(ne);
|
|
||||||
ne = (NamingEntry)ic.lookup("java:comp/env/" + NamingEntry.__contextName + "/zzz/e");
|
|
||||||
assertNotNull(ne);
|
|
||||||
|
|
||||||
plusProcessor.bindEnvEntry("foo", "99");
|
|
||||||
assertEquals("99", ic.lookup("java:comp/env/foo"));
|
|
||||||
|
|
||||||
plusProcessor.bindEnvEntry("xxx/a", "7");
|
|
||||||
assertEquals("900", ic.lookup("java:comp/env/xxx/a")); //webapp overrides web.xml
|
|
||||||
plusProcessor.bindEnvEntry("yyy/b", "7");
|
|
||||||
assertEquals("910", ic.lookup("java:comp/env/yyy/b"));//webapp overrides web.xml
|
|
||||||
plusProcessor.bindEnvEntry("zzz/c", "7");
|
|
||||||
assertEquals("7", ic.lookup("java:comp/env/zzz/c"));//webapp does NOT override web.xml
|
|
||||||
plusProcessor.bindEnvEntry("zzz/d", "7");
|
|
||||||
assertEquals("7", ic.lookup("java:comp/env/zzz/d"));//server does NOT override web.xml
|
|
||||||
plusProcessor.bindEnvEntry("zzz/e", "7");
|
|
||||||
assertEquals("7", ic.lookup("java:comp/env/zzz/e"));//webapp does NOT override web.xml
|
|
||||||
plusProcessor.bindEnvEntry("zzz/f", "7");
|
|
||||||
assertEquals("500", ic.lookup("java:comp/env/zzz/f"));//server overrides web.xml
|
|
||||||
|
|
||||||
((Context)ic.lookup("java:comp")).destroySubcontext("env");
|
|
||||||
ic.destroySubcontext("xxx");
|
|
||||||
ic.destroySubcontext("yyy");
|
|
||||||
ic.destroySubcontext("zzz");
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
Thread.currentThread().setContextClassLoader(oldLoader);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -4,20 +4,70 @@
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
|
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
|
||||||
metadata-complete="false"
|
metadata-complete="false"
|
||||||
version="3.0">
|
version="3.0">
|
||||||
|
|
||||||
<display-name>Test WebApp</display-name>
|
<display-name>Test WebApp</display-name>
|
||||||
|
|
||||||
<resource-ref>
|
<resource-ref>
|
||||||
<res-ref-name>jdbc/mydatasource</res-ref-name>
|
<res-ref-name>jdbc/mydatasource</res-ref-name>
|
||||||
<res-type>javax.sql.DataSource</res-type>
|
<res-type>javax.sql.DataSource</res-type>
|
||||||
<res-auth>Container</res-auth>
|
<res-auth>Container</res-auth>
|
||||||
<!--
|
<!-- <injection-target> <injection-target-class>com.acme.JNDITest</injection-target-class>
|
||||||
<injection-target>
|
<injection-target-name>myDatasource</injection-target-name> </injection-target> -->
|
||||||
<injection-target-class>com.acme.JNDITest</injection-target-class>
|
|
||||||
<injection-target-name>myDatasource</injection-target-name>
|
|
||||||
</injection-target>
|
|
||||||
-->
|
|
||||||
</resource-ref>
|
</resource-ref>
|
||||||
|
|
||||||
|
<!-- env entry for which is there is an overriding EnvEntry -->
|
||||||
|
<env-entry>
|
||||||
|
<env-entry-name>foo</env-entry-name>
|
||||||
|
<env-entry-type>java.lang.String</env-entry-type>
|
||||||
|
<env-entry-value>beer</env-entry-value>
|
||||||
|
<injection-target>
|
||||||
|
<injection-target-class>org.eclipse.jetty.plus.webapp.PlusDescriptorProcessorTest$TestInjections</injection-target-class>
|
||||||
|
<injection-target-name>foo</injection-target-name>
|
||||||
|
</injection-target>
|
||||||
|
</env-entry>
|
||||||
|
|
||||||
|
<!-- env entry for which the EnvEntry does not override -->
|
||||||
|
<env-entry>
|
||||||
|
<env-entry-name>bah</env-entry-name>
|
||||||
|
<env-entry-type>java.lang.String</env-entry-type>
|
||||||
|
<env-entry-value>beer</env-entry-value>
|
||||||
|
<injection-target>
|
||||||
|
<injection-target-class>org.eclipse.jetty.plus.webapp.PlusDescriptorProcessorTest$TestInjections</injection-target-class>
|
||||||
|
<injection-target-name>bah</injection-target-name>
|
||||||
|
</injection-target>
|
||||||
|
</env-entry>
|
||||||
|
|
||||||
|
<!-- env entry with no value for which the EnvEntry should override -->
|
||||||
|
<env-entry>
|
||||||
|
<env-entry-name>empty</env-entry-name>
|
||||||
|
<env-entry-type>java.lang.String</env-entry-type>
|
||||||
|
<env-entry-value></env-entry-value>
|
||||||
|
<injection-target>
|
||||||
|
<injection-target-class>org.eclipse.jetty.plus.webapp.PlusDescriptorProcessorTest$TestInjections</injection-target-class>
|
||||||
|
<injection-target-name>empty</injection-target-name>
|
||||||
|
</injection-target>
|
||||||
|
</env-entry>
|
||||||
|
|
||||||
|
<!-- env entry with no value and EnvEntry does not override -->
|
||||||
|
<env-entry>
|
||||||
|
<env-entry-name>vacuum</env-entry-name>
|
||||||
|
<env-entry-type>java.lang.String</env-entry-type>
|
||||||
|
<env-entry-value></env-entry-value>
|
||||||
|
<injection-target>
|
||||||
|
<injection-target-class>org.eclipse.jetty.plus.webapp.PlusDescriptorProcessorTest$TestInjections</injection-target-class>
|
||||||
|
<injection-target-name>vacuum</injection-target-name>
|
||||||
|
</injection-target>
|
||||||
|
</env-entry>
|
||||||
|
|
||||||
|
<!-- env entry with no matching EnvEntry -->
|
||||||
|
<env-entry>
|
||||||
|
<env-entry-name>webxmlonly</env-entry-name>
|
||||||
|
<env-entry-type>java.lang.String</env-entry-type>
|
||||||
|
<env-entry-value>WEBXMLONLY</env-entry-value>
|
||||||
|
<injection-target>
|
||||||
|
<injection-target-class>org.eclipse.jetty.plus.webapp.PlusDescriptorProcessorTest$TestInjections</injection-target-class>
|
||||||
|
<injection-target-name>webXmlOnly</injection-target-name>
|
||||||
|
</injection-target>
|
||||||
|
</env-entry>
|
||||||
</web-app>
|
</web-app>
|
||||||
|
|
Loading…
Reference in New Issue