KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > Run > SOFACompClassLoader


1 /* $Id: SOFACompClassLoader.java,v 1.3 2004/06/11 09:33:59 bures Exp $ */
2 package SOFA.SOFAnode.Run;
3
4 import java.io.ByteArrayOutputStream JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.io.InputStream JavaDoc;
7 import java.net.MalformedURLException JavaDoc;
8 import java.net.URL JavaDoc;
9 import java.net.URLConnection JavaDoc;
10
11 /** SOFA component classloader. It loads classes of components.
12   *
13   * @author Petr Hnetynka
14   */

15 public class SOFACompClassLoader extends ClassLoader JavaDoc {
16   
17   /** URL with classes */
18   URL JavaDoc url;
19
20   /** Constructor.
21     * @param _url the URL from which to load classes
22     */

23   public SOFACompClassLoader(URL JavaDoc url) {
24     super();
25     this.url = url;
26   }
27   
28   public Class JavaDoc findClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
29     byte[] b;
30     b = loadClassData(name);
31     Class JavaDoc ret = defineClass(name, b, 0, b.length);
32     return ret;
33   }
34
35   private byte[] loadClassData(String JavaDoc name) throws ClassNotFoundException JavaDoc {
36     java.util.StringTokenizer JavaDoc tok = new java.util.StringTokenizer JavaDoc(name, ".");
37     StringBuffer JavaDoc path = null;
38     if (name.startsWith("SOFA.GeneratedConnectors") || name.startsWith("org.objectweb.dsrg.deployment.connector.generated")) {
39       path = new StringBuffer JavaDoc(url.toString()+"/conn/");
40     } else {
41       path = new StringBuffer JavaDoc(url.toString()+"/classes/");
42     }
43     String JavaDoc nm = "";
44     while (tok.hasMoreTokens()) {
45       nm = tok.nextToken();
46       path.append("/");
47       path.append(nm);
48     }
49     path.append(".class");
50     
51     URL JavaDoc defURL;
52     try {
53       defURL = new URL JavaDoc(path.toString());
54     } catch (MalformedURLException JavaDoc e) {
55       throw new ClassNotFoundException JavaDoc("MalformedURLException: "+e.getMessage());
56     }
57     try {
58       URLConnection JavaDoc con = defURL.openConnection();
59       InputStream JavaDoc is = con.getInputStream();
60       ByteArrayOutputStream JavaDoc bo = new ByteArrayOutputStream JavaDoc();
61       int a = is.read();
62       while (a != -1) {
63         bo.write(a);
64         a = is.read();
65       }
66       is.close();
67       byte[] ar = bo.toByteArray();
68       bo.close();
69       return ar;
70     } catch (IOException JavaDoc e) {
71       throw new ClassNotFoundException JavaDoc("IOException: "+e.getMessage());
72     }
73   }
74
75
76
77   private static SOFACompClassLoader classLoader;
78
79   static {
80     try {
81       String JavaDoc baseURL = System.getProperty("sofa.tr.url","file:/sofa/tr/");
82       classLoader = new SOFACompClassLoader(new URL JavaDoc(baseURL));
83     } catch (java.net.MalformedURLException JavaDoc e) {
84       throw new SOFARuntimeException(e.getMessage(), e);
85     }
86   }
87
88   /** Returns sofa component class loader. There is one loader for all components in the dock.
89     * @return class loader
90     */

91   public static SOFACompClassLoader getCompClassLoader() {
92     return classLoader;
93   }
94
95   /** Loades implementation class for component with given implementation version.
96     * It is not for direct usage, it is used from component manager.
97     *
98     * @see SOFA.Component.ComponentManagerImpl#loadComponentClass(java.lang.String) ComponentManagerImpl.loadComponentClass(String)
99     * @param className class name
100     * @param compImplVersion implementation version
101     * @return loaded class
102     * @exception ClassNotFoundException class not found
103     */

104   public static Class JavaDoc loadComponentClass(String JavaDoc className, String JavaDoc compImplVersion) throws ClassNotFoundException JavaDoc {
105     StringBuffer JavaDoc ret = new StringBuffer JavaDoc("_V_");
106     for (int i=0; i<compImplVersion.length(); i++) {
107       switch (compImplVersion.charAt(i)) {
108       case '.':
109         ret.append('_');
110         break;
111       case '!':
112         ret.append("_E_");
113         break;
114       default:
115         ret.append(compImplVersion.charAt(i));
116       }
117     }
118     return classLoader.loadClass(className+ret.toString());
119   }
120 }
121
Popular Tags