use of org.apache.tomcat.util.descriptor.web.ContextResourceLink in project tomee by apache.
the class TomcatWebAppBuilder method loadWebModule.
/**
* Creates a new {@link WebModule} instance from given
* tomcat context instance.
*
* @param standardContext tomcat context instance
*/
private void loadWebModule(final AppModule appModule, final StandardContext standardContext) {
final List<WebModule> webModules = appModule.getWebModules();
if (webModules.isEmpty()) {
final File file = appModule.getFile();
LOGGER.error("Failed to find a single module in: " + file);
return;
}
final WebModule webModule = webModules.get(0);
final WebApp webApp = webModule.getWebApp();
// create the web module
final String path = standardContext.getPath();
LOGGER.debug("context path = " + path);
webModule.setHost(Contexts.getHostname(standardContext));
// Add all Tomcat env entries to context so they can be overriden by the env.properties file
final NamingResourcesImpl naming = standardContext.getNamingResources();
for (final ContextEnvironment environment : naming.findEnvironments()) {
EnvEntry envEntry = webApp.getEnvEntryMap().get(environment.getName());
if (envEntry == null) {
envEntry = new EnvEntry();
envEntry.setName(environment.getName());
webApp.getEnvEntry().add(envEntry);
}
envEntry.setEnvEntryValue(environment.getValue());
envEntry.setEnvEntryType(environment.getType());
}
// remove all jndi entries where there is a configured Tomcat resource or resource-link
for (final ContextResource resource : naming.findResources()) {
final String name = resource.getName();
removeRef(webApp, name);
}
for (final ContextResourceLink resourceLink : naming.findResourceLinks()) {
final String name = resourceLink.getName();
removeRef(webApp, name);
}
// remove all env entries from the web xml that are not overridable
for (final ContextEnvironment environment : naming.findEnvironments()) {
if (!environment.getOverride()) {
// overrides are not allowed
webApp.getEnvEntryMap().remove(environment.getName());
}
}
}
use of org.apache.tomcat.util.descriptor.web.ContextResourceLink in project tomee by apache.
the class OpenEJBNamingContextListener method processInitialNamingResources.
private void processInitialNamingResources() {
// Resource links
final ContextResourceLink[] resourceLinks = namingResources.findResourceLinks();
for (final ContextResourceLink resourceLink : resourceLinks) {
addResourceLink(resourceLink);
}
// Resources
final ContextResource[] resources = namingResources.findResources();
for (final ContextResource resource : resources) {
addResource(resource);
}
// Resources Env
final ContextResourceEnvRef[] resourceEnvRefs = namingResources.findResourceEnvRefs();
for (final ContextResourceEnvRef resourceEnvRef : resourceEnvRefs) {
addResourceEnvRef(resourceEnvRef);
}
// Environment entries
final ContextEnvironment[] contextEnvironments = namingResources.findEnvironments();
for (final ContextEnvironment contextEnvironment : contextEnvironments) {
addEnvironment(contextEnvironment);
}
// EJB references
final ContextEjb[] ejbs = namingResources.findEjbs();
for (final ContextEjb ejb : ejbs) {
addEjb(ejb);
}
}
use of org.apache.tomcat.util.descriptor.web.ContextResourceLink in project tomcat by apache.
the class TestTomcat method testEnableNamingGlobal.
/*
* Test for enabling JNDI and using global resources.
*/
@Test
public void testEnableNamingGlobal() throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
// Enable JNDI - it is disabled by default
tomcat.enableNaming();
ContextEnvironment environment = new ContextEnvironment();
environment.setType("java.lang.String");
environment.setName("globalTest");
environment.setValue("Tomcat User");
tomcat.getServer().getGlobalNamingResources().addEnvironment(environment);
ContextResourceLink link = new ContextResourceLink();
link.setGlobal("globalTest");
link.setName(HelloWorldJndi.JNDI_ENV_NAME);
link.setType("java.lang.String");
ctx.getNamingResources().addResourceLink(link);
Tomcat.addServlet(ctx, "jndiServlet", new HelloWorldJndi());
ctx.addServletMappingDecoded("/", "jndiServlet");
tomcat.start();
ByteChunk res = getUrl("http://localhost:" + getPort() + "/");
Assert.assertEquals("Hello, Tomcat User", res.toString());
}
use of org.apache.tomcat.util.descriptor.web.ContextResourceLink in project tomcat by apache.
the class NamingResourcesImpl method addEnvironment.
/**
* Add an environment entry for this web application.
*
* @param environment New environment entry
*/
@Override
public void addEnvironment(ContextEnvironment environment) {
if (entries.contains(environment.getName())) {
ContextEnvironment ce = findEnvironment(environment.getName());
ContextResourceLink rl = findResourceLink(environment.getName());
if (ce != null) {
if (ce.getOverride()) {
removeEnvironment(environment.getName());
} else {
return;
}
} else if (rl != null) {
// Link. Need to look at the global resources
NamingResourcesImpl global = getServer().getGlobalNamingResources();
if (global.findEnvironment(rl.getGlobal()) != null) {
if (global.findEnvironment(rl.getGlobal()).getOverride()) {
removeResourceLink(environment.getName());
} else {
return;
}
}
} else {
// It exists but it isn't an env or a res link...
return;
}
}
List<InjectionTarget> injectionTargets = environment.getInjectionTargets();
String value = environment.getValue();
String lookupName = environment.getLookupName();
// Entries with injection targets but no value are effectively ignored
if (injectionTargets != null && injectionTargets.size() > 0 && (value == null || value.length() == 0)) {
return;
}
// Entries with lookup-name and value are an error (EE.5.4.1.3)
if (value != null && value.length() > 0 && lookupName != null && lookupName.length() > 0) {
throw new IllegalArgumentException(sm.getString("namingResources.envEntryLookupValue", environment.getName()));
}
if (!checkResourceType(environment)) {
throw new IllegalArgumentException(sm.getString("namingResources.resourceTypeFail", environment.getName(), environment.getType()));
}
entries.add(environment.getName());
synchronized (envs) {
environment.setNamingResources(this);
envs.put(environment.getName(), environment);
}
support.firePropertyChange("environment", null, environment);
// Register with JMX
if (resourceRequireExplicitRegistration) {
try {
MBeanUtils.createMBean(environment);
} catch (Exception e) {
log.warn(sm.getString("namingResources.mbeanCreateFail", environment.getName()), e);
}
}
}
use of org.apache.tomcat.util.descriptor.web.ContextResourceLink in project tomcat by apache.
the class NamingResourcesImpl method destroyInternal.
@Override
protected void destroyInternal() throws LifecycleException {
// Set this before we de-register currently known naming resources to
// avoid timing issues. Duplication de-registration is not an issue.
resourceRequireExplicitRegistration = false;
// Destroy in reverse order to create, although it should not matter
for (ContextResourceLink crl : resourceLinks.values()) {
try {
MBeanUtils.destroyMBean(crl);
} catch (Exception e) {
log.warn(sm.getString("namingResources.mbeanDestroyFail", crl.getName()), e);
}
}
for (ContextEnvironment ce : envs.values()) {
try {
MBeanUtils.destroyMBean(ce);
} catch (Exception e) {
log.warn(sm.getString("namingResources.mbeanDestroyFail", ce.getName()), e);
}
}
for (ContextResource cr : resources.values()) {
try {
MBeanUtils.destroyMBean(cr);
} catch (Exception e) {
log.warn(sm.getString("namingResources.mbeanDestroyFail", cr.getName()), e);
}
}
super.destroyInternal();
}
Aggregations