001: /**
002: * Speedo: an implementation of JDO compliant personality on top of JORM generic
003: * I/O sub-system.
004: * Copyright (C) 2001-2004 France Telecom R&D
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: *
021: *
022: * Contact: speedo@objectweb.org
023: *
024: * Authors: S.Chassande-Barrioz.
025: *
026: */package org.objectweb.speedo.runtime.basic;
027:
028: import javax.jdo.JDOHelper;
029: import javax.jdo.PersistenceManager;
030: import javax.jdo.PersistenceManagerFactory;
031: import java.io.File;
032: import java.io.IOException;
033: import java.io.InputStream;
034: import java.net.MalformedURLException;
035: import java.net.URL;
036: import java.net.URLClassLoader;
037: import java.util.Properties;
038:
039: /**
040: * @author S.Chassande-Barrioz
041: */
042: public class TestClassLoaderIsolation {
043:
044: static String pathSeparator = System.getProperty("path.separator");
045: final static String SPEEDO_CONFIG_FILE = "speedo-jdo.properties";
046:
047: public static void main(String[] args) {
048: if (args.length < 2) {
049: System.err
050: .println("Usage: java org.objectweb.speedo.runtime.basic.TestClassLoaderIsolation <classname> [<class path element>]*");
051: System.exit(-1);
052: }
053:
054: ClassLoader cl = TestClassLoaderIsolation.class
055: .getClassLoader();
056: //fetch a PersistenceManagerFactory
057: InputStream is = cl.getResourceAsStream(SPEEDO_CONFIG_FILE);
058: if (is == null) {
059: System.err
060: .println("The '"
061: + SPEEDO_CONFIG_FILE
062: + "' properties file is not availlable in the classpath");
063: System.exit(-1);
064: }
065: Properties p = new Properties();
066: try {
067: p.load(is);
068: } catch (IOException e) {
069: System.err.println(e.getMessage());
070: System.exit(-1);
071: }
072: PersistenceManagerFactory pmf = JDOHelper
073: .getPersistenceManagerFactory(p);
074:
075: String className = args[0];
076: //System.out.println("Class to load: " + className);
077:
078: //Build a ClassLoader
079: URL[] urls = new URL[args.length - 1];
080: for (int i = 1; i < args.length; i++) {
081: File f = new File(args[i]);
082: if (!f.exists()) {
083: System.err.println("Resource '" + args[i]
084: + "' not availlable");
085: System.exit(-1);
086: }
087: //System.out.println("Resource found: " + args[i]);
088: try {
089: urls[i - 1] = f.toURL();
090: } catch (MalformedURLException e) {
091: System.err.println(e.getMessage());
092: System.exit(-1);
093: }
094: }
095: URLClassLoader ucl = new URLClassLoader(urls, cl);
096: Class userClass = null;
097: try {
098: userClass = ucl.loadClass(className);
099: } catch (ClassNotFoundException e) {
100: System.err.println("Problem to load the class '"
101: + className + "'");
102: System.err.println(e.getMessage());
103: System.exit(-1);
104: }
105: PersistenceManager pm = pmf.getPersistenceManager();
106: pm.getObjectIdClass(userClass);
107: System.out.println("Class '" + className
108: + "' loaded successfully from the classpath:\n"
109: + getClassPath(ucl));
110: pm.close();
111: }
112:
113: /**
114: * Get the class path of an UTL classloader
115: */
116: public static String getClassPath(URLClassLoader cl) {
117: StringBuffer cp = new StringBuffer();
118: String sep = "";
119: URL[] urls = cl.getURLs();
120: for (int i = 0; i < urls.length; i++) {
121: cp.append(sep);
122: cp.append(urls[i].getFile());
123: sep = pathSeparator;
124: }
125: return cp.toString();
126: }
127:
128: }
|