📄 managerservlet.java
字号:
throw new UnavailableException
(sm.getString("managerServlet.cannotInvoke"));
// Identify the request parameters that we need
String command = request.getPathInfo();
if (command == null)
command = request.getServletPath();
String config = request.getParameter("config");
String path = request.getParameter("path");
String type = request.getParameter("type");
String war = request.getParameter("war");
String tag = request.getParameter("tag");
boolean update = false;
if ((request.getParameter("update") != null)
&& (request.getParameter("update").equals("true"))) {
update = true;
}
// Prepare our output writer to generate the response message
response.setContentType("text/plain; charset=" + Constants.CHARSET);
PrintWriter writer = response.getWriter();
// Process the requested command (note - "/deploy" is not listed here)
if (command == null) {
writer.println(sm.getString("managerServlet.noCommand"));
} else if (command.equals("/deploy")) {
if (war != null) {
deploy(writer, config, path, war, update);
} else {
deploy(writer, path, tag);
}
} else if (command.equals("/install")) {
// Deprecated
deploy(writer, config, path, war, false);
} else if (command.equals("/list")) {
list(writer);
} else if (command.equals("/reload")) {
reload(writer, path);
} else if (command.equals("/remove")) {
// Deprecated
remove(writer, path);
} else if (command.equals("/resources")) {
resources(writer, type);
} else if (command.equals("/roles")) {
roles(writer);
} else if (command.equals("/save")) {
save(writer, path);
} else if (command.equals("/serverinfo")) {
serverinfo(writer);
} else if (command.equals("/sessions")) {
sessions(writer, path);
} else if (command.equals("/start")) {
start(writer, path);
} else if (command.equals("/stop")) {
stop(writer, path);
} else if (command.equals("/undeploy")) {
undeploy(writer, path);
} else {
writer.println(sm.getString("managerServlet.unknownCommand",
command));
}
// Finish up the response
writer.flush();
writer.close();
}
/**
* Process a PUT request for the specified resource.
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet-specified error occurs
*/
public void doPut(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
// Verify that we were not accessed using the invoker servlet
if (request.getAttribute(Globals.INVOKED_ATTR) != null)
throw new UnavailableException
(sm.getString("managerServlet.cannotInvoke"));
// Identify the request parameters that we need
String command = request.getPathInfo();
if (command == null)
command = request.getServletPath();
String path = request.getParameter("path");
String tag = request.getParameter("tag");
boolean update = false;
if ((request.getParameter("update") != null)
&& (request.getParameter("update").equals("true"))) {
update = true;
}
// Prepare our output writer to generate the response message
response.setContentType("text/plain;charset="+Constants.CHARSET);
PrintWriter writer = response.getWriter();
// Process the requested command
if (command == null) {
writer.println(sm.getString("managerServlet.noCommand"));
} else if (command.equals("/deploy")) {
deploy(writer, path, tag, update, request);
} else {
writer.println(sm.getString("managerServlet.unknownCommand",
command));
}
// Finish up the response
writer.flush();
writer.close();
}
/**
* Initialize this servlet.
*/
public void init() throws ServletException {
// Ensure that our ContainerServlet properties have been set
if ((wrapper == null) || (context == null))
throw new UnavailableException
(sm.getString("managerServlet.noWrapper"));
// Verify that we were not accessed using the invoker servlet
String servletName = getServletConfig().getServletName();
if (servletName == null)
servletName = "";
if (servletName.startsWith("org.apache.catalina.INVOKER."))
throw new UnavailableException
(sm.getString("managerServlet.cannotInvoke"));
// Set our properties from the initialization parameters
String value = null;
try {
value = getServletConfig().getInitParameter("debug");
debug = Integer.parseInt(value);
} catch (Throwable t) {
;
}
// Acquire global JNDI resources if available
Server server = ServerFactory.getServer();
if ((server != null) && (server instanceof StandardServer)) {
global = ((StandardServer) server).getGlobalNamingContext();
}
// Calculate the directory into which we will be deploying applications
versioned = (File) getServletContext().getAttribute
("javax.servlet.context.tempdir");
// Identify the appBase of the owning Host of this Context
// (if any)
String appBase = ((Host) context.getParent()).getAppBase();
deployed = new File(appBase);
if (!deployed.isAbsolute()) {
deployed = new File(System.getProperty("catalina.base"),
appBase);
}
configBase = new File(System.getProperty("catalina.base"), "conf");
Container container = context;
Container host = null;
Container engine = null;
while (container != null) {
if (container instanceof Host)
host = container;
if (container instanceof Engine)
engine = container;
container = container.getParent();
}
if (engine != null) {
configBase = new File(configBase, engine.getName());
}
if (host != null) {
configBase = new File(configBase, host.getName());
}
// Note: The directory must exist for this to work.
// Log debugging messages as necessary
if (debug >= 1) {
log("init: Associated with Deployer '" +
deployer.getName() + "'");
if (global != null) {
log("init: Global resources are available");
}
}
}
// -------------------------------------------------------- Private Methods
/**
* Store server configuration.
*
* @param path Optional context path to save
*/
protected synchronized void save(PrintWriter writer, String path) {
Server server = ServerFactory.getServer();
if (!(server instanceof StandardServer)) {
writer.println(sm.getString("managerServlet.saveFail", server));
return;
}
if ((path == null) || path.length() == 0 || !path.startsWith("/")) {
try {
((StandardServer) server).storeConfig();
writer.println(sm.getString("managerServlet.saved"));
} catch (Exception e) {
log("managerServlet.storeConfig", e);
writer.println(sm.getString("managerServlet.exception",
e.toString()));
return;
}
} else {
String contextPath = path;
if (path.equals("/")) {
contextPath = "";
}
Context context = deployer.findDeployedApp(contextPath);
if (context == null) {
writer.println(sm.getString("managerServlet.noContext", path));
return;
}
try {
((StandardServer) server).storeContext(context);
writer.println(sm.getString("managerServlet.savedContext",
path));
} catch (Exception e) {
log("managerServlet.save[" + path + "]", e);
writer.println(sm.getString("managerServlet.exception",
e.toString()));
return;
}
}
}
/**
* Deploy a web application archive (included in the current request)
* at the specified context path.
*
* @param writer Writer to render results to
* @param path Context path of the application to be installed
* @param tag Tag to be associated with the webapp
* @param request Servlet request we are processing
*/
protected synchronized void deploy
(PrintWriter writer, String path,
String tag, boolean update, HttpServletRequest request) {
if (debug >= 1) {
log("deploy: Deploying web application at '" + path + "'");
}
// Validate the requested context path
if ((path == null) || path.length() == 0 || !path.startsWith("/")) {
writer.println(sm.getString("managerServlet.invalidPath", path));
return;
}
String displayPath = path;
if( path.equals("/") )
path = "";
String basename = getConfigFile(path);
// Check if app already exists, or undeploy it if updating
Context context = deployer.findDeployedApp(path);
if (update) {
if (context != null) {
undeploy(writer, displayPath);
}
context = deployer.findDeployedApp(path);
}
if (context != null) {
writer.println
(sm.getString("managerServlet.alreadyContext",
displayPath));
return;
}
// Calculate the base path
File deployedPath = deployed;
if (tag != null) {
deployedPath = new File(versioned, tag);
deployedPath.mkdirs();
}
// Upload the web application archive to a local WAR file
File localWar = new File(deployedPath, basename + ".war");
if (debug >= 2) {
log("Uploading WAR file to " + localWar);
}
try {
uploadWar(request, localWar);
} catch (IOException e) {
log("managerServlet.upload[" + displayPath + "]", e);
writer.println(sm.getString("managerServlet.exception",
e.toString()));
return;
}
// Copy WAR and XML to the host base
if (tag != null) {
deployedPath = deployed;
File localWarCopy = new File(deployedPath, basename + ".war");
copy(localWar, localWarCopy);
localWar = localWarCopy;
}
String war = null;
try {
URL url = localWar.toURL();
war = url.toString();
war = "jar:" + war + "!/";
} catch(MalformedURLException e) {
log("managerServlet.badUrl[" + displayPath + "]", e);
writer.println(sm.getString("managerServlet.exception",
e.toString()));
return;
}
// Extract the nested context deployment file (if any)
File localXml = new File(configBase, basename + ".xml");
if (debug >= 2) {
log("Extracting XML file to " + localXml);
}
try {
extractXml(localWar, localXml);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -