📄 managerservlet.java
字号:
if ((path == null) || (!path.startsWith("/") && path.equals(""))) {
writer.println(sm.getString("managerServlet.invalidPath", path));
return;
}
String displayPath = path;
if( path.equals("/") )
path = "";
try {
Context context = deployer.findDeployedApp(path);
if (context == null) {
writer.println(sm.getString("managerServlet.noContext",
displayPath));
return;
}
// It isn't possible for the manager to stop itself
if (context.getPath().equals(this.context.getPath())) {
writer.println(sm.getString("managerServlet.noSelf"));
return;
}
deployer.stop(path);
writer.println(sm.getString("managerServlet.stopped", displayPath));
} catch (Throwable t) {
log("ManagerServlet.stop[" + displayPath + "]", t);
writer.println(sm.getString("managerServlet.exception",
t.toString()));
}
}
/**
* Undeploy the web application at the specified context path.
*
* @param writer Writer to render to
* @param path Context path of the application to be removed
*/
protected void undeploy(PrintWriter writer, String path) {
if (debug >= 1)
log("undeploy: Undeploying web application at '" + path + "'");
if ((path == null) || (!path.startsWith("/") && path.equals(""))) {
writer.println(sm.getString("managerServlet.invalidPath", path));
return;
}
String displayPath = path;
if( path.equals("/") )
path = "";
try {
// Validate the Context of the specified application
Context context = deployer.findDeployedApp(path);
if (context == null) {
writer.println(sm.getString("managerServlet.noContext",
displayPath));
return;
}
// Identify the appBase of the owning Host of this Context (if any)
String appBase = null;
File appBaseDir = null;
if (context.getParent() instanceof Host) {
appBase = ((Host) context.getParent()).getAppBase();
appBaseDir = new File(appBase);
if (!appBaseDir.isAbsolute()) {
appBaseDir = new File(System.getProperty("catalina.base"),
appBase);
}
}
// Validate the docBase path of this application
String deployedPath = deployed.getCanonicalPath();
String docBase = context.getDocBase();
File docBaseDir = new File(docBase);
if (!docBaseDir.isAbsolute()) {
docBaseDir = new File(appBaseDir, docBase);
}
String docBasePath = docBaseDir.getCanonicalPath();
boolean deleteDir = true;
if (!docBasePath.startsWith(deployedPath)) {
deleteDir = false;
}
// Remove this web application and its associated docBase
if (debug >= 2) {
log("Undeploying document base " + docBasePath);
}
// It isn't possible for the manager to undeploy itself
if (context.getPath().equals(this.context.getPath())) {
writer.println(sm.getString("managerServlet.noSelf"));
return;
}
boolean dir = docBaseDir.isDirectory();
deployer.remove(path, true);
if (deleteDir) {
if (dir) {
undeployDir(docBaseDir);
// Delete the WAR file
File docBaseWar = new File(docBasePath + ".war");
docBaseWar.delete();
} else {
// Delete the WAR file
docBaseDir.delete();
}
}
File docBaseXml = new File(context.getConfigFile());
docBaseXml.delete();
writer.println(sm.getString("managerServlet.undeployed",
displayPath));
} catch (Throwable t) {
log("ManagerServlet.undeploy[" + displayPath + "]", t);
writer.println(sm.getString("managerServlet.exception",
t.toString()));
}
}
// -------------------------------------------------------- Support Methods
/**
* Given a context path, get the config file name.
*/
protected String getConfigFile(String path) {
String basename = null;
if (path.equals("")) {
basename = "ROOT";
} else {
basename = path.substring(1).replace('/', '#');
}
return (basename);
}
/**
* Extract the context configuration file from the specified WAR,
* if it is present. If it is not present, ensure that the corresponding
* file does not exist.
*
* @param war File object representing the WAR
* @param xml File object representing where to store the extracted
* context configuration file (if it exists)
*
* @exception IOException if an i/o error occurs
*/
protected void extractXml(File war, File xml) throws IOException {
xml.delete();
JarFile jar = null;
JarEntry entry = null;
InputStream istream = null;
BufferedOutputStream ostream = null;
try {
jar = new JarFile(war);
entry = jar.getJarEntry("META-INF/context.xml");
if (entry == null) {
return;
}
istream = jar.getInputStream(entry);
ostream =
new BufferedOutputStream(new FileOutputStream(xml), 1024);
byte buffer[] = new byte[1024];
while (true) {
int n = istream.read(buffer);
if (n < 0) {
break;
}
ostream.write(buffer, 0, n);
}
ostream.flush();
ostream.close();
ostream = null;
istream.close();
istream = null;
entry = null;
jar.close();
jar = null;
} catch (IOException e) {
xml.delete();
throw e;
} finally {
if (ostream != null) {
try {
ostream.close();
} catch (Throwable t) {
;
}
ostream = null;
}
if (istream != null) {
try {
istream.close();
} catch (Throwable t) {
;
}
istream = null;
}
entry = null;
if (jar != null) {
try {
jar.close();
} catch (Throwable t) {
;
}
jar = null;
}
}
}
/**
* Delete the specified directory, including all of its contents and
* subdirectories recursively.
*
* @param dir File object representing the directory to be deleted
*/
protected void undeployDir(File dir) {
String files[] = dir.list();
if (files == null) {
files = new String[0];
}
for (int i = 0; i < files.length; i++) {
File file = new File(dir, files[i]);
if (file.isDirectory()) {
undeployDir(file);
} else {
file.delete();
}
}
dir.delete();
}
/**
* Upload the WAR file included in this request, and store it at the
* specified file location.
*
* @param request The servlet request we are processing
* @param war The file into which we should store the uploaded WAR
*
* @exception IOException if an I/O error occurs during processing
*/
protected void uploadWar(HttpServletRequest request, File war)
throws IOException {
war.delete();
ServletInputStream istream = null;
BufferedOutputStream ostream = null;
try {
istream = request.getInputStream();
ostream =
new BufferedOutputStream(new FileOutputStream(war), 1024);
byte buffer[] = new byte[1024];
while (true) {
int n = istream.read(buffer);
if (n < 0) {
break;
}
ostream.write(buffer, 0, n);
}
ostream.flush();
ostream.close();
ostream = null;
istream.close();
istream = null;
} catch (IOException e) {
war.delete();
throw e;
} finally {
if (ostream != null) {
try {
ostream.close();
} catch (Throwable t) {
;
}
ostream = null;
}
if (istream != null) {
try {
istream.close();
} catch (Throwable t) {
;
}
istream = null;
}
}
}
/**
* Copy a file.
*/
private boolean copy(File src, File dest) {
FileInputStream is = null;
FileOutputStream os = null;
try {
is = new FileInputStream(src);
os = new FileOutputStream(dest);
byte[] buf = new byte[4096];
while (true) {
int len = is.read(buf);
if (len < 0)
break;
os.write(buf, 0, len);
}
is.close();
os.close();
} catch (IOException e) {
return false;
} finally {
try {
if (is != null) {
is.close();
}
} catch (Exception e) {
// Ignore
}
try {
if (os != null) {
os.close();
}
} catch (Exception e) {
// Ignore
}
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -