/**
* Speedo: an implementation of JDO compliant personality on top of JORM generic
* I/O sub-system.
* Copyright (C) 2001-2004 France Telecom R&D
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*
*
* Contact: speedo@objectweb.org
*
* Authors: S.Chassande-Barrioz.
*
*/
package org.objectweb.speedo.runtime.basic;
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Properties;
/**
* @author S.Chassande-Barrioz
*/
public class TestClassLoaderIsolation {
static String pathSeparator = System.getProperty("path.separator");
final static String SPEEDO_CONFIG_FILE = "speedo-jdo.properties";
public static void main(String[] args) {
if(args.length < 2) {
System.err.println("Usage: java org.objectweb.speedo.runtime.basic.TestClassLoaderIsolation <classname> [<class path element>]*");
System.exit(-1);
}
ClassLoader cl = TestClassLoaderIsolation.class.getClassLoader();
//fetch a PersistenceManagerFactory
InputStream is = cl.getResourceAsStream(SPEEDO_CONFIG_FILE);
if (is == null) {
System.err.println("The '" + SPEEDO_CONFIG_FILE
+ "' properties file is not availlable in the classpath");
System.exit(-1);
}
Properties p = new Properties();
try {
p.load(is);
} catch (IOException e) {
System.err.println(e.getMessage());
System.exit(-1);
}
PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(p);
String className = args[0];
//System.out.println("Class to load: " + className);
//Build a ClassLoader
URL[] urls = new URL[args.length-1];
for(int i=1; i<args.length; i++) {
File f = new File(args[i]);
if (!f.exists()) {
System.err.println("Resource '" + args[i] + "' not availlable");
System.exit(-1);
}
//System.out.println("Resource found: " + args[i]);
try {
urls[i-1] = f.toURL();
} catch (MalformedURLException e) {
System.err.println(e.getMessage());
System.exit(-1);
}
}
URLClassLoader ucl = new URLClassLoader(urls, cl);
Class userClass = null;
try {
userClass = ucl.loadClass(className);
} catch (ClassNotFoundException e) {
System.err.println("Problem to load the class '" + className + "'");
System.err.println(e.getMessage());
System.exit(-1);
}
PersistenceManager pm = pmf.getPersistenceManager();
pm.getObjectIdClass(userClass);
System.out.println("Class '" + className
+ "' loaded successfully from the classpath:\n"
+ getClassPath(ucl));
pm.close();
}
/**
* Get the class path of an UTL classloader
*/
public static String getClassPath(URLClassLoader cl) {
StringBuffer cp = new StringBuffer();
String sep = "";
URL[] urls = cl.getURLs();
for (int i = 0; i < urls.length; i++) {
cp.append(sep);
cp.append(urls[i].getFile());
sep = pathSeparator;
}
return cp.toString();
}
}