Search in sources :

Example 1 with ContextConfig

use of org.apache.catalina.startup.ContextConfig in project tomcat by apache.

the class MBeanFactory method createStandardContext.

/**
     * Create a new StandardContext.
     *
     * @param parent MBean Name of the associated parent component
     * @param path The context path for this Context
     * @param docBase Document base directory (or WAR) for this Context
     * @param xmlValidation if XML descriptors should be validated
     * @param xmlNamespaceAware if the XML processor should namespace aware
     * @return the object name of the created context
     *
     * @exception Exception if an MBean cannot be created or registered
     */
public String createStandardContext(String parent, String path, String docBase, boolean xmlValidation, boolean xmlNamespaceAware) throws Exception {
    // Create a new StandardContext instance
    StandardContext context = new StandardContext();
    path = getPathStr(path);
    context.setPath(path);
    context.setDocBase(docBase);
    context.setXmlValidation(xmlValidation);
    context.setXmlNamespaceAware(xmlNamespaceAware);
    ContextConfig contextConfig = new ContextConfig();
    context.addLifecycleListener(contextConfig);
    // Add the new instance to its parent component
    ObjectName pname = new ObjectName(parent);
    ObjectName deployer = new ObjectName(pname.getDomain() + ":type=Deployer,host=" + pname.getKeyProperty("host"));
    if (mserver.isRegistered(deployer)) {
        String contextName = context.getName();
        mserver.invoke(deployer, "addServiced", new Object[] { contextName }, new String[] { "java.lang.String" });
        String configPath = (String) mserver.getAttribute(deployer, "configBaseName");
        String baseName = context.getBaseName();
        File configFile = new File(new File(configPath), baseName + ".xml");
        if (configFile.isFile()) {
            context.setConfigFile(configFile.toURI().toURL());
        }
        mserver.invoke(deployer, "manageApp", new Object[] { context }, new String[] { "org.apache.catalina.Context" });
        mserver.invoke(deployer, "removeServiced", new Object[] { contextName }, new String[] { "java.lang.String" });
    } else {
        log.warn("Deployer not found for " + pname.getKeyProperty("host"));
        Service service = getService(pname);
        Engine engine = service.getContainer();
        Host host = (Host) engine.findChild(pname.getKeyProperty("host"));
        host.addChild(context);
    }
    // Return the corresponding MBean name
    return context.getObjectName().toString();
}
Also used : ContextConfig(org.apache.catalina.startup.ContextConfig) StandardContext(org.apache.catalina.core.StandardContext) StandardService(org.apache.catalina.core.StandardService) Service(org.apache.catalina.Service) StandardHost(org.apache.catalina.core.StandardHost) Host(org.apache.catalina.Host) File(java.io.File) StandardEngine(org.apache.catalina.core.StandardEngine) Engine(org.apache.catalina.Engine) ObjectName(javax.management.ObjectName)

Example 2 with ContextConfig

use of org.apache.catalina.startup.ContextConfig in project tomcat by apache.

the class ContainerMBean method addChild.

/**
     * Add a new child Container to those associated with this Container,
     * if supported. Won't start the child yet. Has to be started with a call to
     * Start method after necessary configurations are done.
     *
     * @param type ClassName of the child to be added
     * @param name Name of the child to be added
     *
     * @exception MBeanException if the child cannot be added
     */
public void addChild(String type, String name) throws MBeanException {
    Container contained = (Container) newInstance(type);
    contained.setName(name);
    if (contained instanceof StandardHost) {
        HostConfig config = new HostConfig();
        contained.addLifecycleListener(config);
    } else if (contained instanceof StandardContext) {
        ContextConfig config = new ContextConfig();
        contained.addLifecycleListener(config);
    }
    boolean oldValue = true;
    ContainerBase container = doGetManagedResource();
    try {
        oldValue = container.getStartChildren();
        container.setStartChildren(false);
        container.addChild(contained);
        contained.init();
    } catch (LifecycleException e) {
        throw new MBeanException(e);
    } finally {
        if (container != null) {
            container.setStartChildren(oldValue);
        }
    }
}
Also used : ContextConfig(org.apache.catalina.startup.ContextConfig) ContainerBase(org.apache.catalina.core.ContainerBase) Container(org.apache.catalina.Container) LifecycleException(org.apache.catalina.LifecycleException) StandardHost(org.apache.catalina.core.StandardHost) StandardContext(org.apache.catalina.core.StandardContext) HostConfig(org.apache.catalina.startup.HostConfig) MBeanException(javax.management.MBeanException)

Example 3 with ContextConfig

use of org.apache.catalina.startup.ContextConfig in project tomee by apache.

the class TomcatWebAppBuilder method init.

/**
     * {@inheritDoc}
     */
