View Javadoc
1   package org.apache.maven.surefire.booter;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.net.MalformedURLException;
24  import java.net.URL;
25  import java.util.ArrayList;
26  import java.util.Collection;
27  import java.util.Collections;
28  import java.util.Iterator;
29  import java.util.LinkedHashSet;
30  import java.util.List;
31  
32  import static java.io.File.pathSeparatorChar;
33  import static org.apache.maven.surefire.util.internal.UrlUtils.toURL;
34  
35  /**
36   * An ordered list of classpath elements with set behaviour
37   *
38   * A Classpath is immutable and thread safe.
39   *
40   * Immutable and thread safe
41   *
42   * @author Kristian Rosenvold
43   */
44  public class Classpath implements Iterable<String>
45  {
46      private final List<String> unmodifiableElements;
47  
48      public static Classpath join( Classpath firstClasspath, Classpath secondClasspath )
49      {
50          LinkedHashSet<String> accumulated =  new LinkedHashSet<String>(  );
51          if ( firstClasspath != null )
52          {
53              firstClasspath.addTo( accumulated );
54          }
55          if ( secondClasspath != null )
56          {
57              secondClasspath.addTo( accumulated );
58          }
59          return new Classpath( accumulated );
60      }
61  
62  
63      private void addTo( Collection<String> c )
64      {
65          c.addAll( unmodifiableElements );
66      }
67  
68      private Classpath()
69      {
70          this.unmodifiableElements = Collections.emptyList();
71      }
72  
73  
74      public Classpath( Classpath other, String additionalElement )
75      {
76          ArrayList<String> elems = new ArrayList<String>( other.unmodifiableElements );
77          elems.add( additionalElement );
78          unmodifiableElements = Collections.unmodifiableList( elems );
79      }
80  
81      public Classpath( Collection<String> elements )
82      {
83          List<String> newCp = new ArrayList<String>( elements.size() );
84          for ( String element : elements )
85          {
86              element = element.trim();
87              if ( element.length() != 0 )
88              {
89                  newCp.add( element );
90              }
91          }
92          unmodifiableElements = Collections.unmodifiableList( newCp );
93      }
94  
95      public static Classpath emptyClasspath()
96      {
97          return new Classpath();
98      }
99  
100     public Classpath addClassPathElementUrl( String path )
101     {
102         if ( path == null )
103         {
104             throw new IllegalArgumentException( "Null is not a valid class path element url." );
105         }
106         return !unmodifiableElements.contains( path ) ? new Classpath( this, path ) : this;
107     }
108 
109     public List<String> getClassPath()
110     {
111         return unmodifiableElements;
112     }
113 
114     /**
115      * @deprecated this should be package private method which returns List of Files. It will be
116      * removed in the next major version.
117      */
118     @Deprecated
119     public List<URL> getAsUrlList()
120         throws MalformedURLException
121     {
122         List<URL> urls = new ArrayList<URL>();
123         for ( String url : unmodifiableElements )
124         {
125             File f = new File( url );
126             urls.add( toURL( f ) );
127         }
128         return urls;
129     }
130 
131     public void writeToSystemProperty( String propertyName )
132     {
133         StringBuilder sb = new StringBuilder();
134         for ( String element : unmodifiableElements )
135         {
136             sb.append( element )
137               .append( pathSeparatorChar );
138         }
139         System.setProperty( propertyName, sb.toString() );
140     }
141 
142     public boolean equals( Object o )
143     {
144         if ( this == o )
145         {
146             return true;
147         }
148         if ( o == null || getClass() != o.getClass() )
149         {
150             return false;
151         }
152 
153         Classpath classpath = (Classpath) o;
154 
155         return unmodifiableElements.equals( classpath.unmodifiableElements );
156     }
157 
158     public ClassLoader createClassLoader( ClassLoader parent, boolean childDelegation, boolean enableAssertions,
159                                           String roleName )
160         throws SurefireExecutionException
161     {
162         try
163         {
164             IsolatedClassLoader classLoader = new IsolatedClassLoader( parent, childDelegation, roleName );
165             for ( String classPathElement : unmodifiableElements )
166             {
167                 classLoader.addURL( new File( classPathElement ).toURL() );
168             }
169             if ( parent != null )
170             {
171                 parent.setDefaultAssertionStatus( enableAssertions );
172             }
173             classLoader.setDefaultAssertionStatus( enableAssertions );
174             return classLoader;
175         }
176         catch ( MalformedURLException e )
177         {
178             throw new SurefireExecutionException( "When creating classloader", e );
179         }
180     }
181 
182     public int hashCode()
183     {
184         return unmodifiableElements.hashCode();
185     }
186 
187     public String getLogMessage( String descriptor )
188     {
189         StringBuilder result = new StringBuilder();
190         result.append( descriptor ).append( " classpath:" );
191         for ( String element : unmodifiableElements )
192         {
193             result.append( "  " ).append( element );
194         }
195         return result.toString();
196     }
197 
198     public String getCompactLogMessage( String descriptor )
199     {
200         StringBuilder result = new StringBuilder();
201         result.append( descriptor ).append( " classpath:" );
202         for ( String element : unmodifiableElements )
203         {
204             result.append( "  " );
205             if ( element != null )
206             {
207                 int pos = element.lastIndexOf( File.separatorChar );
208                 if ( pos >= 0 )
209                 {
210                     result.append( element.substring( pos + 1 ) );
211                 }
212                 else
213                 {
214                     result.append( element );
215                 }
216 
217             }
218             else
219             {
220                 result.append( (String) null );
221             }
222         }
223         return result.toString();
224     }
225 
226     public Iterator<String> iterator()
227     {
228         return unmodifiableElements.iterator();
229     }
230 }