Search in sources :

Example 1 with ContextResource

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

the class TomcatWebAppBuilder method createReference.

private static Reference createReference(final ResourceBase resource) {
    final Reference ref;
    if (resource instanceof ContextResource) {
        final ContextResource cr = (ContextResource) resource;
        ref = new ResourceRef(resource.getType(), resource.getDescription(), cr.getScope(), cr.getAuth(), cr.getSingleton());
    } else {
        ref = new ResourceEnvRef(resource.getType());
    }
    final Iterator<String> params = resource.listProperties();
    while (params.hasNext()) {
        final String paramName = params.next();
        final String paramValue = (String) resource.getProperty(paramName);
        final StringRefAddr refAddr = new StringRefAddr(paramName, paramValue);
        ref.add(refAddr);
    }
    return ref;
}
Also used : StringRefAddr(javax.naming.StringRefAddr) SystemComponentReference(org.apache.openejb.core.ivm.naming.SystemComponentReference) Reference(javax.naming.Reference) AtomicReference(java.util.concurrent.atomic.AtomicReference) ResourceRef(org.apache.naming.ResourceRef) ResourceEnvRef(org.apache.naming.ResourceEnvRef) ContextResource(org.apache.tomcat.util.descriptor.web.ContextResource)

Example 2 with ContextResource

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

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

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

the class TomcatJndiBuilder method importOpenEJBResourcesInTomcat.

public static void importOpenEJBResourcesInTomcat(final Collection<ResourceInfo> resources, final StandardServer server) {
    final NamingResourcesImpl naming = server.getGlobalNamingResources();
    for (final ResourceInfo info : resources) {
        final String name = info.id;
        // if invalid or existing or lazy just skip it cause doesnt work during startup
        if (name == null || naming.findResource(name) != null || info.properties.containsKey("UseAppClassLoader")) {
            continue;
        }
        final ContextResource resource = new ContextResource();
        resource.setName(name);
        resource.setProperty(Constants.FACTORY, ResourceFactory.class.getName());
        resource.setProperty(NamingUtil.NAME, name);
        resource.setType(info.className);
        resource.setAuth("Container");
        naming.addResource(resource);
    }
}
Also used : ResourceInfo(org.apache.openejb.assembler.classic.ResourceInfo) NamingResourcesImpl(org.apache.catalina.deploy.NamingResourcesImpl) ResourceFactory(org.apache.tomee.common.ResourceFactory) ContextResource(org.apache.tomcat.util.descriptor.web.ContextResource)

Example 5 with ContextResource

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

the class TomcatJndiBuilder method mergeRef.

public void mergeRef(final NamingResourcesImpl naming, final ResourceReferenceInfo ref) {
    if (isLookupRef(naming, ref)) {
        return;
    }
    final String name = ref.referenceName.replaceAll("^comp/env/", "");
    if (isOpenEjb(naming, name)) {
        return;
    }
    ContextResource resource = naming.findResource(name);
    boolean addEntry = false;
    if (resource == null) {
        resource = new ContextResource();
        resource.setName(name);
        addEntry = true;
    }
    resource.setProperty(Constants.FACTORY, ResourceFactory.class.getName());
    resource.setProperty(NamingUtil.NAME, name);
    resource.setType(ref.referenceType);
    resource.setAuth(ref.referenceAuth);
    if (ref.resourceID != null) {
        resource.setProperty(NamingUtil.RESOURCE_ID, ref.resourceID);
    }
    if (ref.location != null) {
        resource.setProperty(NamingUtil.JNDI_NAME, ref.location.jndiName);
        resource.setProperty(NamingUtil.JNDI_PROVIDER_ID, ref.location.jndiProviderId);
    }
    if (addEntry) {
        naming.addResource(resource);
    }
    if (replaceEntry) {
        ContextAccessController.setWritable(namingContextListener.getName(), standardContext.getNamingToken());
        if (!addEntry) {
            namingContextListener.removeResource(resource.getName());
        }
        namingContextListener.addResource(resource);
        ContextAccessController.setReadOnly(namingContextListener.getName());
    }
}
Also used : ResourceFactory(org.apache.tomee.common.ResourceFactory) ContextResource(org.apache.tomcat.util.descriptor.web.ContextResource)

Aggregations

ContextResource (org.apache.tomcat.util.descriptor.web.ContextResource)28 NamingResourcesImpl (org.apache.catalina.deploy.NamingResourcesImpl)10 ContextEnvironment (org.apache.tomcat.util.descriptor.web.ContextEnvironment)9 NamingException (javax.naming.NamingException)7 ContextResourceLink (org.apache.tomcat.util.descriptor.web.ContextResourceLink)7 ContextResourceEnvRef (org.apache.tomcat.util.descriptor.web.ContextResourceEnvRef)6 ContextEjb (org.apache.tomcat.util.descriptor.web.ContextEjb)5 ContextService (org.apache.tomcat.util.descriptor.web.ContextService)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 ArrayList (java.util.ArrayList)3 LifecycleException (org.apache.catalina.LifecycleException)3 Tomcat (org.apache.catalina.startup.Tomcat)3 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)3 ObjectName (javax.management.ObjectName)2 RuntimeOperationsException (javax.management.RuntimeOperationsException)2 Context (javax.naming.Context)2 Reference (javax.naming.Reference)2 StringRefAddr (javax.naming.StringRefAddr)2 EntityManagerFactory (javax.persistence.EntityManagerFactory)2 Server (org.apache.catalina.Server)2