001: // ========================================================================
002: // Copyright 2002-2005 Mort Bay Consulting Pty. Ltd.
003: // ------------------------------------------------------------------------
004: // Licensed under the Apache License, Version 2.0 (the "License");
005: // you may not use this file except in compliance with the License.
006: // You may obtain a copy of the License at
007: // http://www.apache.org/licenses/LICENSE-2.0
008: // Unless required by applicable law or agreed to in writing, software
009: // distributed under the License is distributed on an "AS IS" BASIS,
010: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011: // See the License for the specific language governing permissions and
012: // limitations under the License.
013: // ========================================================================
014:
015: package org.mortbay.start;
016:
017: import java.io.File;
018: import java.io.IOException;
019: import java.net.MalformedURLException;
020: import java.net.URL;
021: import java.net.URLClassLoader;
022: import java.util.Arrays;
023: import java.util.StringTokenizer;
024: import java.util.Vector;
025:
026: /**
027: * Class to handle CLASSPATH construction
028: * @author Jan Hlavaty
029: */
030: public class Classpath {
031:
032: Vector _elements = new Vector();
033:
034: public Classpath() {
035: }
036:
037: public Classpath(String initial) {
038: addClasspath(initial);
039: }
040:
041: public boolean addComponent(String component) {
042: if ((component != null) && (component.length() > 0)) {
043: try {
044: File f = new File(component);
045: if (f.exists()) {
046: File key = f.getCanonicalFile();
047: if (!_elements.contains(key)) {
048: _elements.add(key);
049: return true;
050: }
051: }
052: } catch (IOException e) {
053: }
054: }
055: return false;
056: }
057:
058: public boolean addComponent(File component) {
059: if (component != null) {
060: try {
061: if (component.exists()) {
062: File key = component.getCanonicalFile();
063: if (!_elements.contains(key)) {
064: _elements.add(key);
065: return true;
066: }
067: }
068: } catch (IOException e) {
069: }
070: }
071: return false;
072: }
073:
074: public boolean addClasspath(String s) {
075: boolean added = false;
076: if (s != null) {
077: StringTokenizer t = new StringTokenizer(s,
078: File.pathSeparator);
079: while (t.hasMoreTokens()) {
080: added |= addComponent(t.nextToken());
081: }
082: }
083: return added;
084: }
085:
086: public String toString() {
087: StringBuffer cp = new StringBuffer(1024);
088: int cnt = _elements.size();
089: if (cnt >= 1) {
090: cp.append(((File) (_elements.elementAt(0))).getPath());
091: }
092: for (int i = 1; i < cnt; i++) {
093: cp.append(File.pathSeparatorChar);
094: cp.append(((File) (_elements.elementAt(i))).getPath());
095: }
096: return cp.toString();
097: }
098:
099: public ClassLoader getClassLoader() {
100: int cnt = _elements.size();
101: URL[] urls = new URL[cnt];
102: for (int i = 0; i < cnt; i++) {
103: try {
104: urls[i] = ((File) (_elements.elementAt(i))).toURL();
105: } catch (MalformedURLException e) {
106: }
107: }
108:
109: ClassLoader parent = Thread.currentThread()
110: .getContextClassLoader();
111: if (parent == null) {
112: parent = Classpath.class.getClassLoader();
113: }
114: if (parent == null) {
115: parent = ClassLoader.getSystemClassLoader();
116: }
117: return new Loader(urls, parent);
118: }
119:
120: private class Loader extends URLClassLoader {
121: String name;
122:
123: Loader(URL[] urls, ClassLoader parent) {
124: super (urls, parent);
125: name = "StartLoader" + Arrays.asList(urls);
126: }
127:
128: public String toString() {
129: return name;
130: }
131: }
132:
133: }
|