Search in sources :

Example 1 with ContainerBase

use of org.apache.catalina.core.ContainerBase in project tomcat by apache.

the class HostManagerServlet method remove.

/**
     * Remove the specified host.
     *
     * @param writer Writer to render results to
     * @param name host name
     * @param smClient StringManager for the client's locale
     */
protected synchronized void remove(PrintWriter writer, String name, StringManager smClient) {
    if (debug >= 1) {
        log(sm.getString("hostManagerServlet.remove", name));
    }
    // Validate the requested host name
    if ((name == null) || name.length() == 0) {
        writer.println(smClient.getString("hostManagerServlet.invalidHostName", name));
        return;
    }
    // Check if host exists
    if (engine.findChild(name) == null) {
        writer.println(smClient.getString("hostManagerServlet.noHost", name));
        return;
    }
    // Prevent removing our own host
    if (engine.findChild(name) == installedHost) {
        writer.println(smClient.getString("hostManagerServlet.cannotRemoveOwnHost", name));
        return;
    }
    // Note that the host will not get physically removed
    try {
        Container child = engine.findChild(name);
        engine.removeChild(child);
        if (child instanceof ContainerBase)
            ((ContainerBase) child).destroy();
    } catch (Exception e) {
        writer.println(smClient.getString("hostManagerServlet.exception", e.toString()));
        return;
    }
    Host host = (StandardHost) engine.findChild(name);
    if (host == null) {
        writer.println(smClient.getString("hostManagerServlet.remove", name));
    } else {
        // Something failed
        writer.println(smClient.getString("hostManagerServlet.removeFailed", name));
    }
}
Also used : ContainerBase(org.apache.catalina.core.ContainerBase) Container(org.apache.catalina.Container) StandardHost(org.apache.catalina.core.StandardHost) StandardHost(org.apache.catalina.core.StandardHost) Host(org.apache.catalina.Host) ServletException(javax.servlet.ServletException) InstanceNotFoundException(javax.management.InstanceNotFoundException) IOException(java.io.IOException) UnavailableException(javax.servlet.UnavailableException)

Example 2 with ContainerBase

use of org.apache.catalina.core.ContainerBase 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 ContainerBase

use of org.apache.catalina.core.ContainerBase in project tomee by apache.

the class GlobalListenerSupport method addContextListener.

/**
     * Setting monitoreable child field.
     *
     * @param containerBase host or engine
     */
