
org.sapia.clazzy.CompositeClassLoaderBuilder Maven / Gradle / Ivy
Show all versions of sapia_clazzy Show documentation
package org.sapia.clazzy;
import java.io.File;
import java.util.StringTokenizer;
import org.sapia.clazzy.loader.FileSystemLoader;
import org.sapia.clazzy.loader.JarLoader;
/**
* This class is a utility that parses a classpath and returns the corresponding classloader.
*
* @author Yanick Duchesne
*
*
* - Copyright:
- Copyright © 2002-2004 Sapia Open Source Software. All Rights Reserved.
* - License:
- Read the license.txt file of the jar or visit the
* license page at the Sapia OSS web site
*
*/
public class CompositeClassLoaderBuilder {
/**
* Builds a composite classloader, given a classpath (passed in as a string).
* The classpath format is as follows:
*
* some.jar:path/to/some/directory
*
* or as follows:
*
* some.jar;path/to/some/directory
*
* Both ';' and ':' are properly understood as path delimiters.
*
* In addition, any backslashes ('\') in file paths are replaced by the
* system file separator (System.getProperty("file.separator")
).
*
* @param parent a parent ClassLoader
.
* @param selector a LoaderSelector
.
* @param classpath a classpath
* @return a CompositeClassLoader
.
*/
public static CompositeClassLoader parseClassPath(ClassLoader parent, LoaderSelector selector, String classpath){
StringTokenizer tokenizer = new StringTokenizer(classpath, ":;");
String path;
File f;
CompositeClassLoader loader;
if(parent != null) loader = new CompositeClassLoader(parent, selector);
else loader = new CompositeClassLoader(Thread.currentThread().getContextClassLoader(), selector);
while(tokenizer.hasMoreTokens()){
path = tokenizer.nextToken();
path = path.replace('\\', File.separatorChar);
f = new File(path);
if(f.isDirectory()){
loader.addLoader(new FileSystemLoader(f));
}
else{
loader.addLoader(new JarLoader(f));
}
}
return loader;
}
}