Search in sources :

Example 6 with HostConfig

use of org.apache.catalina.startup.HostConfig 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 7 with HostConfig

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

the class MBeanFactory method createStandardHost.

/**
 * Create a new StandardHost.
 *
 * @param parent MBean Name of the associated parent component
 * @param name Unique name of this Host
 * @param appBase Application base directory name
 * @param autoDeploy Should we auto deploy?
 * @param deployOnStartup Deploy on server startup?
 * @param deployXML Should we deploy Context XML config files property?
 * @param unpackWARs Should we unpack WARs when auto deploying?
 * @return the object name of the created host
 *
 * @exception Exception if an MBean cannot be created or registered
 */
public String createStandardHost(String parent, String name, String appBase, boolean autoDeploy, boolean deployOnStartup, boolean deployXML, boolean unpackWARs) throws Exception {
    // Create a new StandardHost instance
    StandardHost host = new StandardHost();
    host.setName(name);
    host.setAppBase(appBase);
    host.setAutoDeploy(autoDeploy);
    host.setDeployOnStartup(deployOnStartup);
    host.setDeployXML(deployXML);
    host.setUnpackWARs(unpackWARs);
    // add HostConfig for active reloading
    HostConfig hostConfig = new HostConfig();
    host.addLifecycleListener(hostConfig);
    // Add the new instance to its parent component
    ObjectName pname = new ObjectName(parent);
    Service service = getService(pname);
    Engine engine = service.getContainer();
    engine.addChild(host);
    // Return the corresponding MBean name
    return host.getObjectName().toString();
}
Also used : StandardHost(org.apache.catalina.core.StandardHost) HostConfig(org.apache.catalina.startup.HostConfig) StandardService(org.apache.catalina.core.StandardService) Service(org.apache.catalina.Service) StandardEngine(org.apache.catalina.core.StandardEngine) Engine(org.apache.catalina.Engine) ObjectName(javax.management.ObjectName)

Example 8 with HostConfig

use of org.apache.catalina.startup.HostConfig in project uavstack by uavorg.

the class TomcatPlusIT method onDeployUAVApp.

/**
 * on Deploy UAV Application
 *
 * @param args
 */
@SuppressWarnings("unused")
public void onDeployUAVApp(Object... args) {
    if (System.getProperty("com.creditease.uav.iapp.install") != null) {
        return;
    }
    final HostConfig hc = (HostConfig) args[0];
    Host host = (Host) args[1];
    final File appBase = (File) args[2];
    final String mofRoot = (String) args[3];
    String curVersion = (String) args[4];
    String currentVersionDetailed = (String) args[5];
    String[] versions = currentVersionDetailed.split("\\.");
    int action = 0;
    if (curVersion.equals("6")) {
        /**
         * tomcat6
         */
        action = 0;
    } else if (curVersion.equals("7") && versions[1].equals("0") && (Integer.parseInt(versions[2].substring(0, 1)) < 3)) {
        /**
         * tomcat7.并且小版本0.30.0 (不包含)以下。小版本号使用substring是因为存在beta版本,取第一位即可。
         */
        action = 0;
    } else {
        action = 1;
    }
    switch(action) {
        case 0:
            File dir = new File(mofRoot + "/com.creditease.uav");
            ReflectionHelper.invoke("org.apache.catalina.startup.HostConfig", hc, "deployDirectory", new Class<?>[] { String.class, File.class, String.class }, new Object[] { "/com.creditease.uav", dir, mofRoot + "/com.creditease.uav" }, hc.getClass().getClassLoader());
            break;
        case 1:
            ExecutorService es = host.getStartStopExecutor();
            Future<?> f = es.submit(new Runnable() {

                @Override
                public void run() {
                    ContextName cn = new ContextName("com.creditease.uav", "");
                    ReflectionHelper.setField(ContextName.class, cn, "baseName", mofRoot + "/com.creditease.uav");
                    File dir = new File(mofRoot + "/com.creditease.uav");
                    ReflectionHelper.invoke("org.apache.catalina.startup.HostConfig", hc, "deployDirectory", new Class<?>[] { ContextName.class, File.class }, new Object[] { cn, dir }, hc.getClass().getClassLoader());
                }
            });
            try {
                f.get();
            } catch (Exception e) {
            // ignore
            }
            break;
    }
    System.setProperty("com.creditease.uav.iapp.install", "true");
}
Also used : Host(org.apache.catalina.Host) HostConfig(org.apache.catalina.startup.HostConfig) ExecutorService(java.util.concurrent.ExecutorService) File(java.io.File) ContextName(org.apache.catalina.util.ContextName)

Example 9 with HostConfig

use of org.apache.catalina.startup.HostConfig 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 10 with HostConfig

use of org.apache.catalina.startup.HostConfig in project tomcat 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 ?
 * @param autoDeploy Flag value
 * @param deployOnStartup Flag value
 * @param deployXML Flag value
 * @param unpackWARs Flag value
 * @param copyXML Flag value
 * @param smClient StringManager for the client's locale
 */
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(engine.getCatalinaBase(), 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;
        }
        try (InputStream is = getServletContext().getResourceAsStream("/WEB-INF/manager.xml")) {
            Path dest = (new File(configBaseFile, "manager.xml")).toPath();
            Files.copy(is, dest);
        } catch (IOException e) {
            writer.println(smClient.getString("hostManagerServlet.managerXml"));
            return;
        }
    }
    StandardHost host = new StandardHost();
    host.setAppBase(applicationBase);
    host.setName(name);
    host.addLifecycleListener(new HostConfig());
    // Add host aliases
    if ((aliases != null) && !aliases.isEmpty()) {
        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.addSuccess", name));
    } else {
        // Something failed
        writer.println(smClient.getString("hostManagerServlet.addFailed", name));
    }
}
Also used : Path(java.nio.file.Path) StringTokenizer(java.util.StringTokenizer) InputStream(java.io.InputStream) StandardHost(org.apache.catalina.core.StandardHost) HostConfig(org.apache.catalina.startup.HostConfig) IOException(java.io.IOException) File(java.io.File) UnavailableException(jakarta.servlet.UnavailableException) ServletException(jakarta.servlet.ServletException) InstanceNotFoundException(javax.management.InstanceNotFoundException) IOException(java.io.IOException)

Aggregations

HostConfig (org.apache.catalina.startup.HostConfig)11 StandardHost (org.apache.catalina.core.StandardHost)9 File (java.io.File)6 StandardContext (org.apache.catalina.core.StandardContext)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 StringTokenizer (java.util.StringTokenizer)2 InstanceNotFoundException (javax.management.InstanceNotFoundException)2 MBeanException (javax.management.MBeanException)2 ObjectName (javax.management.ObjectName)2 Container (org.apache.catalina.Container)2 Engine (org.apache.catalina.Engine)2 Host (org.apache.catalina.Host)2 LifecycleEvent (org.apache.catalina.LifecycleEvent)2 LifecycleException (org.apache.catalina.LifecycleException)2 Service (org.apache.catalina.Service)2 ContainerBase (org.apache.catalina.core.ContainerBase)2 StandardEngine (org.apache.catalina.core.StandardEngine)2 StandardService (org.apache.catalina.core.StandardService)2 ContextConfig (org.apache.catalina.startup.ContextConfig)2