📄 managerservlet.java
字号:
if (debug >= 1)
log("stop: Stopping web application at '" + path + "'");
if ((path == null) || (!path.startsWith("/") && path.equals(""))) {
writer.println(sm.getString("managerServlet.invalidPath",
RequestUtil.filter(path)));
return;
}
String displayPath = path;
if( path.equals("/") )
path = "";
try {
Context context = (Context) host.findChild(path);
if (context == null) {
writer.println(sm.getString("managerServlet.noContext",
RequestUtil.filter(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;
}
((Lifecycle) context).stop();
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",
RequestUtil.filter(path)));
return;
}
String displayPath = path;
if( path.equals("/") )
path = "";
try {
// Validate the Context of the specified application
Context context = (Context) host.findChild(path);
if (context == null) {
writer.println(sm.getString("managerServlet.noContext",
RequestUtil.filter(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);
}
}
if (!isDeployed(path)) {
writer.println(sm.getString("managerServlet.notDeployed",
RequestUtil.filter(displayPath)));
return;
}
if (!isServiced(path)) {
addServiced(path);
try {
// Try to stop the context first to be nicer
((Lifecycle) context).stop();
} catch (Throwable t) {
// Ignore
}
try {
File war = new File(getAppBase(), getDocBase(path) + ".war");
File dir = new File(getAppBase(), getDocBase(path));
File xml = new File(configBase, getConfigFile(path) + ".xml");
if (war.exists()) {
war.delete();
} else if (dir.exists()) {
undeployDir(dir);
} else {
xml.delete();
}
// Perform new deployment
check(path);
} finally {
removeServiced(path);
}
}
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);
}
/**
* Given a context path, get the config file name.
*/
protected String getDocBase(String path) {
String basename = null;
if (path.equals("")) {
basename = "ROOT";
} else {
basename = path.substring(1);
}
return (basename);
}
/**
* Return a File object representing the "application root" directory
* for our associated Host.
*/
protected File getAppBase() {
if (appBase != null) {
return appBase;
}
File file = new File(host.getAppBase());
if (!file.isAbsolute())
file = new File(System.getProperty("catalina.base"),
host.getAppBase());
try {
appBase = file.getCanonicalFile();
} catch (IOException e) {
appBase = file;
}
return (appBase);
}
/**
* Invoke the isDeployed method on the deployer.
*/
protected boolean isDeployed(String name)
throws Exception {
String[] params = { name };
String[] signature = { "java.lang.String" };
Boolean result =
(Boolean) mBeanServer.invoke(oname, "isDeployed", params, signature);
return result.booleanValue();
}
/**
* Invoke the check method on the deployer.
*/
protected void check(String name)
throws Exception {
String[] params = { name };
String[] signature = { "java.lang.String" };
mBeanServer.invoke(oname, "check", params, signature);
}
/**
* Invoke the isServiced method on the deployer.
*/
protected boolean isServiced(String name)
throws Exception {
String[] params = { name };
String[] signature = { "java.lang.String" };
Boolean result =
(Boolean) mBeanServer.invoke(oname, "isServiced", params, signature);
return result.booleanValue();
}
/**
* Invoke the addServiced method on the deployer.
*/
protected void addServiced(String name)
throws Exception {
String[] params = { name };
String[] signature = { "java.lang.String" };
mBeanServer.invoke(oname, "addServiced", params, signature);
}
/**
* Invoke the removeServiced method on the deployer.
*/
protected void removeServiced(String name)
throws Exception {
String[] params = { name };
String[] signature = { "java.lang.String" };
mBeanServer.invoke(oname, "removeServiced", params, signature);
}
/**
* 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 the specified file or directory to the destination.
*
* @param src File object representing the source
* @param dest File object representing the destination
*/
public static boolean copy(File src, File dest) {
boolean result = false;
try {
if( src != null &&
!src.getCanonicalPath().equals(dest.getCanonicalPath()) ) {
result = copyInternal(src, dest, new byte[4096]);
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
/**
* Copy the specified file or directory to the destination.
*
* @param src File object representing the source
* @param dest File object representing the destination
*/
public static boolean copyInternal(File src, File dest, byte[] buf) {
boolean result = true;
String files[] = null;
if (src.isDirectory()) {
files = src.list();
result = dest.mkdir();
} else {
files = new String[1];
files[0] = "";
}
if (files == null) {
files = new String[0];
}
for (int i = 0; (i < files.length) && result; i++) {
File fileSrc = new File(src, files[i]);
File fileDest = new File(dest, files[i]);
if (fileSrc.isDirectory()) {
result = copyInternal(fileSrc, fileDest, buf);
} else {
FileInputStream is = null;
FileOutputStream os = null;
try {
is = new FileInputStream(fileSrc);
os = new FileOutputStream(fileDest);
int len = 0;
while (true) {
len = is.read(buf);
if (len == -1)
break;
os.write(buf, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
result = false;
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
}
}
}
}
}
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -