diff --git a/src/main/java/com/profiler/ClassPathResolver.java b/src/main/java/com/profiler/ClassPathResolver.java index 67caeef53..8677bd80c 100644 --- a/src/main/java/com/profiler/ClassPathResolver.java +++ b/src/main/java/com/profiler/ClassPathResolver.java @@ -2,8 +2,14 @@ package com.profiler; import java.io.File; +import java.io.FileFilter; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URL; import java.util.ArrayList; +import java.util.Collections; import java.util.List; +import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -11,7 +17,10 @@ import java.util.regex.Pattern; * */ public class ClassPathResolver { -// Pattern.compile("hippo-tomcat-profiler-([0-9]+\\){2}.jar"); + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + // Pattern.compile("hippo-tomcat-profiler-([0-9]+\\){2}.jar"); private static final Pattern DEFAULT_AGENT_PATTERN = Pattern.compile("hippo-tomcat-profiler-[0-9]+\\.[0-9]+\\.[0-9]+\\.jar"); private String classPath; @@ -21,6 +30,10 @@ public class ClassPathResolver { private String agentDirPath; private Pattern agentPattern; + public ClassPathResolver() { + this.classPath = getClassPathFromSystemProperty(); + this.agentPattern = DEFAULT_AGENT_PATTERN; + } public ClassPathResolver(String classPath) { this.classPath = classPath; @@ -51,17 +64,15 @@ public class ClassPathResolver { } this.agentJarName = parseAgentJar(matcher); this.agentJarFullPath = parseAgentJarPath(classPath, agentJarName); - this.agentDirPath = parseAgentDirPath(agentJarFullPath); + this.agentDirPath = parseAgentDirPath(agentJarFullPath); return true; } - - private String parseAgentJar(Matcher matcher) { int start = matcher.start(); int end = matcher.end(); - return this.classPath.substring(start, end); + return this.classPath.substring(start, end); } @@ -72,7 +83,7 @@ public class ClassPathResolver { private String parseAgentJarPath(String classPath, String agentJar) { String[] classPathList = classPath.split(File.pathSeparator); - for(String findPath : classPathList) { + for (String findPath : classPathList) { boolean find = findPath.contains(agentJar); if (find) { return findPath; @@ -85,14 +96,49 @@ public class ClassPathResolver { return agentJarFullPath; } - public String getAgentLibPath() { + public String getAgentLibPath() { return this.agentDirPath + File.separator + "lib"; } - public List resolveLib() { + public List resolveLib() { + String agentLibPath = getAgentLibPath(); + File libDir = new File(agentLibPath); + if (!libDir.exists()) { + logger.warning(agentLibPath + " not found"); + return Collections.emptyList(); + } + if (!libDir.isDirectory()) { + logger.warning(agentLibPath + " not Directory"); + return Collections.emptyList(); + } + File[] findJarList = findjar(libDir); + List jarURLList = new ArrayList(findJarList.length); + for (File file : findJarList) { + URL url = toURI(file); + if (url != null) { + jarURLList.add(url); + } + } + return jarURLList; + } - return new ArrayList(); + private URL toURI(File file) { + URI uri = file.toURI(); + try { + return uri.toURL(); + } catch (MalformedURLException e) { + return null; + } + } + + private File[] findjar(File libDir) { + return libDir.listFiles(new FileFilter() { + @Override + public boolean accept(File pathname) { + return pathname.getName().lastIndexOf(".jar") != -1; + } + }); } public String parseAgentDirPath(String agentJarFullPath) { @@ -109,4 +155,8 @@ public class ClassPathResolver { return agentDirPath; } + public String getAgentConfigPath() { + return agentDirPath + File.separator + "hippo.config"; + } + } diff --git a/src/main/java/com/profiler/TomcatProfiler.java b/src/main/java/com/profiler/TomcatProfiler.java index 3689e254f..8ab27d068 100644 --- a/src/main/java/com/profiler/TomcatProfiler.java +++ b/src/main/java/com/profiler/TomcatProfiler.java @@ -1,9 +1,12 @@ package com.profiler; +import java.io.IOException; import java.lang.instrument.ClassFileTransformer; import java.lang.instrument.IllegalClassFormatException; import java.lang.instrument.Instrumentation; +import java.net.URL; import java.security.ProtectionDomain; +import java.util.List; import java.util.Properties; import java.util.Set; import java.util.logging.Level; @@ -38,11 +41,19 @@ public class TomcatProfiler implements ClassFileTransformer { if (agentArgs != null) { logger.info("HIPPO agentArgs:" + agentArgs); } - dumpSystemProperties(); +// dumpSystemProperties(); + + ClassPathResolver classPathResolver = new ClassPathResolver(); + boolean agentJarNotFound = classPathResolver.findAgentJar(); + if (!agentJarNotFound) { + logger.severe("hippo-tomcat-profiler-x.x.x.jar not found."); + return; + } + // 이게 로드할 lib List임. + List libUrlList = resolveLib(classPathResolver); try { - ProfilerConfig profilerConfig = new ProfilerConfig(); - profilerConfig.readConfigFile(); + ProfilerConfig profilerConfig = readConfig(classPathResolver); if (!profilerConfig.isProfileEnable()) { logger.warning("Profiler Agent not started. profile.enable=" + profilerConfig.isProfileEnable()); return; @@ -54,6 +65,35 @@ public class TomcatProfiler implements ClassFileTransformer { } } + private static ProfilerConfig readConfig(ClassPathResolver classPathResolver) throws IOException { + + ProfilerConfig profilerConfig = new ProfilerConfig(); + String hippoConfigFormSystemProperty = findHippoConfigFormSystemProeprty(); + if (hippoConfigFormSystemProperty != null) { + profilerConfig.readConfigFile(hippoConfigFormSystemProperty); + } else { + String agentConfigPath = classPathResolver.getAgentConfigPath(); + profilerConfig.readConfigFile(agentConfigPath); + } + return profilerConfig; + } + + private static List resolveLib(ClassPathResolver classPathResolver) { + String agentJarFullPath = classPathResolver.getAgentJarFullPath(); + logger.info("agentJarPath:" + agentJarFullPath); + + String agentLibPath = classPathResolver.getAgentLibPath(); + logger.info("agentLibPath:" + agentLibPath); + + List urlList = classPathResolver.resolveLib(); + logger.info("agent lib list:" + urlList); + + String agentConfigPath = classPathResolver.getAgentConfigPath(); + logger.info("agent config:" + agentConfigPath); + + return urlList; + } + private static void dumpSystemProperties() { if (logger.isLoggable(Level.FINE)) { Properties properties = System.getProperties(); @@ -78,6 +118,15 @@ public class TomcatProfiler implements ClassFileTransformer { this.modifierRepository = createModifierRegistry(); } + public static String findHippoConfigFormSystemProeprty() { + String hippoConfigFileName = System.getProperty("hippo.config"); + if (hippoConfigFileName == null) { + return null; + } + logger.info("hippo.config property found. " + hippoConfigFileName); + return hippoConfigFileName; + } + private String[] getTomcatlibPath() { String catalinaHome = System.getProperty("catalina.home"); diff --git a/src/main/java/com/profiler/config/ProfilerConfig.java b/src/main/java/com/profiler/config/ProfilerConfig.java index 43918a011..6ba23799d 100644 --- a/src/main/java/com/profiler/config/ProfilerConfig.java +++ b/src/main/java/com/profiler/config/ProfilerConfig.java @@ -45,15 +45,8 @@ public class ProfilerConfig { public ProfilerConfig() { } - public void readConfigFile() throws IOException { - String hippoConfigFileName = System.getProperty("hippo.config"); - if (hippoConfigFileName == null) { - logger.info("hippo.config property is not set. Using default value:" + this); - return; - } - + public void readConfigFile(String hippoConfigFileName) throws IOException { try { - // TODO file path를 찾는 부분을 수정해야됨 현재 제대로 안찾아짐. 설정파일이 classpath 에 걸려 있지않으므로 파일위치를 못찾음. Properties properties = PropertyUtils.readProperties(hippoConfigFileName); readPropertyValues(properties); } catch (FileNotFoundException fe) {