KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > start > Classpath


1 // ========================================================================
2
// $Id: Classpath.java,v 1.5 2004/05/09 20:32:46 gregwilkins Exp $
3
// Copyright 2002-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.start;
17
18 import java.io.File JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.net.MalformedURLException JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.net.URLClassLoader JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25 import java.util.Vector JavaDoc;
26
27
28 /**
29  * Class to handle CLASSPATH construction
30  * @author Jan Hlavatý
31  */

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