Search in sources :

Example 16 with ContextResource

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

the class NamingResourcesSF method storeChildren.

/**
 * Store the specified NamingResources properties.
 *
 * @param aWriter
 *            PrintWriter to which we are storing
 * @param indent
 *            Number of spaces to indent this element
 * @param aElement
 *            Object whose properties are being stored
 * @param elementDesc
 *            element descriptor
 *
 * @exception Exception
 *                if an exception occurs while storing
 *
 * @see org.apache.catalina.storeconfig.StoreFactoryBase#storeChildren(java.io.PrintWriter,
 *      int, java.lang.Object, StoreDescription)
 */
@Override
public void storeChildren(PrintWriter aWriter, int indent, Object aElement, StoreDescription elementDesc) throws Exception {
    if (aElement instanceof NamingResourcesImpl) {
        NamingResourcesImpl resources = (NamingResourcesImpl) aElement;
        // Store nested <Ejb> elements
        ContextEjb[] ejbs = resources.findEjbs();
        storeElementArray(aWriter, indent, ejbs);
        // Store nested <Environment> elements
        ContextEnvironment[] envs = resources.findEnvironments();
        storeElementArray(aWriter, indent, envs);
        // Store nested <LocalEjb> elements
        ContextLocalEjb[] lejbs = resources.findLocalEjbs();
        storeElementArray(aWriter, indent, lejbs);
        // Store nested <Resource> elements
        ContextResource[] dresources = resources.findResources();
        storeElementArray(aWriter, indent, dresources);
        // Store nested <ResourceEnvRef> elements
        ContextResourceEnvRef[] resEnv = resources.findResourceEnvRefs();
        storeElementArray(aWriter, indent, resEnv);
        // Store nested <ResourceLink> elements
        ContextResourceLink[] resourceLinks = resources.findResourceLinks();
        storeElementArray(aWriter, indent, resourceLinks);
    }
}
Also used : ContextEnvironment(org.apache.tomcat.util.descriptor.web.ContextEnvironment) ContextResourceLink(org.apache.tomcat.util.descriptor.web.ContextResourceLink) ContextEjb(org.apache.tomcat.util.descriptor.web.ContextEjb) ContextResource(org.apache.tomcat.util.descriptor.web.ContextResource) ContextLocalEjb(org.apache.tomcat.util.descriptor.web.ContextLocalEjb) NamingResourcesImpl(org.apache.catalina.deploy.NamingResourcesImpl) ContextResourceEnvRef(org.apache.tomcat.util.descriptor.web.ContextResourceEnvRef)

Example 17 with ContextResource

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

use of org.apache.tomcat.util.descriptor.web.ContextResource 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]);
    }
    // Message Destination References
    MessageDestinationRef[] mdrs = namingResources.findMessageDestinationRefs();
    for (i = 0; i < mdrs.length; i++) {
        addMessageDestinationRef(mdrs[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 : ContextService(org.apache.tomcat.util.descriptor.web.ContextService) Server(org.apache.catalina.Server) MessageDestinationRef(org.apache.tomcat.util.descriptor.web.MessageDestinationRef) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) NamingException(javax.naming.NamingException) ContextEnvironment(org.apache.tomcat.util.descriptor.web.ContextEnvironment) NamingContext(org.apache.naming.NamingContext) Context(org.apache.catalina.Context) 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) StringRefAddr(javax.naming.StringRefAddr) TransactionRef(org.apache.naming.TransactionRef) NamingResourcesImpl(org.apache.catalina.deploy.NamingResourcesImpl) ContextResourceEnvRef(org.apache.tomcat.util.descriptor.web.ContextResourceEnvRef)

Example 19 with ContextResource

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

the class NamingResourcesImpl method cleanUp.

/**
 * Close those resources that an explicit close may help clean-up faster.
 */
private void cleanUp() {
    if (resources.size() == 0) {
        return;
    }
    javax.naming.Context ctxt;
    try {
        if (container instanceof Server) {
            ctxt = ((Server) container).getGlobalNamingContext();
        } else {
            ctxt = ContextBindings.getClassLoader();
            ctxt = (javax.naming.Context) ctxt.lookup("comp/env");
        }
    } catch (NamingException e) {
        log.warn(sm.getString("namingResources.cleanupNoContext", container), e);
        return;
    }
    for (ContextResource cr : resources.values()) {
        if (cr.getSingleton()) {
            String closeMethod = cr.getCloseMethod();
            if (closeMethod != null && closeMethod.length() > 0) {
                String name = cr.getName();
                Object resource;
                try {
                    resource = ctxt.lookup(name);
                } catch (NamingException e) {
                    log.warn(sm.getString("namingResources.cleanupNoResource", cr.getName(), container), e);
                    continue;
                }
                cleanUp(resource, name, closeMethod);
            }
        }
    }
}
Also used : Server(org.apache.catalina.Server) NamingException(javax.naming.NamingException) ContextResource(org.apache.tomcat.util.descriptor.web.ContextResource)

Example 20 with ContextResource

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

the class NamingResourcesImpl method initInternal.

// ------------------------------------------------------- Lifecycle methods
@Override
protected void initInternal() throws LifecycleException {
    super.initInternal();
    // Set this before we register currently known naming resources to avoid
    // timing issues. Duplication registration is not an issue.
    resourceRequireExplicitRegistration = true;
    for (ContextResource cr : resources.values()) {
        try {
            MBeanUtils.createMBean(cr);
        } catch (Exception e) {
            log.warn(sm.getString("namingResources.mbeanCreateFail", cr.getName()), e);
        }
    }
    for (ContextEnvironment ce : envs.values()) {
        try {
            MBeanUtils.createMBean(ce);
        } catch (Exception e) {
            log.warn(sm.getString("namingResources.mbeanCreateFail", ce.getName()), e);
        }
    }
    for (ContextResourceLink crl : resourceLinks.values()) {
        try {
            MBeanUtils.createMBean(crl);
        } catch (Exception e) {
            log.warn(sm.getString("namingResources.mbeanCreateFail", crl.getName()), e);
        }
    }
}
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

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