1 6 package org.ofbiz.base.start; 8 9 import java.io.File ; 10 import java.io.IOException ; 11 import java.net.MalformedURLException ; 12 import java.net.URL ; 13 import java.net.URLClassLoader ; 14 import java.util.ArrayList ; 15 import java.util.List ; 16 import java.util.StringTokenizer ; 17 18 22 public class Classpath { 23 24 private List _elements = new ArrayList (); 25 26 public Classpath() {} 27 28 public Classpath(String initial) { 29 addClasspath(initial); 30 } 31 32 public boolean addComponent(String component) { 33 if ((component != null) && (component.length() > 0)) { 34 try { 35 File f = new File (component); 36 if (f.exists()) { 37 File key = f.getCanonicalFile(); 38 if (!_elements.contains(key)) { 39 _elements.add(key); 40 return true; 41 } 42 } 43 } catch (IOException e) {} 44 } 45 return false; 46 } 47 48 public boolean addComponent(File component) { 49 if (component != null) { 50 try { 51 if (component.exists()) { 52 File key = component.getCanonicalFile(); 53 if (!_elements.contains(key)) { 54 _elements.add(key); 55 return true; 56 } 57 } 58 } catch (IOException e) {} 59 } 60 return false; 61 } 62 63 public boolean addClasspath(String s) { 64 boolean added = false; 65 if (s != null) { 66 StringTokenizer t = new StringTokenizer (s, File.pathSeparator); 67 while (t.hasMoreTokens()) { 68 added |= addComponent(t.nextToken()); 69 } 70 } 71 return added; 72 } 73 74 public String toString() { 75 StringBuffer cp = new StringBuffer (1024); 76 int cnt = _elements.size(); 77 if (cnt >= 1) { 78 cp.append(((File ) (_elements.get(0))).getPath()); 79 } 80 for (int i = 1; i < cnt; i++) { 81 cp.append(File.pathSeparatorChar); 82 cp.append(((File ) (_elements.get(i))).getPath()); 83 } 84 return cp.toString(); 85 } 86 87 public URL [] getUrls() { 88 int cnt = _elements.size(); 89 URL [] urls = new URL [cnt]; 90 for (int i = 0; i < cnt; i++) { 91 try { 92 urls[i] = ((File ) (_elements.get(i))).toURL(); 93 } catch (MalformedURLException e) {} 94 } 95 return urls; 96 } 97 98 public ClassLoader getClassLoader() { 99 URL [] urls = getUrls(); 100 101 ClassLoader parent = Thread.currentThread().getContextClassLoader(); 102 if (parent == null) { 103 parent = Classpath.class.getClassLoader(); 104 } 105 if (parent == null) { 106 parent = ClassLoader.getSystemClassLoader(); 107 } 108 return new URLClassLoader (urls, parent); 109 } 110 111 public List getElements() { 112 return _elements; 113 } 114 } 115 | Popular Tags |