1 2 package SOFA.SOFAnode.Run; 3 4 import java.io.ByteArrayOutputStream ; 5 import java.io.IOException ; 6 import java.io.InputStream ; 7 import java.net.MalformedURLException ; 8 import java.net.URL ; 9 import java.net.URLConnection ; 10 11 15 public class SOFACompClassLoader extends ClassLoader { 16 17 18 URL url; 19 20 23 public SOFACompClassLoader(URL url) { 24 super(); 25 this.url = url; 26 } 27 28 public Class findClass(String name) throws ClassNotFoundException { 29 byte[] b; 30 b = loadClassData(name); 31 Class ret = defineClass(name, b, 0, b.length); 32 return ret; 33 } 34 35 private byte[] loadClassData(String name) throws ClassNotFoundException { 36 java.util.StringTokenizer tok = new java.util.StringTokenizer (name, "."); 37 StringBuffer path = null; 38 if (name.startsWith("SOFA.GeneratedConnectors") || name.startsWith("org.objectweb.dsrg.deployment.connector.generated")) { 39 path = new StringBuffer (url.toString()+"/conn/"); 40 } else { 41 path = new StringBuffer (url.toString()+"/classes/"); 42 } 43 String nm = ""; 44 while (tok.hasMoreTokens()) { 45 nm = tok.nextToken(); 46 path.append("/"); 47 path.append(nm); 48 } 49 path.append(".class"); 50 51 URL defURL; 52 try { 53 defURL = new URL (path.toString()); 54 } catch (MalformedURLException e) { 55 throw new ClassNotFoundException ("MalformedURLException: "+e.getMessage()); 56 } 57 try { 58 URLConnection con = defURL.openConnection(); 59 InputStream is = con.getInputStream(); 60 ByteArrayOutputStream bo = new ByteArrayOutputStream (); 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 e) { 71 throw new ClassNotFoundException ("IOException: "+e.getMessage()); 72 } 73 } 74 75 76 77 private static SOFACompClassLoader classLoader; 78 79 static { 80 try { 81 String baseURL = System.getProperty("sofa.tr.url","file:/sofa/tr/"); 82 classLoader = new SOFACompClassLoader(new URL (baseURL)); 83 } catch (java.net.MalformedURLException e) { 84 throw new SOFARuntimeException(e.getMessage(), e); 85 } 86 } 87 88 91 public static SOFACompClassLoader getCompClassLoader() { 92 return classLoader; 93 } 94 95 104 public static Class loadComponentClass(String className, String compImplVersion) throws ClassNotFoundException { 105 StringBuffer ret = new StringBuffer ("_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 |