Search in sources :

Example 1 with ContextResourceLink

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());
        }
    }
}
Also used : ContextEnvironment(org.apache.tomcat.util.descriptor.web.ContextEnvironment) ContextResourceLink(org.apache.tomcat.util.descriptor.web.ContextResourceLink) NamingResourcesImpl(org.apache.catalina.deploy.NamingResourcesImpl) WebModule(org.apache.openejb.config.WebModule) File(java.io.File) JarFile(java.util.jar.JarFile) WebApp(org.apache.openejb.jee.WebApp) EnvEntry(org.apache.openejb.jee.EnvEntry) ContextResource(org.apache.tomcat.util.descriptor.web.ContextResource)

Example 2 with ContextResourceLink

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);
    }
}
Also used : ContextEnvironment(org.apache.tomcat.util.descriptor.web.ContextEnvironment) ContextResourceLink(org.apache.tomcat.util.descriptor.web.ContextResourceLink) ContextResourceEnvRef(org.apache.tomcat.util.descriptor.web.ContextResourceEnvRef) ContextEjb(org.apache.tomcat.util.descriptor.web.ContextEjb) ContextResource(org.apache.tomcat.util.descriptor.web.ContextResource)

Example 3 with ContextResourceLink

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());
}
Also used : ReplicatedContext(org.apache.catalina.ha.context.ReplicatedContext) InitialContext(javax.naming.InitialContext) Context(org.apache.catalina.Context) StandardContext(org.apache.catalina.core.StandardContext) ContextEnvironment(org.apache.tomcat.util.descriptor.web.ContextEnvironment) ContextResourceLink(org.apache.tomcat.util.descriptor.web.ContextResourceLink) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) Test(org.junit.Test)

Example 4 with ContextResourceLink

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);
        }
    }
}
Also used : ContextEnvironment(org.apache.tomcat.util.descriptor.web.ContextEnvironment) ContextResourceLink(org.apache.tomcat.util.descriptor.web.ContextResourceLink) InjectionTarget(org.apache.tomcat.util.descriptor.web.InjectionTarget) NamingException(javax.naming.NamingException) LifecycleException(org.apache.catalina.LifecycleException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 5 with ContextResourceLink

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();
}
Also used : ContextEnvironment(org.apache.tomcat.util.descriptor.web.ContextEnvironment) ContextResourceLink(org.apache.tomcat.util.descriptor.web.ContextResourceLink) NamingException(javax.naming.NamingException) LifecycleException(org.apache.catalina.LifecycleException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ContextResource(org.apache.tomcat.util.descriptor.web.ContextResource)

Aggregations

ContextResourceLink (org.apache.tomcat.util.descriptor.web.ContextResourceLink)16 ContextEnvironment (org.apache.tomcat.util.descriptor.web.ContextEnvironment)10 ContextResource (org.apache.tomcat.util.descriptor.web.ContextResource)7 NamingResourcesImpl (org.apache.catalina.deploy.NamingResourcesImpl)6 NamingException (javax.naming.NamingException)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 LifecycleException (org.apache.catalina.LifecycleException)4 ContextEjb (org.apache.tomcat.util.descriptor.web.ContextEjb)4 ContextResourceEnvRef (org.apache.tomcat.util.descriptor.web.ContextResourceEnvRef)4 ObjectName (javax.management.ObjectName)2 RuntimeOperationsException (javax.management.RuntimeOperationsException)2 Context (org.apache.catalina.Context)2 ContextLocalEjb (org.apache.tomcat.util.descriptor.web.ContextLocalEjb)2 ContextService (org.apache.tomcat.util.descriptor.web.ContextService)2 Test (org.junit.Test)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 JarFile (java.util.jar.JarFile)1 AttributeNotFoundException (javax.management.AttributeNotFoundException)1 MalformedObjectNameException (javax.management.MalformedObjectNameException)1