1 16 package org.mortbay.start; 17 18 import java.io.File ; 19 import java.io.IOException ; 20 import java.net.MalformedURLException ; 21 import java.net.URL ; 22 import java.net.URLClassLoader ; 23 import java.util.Arrays ; 24 import java.util.StringTokenizer ; 25 import java.util.Vector ; 26 27 28 32 public class Classpath { 33 34 Vector _elements = new Vector (); 35 36 public Classpath() 37 {} 38 39 public Classpath(String initial) 40 { 41 addClasspath(initial); 42 } 43 44 public boolean addComponent(String component) 45 { 46 if ((component != null)&&(component.length()>0)) { 47 try { 48 File f = new File (component); 49 if (f.exists()) 50 { 51 File key = f.getCanonicalFile(); 52 if (!_elements.contains(key)) 53 { 54 _elements.add(key); 55 return true; 56 } 57 } 58 } catch (IOException e) {} 59 } 60 return false; 61 } 62 63 public boolean addComponent(File component) 64 { 65 if (component != null) { 66 try { 67 if (component.exists()) { 68 File key = component.getCanonicalFile(); 69 if (!_elements.contains(key)) { 70 _elements.add(key); 71 return true; 72 } 73 } 74 } catch (IOException e) {} 75 } 76 return false; 77 } 78 79 public boolean addClasspath(String s) 80 { 81 boolean added=false; 82 if (s != null) 83 { 84 StringTokenizer t = new StringTokenizer (s, File.pathSeparator); 85 while (t.hasMoreTokens()) 86 { 87 added|=addComponent(t.nextToken()); 88 } 89 } 90 return added; 91 } 92 93 public String toString() 94 { 95 StringBuffer cp = new StringBuffer (1024); 96 int cnt = _elements.size(); 97 if (cnt >= 1) { 98 cp.append( ((File )(_elements.elementAt(0))).getPath() ); 99 } 100 for (int i=1; i < cnt; i++) { 101 cp.append(File.pathSeparatorChar); 102 cp.append( ((File )(_elements.elementAt(i))).getPath() ); 103 } 104 return cp.toString(); 105 } 106 107 public ClassLoader getClassLoader() { 108 int cnt = _elements.size(); 109 URL [] urls = new URL [cnt]; 110 for (int i=0; i < cnt; i++) { 111 try { 112 urls[i] = ((File )(_elements.elementAt(i))).toURL(); 113 } catch (MalformedURLException e) {} 114 } 115 116 ClassLoader parent = Thread.currentThread().getContextClassLoader(); 117 if (parent == null) { 118 parent = Classpath.class.getClassLoader(); 119 } 120 if (parent == null) { 121 parent = ClassLoader.getSystemClassLoader(); 122 } 123 return new Loader (urls, parent); 124 } 125 126 private class Loader extends URLClassLoader 127 { 128 String name; 129 130 Loader(URL [] urls, ClassLoader parent) 131 { 132 super(urls, parent); 133 name = "StartLoader"+Arrays.asList(urls); 134 } 135 136 public String toString() 137 { 138 return name; 139 } 140 } 141 142 } 143 | Popular Tags |