use of org.apache.catalina.core.StandardHost 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));
}
}
use of org.apache.catalina.core.StandardHost in project tomcat by apache.
the class HostConfig method deployWAR.
/**
* Deploy packed WAR.
* <p>
* Note: It is expected that the caller has successfully added the app
* to servicedSet before calling this method.
*
* @param cn The context name
* @param war The WAR file
*/
protected void deployWAR(ContextName cn, File war) {
File xml = new File(host.getAppBaseFile(), cn.getBaseName() + "/" + Constants.ApplicationContextXml);
File warTracker = new File(host.getAppBaseFile(), cn.getBaseName() + Constants.WarTracker);
boolean xmlInWar = false;
try (JarFile jar = new JarFile(war)) {
JarEntry entry = jar.getJarEntry(Constants.ApplicationContextXml);
if (entry != null) {
xmlInWar = true;
}
} catch (IOException e) {
/* Ignore */
}
// If there is an expanded directory then any xml in that directory
// should only be used if the directory is not out of date and
// unpackWARs is true. Note the code below may apply further limits
boolean useXml = false;
// test that here
if (xml.exists() && unpackWARs && (!warTracker.exists() || warTracker.lastModified() == war.lastModified())) {
useXml = true;
}
Context context = null;
boolean deployThisXML = isDeployThisXML(war, cn);
try {
if (deployThisXML && useXml && !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 (JarFile jar = new JarFile(war)) {
JarEntry entry = jar.getJarEntry(Constants.ApplicationContextXml);
try (InputStream 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 (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(host.getConfigBaseFile(), cn.getBaseName() + ".xml")));
} else {
context = (Context) Class.forName(contextClass).getConstructor().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(host.getConfigBaseFile(), cn.getBaseName() + ".xml");
try (JarFile jar = new JarFile(war)) {
JarEntry entry = jar.getJarEntry(Constants.ApplicationContextXml);
try (InputStream istream = jar.getInputStream(entry);
OutputStream ostream = new FileOutputStream(xml)) {
IOTools.flow(istream, ostream);
}
} catch (IOException e) {
/* Ignore */
}
}
}
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(host.getConfigBaseFile(), cn.getBaseName() + ".xml")).getAbsolutePath(), Long.valueOf(0));
}
Class<?> clazz = Class.forName(host.getConfigClass());
LifecycleListener listener = (LifecycleListener) clazz.getConstructor().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(host.getAppBaseFile(), 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)));
}
}
use of org.apache.catalina.core.StandardHost in project tomcat by apache.
the class Tomcat method getHost.
public Host getHost() {
Engine engine = getEngine();
if (engine.findChildren().length > 0) {
return (Host) engine.findChildren()[0];
}
Host host = new StandardHost();
host.setName(hostname);
getEngine().addChild(host);
return host;
}
use of org.apache.catalina.core.StandardHost in project tomcat by apache.
the class StoreContextAppender method isPrintValue.
/**
* Print Context Values. <ul><li> Special handling to default workDir.
* </li><li> Don't save path at external context.xml </li><li> Don't
* generate docBase for host.appBase webapps <LI></ul>
*
* @see org.apache.catalina.storeconfig.StoreAppender#isPrintValue(java.lang.Object,
* java.lang.Object, java.lang.String,
* org.apache.catalina.storeconfig.StoreDescription)
*/
@Override
public boolean isPrintValue(Object bean, Object bean2, String attrName, StoreDescription desc) {
boolean isPrint = super.isPrintValue(bean, bean2, attrName, desc);
if (isPrint) {
StandardContext context = ((StandardContext) bean);
if ("workDir".equals(attrName)) {
String defaultWorkDir = getDefaultWorkDir(context);
isPrint = !defaultWorkDir.equals(context.getWorkDir());
} else if ("path".equals(attrName)) {
isPrint = desc.isStoreSeparate() && desc.isExternalAllowed() && context.getConfigFile() == null;
} else if ("docBase".equals(attrName)) {
Container host = context.getParent();
if (host instanceof StandardHost) {
File appBase = getAppBase(((StandardHost) host));
File docBase = getDocBase(context, appBase);
isPrint = !appBase.equals(docBase.getParentFile());
}
}
}
return isPrint;
}
use of org.apache.catalina.core.StandardHost in project tomcat by apache.
the class StoreContextAppender method getDefaultWorkDir.
/**
* Make default Work Dir.
*
* @param context The context
* @return The default working directory for the context.
*/
protected String getDefaultWorkDir(StandardContext context) {
String defaultWorkDir = null;
String contextWorkDir = context.getName();
if (contextWorkDir.length() == 0) {
contextWorkDir = "_";
}
if (contextWorkDir.startsWith("/")) {
contextWorkDir = contextWorkDir.substring(1);
}
Container host = context.getParent();
if (host instanceof StandardHost) {
String hostWorkDir = ((StandardHost) host).getWorkDir();
if (hostWorkDir != null) {
defaultWorkDir = hostWorkDir + File.separator + contextWorkDir;
} else {
String engineName = context.getParent().getParent().getName();
String hostName = context.getParent().getName();
defaultWorkDir = "work" + File.separator + engineName + File.separator + hostName + File.separator + contextWorkDir;
}
}
return defaultWorkDir;
}
Aggregations