Search in sources :

Example 11 with ContextEnvironment

use of org.apache.tomcat.util.descriptor.web.ContextEnvironment 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 12 with ContextEnvironment

use of org.apache.tomcat.util.descriptor.web.ContextEnvironment 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 13 with ContextEnvironment

use of org.apache.tomcat.util.descriptor.web.ContextEnvironment in project tomee by apache.

the class TomcatJndiBuilder method mergeRef.

public void mergeRef(final NamingResourcesImpl naming, final EnvEntryInfo ref) {
    // if (!ref.referenceName.startsWith("comp/")) return;
    if ("java.lang.Class".equals(ref.type)) {
        final ContextResourceEnvRef resourceEnv = new ContextResourceEnvRef();
        resourceEnv.setName(ref.referenceName.replaceAll("^comp/env/", ""));
        resourceEnv.setProperty(Constants.FACTORY, ResourceFactory.class.getName());
        resourceEnv.setType(ref.type);
        resourceEnv.setProperty(NamingUtil.RESOURCE_ID, ref.value);
        resourceEnv.setOverride(false);
        naming.addResourceEnvRef(resourceEnv);
        return;
    }
    try {
        final ClassLoader loader = this.standardContext.getLoader().getClassLoader();
        final Class<?> type = loader.loadClass(ref.type);
        if (Enum.class.isAssignableFrom(type)) {
            final ContextResourceEnvRef enumRef = new ContextResourceEnvRef();
            enumRef.setName(ref.referenceName.replaceAll("^comp/env/", ""));
            enumRef.setProperty(Constants.FACTORY, EnumFactory.class.getName());
            enumRef.setProperty(EnumFactory.ENUM_VALUE, ref.value);
            enumRef.setType(ref.type);
            enumRef.setOverride(false);
            naming.addResourceEnvRef(enumRef);
            return;
        }
    } catch (final Throwable e) {
    // no-op
    }
    if (isLookupRef(naming, ref)) {
        return;
    }
    ContextEnvironment environment = naming.findEnvironment(ref.referenceName.replaceAll("^comp/env/", ""));
    boolean addEntry = false;
    if (environment == null) {
        environment = new ContextEnvironment();
        environment.setName(ref.referenceName.replaceAll("^comp/env/", ""));
        addEntry = true;
    }
    environment.setType(ref.type);
    environment.setValue(ref.value);
    environment.setOverride(false);
    if (addEntry) {
        naming.addEnvironment(environment);
    }
    if (replaceEntry) {
        ContextAccessController.setWritable(namingContextListener.getName(), standardContext.getNamingToken());
        if (!addEntry) {
            namingContextListener.removeEnvironment(environment.getName());
        }
        namingContextListener.addEnvironment(environment);
        ContextAccessController.setReadOnly(namingContextListener.getName());
    }
}
Also used : ContextEnvironment(org.apache.tomcat.util.descriptor.web.ContextEnvironment) EnumFactory(org.apache.tomee.common.EnumFactory) ResourceFactory(org.apache.tomee.common.ResourceFactory) ContextResourceEnvRef(org.apache.tomcat.util.descriptor.web.ContextResourceEnvRef)

Example 14 with ContextEnvironment

use of org.apache.tomcat.util.descriptor.web.ContextEnvironment in project tomcat by apache.

the class NamingContextListener method containerEvent.

// ---------------------------------------------- ContainerListener Methods
/**
     * Acknowledge the occurrence of the specified event.
     * Note: Will never be called when the listener is associated to a Server,
     * since it is not a Container.
     *
     * @param event ContainerEvent that has occurred
     */
