KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > start > Classpath


1 // ========================================================================
2
// Copyright (c) 2002 Mort Bay Consulting (Australia) Pty. Ltd.
3
// $Id: Classpath.java 5462 2005-08-05 18:35:48Z jonesde $
4
// ========================================================================
5

6 //package org.mortbay.start;
7
package org.ofbiz.base.start;
8
9 import java.io.File JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.net.MalformedURLException JavaDoc;
12 import java.net.URL JavaDoc;
13 import java.net.URLClassLoader JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.StringTokenizer JavaDoc;
17
18 /**
19  * Class to handle CLASSPATH construction
20  * @author Jan Hlavat
21  */

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