001: // ========================================================================
002: // Copyright (c) 2002 Mort Bay Consulting (Australia) Pty. Ltd.
003: // $Id: Classpath.java,v 1.2 2003/12/02 01:01:58 ajzeneski Exp $
004: // ========================================================================
005:
006: //package org.mortbay.start;
007: package org.ofbiz.base.start;
008:
009: import java.io.File;
010: import java.io.IOException;
011: import java.net.MalformedURLException;
012: import java.net.URL;
013: import java.net.URLClassLoader;
014: import java.util.StringTokenizer;
015: import java.util.Vector;
016:
017: /**
018: * Class to handle CLASSPATH construction
019: * @author Jan Hlavatý
020: */
021: public class Classpath {
022:
023: Vector _elements = new Vector();
024:
025: public Classpath() {
026: }
027:
028: public Classpath(String initial) {
029: addClasspath(initial);
030: }
031:
032: public boolean addComponent(String component) {
033: if ((component != null) && (component.length() > 0)) {
034: try {
035: File f = new File(component);
036: if (f.exists()) {
037: File key = f.getCanonicalFile();
038: if (!_elements.contains(key)) {
039: _elements.add(key);
040: return true;
041: }
042: }
043: } catch (IOException e) {
044: }
045: }
046: return false;
047: }
048:
049: public boolean addComponent(File component) {
050: if (component != null) {
051: try {
052: if (component.exists()) {
053: File key = component.getCanonicalFile();
054: if (!_elements.contains(key)) {
055: _elements.add(key);
056: return true;
057: }
058: }
059: } catch (IOException e) {
060: }
061: }
062: return false;
063: }
064:
065: public boolean addClasspath(String s) {
066: boolean added = false;
067: if (s != null) {
068: StringTokenizer t = new StringTokenizer(s,
069: File.pathSeparator);
070: while (t.hasMoreTokens()) {
071: added |= addComponent(t.nextToken());
072: }
073: }
074: return added;
075: }
076:
077: public String toString() {
078: StringBuffer cp = new StringBuffer(1024);
079: int cnt = _elements.size();
080: if (cnt >= 1) {
081: cp.append(((File) (_elements.elementAt(0))).getPath());
082: }
083: for (int i = 1; i < cnt; i++) {
084: cp.append(File.pathSeparatorChar);
085: cp.append(((File) (_elements.elementAt(i))).getPath());
086: }
087: return cp.toString();
088: }
089:
090: public URL[] getUrls() {
091: int cnt = _elements.size();
092: URL[] urls = new URL[cnt];
093: for (int i = 0; i < cnt; i++) {
094: try {
095: urls[i] = ((File) (_elements.elementAt(i))).toURL();
096: } catch (MalformedURLException e) {
097: }
098: }
099: return urls;
100: }
101:
102: public ClassLoader getClassLoader() {
103: URL[] urls = getUrls();
104:
105: ClassLoader parent = Thread.currentThread()
106: .getContextClassLoader();
107: if (parent == null) {
108: parent = Classpath.class.getClassLoader();
109: }
110: if (parent == null) {
111: parent = ClassLoader.getSystemClassLoader();
112: }
113: return new URLClassLoader(urls, parent);
114: }
115: }
|