@Override
public void init(final StandardContext standardContext) {
    if (isIgnored(standardContext)) {
        return;
    }
    // just adding a carriage return to get logs more readable
    logger.info("------------------------- " + Contexts.getHostname(standardContext).replace("_", hosts.getDefaultHost()) + " -> " + finalName(standardContext.getPath()));
    if (FORCE_RELOADABLE) {
        final ContextInfo ctxInfo = getContextInfo(standardContext);
        if (ctxInfo == null || (ctxInfo.appInfo != null && ctxInfo.appInfo.webAppAlone)) {
            // don't do it for ears
            standardContext.setReloadable(true);
        }
    }
    if (SKIP_TLD) {
        if (standardContext.getJarScanner() != null && standardContext.getJarScanner().getJarScanFilter() != null) {
            final JarScanFilter jarScanFilter = standardContext.getJarScanner().getJarScanFilter();
            if (StandardJarScanFilter.class.isInstance(jarScanFilter)) {
                StandardJarScanFilter.class.cast(jarScanFilter).setDefaultTldScan(false);
            }
        }
    }
    final String name = standardContext.getName();
    initJ2EEInfo(standardContext);
    File warFile = Contexts.warPath(standardContext);
    if (!warFile.exists()) {
        return;
    }
    if (!warFile.isDirectory()) {
        try {
            warFile = DeploymentLoader.unpack(warFile);
        } catch (final OpenEJBException e) {
            logger.error("can't unpack '" + warFile.getAbsolutePath() + "'");
        }
    }
    standardContext.setCrossContext(SystemInstance.get().getOptions().get(OPENEJB_CROSSCONTEXT_PROPERTY, false));
    standardContext.setNamingResources(new OpenEJBNamingResource(standardContext.getNamingResources()));
    String sessionManager = SystemInstance.get().getOptions().get(OPENEJB_SESSION_MANAGER_PROPERTY + "." + name, (String) null);
    if (sessionManager == null) {
        sessionManager = SystemInstance.get().getOptions().get(OPENEJB_SESSION_MANAGER_PROPERTY, (String) null);
    }
    if (sessionManager != null) {
        if (sessionManagerClass == null) {
            try {
                // the manager should be in standardclassloader
                sessionManagerClass = ParentClassLoaderFinder.Helper.get().loadClass(sessionManager);
            } catch (final ClassNotFoundException e) {
                logger.error("can't find '" + sessionManager + "', StandardManager will be used", e);
                sessionManagerClass = StandardManager.class;
            }
        }
        try {
            final Manager mgr = (Manager) sessionManagerClass.newInstance();
            standardContext.setManager(mgr);
        } catch (final Exception e) {
            logger.error("can't instantiate '" + sessionManager + "', StandardManager will be used", e);
        }
    }
    final LifecycleListener[] listeners = standardContext.findLifecycleListeners();
    for (final LifecycleListener l : listeners) {
        if (l instanceof ContextConfig) {
            standardContext.removeLifecycleListener(l);
        }
    }
    standardContext.addLifecycleListener(new OpenEJBContextConfig(new StandardContextInfo(standardContext)));
    // force manually the namingContextListener to merge jndi in an easier way
    final NamingContextListener ncl = new NamingContextListener();
    try {
        ncl.setName((String) getNamingContextName.invoke(standardContext));
    } catch (final Exception e) {
        ncl.setName(getId(standardContext));
    }
    ncl.setExceptionOnFailedWrite(standardContext.getJndiExceptionOnFailedWrite());
    standardContext.setNamingContextListener(ncl);
    standardContext.addLifecycleListener(ncl);
    standardContext.addLifecycleListener(new TomcatJavaJndiBinder());
    // listen some events
    standardContext.addContainerListener(new TomEEContainerListener());
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) StandardManager(org.apache.catalina.session.StandardManager) JarScanFilter(org.apache.tomcat.JarScanFilter) StandardJarScanFilter(org.apache.tomcat.util.scan.StandardJarScanFilter) OpenEJBContextConfig(org.apache.catalina.startup.OpenEJBContextConfig) LifecycleListener(org.apache.catalina.LifecycleListener) Manager(org.apache.catalina.Manager) InstanceManager(org.apache.tomcat.InstanceManager) StandardManager(org.apache.catalina.session.StandardManager) DeploymentExceptionManager(org.apache.openejb.assembler.classic.DeploymentExceptionManager) TransactionManager(javax.transaction.TransactionManager) StandardJarScanFilter(org.apache.tomcat.util.scan.StandardJarScanFilter) LifecycleException(org.apache.catalina.LifecycleException) NameNotFoundException(javax.naming.NameNotFoundException) IOException(java.io.IOException) NamingException(javax.naming.NamingException) OpenEJBException(org.apache.openejb.OpenEJBException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) ContextConfig(org.apache.catalina.startup.ContextConfig) OpenEJBContextConfig(org.apache.catalina.startup.OpenEJBContextConfig) NamingContextListener(org.apache.catalina.core.NamingContextListener) File(java.io.File) JarFile(java.util.jar.JarFile)

Aggregations

ContextConfig (org.apache.catalina.startup.ContextConfig)3 File (java.io.File)2 LifecycleException (org.apache.catalina.LifecycleException)2 StandardContext (org.apache.catalina.core.StandardContext)2 StandardHost (org.apache.catalina.core.StandardHost)2 IOException (java.io.IOException)1 JarFile (java.util.jar.JarFile)1 MBeanException (javax.management.MBeanException)1 ObjectName (javax.management.ObjectName)1 NameNotFoundException (javax.naming.NameNotFoundException)1 NamingException (javax.naming.NamingException)1 TransactionManager (javax.transaction.TransactionManager)1 Container (org.apache.catalina.Container)1 Engine (org.apache.catalina.Engine)1 Host (org.apache.catalina.Host)1 LifecycleListener (org.apache.catalina.LifecycleListener)1 Manager (org.apache.catalina.Manager)1 Service (org.apache.catalina.Service)1 ContainerBase (org.apache.catalina.core.ContainerBase)1 NamingContextListener (org.apache.catalina.core.NamingContextListener)1