最近项目需要做一个可以动态添加webApp并且做个桌面程序来监控tomcat中部署的webApp的状态以及控制项目的启动与停止,在度娘上搜资料,这一篇介绍的很全面,链接:http://yunzhu.iteye.com/blog/953387,又经过一番研究之后,总结了一套简单可行的方案,核心技术文档参见apache tomcat文档(采用授权url的方式控制,类似于常见的localhost:8080网页控制):http://tomcat.apache.org/tomcat-8.0-doc/manager-howto.html#List_Currently_Deployed_Applications
步骤如下:
①:首先授权用户使获得这些权限
You can find the role names in the web.xml file of the Manager web application. The available roles are:
manager-gui — Access to the HTML interface.
manager-status — Access to the “Server Status” page only.
manager-script — Access to the tools-friendly plain text interface that is described in this document, and to the “Server Status” page.
manager-jmx — Access to JMX proxy interface and to the “Server Status” page.
具体方法:修改/conf目录下的tomcat-users.xml文件,添加如下代码:
<role rolename="manager-status"/>
<role rolename="manager"/>
<role rolename="manager-jmx"/>
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="admin"/>
<user username="admin" password="123456" roles="manager,manager-gui,admin,manager-status,manager-jmx,manager-script"/>
这里创建一个admin 密码为123456的用户
②:创建通用tomcatUtil方法:
public class TomcatHTMLUtil {
public static String message(String operateURL) {
StringBuffer dataResult = new StringBuffer();
URL url = null;
try {
url = new URL(operateURL);
URLConnection conn = (URLConnection) url.openConnection();
//这里我是把admin,123456 这个用户信息放到了一个json文件中以json形式存放,然后取出来,如果不是以这种方式存放,则可以直接设置username = admin ,password =123456
String data = FileUtil.readJson();
String username = data.substring(1,data.length()-1).split(",")[0].split(":")[1].substring
(1,data.substring(1,data.length()-1).split(",")[0].split(":")[1].length()-1);
String password =data.substring(1,data.length()-1).split(",")[1].split(":")[1].substring
(1,data.substring(1,data.length()-1).split(",")[0].split(":")[1].length());
String configuration = username+":"+password; // manager角色的用户
String encodedPassword = new BASE64Encoder().encode(configuration.getBytes());
conn.setRequestProperty("Authorization", "Basic " + encodedPassword);
// URL授权访问 -- End
InputStream is = conn.getInputStream();
BufferedReader bufreader = new BufferedReader(new InputStreamReader(is));
String line = null;
while ((line = bufreader.readLine()) != null) {
dataResult.append(line);
}
} catch (Exception e) {
e.printStackTrace();
}
return dataResult.toString();
}
}
③:
/**
* 获取tomcat正在运行的webApp的参数
* @return
*/
public ArrayList getTomcatWebAppData(){
ArrayList webAppArrayList = new ArrayList();
String data = TomcatHTMLUtil.message("http://localhost:8080/manager/text/list");
String[] oldDataStrs = data.split("/");
if(oldDataStrs[0].startsWith("OK")){
for (int i = 0; i < oldDataStrs.length; i++) {
String name = oldDataStrs[i].split(":")[0];
if(name.startsWith("legacy-proxy")){
WebApp webApp = new WebApp();
webApp.setName(name);
if(oldDataStrs[i].split(":")[1].equals("running")){
if(oldDataStrs[i].split(":")[2].equals("0")){
webApp.setStatus("运行");
}
else{
webApp.setStatus("异常");
}
}
else if(oldDataStrs[i].split(":")[1].equals("stopped")){
if(oldDataStrs[i].split(":")[2].equals("0")){
webApp.setStatus("停止");
}
else{
webApp.setStatus("异常");
}
}
else{
webApp.setStatus("异常");
}
webAppArrayList.add(webApp);
}
}
}
return webAppArrayList;
}
/**
* 重新部署一个项目
* @param webAppName
* @return
*/
public boolean reloadWebApp(String webAppName){
String data = TomcatHTMLUtil.message("http://localhost:8080/manager/text/reload?path=/"+webAppName);
if(data.startsWith("OK")){
return true;
}
else {
return false;
}
}
/**
* 停止一个项目
* @param webAppName
* @return
*/
public boolean stopWebApp(String webAppName){
String data = TomcatHTMLUtil.message("http://localhost:8080/manager/text/stop?path=/"+webAppName);
if(data.startsWith("OK")){
return true;
}
else {
return false;
}
}
/**
* 开始一个项目
* @param webAppName
* @return
*/
public boolean startWebApp(String webAppName){
String data = TomcatHTMLUtil.message("http://localhost:8080/manager/text/start?path=/"+webAppName);
if(data.startsWith("OK")){
return true;
}
else {
return false;
}
}
webApp的domain对象:
package domain;
/**
* Created by Administrator on 2015/10/20.
* webApp的相关信息
*/
public class WebApp {
/**
* 项目名称
*/
private String name;
/**
* 运行状态 "成功" or "停止"
*/
private String status;
public String getName() {
return name;
}
public String getStatus() {
return status;
}
public void setName(String name) {
this.name = name;
}
public void setStatus(String status) {
this.status = status;
}
}
基本上核心功能已经出来了,剩下的就是写桌面swing程序了,我就不贴代码了
OK,完成收工,其实是很简单的。