@Override
public void containerEvent(ContainerEvent event) {
    if (!initialized)
        return;
    // Setting the context in read/write mode
    ContextAccessController.setWritable(getName(), token);
    String type = event.getType();
    if (type.equals("addEjb")) {
        String ejbName = (String) event.getData();
        if (ejbName != null) {
            ContextEjb ejb = namingResources.findEjb(ejbName);
            addEjb(ejb);
        }
    } else if (type.equals("addEnvironment")) {
        String environmentName = (String) event.getData();
        if (environmentName != null) {
            ContextEnvironment env = namingResources.findEnvironment(environmentName);
            addEnvironment(env);
        }
    } else if (type.equals("addLocalEjb")) {
        String localEjbName = (String) event.getData();
        if (localEjbName != null) {
            ContextLocalEjb localEjb = namingResources.findLocalEjb(localEjbName);
            addLocalEjb(localEjb);
        }
    } else if (type.equals("addResource")) {
        String resourceName = (String) event.getData();
        if (resourceName != null) {
            ContextResource resource = namingResources.findResource(resourceName);
            addResource(resource);
        }
    } else if (type.equals("addResourceLink")) {
        String resourceLinkName = (String) event.getData();
        if (resourceLinkName != null) {
            ContextResourceLink resourceLink = namingResources.findResourceLink(resourceLinkName);
            addResourceLink(resourceLink);
        }
    } else if (type.equals("addResourceEnvRef")) {
        String resourceEnvRefName = (String) event.getData();
        if (resourceEnvRefName != null) {
            ContextResourceEnvRef resourceEnvRef = namingResources.findResourceEnvRef(resourceEnvRefName);
            addResourceEnvRef(resourceEnvRef);
        }
    } else if (type.equals("addService")) {
        String serviceName = (String) event.getData();
        if (serviceName != null) {
            ContextService service = namingResources.findService(serviceName);
            addService(service);
        }
    } else if (type.equals("removeEjb")) {
        String ejbName = (String) event.getData();
        if (ejbName != null) {
            removeEjb(ejbName);
        }
    } else if (type.equals("removeEnvironment")) {
        String environmentName = (String) event.getData();
        if (environmentName != null) {
            removeEnvironment(environmentName);
        }
    } else if (type.equals("removeLocalEjb")) {
        String localEjbName = (String) event.getData();
        if (localEjbName != null) {
            removeLocalEjb(localEjbName);
        }
    } else if (type.equals("removeResource")) {
        String resourceName = (String) event.getData();
        if (resourceName != null) {
            removeResource(resourceName);
        }
    } else if (type.equals("removeResourceLink")) {
        String resourceLinkName = (String) event.getData();
        if (resourceLinkName != null) {
            removeResourceLink(resourceLinkName);
        }
    } else if (type.equals("removeResourceEnvRef")) {
        String resourceEnvRefName = (String) event.getData();
        if (resourceEnvRefName != null) {
            removeResourceEnvRef(resourceEnvRefName);
        }
    } else if (type.equals("removeService")) {
        String serviceName = (String) event.getData();
        if (serviceName != null) {
            removeService(serviceName);
        }
    }
    // Setting the context in read only mode
    ContextAccessController.setReadOnly(getName());
}
Also used : ContextEnvironment(org.apache.tomcat.util.descriptor.web.ContextEnvironment) ContextService(org.apache.tomcat.util.descriptor.web.ContextService) ContextLocalEjb(org.apache.tomcat.util.descriptor.web.ContextLocalEjb) 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 15 with ContextEnvironment

use of org.apache.tomcat.util.descriptor.web.ContextEnvironment in project tomcat by apache.

the class NamingContextListener method createNamingContext.

/**
     * Create and initialize the JNDI naming context.
     */
private void createNamingContext() throws NamingException {
    // Creating the comp subcontext
    if (container instanceof Server) {
        compCtx = namingContext;
        envCtx = namingContext;
    } else {
        compCtx = namingContext.createSubcontext("comp");
        envCtx = compCtx.createSubcontext("env");
    }
    int i;
    if (log.isDebugEnabled())
        log.debug("Creating JNDI naming context");
    if (namingResources == null) {
        namingResources = new NamingResourcesImpl();
        namingResources.setContainer(container);
    }
    // Resource links
    ContextResourceLink[] resourceLinks = namingResources.findResourceLinks();
    for (i = 0; i < resourceLinks.length; i++) {
        addResourceLink(resourceLinks[i]);
    }
    // Resources
    ContextResource[] resources = namingResources.findResources();
    for (i = 0; i < resources.length; i++) {
        addResource(resources[i]);
    }
    // Resources Env
    ContextResourceEnvRef[] resourceEnvRefs = namingResources.findResourceEnvRefs();
    for (i = 0; i < resourceEnvRefs.length; i++) {
        addResourceEnvRef(resourceEnvRefs[i]);
    }
    // Environment entries
    ContextEnvironment[] contextEnvironments = namingResources.findEnvironments();
    for (i = 0; i < contextEnvironments.length; i++) {
        addEnvironment(contextEnvironments[i]);
    }
    // EJB references
    ContextEjb[] ejbs = namingResources.findEjbs();
    for (i = 0; i < ejbs.length; i++) {
        addEjb(ejbs[i]);
    }
    // WebServices references
    ContextService[] services = namingResources.findServices();
    for (i = 0; i < services.length; i++) {
        addService(services[i]);
    }
    // Binding a User Transaction reference
    if (container instanceof Context) {
        try {
            Reference ref = new TransactionRef();
            compCtx.bind("UserTransaction", ref);
            ContextTransaction transaction = namingResources.getTransaction();
            if (transaction != null) {
                Iterator<String> params = transaction.listProperties();
                while (params.hasNext()) {
                    String paramName = params.next();
                    String paramValue = (String) transaction.getProperty(paramName);
                    StringRefAddr refAddr = new StringRefAddr(paramName, paramValue);
                    ref.add(refAddr);
                }
            }
        } catch (NameAlreadyBoundException e) {
        // Ignore because UserTransaction was obviously
        // added via ResourceLink
        } catch (NamingException e) {
            log.error(sm.getString("naming.bindFailed", e));
        }
    }
    // Binding the resources directory context
    if (container instanceof Context) {
        try {
            compCtx.bind("Resources", ((Context) container).getResources());
        } catch (NamingException e) {
            log.error(sm.getString("naming.bindFailed", e));
        }
    }
}
Also used : ContextEnvironment(org.apache.tomcat.util.descriptor.web.ContextEnvironment) NamingContext(org.apache.naming.NamingContext) Context(org.apache.catalina.Context) ContextService(org.apache.tomcat.util.descriptor.web.ContextService) Server(org.apache.catalina.Server) ContextResourceLink(org.apache.tomcat.util.descriptor.web.ContextResourceLink) Reference(javax.naming.Reference) ContextTransaction(org.apache.tomcat.util.descriptor.web.ContextTransaction) ContextEjb(org.apache.tomcat.util.descriptor.web.ContextEjb) ContextResource(org.apache.tomcat.util.descriptor.web.ContextResource) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) StringRefAddr(javax.naming.StringRefAddr) TransactionRef(org.apache.naming.TransactionRef) NamingResourcesImpl(org.apache.catalina.deploy.NamingResourcesImpl) NamingException(javax.naming.NamingException) ContextResourceEnvRef(org.apache.tomcat.util.descriptor.web.ContextResourceEnvRef)

Aggregations

ContextEnvironment (org.apache.tomcat.util.descriptor.web.ContextEnvironment)23 ContextResourceLink (org.apache.tomcat.util.descriptor.web.ContextResourceLink)10 ContextResource (org.apache.tomcat.util.descriptor.web.ContextResource)9 ContextResourceEnvRef (org.apache.tomcat.util.descriptor.web.ContextResourceEnvRef)7 Test (org.junit.Test)7 NamingResourcesImpl (org.apache.catalina.deploy.NamingResourcesImpl)6 NamingException (javax.naming.NamingException)5 Context (org.apache.catalina.Context)5 Tomcat (org.apache.catalina.startup.Tomcat)5 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)5 ContextEjb (org.apache.tomcat.util.descriptor.web.ContextEjb)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 InitialContext (javax.naming.InitialContext)4 LifecycleException (org.apache.catalina.LifecycleException)4 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)4 ContextService (org.apache.tomcat.util.descriptor.web.ContextService)4 ContextLocalEjb (org.apache.tomcat.util.descriptor.web.ContextLocalEjb)3 File (java.io.File)2 ObjectName (javax.management.ObjectName)2 StandardContext (org.apache.catalina.core.StandardContext)2