📄 hostmanagerservlet.java
字号:
// -------------------------------------------------------- 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 xmlNamespaceAware,
boolean xmlValidation) {
if (debug >= 1) {
log("add: Adding host '" + name + "'");
}
// Validate the requested host name
if ((name == null) || name.length() == 0) {
writer.println(sm.getString("hostManagerServlet.invalidHostName", name));
return;
}
// Check if host already exists
if (engine.findChild(name) != null) {
writer.println
(sm.getString("hostManagerServlet.alreadyHost", name));
return;
}
// Validate and create appBase
File appBaseFile = null;
if (appBase == null || appBase.length() == 0) {
appBase = name;
}
File file = new File(appBase);
if (!file.isAbsolute())
file = new File(System.getProperty("catalina.base"), appBase);
try {
appBaseFile = file.getCanonicalFile();
} catch (IOException e) {
appBaseFile = file;
}
if (!appBaseFile.exists()) {
appBaseFile.mkdirs();
}
// Create base for config files
File configBaseFile = getConfigBase(name);
// Copy manager.xml if requested
if (manager) {
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
(sm.getString("hostManagerServlet.managerXml"));
return;
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
}
}
}
}
StandardHost host = new StandardHost();
host.setAppBase(appBase);
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.setXmlNamespaceAware(xmlNamespaceAware);
host.setXmlValidation(xmlValidation);
// Add new host
try {
engine.addChild(host);
} catch (Exception e) {
writer.println(sm.getString("hostManagerServlet.exception",
e.toString()));
return;
}
host = (StandardHost) engine.findChild(name);
if (host != null) {
writer.println(sm.getString("hostManagerServlet.add", name));
} else {
// Something failed
writer.println(sm.getString("hostManagerServlet.addFailed", name));
}
}
/**
* Remove the specified host.
*
* @param writer Writer to render results to
* @param name host name
*/
protected synchronized void remove(PrintWriter writer, String name) {
if (debug >= 1) {
log("remove: Removing host '" + name + "'");
}
// Validate the requested host name
if ((name == null) || name.length() == 0) {
writer.println(sm.getString("hostManagerServlet.invalidHostName", name));
return;
}
// Check if host exists
if (engine.findChild(name) == null) {
writer.println
(sm.getString("hostManagerServlet.noHost", name));
return;
}
// Prevent removing our own host
if (engine.findChild(name) == host) {
writer.println
(sm.getString("hostManagerServlet.cannotRemoveOwnHost", name));
return;
}
// Remove host
// 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(sm.getString("hostManagerServlet.exception",
e.toString()));
return;
}
Host host = (StandardHost) engine.findChild(name);
if (host == null) {
writer.println(sm.getString("hostManagerServlet.remove", name));
} else {
// Something failed
writer.println(sm.getString("hostManagerServlet.removeFailed", name));
}
}
/**
* Render a list of the currently active Contexts in our virtual host.
*
* @param writer Writer to render to
*/
protected void list(PrintWriter writer) {
if (debug >= 1)
log("list: Listing hosts for engine '"
+ engine.getName() + "'");
writer.println(sm.getString("hostManagerServlet.listed",
engine.getName()));
Container[] hosts = engine.findChildren();
for (int i = 0; i < hosts.length; i++) {
Host host = (Host) hosts[i];
String name = host.getName();
String[] aliases = host.findAliases();
StringBuffer buf = new StringBuffer();
if (aliases.length > 0) {
buf.append(aliases[0]);
for (int j = 1; j < aliases.length; j++) {
buf.append(',').append(aliases[j]);
}
}
writer.println(sm.getString("hostManagerServlet.listitem",
name, buf.toString()));
}
}
/**
* Start the host with the specified name.
*
* @param writer Writer to render to
* @param name Host name
*/
protected void start(PrintWriter writer, String name) {
if (debug >= 1)
log("start: Starting host with name '" + name + "'");
// Validate the requested host name
if ((name == null) || name.length() == 0) {
writer.println(sm.getString("hostManagerServlet.invalidHostName", name));
return;
}
// Check if host exists
if (engine.findChild(name) == null) {
writer.println
(sm.getString("hostManagerServlet.noHost", name));
return;
}
// Prevent starting our own host
if (engine.findChild(name) == host) {
writer.println
(sm.getString("hostManagerServlet.cannotStartOwnHost", name));
return;
}
// Start host
try {
((Lifecycle) engine.findChild(name)).start();
writer.println
(sm.getString("hostManagerServlet.started", name));
} catch (Throwable t) {
getServletContext().log
(sm.getString("hostManagerServlet.startFailed", name), t);
writer.println
(sm.getString("hostManagerServlet.startFailed", name));
writer.println(sm.getString("hostManagerServlet.exception",
t.toString()));
return;
}
}
/**
* Start the host with the specified name.
*
* @param writer Writer to render to
* @param name Host name
*/
protected void stop(PrintWriter writer, String name) {
if (debug >= 1)
log("stop: Stopping host with name '" + name + "'");
// Validate the requested host name
if ((name == null) || name.length() == 0) {
writer.println(sm.getString("hostManagerServlet.invalidHostName", name));
return;
}
// Check if host exists
if (engine.findChild(name) == null) {
writer.println
(sm.getString("hostManagerServlet.noHost", name));
return;
}
// Prevent starting our own host
if (engine.findChild(name) == host) {
writer.println
(sm.getString("hostManagerServlet.cannotStopOwnHost", name));
return;
}
// Start host
try {
((Lifecycle) engine.findChild(name)).stop();
writer.println
(sm.getString("hostManagerServlet.stopped", name));
} catch (Throwable t) {
getServletContext().log
(sm.getString("hostManagerServlet.stopFailed", name), t);
writer.println
(sm.getString("hostManagerServlet.stopFailed", name));
writer.println(sm.getString("hostManagerServlet.exception",
t.toString()));
return;
}
}
// -------------------------------------------------------- Support Methods
/**
* Get config base.
*/
protected File getConfigBase(String hostName) {
File configBase =
new File(System.getProperty("catalina.base"), "conf");
if (!configBase.exists()) {
return null;
}
if (engine != null) {
configBase = new File(configBase, engine.getName());
}
if (host != null) {
configBase = new File(configBase, hostName);
}
configBase.mkdirs();
return configBase;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -