KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > runtime > basic > TestClassLoaderIsolation


1 /**
2  * Speedo: an implementation of JDO compliant personality on top of JORM generic
3  * I/O sub-system.
4  * Copyright (C) 2001-2004 France Telecom R&D
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  *
21  *
22  * Contact: speedo@objectweb.org
23  *
24  * Authors: S.Chassande-Barrioz.
25  *
26  */

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 JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.InputStream JavaDoc;
35 import java.net.MalformedURLException JavaDoc;
36 import java.net.URL JavaDoc;
37 import java.net.URLClassLoader JavaDoc;
38 import java.util.Properties JavaDoc;
39
40 /**
41  * @author S.Chassande-Barrioz
42  */

43 public class TestClassLoaderIsolation {
44
45     static String JavaDoc pathSeparator = System.getProperty("path.separator");
46     final static String JavaDoc SPEEDO_CONFIG_FILE = "speedo.properties";
47
48     public static void main(String JavaDoc[] 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 JavaDoc cl = TestClassLoaderIsolation.class.getClassLoader();
55         //fetch a PersistenceManagerFactory
56
InputStream JavaDoc 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 JavaDoc p = new Properties JavaDoc();
63         try {
64             p.load(is);
65         } catch (IOException JavaDoc e) {
66             System.err.println(e.getMessage());
67             System.exit(-1);
68         }
69         PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(p);
70
71         String JavaDoc className = args[0];
72         //System.out.println("Class to load: " + className);
73

74         //Build a ClassLoader
75
URL JavaDoc[] urls = new URL JavaDoc[args.length-1];
76         for(int i=1; i<args.length; i++) {
77             File JavaDoc f = new File JavaDoc(args[i]);
78             if (!f.exists()) {
79                 System.err.println("Resource '" + args[i] + "' not availlable");
80                 System.exit(-1);
81             }
82             //System.out.println("Resource found: " + args[i]);
83
try {
84                 urls[i-1] = f.toURL();
85             } catch (MalformedURLException JavaDoc e) {
86                 System.err.println(e.getMessage());
87                 System.exit(-1);
88             }
89         }
90         URLClassLoader JavaDoc ucl = new URLClassLoader JavaDoc(urls, cl);
91         Class JavaDoc userClass = null;
92         try {
93             userClass = ucl.loadClass(className);
94         } catch (ClassNotFoundException JavaDoc 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     /**
108      * Get the class path of an UTL classloader
109      */

110     public static String JavaDoc getClassPath(URLClassLoader JavaDoc cl) {
111         StringBuffer JavaDoc cp = new StringBuffer JavaDoc();
112         String JavaDoc sep = "";
113         URL JavaDoc[] 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