/** * Creates and binds a new context. Creates a new context with the given name and binds it in the * target context (that named by all but terminal atomic component of the name). All intermediate * contexts and the target context must already exist. * * @param name the name of the context to create; may not be empty * @return the newly created context * @exception NameAlreadyBoundException if name is already bound * @exception javax.naming.directory.InvalidAttributesException if creation of the sub-context * requires specification of mandatory attributes * @exception NamingException if a naming exception is encountered */ @Override public Context createSubcontext(Name name) throws NamingException { if (!checkWritable()) { return null; } NamingContext newContext = new NamingContext(env, this.name); bind(name, newContext); newContext.setExceptionOnFailedWrite(getExceptionOnFailedWrite()); return newContext; }
/** * 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(); logger = log; 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) { logger.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) { logger.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) { logger.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); } } } finally { objectNames.clear(); namingContext = null; envCtx = null; compCtx = null; initialized = false; } } }