Search in sources :

Example 1 with NamingContext

use of org.apache.naming.NamingContext in project tomcat by apache.

the class NamingContextListener method lifecycleEvent.

// ---------------------------------------------- LifecycleListener Methods
/**
     * Acknowledge the occurrence of the specified event.
     *
     * @param event LifecycleEvent that has occurred
     */
@Override
public void lifecycleEvent(LifecycleEvent event) {
    container = event.getLifecycle();
    if (container instanceof Context) {
        namingResources = ((Context) container).getNamingResources();
        token = ((Context) container).getNamingToken();
    } else if (container instanceof Server) {
        namingResources = ((Server) container).getGlobalNamingResources();
        token = ((Server) container).getNamingToken();
    } else {
        return;
    }
    if (Lifecycle.CONFIGURE_START_EVENT.equals(event.getType())) {
        if (initialized)
            return;
        try {
            Hashtable<String, Object> contextEnv = new Hashtable<>();
            namingContext = new NamingContext(contextEnv, getName());
            ContextAccessController.setSecurityToken(getName(), token);
            ContextAccessController.setSecurityToken(container, token);
            ContextBindings.bindContext(container, namingContext, token);
            if (log.isDebugEnabled()) {
                log.debug("Bound " + container);
            }
            // Configure write when read-only behaviour
            namingContext.setExceptionOnFailedWrite(getExceptionOnFailedWrite());
            // Setting the context in read/write mode
            ContextAccessController.setWritable(getName(), token);
            try {
                createNamingContext();
            } catch (NamingException e) {
                log.error(sm.getString("naming.namingContextCreationFailed", e));
            }
            namingResources.addPropertyChangeListener(this);
            // Binding the naming context to the class loader
            if (container instanceof Context) {
                // Setting the context in read only mode
                ContextAccessController.setReadOnly(getName());
                try {
                    ContextBindings.bindClassLoader(container, token, ((Context) container).getLoader().getClassLoader());
                } catch (NamingException e) {
                    log.error(sm.getString("naming.bindFailed", e));
                }
            }
            if (container instanceof Server) {
                org.apache.naming.factory.ResourceLinkFactory.setGlobalContext(namingContext);
                try {
                    ContextBindings.bindClassLoader(container, token, this.getClass().getClassLoader());
                } catch (NamingException e) {
                    log.error(sm.getString("naming.bindFailed", e));
                }
                if (container instanceof StandardServer) {
                    ((StandardServer) container).setGlobalNamingContext(namingContext);
                }
            }
        } finally {
            // Regardless of success, so that we can do cleanup on configure_stop
            initialized = true;
        }
    } else if (Lifecycle.CONFIGURE_STOP_EVENT.equals(event.getType())) {
        if (!initialized)
            return;
        try {
            // Setting the context in read/write mode
            ContextAccessController.setWritable(getName(), token);
            ContextBindings.unbindContext(container, token);
            if (container instanceof Context) {
                ContextBindings.unbindClassLoader(container, token, ((Context) container).getLoader().getClassLoader());
            }
            if (container instanceof Server) {
                namingResources.removePropertyChangeListener(this);
                ContextBindings.unbindClassLoader(container, token, this.getClass().getClassLoader());
            }
            ContextAccessController.unsetSecurityToken(getName(), token);
            ContextAccessController.unsetSecurityToken(container, token);
            // unregister mbeans.
            if (!objectNames.isEmpty()) {
                Collection<ObjectName> names = objectNames.values();
                Registry registry = Registry.getRegistry(null, null);
                for (ObjectName objectName : names) {
                    registry.unregisterComponent(objectName);
                }
            }
            javax.naming.Context global = getGlobalNamingContext();
            if (global != null) {
                ResourceLinkFactory.deregisterGlobalResourceAccess(global);
            }
        } finally {
            objectNames.clear();
            namingContext = null;
            envCtx = null;
            compCtx = null;
            initialized = false;
        }
    }
}
Also used : NamingContext(org.apache.naming.NamingContext) Context(org.apache.catalina.Context) Server(org.apache.catalina.Server) Hashtable(java.util.Hashtable) Registry(org.apache.tomcat.util.modeler.Registry) NamingContext(org.apache.naming.NamingContext) ObjectName(javax.management.ObjectName) Collection(java.util.Collection) NamingException(javax.naming.NamingException)

Example 2 with NamingContext

use of org.apache.naming.NamingContext 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

NamingException (javax.naming.NamingException)2 Context (org.apache.catalina.Context)2 Server (org.apache.catalina.Server)2 NamingContext (org.apache.naming.NamingContext)2 Collection (java.util.Collection)1 Hashtable (java.util.Hashtable)1 ObjectName (javax.management.ObjectName)1 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)1 Reference (javax.naming.Reference)1 StringRefAddr (javax.naming.StringRefAddr)1 NamingResourcesImpl (org.apache.catalina.deploy.NamingResourcesImpl)1 TransactionRef (org.apache.naming.TransactionRef)1 ContextEjb (org.apache.tomcat.util.descriptor.web.ContextEjb)1 ContextEnvironment (org.apache.tomcat.util.descriptor.web.ContextEnvironment)1 ContextResource (org.apache.tomcat.util.descriptor.web.ContextResource)1 ContextResourceEnvRef (org.apache.tomcat.util.descriptor.web.ContextResourceEnvRef)1 ContextResourceLink (org.apache.tomcat.util.descriptor.web.ContextResourceLink)1 ContextService (org.apache.tomcat.util.descriptor.web.ContextService)1 ContextTransaction (org.apache.tomcat.util.descriptor.web.ContextTransaction)1 Registry (org.apache.tomcat.util.modeler.Registry)1