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