Tomcat 7 - Get the application name during runtime without login via java-agent/aspectj

末鹿安然 提交于 2019-12-08 02:37:59

问题


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.

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.

Thanks, Lin


回答1:


The question consists of 2 parts:

  • 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)
    

    It includes the war/folder name (which usually means the application name+-).

  • 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).



来源:https://stackoverflow.com/questions/39488852/tomcat-7-get-the-application-name-during-runtime-without-login-via-java-agent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!