View Javadoc

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