View Javadoc

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