Search in sources :

Example 56 with StandardHost

use of org.apache.catalina.core.StandardHost in project tomcat70 by apache.

the class HostManagerServlet method add.

// -------------------------------------------------------- Private Methods
/**
 * Add a host using the specified parameters.
 *
 * @param writer Writer to render results to
 * @param name host name
 * @param aliases comma separated alias list
 * @param appBase application base for the host
 * @param manager should the manager webapp be deployed to the new host ?
 */
protected synchronized void add(PrintWriter writer, String name, String aliases, String appBase, boolean manager, boolean autoDeploy, boolean deployOnStartup, boolean deployXML, boolean unpackWARs, boolean copyXML, StringManager smClient) {
    if (debug >= 1) {
        log(sm.getString("hostManagerServlet.add", name));
    }
    // Validate the requested host name
    if ((name == null) || name.length() == 0) {
        writer.println(smClient.getString("hostManagerServlet.invalidHostName", name));
        return;
    }
    // Check if host already exists
    if (engine.findChild(name) != null) {
        writer.println(smClient.getString("hostManagerServlet.alreadyHost", name));
        return;
    }
    // Validate and create appBase
    File appBaseFile = null;
    File file = null;
    String applicationBase = appBase;
    if (applicationBase == null || applicationBase.length() == 0) {
        applicationBase = name;
    }
    file = new File(applicationBase);
    if (!file.isAbsolute())
        file = new File(System.getProperty(Globals.CATALINA_BASE_PROP), file.getPath());
    try {
        appBaseFile = file.getCanonicalFile();
    } catch (IOException e) {
        appBaseFile = file;
    }
    if (!appBaseFile.mkdirs() && !appBaseFile.isDirectory()) {
        writer.println(smClient.getString("hostManagerServlet.appBaseCreateFail", appBaseFile.toString(), name));
        return;
    }
    // Create base for config files
    File configBaseFile = getConfigBase(name);
    // Copy manager.xml if requested
    if (manager) {
        if (configBaseFile == null) {
            writer.println(smClient.getString("hostManagerServlet.configBaseCreateFail", name));
            return;
        }
        InputStream is = null;
        OutputStream os = null;
        try {
            is = getServletContext().getResourceAsStream("/manager.xml");
            os = new FileOutputStream(new File(configBaseFile, "manager.xml"));
            byte[] buffer = new byte[512];
            int len = buffer.length;
            while (true) {
                len = is.read(buffer);
                if (len == -1)
                    break;
                os.write(buffer, 0, len);
            }
        } catch (IOException e) {
            writer.println(smClient.getString("hostManagerServlet.managerXml"));
            return;
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                // Ignore
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                // Ignore
                }
            }
        }
    }
    StandardHost host = new StandardHost();
    host.setAppBase(applicationBase);
    host.setName(name);
    host.addLifecycleListener(new HostConfig());
    // Add host aliases
    if ((aliases != null) && !("".equals(aliases))) {
        StringTokenizer tok = new StringTokenizer(aliases, ", ");
        while (tok.hasMoreTokens()) {
            host.addAlias(tok.nextToken());
        }
    }
    host.setAutoDeploy(autoDeploy);
    host.setDeployOnStartup(deployOnStartup);
    host.setDeployXML(deployXML);
    host.setUnpackWARs(unpackWARs);
    host.setCopyXML(copyXML);
    // Add new host
    try {
        engine.addChild(host);
    } catch (Exception e) {
        writer.println(smClient.getString("hostManagerServlet.exception", e.toString()));
        return;
    }
    host = (StandardHost) engine.findChild(name);
    if (host != null) {
        writer.println(smClient.getString("hostManagerServlet.add", name));
    } else {
        // Something failed
        writer.println(smClient.getString("hostManagerServlet.addFailed", name));
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) InputStream(java.io.InputStream) StandardHost(org.apache.catalina.core.StandardHost) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) HostConfig(org.apache.catalina.startup.HostConfig) IOException(java.io.IOException) File(java.io.File) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) UnavailableException(javax.servlet.UnavailableException)

Example 57 with StandardHost

use of org.apache.catalina.core.StandardHost in project tomcat70 by apache.

the class HostManagerServlet method remove.

/**
 * Remove the specified host.
 *
 * @param writer Writer to render results to
 * @param name host name
 */
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) IOException(java.io.IOException) UnavailableException(javax.servlet.UnavailableException)

Example 58 with StandardHost

use of org.apache.catalina.core.StandardHost in project tomcat70 by apache.

the class Embedded method createHost.

/**
 * Create, configure, and return a Host that will process all
 * HTTP requests received from one of the associated Connectors,
 * and directed to the specified virtual host.
 * <p>
 * After you have customized the properties, listeners, and Valves
 * for this Host, you must attach it to the corresponding Engine
 * by calling:
 * <pre>
 *   engine.addChild(host);
 * </pre>
 * which will also cause the Host to be started if the Engine has
 * already been started.  If this is the default (or only) Host you
 * will be defining, you may also tell the Engine to pass all requests
 * not assigned to another virtual host to this one:
 * <pre>
 *   engine.setDefaultHost(host.getName());
 * </pre>
 *
 * @param name Canonical name of this virtual host
 * @param appBase Absolute pathname to the application base directory
 *  for this virtual host
 *
 * @exception IllegalArgumentException if an invalid parameter
 *  is specified
 */
