Skip to content

Commit

Permalink
Merge pull request #106 from linyimin0812/refactor/20230926_get_lib_h…
Browse files Browse the repository at this point in the history
…ome_1

refactor: use input arguments to parse lib home
  • Loading branch information
linyimin0812 committed Oct 1, 2023
2 parents 0fe6667 + dd95a39 commit 0e5c820
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ jobs:
- name: Execute spring-boot-demo.jar
run: |
dir
Start-Process java `
"-javaagent:C:\runner\spring-startup-analyzer\lib\spring-profiler-agent.jar", `
"-Dproject.name=generic-windows-x64-demo", `
Expand Down Expand Up @@ -272,7 +273,7 @@ jobs:
-Dspring-startup-analyzer.admin.http.server.port=8066 \
-jar /home/runner/spring-startup-analyzer/spring-boot-demo.jar &
sleep 600
sleep 800
file_count=$(ls -al /home/runner/spring-startup-analyzer/output/ | grep -c "alpine-linux-arm64-demo")
if [ "$file_count" -eq 2 ]; then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;

/**
* @author linyimin
**/
public class ProfilerAgentBoostrap {

private static final String BRIDGE_JAR = "spring-profiler-bridge.jar";
private static final String AGENT_JAR = "spring-profiler-agent.jar";

private static final Logger logger = Logger.getLogger(ProfilerAgentBoostrap.class.getSimpleName());
private static final String LIB_HOME = getLibHome();
Expand All @@ -29,6 +33,8 @@ public static void premain(String args, Instrumentation instrumentation) {

logger.info("command args: " + args);

System.out.println("premain LibHome: " + LIB_HOME);

// bridge.jar
File spyJarFile = new File(LIB_HOME + BRIDGE_JAR);
if (!spyJarFile.exists()) {
Expand Down Expand Up @@ -85,7 +91,7 @@ private static List<URL> getJars(String path) throws MalformedURLException {
throw new IllegalStateException(path + " is not exit.");
}

File[] files = folder.listFiles(file -> !file.getName().contains(BRIDGE_JAR) && file.isFile() && file.getName().endsWith("jar"));
File[] files = folder.listFiles(file -> file.isFile() && file.getName().endsWith("jar") && !file.getName().contains(BRIDGE_JAR) && !file.getName().contains(AGENT_JAR));

if (files == null) {
throw new IllegalStateException(path + " does not contain any jar files.");
Expand All @@ -101,18 +107,24 @@ private static List<URL> getJars(String path) throws MalformedURLException {

private static String getLibHome() {

String currentFilePath = ProfilerAgentBoostrap.class.getProtectionDomain()
.getCodeSource()
.getLocation()
.getPath();

File file = new File(currentFilePath);

if (!file.exists()) {
return System.getProperty("user.home") + File.separator + "spring-startup-analyzer" + File.separator;
RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();
List<String> jvmArgs = bean.getInputArguments();

for (String jvmArg : jvmArgs) {

int index= jvmArg.indexOf(":");
if (index + 1 >= jvmArg.length()) {
continue;
}

String value = jvmArg.substring(index + 1);

if (value.endsWith(AGENT_JAR)) {
return value.substring(0, value.lastIndexOf(File.separator) + 1);
}
}

return file.getParent() + File.separator;
return System.getProperty("user.home") + File.separator + "spring-startup-analyzer" + File.separator + "lib" + File.separator;

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,5 @@ private String buildMethodQualifier(AtEnterEvent event) {

return className + "." + event.methodName;
}

}

0 comments on commit 0e5c820

Please sign in to comment.