@SuppressWarnings("unchecked")
private void addContextListener(final ContainerBase containerBase) {
    boolean accessible = false;
    Field field = null;
    try {
        field = ContainerBase.class.getDeclaredField("children");
        accessible = field.isAccessible();
        field.setAccessible(true);
        Map<Object, Object> children = (Map<Object, Object>) field.get(containerBase);
        if (children instanceof GlobalListenerSupport.MoniterableHashMap) {
            return;
        }
        children = new GlobalListenerSupport.MoniterableHashMap(children, containerBase, "children", this);
        field.set(containerBase, children);
    } catch (final Exception e) {
        e.printStackTrace();
    } finally {
        if (field != null) {
            if (!accessible) {
                field.setAccessible(false);
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) ContainerBase(org.apache.catalina.core.ContainerBase) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 4 with ContainerBase

use of org.apache.catalina.core.ContainerBase in project tomee by apache.

the class TomcatWebAppBuilder method checkHost.

/**
     * {@inheritDoc}
     */
@Override
public synchronized void checkHost(final StandardHost standardHost) {
    if (noHostCheck) {
        return;
    }
    if (standardHost.getAutoDeploy()) {
        // Undeploy any modified application
        for (final Iterator<Map.Entry<String, DeployedApplication>> iterator = deployedApps.entrySet().iterator(); iterator.hasNext(); ) {
            final Map.Entry<String, DeployedApplication> entry = iterator.next();
            final DeployedApplication deployedApplication = entry.getValue();
            if (deployedApplication.isModified()) {
                // TODO: for war use StandardContext.redeploy()
                if (deployedApplication.appInfo != null) {
                    // can happen with badly formed config
                    try {
                        getAssembler().destroyApplication(deployedApplication.appInfo.path);
                    } catch (final Exception e) {
                        logger.error("Unable to application " + deployedApplication.appInfo.path, e);
                    }
                } else {
                    logger.error("appinfo is null for " + deployedApplication);
                }
                iterator.remove();
            }
        }
        // Deploy new applications
        final File appBase = appBase(standardHost);
        final File[] files = appBase.listFiles();
        if (null != files) {
            for (File file : files) {
                if (file.getName().endsWith(".tmp")) {
                    // tomcat is uploading, see org.apache.catalina.manager.ManagerServlet.deploy(java.io.PrintWriter, org.apache.catalina.util.ContextName, java.lang.String, boolean, javax.servlet.http.HttpServletRequest, org.apache.tomcat.util.res.StringManager)
                    continue;
                }
                final String name = file.getName();
                // ignore war files
                if (name.toLowerCase().endsWith(".war") || isRoot(name) || name.equalsIgnoreCase("META-INF") || name.equalsIgnoreCase("WEB-INF")) {
                    continue;
                }
                // Simple fix for TOMEE-23
                if (name.toLowerCase().equals(".ds_store")) {
                    continue;
                }
                // ignore unpacked web apps
                if (file.isDirectory() && new File(file, "WEB-INF").exists()) {
                    continue;
                }
                // ignore unpacked apps where packed version is present (packed version is owner)
                if (file.isDirectory() && (new File(file.getParent(), file.getName() + ".ear").exists() || new File(file.getParent(), file.getName() + ".war").exists() || new File(file.getParent(), file.getName() + ".rar").exists())) {
                    continue;
                }
                // ignore already deployed apps
                if (isDeployed(file, standardHost)) {
                    continue;
                }
                final AppInfo appInfo;
                try {
                    file = file.getCanonicalFile().getAbsoluteFile();
                    final AppModule appModule = deploymentLoader.load(file, null);
                    // Ignore any standalone web modules - this happens when the app is unpaked and doesn't have a WEB-INF dir
                    if (appModule.getDeploymentModule().size() == 1 && appModule.getWebModules().size() == 1) {
                        final WebModule webModule = appModule.getWebModules().iterator().next();
                        if (file.getAbsolutePath().equals(webModule.getJarLocation())) {
                            continue;
                        }
                    }
                    // tell web modules to deploy using this host
                    for (final WebModule webModule : appModule.getWebModules()) {
                        webModule.setHost(standardHost.getName());
                    }
                    appInfo = configurationFactory.configureApplication(appModule);
                    // if this is an unpacked dir, tomcat will pick it up as a webapp so undeploy it first
                    if (file.isDirectory()) {
                        final ContainerBase context = (ContainerBase) standardHost.findChild("/" + name);
                        if (context != null) {
                            try {
                                standardHost.removeChild(context);
                            } catch (final Throwable t) {
                                logger.warning("Error undeploying wep application from Tomcat  " + name, t);
                            }
                            try {
                                context.destroy();
                            } catch (final Throwable t) {
                                logger.warning("Error destroying Tomcat web context " + name, t);
                            }
                        }
                    }
                    getAssembler().createApplication(appInfo);
                    deployedApps.put(file.getAbsolutePath(), new DeployedApplication(file, appInfo));
                } catch (final Throwable e) {
                    logger.warning("Error deploying application " + file.getAbsolutePath(), e);
                }
            }
        }
    }
}
Also used : ContainerBase(org.apache.catalina.core.ContainerBase) AppModule(org.apache.openejb.config.AppModule) WebModule(org.apache.openejb.config.WebModule) 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) AppInfo(org.apache.openejb.assembler.classic.AppInfo) WebAppInfo(org.apache.openejb.assembler.classic.WebAppInfo) EnvEntry(org.apache.openejb.jee.EnvEntry) JarEntry(java.util.jar.JarEntry) Map(java.util.Map) TreeMap(java.util.TreeMap) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) File(java.io.File) JarFile(java.util.jar.JarFile)

Aggregations

ContainerBase (org.apache.catalina.core.ContainerBase)4 IOException (java.io.IOException)2 Map (java.util.Map)2 Container (org.apache.catalina.Container)2 LifecycleException (org.apache.catalina.LifecycleException)2 StandardHost (org.apache.catalina.core.StandardHost)2 File (java.io.File)1 Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 TreeMap (java.util.TreeMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 JarEntry (java.util.jar.JarEntry)1 JarFile (java.util.jar.JarFile)1 InstanceNotFoundException (javax.management.InstanceNotFoundException)1 MBeanException (javax.management.MBeanException)1 NameNotFoundException (javax.naming.NameNotFoundException)1 NamingException (javax.naming.NamingException)1 ServletException (javax.servlet.ServletException)1 UnavailableException (javax.servlet.UnavailableException)1