public Host createHost(String name, String appBase) {
    if (log.isDebugEnabled())
        log.debug("Creating host '" + name + "' with appBase '" + appBase + "'");
    StandardHost host = new StandardHost();
    host.setAppBase(appBase);
    host.setName(name);
    return (host);
}
Also used : StandardHost(org.apache.catalina.core.StandardHost)

Example 59 with StandardHost

use of org.apache.catalina.core.StandardHost in project tomcat70 by apache.

the class Tomcat method getHost.

public Host getHost() {
    if (host == null) {
        host = new StandardHost();
        host.setName(hostname);
        getEngine().addChild(host);
    }
    return host;
}
Also used : StandardHost(org.apache.catalina.core.StandardHost)

Example 60 with StandardHost

use of org.apache.catalina.core.StandardHost in project tomcat70 by apache.

the class HostConfig method deployWAR.

/**
 * @param cn
 * @param war
 */
protected void deployWAR(ContextName cn, File war) {
    // Checking for a nested /META-INF/context.xml
    JarFile jar = null;
    InputStream istream = null;
    FileOutputStream fos = null;
    BufferedOutputStream ostream = null;
    File xml = new File(appBase(), cn.getBaseName() + "/META-INF/context.xml");
    boolean xmlInWar = false;
    try {
        jar = new JarFile(war);
        JarEntry entry = jar.getJarEntry(Constants.ApplicationContextXml);
        if (entry != null) {
            xmlInWar = true;
        }
    } catch (IOException e) {
    /* Ignore */
    } finally {
        if (jar != null) {
            try {
                jar.close();
            } catch (IOException ioe) {
            // Ignore;
            }
            jar = null;
        }
    }
    Context context = null;
    boolean deployThisXML = isDeployThisXML(war, cn);
    try {
        if (deployThisXML && xml.exists() && unpackWARs && !copyXML) {
            synchronized (digesterLock) {
                try {
                    context = (Context) digester.parse(xml);
                } catch (Exception e) {
                    log.error(sm.getString("hostConfig.deployDescriptor.error", war.getAbsolutePath()), e);
                } finally {
                    digester.reset();
                    if (context == null) {
                        context = new FailedContext();
                    }
                }
            }
            context.setConfigFile(xml.toURI().toURL());
        } else if (deployThisXML && xmlInWar) {
            synchronized (digesterLock) {
                try {
                    jar = new JarFile(war);
                    JarEntry entry = jar.getJarEntry(Constants.ApplicationContextXml);
                    istream = jar.getInputStream(entry);
                    context = (Context) digester.parse(istream);
                } catch (Exception e) {
                    log.error(sm.getString("hostConfig.deployDescriptor.error", war.getAbsolutePath()), e);
                } finally {
                    digester.reset();
                    if (istream != null) {
                        try {
                            istream.close();
                        } catch (IOException e) {
                        /* Ignore */
                        }
                        istream = null;
                    }
                    if (jar != null) {
                        try {
                            jar.close();
                        } catch (IOException e) {
                        /* Ignore */
                        }
                        jar = null;
                    }
                    if (context == null) {
                        context = new FailedContext();
                    }
                    context.setConfigFile(UriUtil.buildJarUrl(war, Constants.ApplicationContextXml));
                }
            }
        } else if (!deployThisXML && xmlInWar) {
            // Block deployment as META-INF/context.xml may contain security
            // configuration necessary for a secure deployment.
            log.error(sm.getString("hostConfig.deployDescriptor.blocked", cn.getPath(), Constants.ApplicationContextXml, new File(configBase(), cn.getBaseName() + ".xml")));
        } else {
            context = (Context) Class.forName(contextClass).newInstance();
        }
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        log.error(sm.getString("hostConfig.deployWar.error", war.getAbsolutePath()), t);
    } finally {
        if (context == null) {
            context = new FailedContext();
        }
    }
    boolean copyThisXml = false;
    if (deployThisXML) {
        if (host instanceof StandardHost) {
            copyThisXml = ((StandardHost) host).isCopyXML();
        }
        // If Host is using default value Context can override it.
        if (!copyThisXml && context instanceof StandardContext) {
            copyThisXml = ((StandardContext) context).getCopyXML();
        }
        if (xmlInWar && copyThisXml) {
            // Change location of XML file to config base
            xml = new File(configBase(), cn.getBaseName() + ".xml");
            try {
                jar = new JarFile(war);
                JarEntry entry = jar.getJarEntry(Constants.ApplicationContextXml);
                istream = jar.getInputStream(entry);
                fos = new FileOutputStream(xml);
                ostream = new BufferedOutputStream(fos, 1024);
                byte[] buffer = new byte[1024];
                while (true) {
                    int n = istream.read(buffer);
                    if (n < 0) {
                        break;
                    }
                    ostream.write(buffer, 0, n);
                }
                ostream.flush();
            } catch (IOException e) {
            /* Ignore */
            } finally {
                if (ostream != null) {
                    try {
                        ostream.close();
                    } catch (IOException ioe) {
                    // Ignore
                    }
                    ostream = null;
                }
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException ioe) {
                    // Ignore
                    }
                    fos = null;
                }
                if (istream != null) {
                    try {
                        istream.close();
                    } catch (IOException ioe) {
                    // Ignore
                    }
                    istream = null;
                }
                if (jar != null) {
                    try {
                        jar.close();
                    } catch (IOException ioe) {
                    // Ignore;
                    }
                    jar = null;
                }
            }
        }
    }
    DeployedApplication deployedApp = new DeployedApplication(cn.getName(), xml.exists() && deployThisXML && copyThisXml);
    long startTime = 0;
    // Deploy the application in this WAR file
    if (log.isInfoEnabled()) {
        startTime = System.currentTimeMillis();
        log.info(sm.getString("hostConfig.deployWar", war.getAbsolutePath()));
    }
    try {
        // Populate redeploy resources with the WAR file
        deployedApp.redeployResources.put(war.getAbsolutePath(), Long.valueOf(war.lastModified()));
        if (deployThisXML && xml.exists() && copyThisXml) {
            deployedApp.redeployResources.put(xml.getAbsolutePath(), Long.valueOf(xml.lastModified()));
        } else {
            // In case an XML file is added to the config base later
            deployedApp.redeployResources.put((new File(configBase(), cn.getBaseName() + ".xml")).getAbsolutePath(), Long.valueOf(0));
        }
        Class<?> clazz = Class.forName(host.getConfigClass());
        LifecycleListener listener = (LifecycleListener) clazz.newInstance();
        context.addLifecycleListener(listener);
        context.setName(cn.getName());
        context.setPath(cn.getPath());
        context.setWebappVersion(cn.getVersion());
        context.setDocBase(cn.getBaseName() + ".war");
        host.addChild(context);
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        log.error(sm.getString("hostConfig.deployWar.error", war.getAbsolutePath()), t);
    } finally {
        // If we're unpacking WARs, the docBase will be mutated after
        // starting the context
        boolean unpackWAR = unpackWARs;
        if (unpackWAR && context instanceof StandardContext) {
            unpackWAR = ((StandardContext) context).getUnpackWAR();
        }
        if (unpackWAR && context.getDocBase() != null) {
            File docBase = new File(appBase(), cn.getBaseName());
            deployedApp.redeployResources.put(docBase.getAbsolutePath(), Long.valueOf(docBase.lastModified()));
            addWatchedResources(deployedApp, docBase.getAbsolutePath(), context);
            if (deployThisXML && !copyThisXml && (xmlInWar || xml.exists())) {
                deployedApp.redeployResources.put(xml.getAbsolutePath(), Long.valueOf(xml.lastModified()));
            }
        } else {
            // Passing null for docBase means that no resources will be
            // watched. This will be logged at debug level.
            addWatchedResources(deployedApp, null, context);
        }
        // Add the global redeploy resources (which are never deleted) at
        // the end so they don't interfere with the deletion process
        addGlobalRedeployResources(deployedApp);
    }
    deployed.put(cn.getName(), deployedApp);
    if (log.isInfoEnabled()) {
        log.info(sm.getString("hostConfig.deployWar.finished", war.getAbsolutePath(), Long.valueOf(System.currentTimeMillis() - startTime)));
    }
}
Also used : StandardContext(org.apache.catalina.core.StandardContext) Context(org.apache.catalina.Context) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) LifecycleListener(org.apache.catalina.LifecycleListener) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) StandardHost(org.apache.catalina.core.StandardHost) FileOutputStream(java.io.FileOutputStream) StandardContext(org.apache.catalina.core.StandardContext) BufferedOutputStream(java.io.BufferedOutputStream) JarFile(java.util.jar.JarFile) File(java.io.File)

Aggregations

StandardHost (org.apache.catalina.core.StandardHost)90 File (java.io.File)48 Context (org.apache.catalina.Context)38 StandardContext (org.apache.catalina.core.StandardContext)36 Host (org.apache.catalina.Host)29 Test (org.junit.Test)26 Tomcat (org.apache.catalina.startup.Tomcat)16 Container (org.apache.catalina.Container)12 StandardEngine (org.apache.catalina.core.StandardEngine)12 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)12 IOException (java.io.IOException)10 URL (java.net.URL)8 Service (org.apache.catalina.Service)8 HostConfig (org.apache.catalina.startup.HostConfig)8 InitialContext (javax.naming.InitialContext)7 Engine (org.apache.catalina.Engine)7 InputStream (java.io.InputStream)6 ObjectName (javax.management.ObjectName)6 ReplicatedContext (org.apache.catalina.ha.context.ReplicatedContext)6 ArrayList (java.util.ArrayList)5