1 27 package org.objectweb.speedo.runtime.basic; 28 29 import javax.jdo.JDOHelper; 30 import javax.jdo.PersistenceManager; 31 import javax.jdo.PersistenceManagerFactory; 32 import java.io.File ; 33 import java.io.IOException ; 34 import java.io.InputStream ; 35 import java.net.MalformedURLException ; 36 import java.net.URL ; 37 import java.net.URLClassLoader ; 38 import java.util.Properties ; 39 40 43 public class TestClassLoaderIsolation { 44 45 static String pathSeparator = System.getProperty("path.separator"); 46 final static String SPEEDO_CONFIG_FILE = "speedo.properties"; 47 48 public static void main(String [] args) { 49 if(args.length < 2) { 50 System.err.println("Usage: java org.objectweb.speedo.runtime.basic.TestClassLoaderIsolation <classname> [<class path element>]*"); 51 System.exit(-1); 52 } 53 54 ClassLoader cl = TestClassLoaderIsolation.class.getClassLoader(); 55 InputStream is = cl.getResourceAsStream(SPEEDO_CONFIG_FILE); 57 if (is == null) { 58 System.err.println("The '" + SPEEDO_CONFIG_FILE 59 + "' properties file is not availlable in the classpath"); 60 System.exit(-1); 61 } 62 Properties p = new Properties (); 63 try { 64 p.load(is); 65 } catch (IOException e) { 66 System.err.println(e.getMessage()); 67 System.exit(-1); 68 } 69 PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(p); 70 71 String className = args[0]; 72 74 URL [] urls = new URL [args.length-1]; 76 for(int i=1; i<args.length; i++) { 77 File f = new File (args[i]); 78 if (!f.exists()) { 79 System.err.println("Resource '" + args[i] + "' not availlable"); 80 System.exit(-1); 81 } 82 try { 84 urls[i-1] = f.toURL(); 85 } catch (MalformedURLException e) { 86 System.err.println(e.getMessage()); 87 System.exit(-1); 88 } 89 } 90 URLClassLoader ucl = new URLClassLoader (urls, cl); 91 Class userClass = null; 92 try { 93 userClass = ucl.loadClass(className); 94 } catch (ClassNotFoundException e) { 95 System.err.println("Problem to load the class '" + className + "'"); 96 System.err.println(e.getMessage()); 97 System.exit(-1); 98 } 99 PersistenceManager pm = pmf.getPersistenceManager(); 100 pm.getObjectIdClass(userClass); 101 System.out.println("Class '" + className 102 + "' loaded successfully from the classpath:\n" 103 + getClassPath(ucl)); 104 pm.close(); 105 } 106 107 110 public static String getClassPath(URLClassLoader cl) { 111 StringBuffer cp = new StringBuffer (); 112 String sep = ""; 113 URL [] urls = cl.getURLs(); 114 for (int i = 0; i < urls.length; i++) { 115 cp.append(sep); 116 cp.append(urls[i].getFile()); 117 sep = pathSeparator; 118 } 119 return cp.toString(); 120 } 121 122 } 123 124 125 | Popular Tags |