Tomcat 7 - 在运行时获取应用程序名称,无需通过java-agent / aspectj登录 [英] Tomcat 7 - Get the application name during runtime without login via java-agent/aspectj
问题描述
我正在尝试获取所有已部署应用程序的列表,特别是映射到tomcat root 的应用程序的名称。
我希望能够在运行时使用收集tomcat服务器上的信息的java代理来完成它。
我尝试使用此代码示例:
I'm trying to get a list of all deployed applications, and specifically the name of the application mapped to tomcat root. I want to be able to do it during runtime, using a java agent that collects information on the tomcat server. I tried using this code sample:
private Iterable<String> collectAllDeployedApps() {
try {
final Set<String> result = new HashSet<>();
final Set<ObjectName> instances = findServer()
.queryNames(new ObjectName("Tomcat:j2eeType=WebModule,*"), null);
for (ObjectName each : instances) {
result.add(substringAfterLast(each.getKeyProperty("name"), "/")); //it will be in format like //localhost/appname
}
return result;
} catch (MalformedObjectNameException e) {
//handle
}
}
取自类似问题,但自我没有登录经理应用程序,我没有正确的权限,所以我得到一个空列表。
taken from a similar question but since I'm not logged into the manager app, I don't have the right permissions, so I get an empty list.
我真正想要的是什么 - 我有一个java代理(基于aspectJ),我希望在运行时/部署时间等能够获得所有已部署应用程序的列表,而无需自己实际登录管理器。
我该怎么办?我不介意检测tomcat的部署代码(由于我已经在使用代码,因此我不需要从我这边登录),但是我不确定要使用哪种功能。
What I actually want - I have a java agent (based on aspectJ), and I'd like during runtime/deployment time etc. to be able to get the list of all deployed apps without actually logging in to the manager myself. How can I do this? I don't mind instrumenting tomcat's deployment code (which doesn't require any login from my side as I'm already instrumenting the code), but I'm not sure which function to instrument.
谢谢,
林
推荐答案
问题包括两部分:
-
获取所有部署的应用程序的列表 - 在查看Tomcat的API之后,我发现了几个可以检测的相关部署代码部分:
WarWatcher .java(允许检测更改),我们还可以看到以下应用程序 - UserConfig.java (可以在setDirectory名称上进行检测等),当然还有 HostConfig.java :
Get a list of all deployed applications - After reviewing Tomcat's API, I found several relevant deployment code parts which can be instrumented: WarWatcher.java (allows to detect changes), and we can also see the apps from - UserConfig.java which is called on startup (instrumentation can be done on setDirectory name etc.), and of course HostConfig.java that is called on stratup:
protected void org.apache.catalina.startup.HostConfig.deployWARs(java.io.File, java.lang.String[])
protected void org.apache.catalina.startup.HostConfig.deployApps()
protected void org.apache.catalina.startup.HostConfig.deployWAR(org.apache.catalina.util.ContextName, java.io.File)
此外 - 您可以检查参数:
In addition - you can check the argument for:
protected boolean org.apache.catalina.startup.HostConfig.deploymentExists(java.lang.String)
它包含war /文件夹名称(通常表示应用程序名称+ - )。
It includes the war/folder name (which usually means the application name+-).
获取根应用程序名称 - 这可以通过使用 ServletContext.getRealPath() - 它返回文件夹名称,从中战争名称可以是extr行动(并且可以在我的情况下至少作为应用名称使用)。
Get the root application name - This can be done by using ServletContext.getRealPath() - It returns the folder name, from which the war name can be extracted (and can be used, in my case at least as the app name).
这篇关于Tomcat 7 - 在运行时获取应用程序名称,无需通过java-agent / aspectj登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!