1 22 package org.jboss.system.server; 23 24 26 import java.io.File ; 27 import java.net.MalformedURLException ; 28 import java.net.URL ; 29 import java.net.URLClassLoader ; 30 import java.util.LinkedList ; 31 import java.util.List ; 32 import java.util.Properties ; 33 import java.util.StringTokenizer ; 34 35 72 public class ServerLoader 73 { 74 80 public static final String DEFAULT_BOOT_LIBRARY_LIST = 81 "log4j-boot.jar,jboss-common-core.jar,jboss-logging-spi.jar,jboss-logging-log4j.jar,jboss-logging-jdk.jar,jboss-system.jar,jboss-xml-binding.jar,javassist.jar,jboss-aop-mc-int-boot.jar,jboss-container.jar,jboss-dependency.jar,jboss-microcontainer.jar,jboss-deployers.jar,dom4j.jar,jboss-j2se.jar,jboss-mbeans.jar,jboss-jmx.jar,jboss-system-jmx.jar"; 82 83 84 public static final String DEFAULT_SERVER_TYPE = "org.jboss.system.server.profileservice.ServerImpl"; 85 86 89 protected Properties props; 90 91 94 protected URL libraryURL; 95 96 100 protected List extraClasspath = new LinkedList (); 101 102 109 public ServerLoader(final Properties props) throws Exception 110 { 111 if (props == null) 112 throw new IllegalArgumentException ("props is null"); 113 114 this.props = props; 115 116 URL homeURL = getURL(ServerConfig.HOME_URL); 118 if (homeURL == null) 119 { 120 throw new Exception ("Missing configuration value for: " 121 + ServerConfig.HOME_URL); 122 } 123 124 libraryURL = getURL(ServerConfig.LIBRARY_URL); 125 if (libraryURL == null) 126 { 127 libraryURL = new URL (homeURL, ServerConfig.LIBRARY_URL_SUFFIX); 129 } 130 131 if( homeURL.getProtocol().startsWith("http") == true ) 133 { 134 this.addLibrary("webdavlib.jar"); 135 this.addLibrary("commons-httpclient.jar"); 136 this.addLibrary("commons-logging.jar"); 137 } 138 } 139 140 149 public void addLibrary(final String filename) throws MalformedURLException 150 { 151 if (filename == null) 152 throw new IllegalArgumentException ("filename is null"); 153 154 URL jarURL = new URL (libraryURL, filename); 155 extraClasspath.add(jarURL); 156 } 157 158 165 public void addLibraries(final String filenames) throws MalformedURLException 166 { 167 if (filenames == null) 168 throw new IllegalArgumentException ("filenames is null"); 169 170 StringTokenizer stok = new StringTokenizer (filenames, ","); 171 while (stok.hasMoreElements()) 172 { 173 addLibrary(stok.nextToken().trim()); 174 } 175 } 176 177 182 public void addURL(final URL url) 183 { 184 if (url == null) 185 throw new IllegalArgumentException ("url is null"); 186 187 extraClasspath.add(url); 188 } 189 190 195 public void addEndorsedJars() throws MalformedURLException 196 { 197 File endorsedDir = new File (libraryURL.getPath() + "/endorsed"); 198 if (endorsedDir.exists()) 199 { 200 String [] list = endorsedDir.list(); 201 for (int i = 0; list != null && i < list.length; i++) 202 { 203 String jarname = list[i]; 204 addLibrary("endorsed/" + jarname); 205 } 206 } 207 } 208 209 212 protected URL getURL(final String name) throws MalformedURLException 213 { 214 String value = props.getProperty(name, null); 215 if (value != null) 216 { 217 if (!value.endsWith("/")) value += "/"; 218 return new URL (value); 219 } 220 return null; 221 } 222 223 227 protected URL [] getBootClasspath() throws MalformedURLException 228 { 229 List list = new LinkedList (); 230 231 list.addAll(extraClasspath); 233 234 String value = props.getProperty(ServerConfig.BOOT_LIBRARY_LIST, DEFAULT_BOOT_LIBRARY_LIST); 235 236 StringTokenizer stok = new StringTokenizer (value, ","); 237 while (stok.hasMoreElements()) 238 { 239 URL url = new URL (libraryURL, stok.nextToken().trim()); 240 list.add(url); 241 } 242 243 return (URL []) list.toArray(new URL [list.size()]); 244 } 245 246 254 public Server load(final ClassLoader parent) throws Exception 255 { 256 Server server; 257 ClassLoader oldCL = Thread.currentThread().getContextClassLoader(); 258 259 try 260 { 261 URL [] urls = getBootClasspath(); 263 URLClassLoader classLoader = new NoAnnotationURLClassLoader(urls, parent); 264 Thread.currentThread().setContextClassLoader(classLoader); 265 266 String typename = props.getProperty(ServerConfig.SERVER_TYPE, DEFAULT_SERVER_TYPE); 268 server = createServer(typename, classLoader); 269 } 270 finally 271 { 272 Thread.currentThread().setContextClassLoader(oldCL); 273 } 274 275 return server; 277 } 278 279 285 protected Server createServer(final String typename, final ClassLoader loader) 286 throws Exception 287 { 288 Class type = loader.loadClass(typename); 290 291 Server server = (Server) type.newInstance(); 293 return server; 294 } 295 } 296 | Popular Tags |