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 org.apache.maven.surefire.util.UrlUtils;
23  
24  import java.io.File;
25  import java.net.MalformedURLException;
26  import java.net.URL;
27  import java.util.ArrayList;
28  import java.util.Collection;
29  import java.util.Collections;
30  import java.util.Iterator;
31  import java.util.LinkedHashSet;
32  import java.util.List;
33  
34  /**
35   * An ordered list of classpath elements with set behaviour
36   *
37   * A Classpath is immutable and thread safe.
38   *
39   * Immutable and thread safe
40   *
41   * @author Kristian Rosenvold
42   */
43  public class Classpath implements Iterable<String>
44  {
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          this.unmodifiableElements = Collections.unmodifiableList( elems );
79      }
80  
81      public Classpath( Iterable<String> elements )
82      {
83          List<String> newCp = new ArrayList<String>(  );
84          for ( String element : elements )
85          {
86              newCp.add( element );
87          }
88          this.unmodifiableElements = Collections.unmodifiableList( newCp );
89      }
90  
91      public static Classpath emptyClasspath()
92      {
93          return new Classpath();
94      }
95  
96      public Classpath addClassPathElementUrl( String path )
97      {
98          if ( path == null )
99          {
100             throw new IllegalArgumentException( "Null is not a valid class path element url." );
101         }
102         return !unmodifiableElements.contains( path ) ? new Classpath( this, path ) : this;
103     }
104 
105     public List<String> getClassPath()
106     {
107         return unmodifiableElements;
108     }
109 
110     public List<URL> getAsUrlList()
111         throws MalformedURLException
112     {
113         List<URL> urls = new ArrayList<URL>();
114         for ( String url : unmodifiableElements )
115         {
116             File f = new File( url );
117             urls.add( UrlUtils.getURL( f ) );
118         }
119         return urls;
120     }
121 
122     public void writeToSystemProperty( String propertyName )
123     {
124         StringBuilder sb = new StringBuilder();
125         for ( String element : unmodifiableElements )
126         {
127             sb.append( element ).append( File.pathSeparatorChar );
128         }
129         System.setProperty( propertyName, sb.toString() );
130     }
131 
132     public boolean equals( Object o )
133     {
134         if ( this == o )
135         {
136             return true;
137         }
138         if ( o == null || getClass() != o.getClass() )
139         {
140             return false;
141         }
142 
143         Classpath classpath = (Classpath) o;
144 
145         return !( unmodifiableElements != null
146                         ? !unmodifiableElements.equals( classpath.unmodifiableElements )
147                         : classpath.unmodifiableElements != null );
148 
149     }
150 
151     public ClassLoader createClassLoader( ClassLoader parent, boolean childDelegation, boolean enableAssertions,
152                                           String roleName )
153         throws SurefireExecutionException
154     {
155         try
156         {
157             List urls = getAsUrlList();
158             IsolatedClassLoader classLoader = new IsolatedClassLoader( parent, childDelegation, roleName );
159             for ( Object url1 : urls )
160             {
161                 URL url = (URL) url1;
162                 classLoader.addURL( url );
163             }
164             if ( parent != null )
165             {
166                 parent.setDefaultAssertionStatus( enableAssertions );
167             }
168             classLoader.setDefaultAssertionStatus( enableAssertions );
169             return classLoader;
170         }
171         catch ( MalformedURLException e )
172         {
173             throw new SurefireExecutionException( "When creating classloader", e );
174         }
175     }
176 
177 
178     public int hashCode()
179     {
180         return unmodifiableElements != null ? unmodifiableElements.hashCode() : 0;
181     }
182 
183     public String getLogMessage( String descriptor )
184     {
185         StringBuilder result = new StringBuilder();
186         result.append( descriptor ).append( " classpath:" );
187         for ( String element : unmodifiableElements )
188         {
189             result.append( "  " ).append( element );
190         }
191         return result.toString();
192     }
193 
194     public String getCompactLogMessage( String descriptor )
195     {
196         StringBuilder result = new StringBuilder();
197         result.append( descriptor ).append( " classpath:" );
198         for ( String element : unmodifiableElements )
199         {
200             result.append( "  " );
201             if ( element != null )
202             {
203                 int pos = element.lastIndexOf( File.separatorChar );
204                 if ( pos >= 0 )
205                 {
206                     result.append( element.substring( pos + 1 ) );
207                 }
208                 else
209                 {
210                     result.append( element );
211                 }
212 
213             }
214             else
215             {
216                 result.append( element );
217             }
218         }
219         return result.toString();
220     }
221 
222     public Iterator<String> iterator()
223     {
224         return unmodifiableElements.iterator();
225     }
226 }