zhourui 5 years ago
parent
commit
c28967cf84

+ 23 - 53
o2server/x_console/src/main/java/com/x/server/console/InstrumentationAgent.java

@@ -2,9 +2,7 @@ package com.x.server.console;
 
 import java.io.IOException;
 import java.lang.instrument.Instrumentation;
-import java.lang.reflect.Method;
-import java.net.URL;
-import java.net.URLClassLoader;
+import java.net.URISyntaxException;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
@@ -14,65 +12,54 @@ import java.util.stream.Stream;
 
 public class InstrumentationAgent {
 
+	private InstrumentationAgent() {
+		// nothing
+	}
+
 	private static Instrumentation INST;
 
 	private static final String CFG = "manifest.cfg";
 	private static final String GIT = ".gitignore";
-
-	public static void agentmain(String args, Instrumentation inst) {
-		INST = inst;
-		try {
-			Path base = getBasePath();
-			if (Files.exists(base.resolve("custom/jars"))) {
-				load(base, "custom/jars");
-			}
-			if (Files.exists(base.resolve("dynamic/jars"))) {
-				load(base, "dynamic/jars");
-			}
-			load(base, "store/jars");
-			load(base, "commons/ext");
-		} catch (Exception e) {
-			e.printStackTrace();
-		}
-	}
+	private static final String CUSTOM_JARS = "custom/jars";
+	private static final String DYNAMIC_JARS = "dynamic/jars";
+	private static final String STORE_JARS = "store/jars";
+	private static final String COMMONS_EXT = "commons/ext";
 
 	public static void premain(String args, Instrumentation inst) {
 		INST = inst;
 		try {
 			Path base = getBasePath();
-			if (Files.exists(base.resolve("custom/jars"))) {
-				load(base, "custom/jars");
+			if (Files.exists(base.resolve(CUSTOM_JARS))) {
+				load(base, CUSTOM_JARS);
 			}
-			if (Files.exists(base.resolve("dynamic/jars"))) {
-				load(base, "dynamic/jars");
+			if (Files.exists(base.resolve(DYNAMIC_JARS))) {
+				load(base, DYNAMIC_JARS);
 			}
-			load(base, "store/jars");
-			load(base, "commons/ext");
+			load(base, STORE_JARS);
+			load(base, COMMONS_EXT);
 		} catch (Exception e) {
 			e.printStackTrace();
 		}
 	}
 
-	private static void load(Path base, String sub) throws Exception {
+	private static void load(Path base, String sub) throws IOException {
 		Path dir = base.resolve(sub);
 		Path cfg = dir.resolve(CFG);
 		if (Files.exists(dir) && Files.isDirectory(dir) && Files.exists(cfg) && Files.isRegularFile(cfg)) {
 			List<String> names = Files.readAllLines(cfg);
 			if (names.isEmpty()) {
-				throw new Exception(String.format("%s manifest is empty.", sub));
+				throw new IOException(String.format("%s manifest is empty.", sub));
 			}
 			try (Stream<Path> stream = Files.list(dir)) {
 				stream.filter(o -> !(o.getFileName().toString().equalsIgnoreCase(CFG)
 						|| o.getFileName().toString().equalsIgnoreCase(GIT))).forEach(o -> {
 							try {
 								if (names.remove(o.getFileName().toString())) {
-									// addURLToClassPath(o);
-									System.out.println("load jar:" + o.toString());
 									INST.appendToSystemClassLoaderSearch(new JarFile(o.toString()));
 								} else {
 									Files.delete(o);
-									System.out.printf("delete unnecessary file from %s: %s.", sub,
-											o.getFileName().toString());
+									System.out.println(String.format("delete unnecessary file from %s: %s.", sub,
+											o.getFileName().toString()));
 								}
 							} catch (IOException e) {
 								e.printStackTrace();
@@ -80,37 +67,20 @@ public class InstrumentationAgent {
 						});
 			}
 			for (String name : names) {
-				System.out.printf("can not load jar from %s: %s", sub, name);
+				System.out.println(String.format("can not load jar from %s: %s", sub, name));
 			}
 		} else {
-			throw new Exception(String.format("invalid directory: %s", sub));
+			throw new IOException(String.format("invalid directory: %s", sub));
 		}
 	}
 
-	private static Path getBasePath() throws Exception {
+	private static Path getBasePath() throws IOException, URISyntaxException {
 		Path path = Paths.get(InstrumentationAgent.class.getProtectionDomain().getCodeSource().getLocation().toURI());
 		Path version = path.resolveSibling("version.o2");
 		if (Files.exists(version) && Files.isRegularFile(version)) {
 			return version.getParent();
 		}
-		throw new Exception("can not define o2server base directory.");
+		throw new IOException("can not define o2server base directory.");
 	}
 
-	private static void addURLToClassPath(Path jarPath) {
-		ClassLoader classLoader = ClassLoader.getSystemClassLoader();
-		try {
-			if (classLoader instanceof URLClassLoader) {
-				Method method = classLoader.getClass().getDeclaredMethod("addURL", URL.class);
-				method.setAccessible(true);
-				method.invoke(classLoader, jarPath.toUri().toURL());
-			} else {
-				Method method = classLoader.getClass().getDeclaredMethod("appendToClassPathForInstrumentation",
-						String.class);
-				method.setAccessible(true);
-				method.invoke(classLoader, jarPath.toAbsolutePath().toString());
-			}
-		} catch (Exception e) {
-			e.printStackTrace();
-		}
-	}
 }