1 23 24 37 package com.sun.enterprise.loader; 38 39 import java.io.File ; 40 import java.util.List ; 41 import java.util.ArrayList ; 42 import java.net.URL ; 43 import java.net.URLClassLoader ; 44 import java.util.StringTokenizer ; 45 46 import com.sun.enterprise.util.io.FileUtils; 47 48 import java.io.IOException ; 49 50 import java.util.logging.Level ; 51 import java.util.logging.Logger ; 52 import com.sun.logging.LogDomains; 53 54 55 61 public class ClassLoaderUtils { 62 63 64 private static Logger _logger = 65 LogDomains.getLogger(LogDomains.LOADER_LOGGER); 66 67 80 public static ClassLoader getClassLoader(File [] dirs, File [] jarDirs, 81 ClassLoader parent) throws IOException { 82 83 URLClassLoader loader = null; 84 URL [] urls = getUrls(dirs, jarDirs); 85 86 if (urls != null) { 87 if (parent != null) { 88 loader = new URLClassLoader (urls, parent); 89 } else { 90 loader = new URLClassLoader (urls); 91 } 92 } 93 94 return loader; 95 } 96 97 113 public static URL [] getUrls(File [] dirs, File [] jarDirs) 114 throws IOException { 115 116 URL [] urls = null; 117 List list = new ArrayList (); 118 119 if (dirs != null) { 121 for (int i=0; i<dirs.length; i++) { 122 File dir = dirs[i]; 123 if (dir.isDirectory() || dir.canRead()) { 124 URL url = dir.toURI().toURL(); 125 list.add(url); 126 127 if (_logger.isLoggable(Level.FINE)) { 128 _logger.log(Level.FINE, 129 "Adding directory to class path:" + url.toString()); 130 } 131 } 132 } 133 } 134 135 if (jarDirs != null) { 137 for (int i=0; i<jarDirs.length; i++) { 138 File jarDir = jarDirs[i]; 139 140 if (jarDir.isDirectory() || jarDir.canRead()) { 141 File [] files = jarDir.listFiles(); 142 143 for (int j=0; j<files.length; j++) { 144 File jar = files[j]; 145 146 if ( FileUtils.isJar(jar) || FileUtils.isZip(jar) ) { 147 list.add(jar.toURI().toURL()); 148 149 if (_logger.isLoggable(Level.FINE)) { 150 _logger.log(Level.FINE, 151 "Adding jar to class path:" + jar.toURL()); 152 } 153 } 154 } 155 } 156 } 157 } 158 159 if (list.size() > 0) { 161 urls = new URL [list.size()]; 162 urls = (URL []) list.toArray(urls); 163 } 164 165 return urls; 166 } 167 168 190 public static List getUrlList(File [] dirs, File [] jarDirs) 191 throws IOException { 192 return getUrlList(dirs, jarDirs, false); 193 } 194 195 218 public static List getUrlList(File [] dirs, File [] jarDirs, 219 boolean ignoreZip) throws IOException { 220 221 List list = new ArrayList (); 222 223 if (dirs != null) { 225 for (int i=0; i<dirs.length; i++) { 226 File dir = dirs[i]; 227 if (dir.isDirectory() || dir.canRead()) { 228 list.add( dir.getCanonicalPath() ); 229 230 if (_logger.isLoggable(Level.FINE)) { 231 _logger.log(Level.FINE, 232 "Adding directory to class path:" 233 + dir.getCanonicalPath()); 234 } 235 } 236 } 237 } 238 239 if (jarDirs != null) { 241 for (int i=0; i<jarDirs.length; i++) { 242 File jarDir = jarDirs[i]; 243 244 if (jarDir.isDirectory() || jarDir.canRead()) { 245 File [] files = jarDir.listFiles(); 246 247 for (int j=0; j<files.length; j++) { 248 File jar = files[j]; 249 250 if ( FileUtils.isJar(jar) || 251 (!ignoreZip && FileUtils.isZip(jar)) ) { 252 list.add( jar.getCanonicalPath() ); 253 254 if (_logger.isLoggable(Level.FINE)) { 255 _logger.log(Level.FINE, 256 "Adding jar to class path:" 257 + jar.getCanonicalPath()); 258 } 259 } 260 } 261 } 262 } 263 } 264 265 return list; 266 } 267 268 272 public static URL [] getUrlsFromClasspath(String classpath) { 273 274 List urls = new ArrayList (); 275 276 if (classpath == null) return (URL []) urls.toArray(); 278 StringTokenizer st = new StringTokenizer (classpath, File.pathSeparator); 280 while (st.hasMoreTokens()) { 281 try { 282 File f = new File (st.nextToken()); 283 urls.add(f.toURI().toURL()); 284 } catch(Exception e) { 285 _logger.log(Level.WARNING, 286 "loader.unexpected_error_while_creating_urls",e); 287 } 288 } 289 290 URL [] ret; 292 if (urls.size() > 0) { 293 ret = new URL [urls.size()]; 294 ret = (URL []) urls.toArray(ret); 295 } else { 296 ret = new URL [0]; 297 } 298 299 return ret; 300 } 301 302 305 public static void main(String [] args) { 306 307 try { 308 URL [] urls = getUrls(new File [] {new File (args[0])}, 309 new File [] {new File (args[1])}); 310 for (int i=0; i<urls.length; i++) { 311 System.out.println(urls[i]); 312 } 313 314 URLClassLoader loader = (URLClassLoader ) 315 getClassLoader(new File [] {new File (args[0])}, 316 new File [] {new File (args[1])}, null); 317 318 Class c = loader.loadClass(args[2]); 320 System.out.println("Loaded: " + c.getName()); 321 System.out.println("Loaded class has the following methods..."); 322 java.lang.reflect.Method [] m = c.getDeclaredMethods(); 323 for (int i=0; i<m.length; i++) { 324 System.out.println(m[i]); 325 } 326 } catch (Exception e) { 327 e.printStackTrace(); 328 } 329 } 330 } 331 | Popular Tags |