1 4 package com.tc.object.bytecode.hook.impl; 5 6 import com.tc.text.Banner; 7 8 import java.io.File ; 9 import java.lang.reflect.Method ; 10 import java.net.URL ; 11 import java.net.URLClassLoader ; 12 import java.util.ArrayList ; 13 import java.util.Iterator ; 14 import java.util.List ; 15 16 public class SessionsHelper { 17 18 private static final String TC_SESSION_CLASSPATH = "tc.session.classpath"; 20 21 private SessionsHelper() { 22 } 24 25 public static void injectClasses(ClassLoader loader) throws Exception { 26 List classPaths = new ArrayList (); 27 28 String tcSessionCP = System.getProperty(TC_SESSION_CLASSPATH); 29 if (tcSessionCP != null) { 30 tcSessionCP = tcSessionCP.replace('\\', '/'); 32 String [] paths = tcSessionCP.split(File.pathSeparator); 33 for (int i = 0; i < paths.length; i++) { 34 String path = paths[i]; 35 if (!path.endsWith("/")) { 36 path += "/"; 37 } 38 39 if (!path.startsWith("/")) { 40 path = "/" + path; 41 } 42 43 classPaths.add(path); 44 } 45 } else { 46 File installRoot = ClassProcessorHelper.getTCInstallDir(false); 47 File sessionsLib = new File (installRoot, "lib"); 48 File tcSessionLib = new File (sessionsLib, "session"); 49 50 if (!tcSessionLib.exists() || !tcSessionLib.isDirectory() || !tcSessionLib.canRead()) { 51 Banner.errorBanner(tcSessionLib + " does not exist, or is not an accessible directory"); 52 Util.exit(); 53 } 54 55 classPaths.add(appendPath(tcSessionLib.getAbsolutePath(), "tc-session.jar")); 56 } 57 58 Method m = URLClassLoader .class.getDeclaredMethod("addURL", new Class [] { URL .class }); 59 m.setAccessible(true); 60 for (Iterator iter = classPaths.iterator(); iter.hasNext();) { 61 m.invoke(loader, new Object [] { new URL ("file", "", (String ) iter.next()) }); 62 } 63 64 } 65 66 private static String appendPath(String value, String path) { 67 if (value == null) { return path; } 68 if (value.endsWith(File.separator)) { return value + path; } 69 return value + File.separator + path; 70 } 71 72 } 73 | Popular Tags |