1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.start;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.PrintStream;
24 import java.io.UnsupportedEncodingException;
25 import java.net.MalformedURLException;
26 import java.net.URL;
27 import java.net.URLClassLoader;
28 import java.util.StringTokenizer;
29 import java.util.Vector;
30
31
32
33
34 public class Classpath
35 {
36
37 private final Vector<File> _elements = new Vector<File>();
38
39 public Classpath()
40 {
41 }
42
43 public Classpath(String initial)
44 {
45 addClasspath(initial);
46 }
47
48 public File[] getElements()
49 {
50 return _elements.toArray(new File[_elements.size()]);
51 }
52
53 public int count()
54 {
55 return _elements.size();
56 }
57
58 public boolean addComponent(String component)
59 {
60 if ((component != null) && (component.length() > 0))
61 {
62 try
63 {
64 File f = new File(component);
65 if (f.exists())
66 {
67 File key = f.getCanonicalFile();
68 if (!_elements.contains(key))
69 {
70 _elements.add(key);
71 return true;
72 }
73 }
74 }
75 catch (IOException e)
76 {
77 }
78 }
79 return false;
80 }
81
82 public boolean addComponent(File component)
83 {
84 if (component != null)
85 {
86 try
87 {
88 if (component.exists())
89 {
90 File key = component.getCanonicalFile();
91 if (!_elements.contains(key))
92 {
93 _elements.add(key);
94 return true;
95 }
96 }
97 }
98 catch (IOException e)
99 {
100 }
101 }
102 return false;
103 }
104
105 public boolean addClasspath(String s)
106 {
107 boolean added = false;
108 if (s != null)
109 {
110 StringTokenizer t = new StringTokenizer(s, File.pathSeparator);
111 while (t.hasMoreTokens())
112 {
113 added |= addComponent(t.nextToken());
114 }
115 }
116 return added;
117 }
118
119 public void dump(PrintStream out)
120 {
121 int i = 0;
122 for (File element : _elements)
123 {
124 out.printf("%2d: %s\n", i++, element.getAbsolutePath());
125 }
126 }
127
128 @Override
129 public String toString()
130 {
131 StringBuffer cp = new StringBuffer(1024);
132 int cnt = _elements.size();
133 if (cnt >= 1)
134 {
135 cp.append(((_elements.elementAt(0))).getPath());
136 }
137 for (int i = 1; i < cnt; i++)
138 {
139 cp.append(File.pathSeparatorChar);
140 cp.append(((_elements.elementAt(i))).getPath());
141 }
142 return cp.toString();
143 }
144
145 public ClassLoader getClassLoader()
146 {
147 int cnt = _elements.size();
148 URL[] urls = new URL[cnt];
149 for (int i = 0; i < cnt; i++)
150 {
151 try
152 {
153 urls[i] = _elements.elementAt(i).toURI().toURL();
154 }
155 catch (MalformedURLException e)
156 {
157 }
158 }
159
160 ClassLoader parent = Thread.currentThread().getContextClassLoader();
161 if (parent == null)
162 {
163 parent = Classpath.class.getClassLoader();
164 }
165 if (parent == null)
166 {
167 parent = ClassLoader.getSystemClassLoader();
168 }
169 return new Loader(urls, parent);
170 }
171
172 private static class Loader extends URLClassLoader
173 {
174 Loader(URL[] urls, ClassLoader parent)
175 {
176 super(urls, parent);
177 }
178
179 @Override
180 public String toString()
181 {
182 return "startJarLoader@" + Long.toHexString(hashCode());
183 }
184 }
185
186
187
188
189
190
191
192
193
194 public void overlay(Classpath cpOther)
195 {
196 for (File otherElement : cpOther._elements)
197 {
198 if (this._elements.contains(otherElement))
199 {
200
201 continue;
202 }
203 this._elements.add(otherElement);
204 }
205 }
206
207 public boolean isEmpty()
208 {
209 return (_elements == null) || (_elements.isEmpty());
210 }
211 }