package org.jboss.system.server;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.List;
import java.util.LinkedList;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.MalformedURLException;
import java.io.File;
public class ServerLoader
{
public static final String DEFAULT_BOOT_LIBRARY_LIST =
"log4j-boot.jar,jboss-common.jar,jboss-system.jar,namespace.jar";
public static final String DEFAULT_SERVER_TYPE = "org.jboss.system.server.ServerImpl";
protected Properties props;
protected URL libraryURL;
protected List extraClasspath = new LinkedList();
public ServerLoader(final Properties props) throws Exception
{
if (props == null)
throw new IllegalArgumentException("props is null");
this.props = props;
URL homeURL = getURL(ServerConfig.HOME_URL);
if (homeURL == null)
{
throw new Exception("Missing configuration value for: "
+ ServerConfig.HOME_URL);
}
libraryURL = getURL(ServerConfig.LIBRARY_URL);
if (libraryURL == null)
{
libraryURL = new URL(homeURL, ServerConfig.LIBRARY_URL_SUFFIX);
}
if( homeURL.getProtocol().startsWith("http") == true )
{
this.addLibrary("webdavlib.jar");
this.addLibrary("commons-httpclient.jar");
this.addLibrary("commons-logging.jar");
}
}
public void addLibrary(final String filename) throws MalformedURLException
{
if (filename == null)
throw new IllegalArgumentException("filename is null");
URL jarURL = new URL(libraryURL, filename);
extraClasspath.add(jarURL);
}
public void addLibraries(final String filenames) throws MalformedURLException
{
if (filenames == null)
throw new IllegalArgumentException("filenames is null");
StringTokenizer stok = new StringTokenizer(filenames, ",");
while (stok.hasMoreElements())
{
addLibrary(stok.nextToken().trim());
}
}
public void addURL(final URL url)
{
if (url == null)
throw new IllegalArgumentException("url is null");
extraClasspath.add(url);
}
public void addEndorsedJars() throws MalformedURLException
{
File endorsedDir = new File(libraryURL.getPath() + "/endorsed");
if (endorsedDir.exists())
{
String [] list = endorsedDir.list();
for (int i = 0; list != null && i < list.length; i++)
{
String jarname = list[i];
addLibrary("endorsed/" + jarname);
}
}
}
protected URL getURL(final String name) throws MalformedURLException
{
String value = props.getProperty(name, null);
if (value != null)
{
if (!value.endsWith("/")) value += "/";
return new URL(value);
}
return null;
}
protected URL[] getBootClasspath() throws MalformedURLException
{
List list = new LinkedList();
list.addAll(extraClasspath);
String value = props.getProperty(ServerConfig.BOOT_LIBRARY_LIST, DEFAULT_BOOT_LIBRARY_LIST);
StringTokenizer stok = new StringTokenizer(value, ",");
while (stok.hasMoreElements())
{
URL url = new URL(libraryURL, stok.nextToken().trim());
list.add(url);
}
return (URL[]) list.toArray(new URL[list.size()]);
}
public Server load(final ClassLoader parent) throws Exception
{
Server server;
ClassLoader oldCL = Thread.currentThread().getContextClassLoader();
try
{
URL[] urls = getBootClasspath();
URLClassLoader classLoader = new NoAnnotationURLClassLoader(urls, parent);
Thread.currentThread().setContextClassLoader(classLoader);
String typename = props.getProperty(ServerConfig.SERVER_TYPE, DEFAULT_SERVER_TYPE);
server = createServer(typename, classLoader);
}
finally
{
Thread.currentThread().setContextClassLoader(oldCL);
}
return server;
}
protected Server createServer(final String typename, final ClassLoader classLoader) throws Exception
{
Class type = classLoader.loadClass(typename);
Server server = (Server) type.newInstance();
return server;
}
}