KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > bytecode > hook > impl > SessionsHelper


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.object.bytecode.hook.impl;
5
6 import com.tc.text.Banner;
7
8 import java.io.File JavaDoc;
9 import java.lang.reflect.Method JavaDoc;
10 import java.net.URL JavaDoc;
11 import java.net.URLClassLoader JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15
16 public class SessionsHelper {
17
18   // For dev/test use only, not intended for customer usage
19
private static final String JavaDoc TC_SESSION_CLASSPATH = "tc.session.classpath";
20
21   private SessionsHelper() {
22     //
23
}
24
25   public static void injectClasses(ClassLoader JavaDoc loader) throws Exception JavaDoc {
26     List JavaDoc classPaths = new ArrayList JavaDoc();
27
28     String JavaDoc tcSessionCP = System.getProperty(TC_SESSION_CLASSPATH);
29     if (tcSessionCP != null) {
30       tcSessionCP = tcSessionCP.replace('\\', '/'); // for windows
31

32       String JavaDoc[] paths = tcSessionCP.split(File.pathSeparator);
33       for (int i = 0; i < paths.length; i++) {
34         String JavaDoc 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 JavaDoc installRoot = ClassProcessorHelper.getTCInstallDir(false);
47       File JavaDoc sessionsLib = new File JavaDoc(installRoot, "lib");
48       File JavaDoc tcSessionLib = new File JavaDoc(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 JavaDoc m = URLClassLoader JavaDoc.class.getDeclaredMethod("addURL", new Class JavaDoc[] { URL JavaDoc.class });
59     m.setAccessible(true);
60     for (Iterator JavaDoc iter = classPaths.iterator(); iter.hasNext();) {
61       m.invoke(loader, new Object JavaDoc[] { new URL JavaDoc("file", "", (String JavaDoc) iter.next()) });
62     }
63
64   }
65
66   private static String JavaDoc appendPath(String JavaDoc value, String JavaDoc 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