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) firstClasspath.addTo( accumulated );
52          if (secondClasspath != null) secondClasspath.addTo( accumulated );
53          return new Classpath( accumulated );
54      }
55  
56  
57      private void addTo(Collection<String> c){
58          c.addAll( unmodifiableElements );
59      }
60  
61      private Classpath()
62      {
63          this.unmodifiableElements = Collections.emptyList();
64      }
65  
66  
67      public Classpath( Classpath other, String additionalElement )
68      {
69          ArrayList<String> elems = new ArrayList<String>( other.unmodifiableElements );
70          elems.add( additionalElement );
71          this.unmodifiableElements = Collections.unmodifiableList( elems );
72      }
73  
74      public Classpath( Iterable<String> elements )
75      {
76          List<String> newCp = new ArrayList<String>(  );
77          for ( String element : elements )
78          {
79              newCp.add( element );
80          }
81          this.unmodifiableElements = Collections.unmodifiableList( newCp );
82      }
83  
84      public static Classpath emptyClasspath()
85      {
86          return new Classpath();
87      }
88  
89      public Classpath addClassPathElementUrl( String path )
90      {
91          if ( path == null )
92          {
93              throw new IllegalArgumentException( "Null is not a valid class path element url." );
94          }
95          return !unmodifiableElements.contains( path ) ? new Classpath( this, path ) : this;
96      }
97  
98      public List<String> getClassPath()
99      {
100         return unmodifiableElements;
101     }
102 
103     public List<URL> getAsUrlList()
104         throws MalformedURLException
105     {
106         List<URL> urls = new ArrayList<URL>();
107         for ( String url : unmodifiableElements )
108         {
109             File f = new File( url );
110             urls.add( UrlUtils.getURL( f ) );
111         }
112         return urls;
113     }
114 
115     public void writeToSystemProperty( String propertyName )
116     {
117         StringBuilder sb = new StringBuilder();
118         for ( String element : unmodifiableElements )
119         {
120             sb.append( element ).append( File.pathSeparatorChar );
121         }
122         System.setProperty( propertyName, sb.toString() );
123     }
124 
125     public boolean equals( Object o )
126     {
127         if ( this == o )
128         {
129             return true;
130         }
131         if ( o == null || getClass() != o.getClass() )
132         {
133             return false;
134         }
135 
136         Classpath classpath = (Classpath) o;
137 
138         return !( unmodifiableElements
139             != null ? !unmodifiableElements.equals( classpath.unmodifiableElements ) : classpath.unmodifiableElements != null );
140 
141     }
142 
143     public ClassLoader createClassLoader( ClassLoader parent, boolean childDelegation, boolean enableAssertions,
144                                           String roleName )
145         throws SurefireExecutionException
146     {
147         try
148         {
149             List urls = getAsUrlList();
150             IsolatedClassLoader classLoader = new IsolatedClassLoader( parent, childDelegation, roleName );
151             for ( Object url1 : urls )
152             {
153                 URL url = (URL) url1;
154                 classLoader.addURL( url );
155             }
156             if ( parent != null )
157             {
158                 parent.setDefaultAssertionStatus( enableAssertions );
159             }
160             classLoader.setDefaultAssertionStatus( enableAssertions );
161             return classLoader;
162         }
163         catch ( MalformedURLException e )
164         {
165             throw new SurefireExecutionException( "When creating classloader", e );
166         }
167     }
168 
169 
170     public int hashCode()
171     {
172         return unmodifiableElements != null ? unmodifiableElements.hashCode() : 0;
173     }
174 
175     public String getLogMessage( String descriptor )
176     {
177         StringBuilder result = new StringBuilder();
178         result.append( descriptor ).append( " classpath:" );
179         for ( String element : unmodifiableElements )
180         {
181             result.append( "  " ).append( element );
182         }
183         return result.toString();
184     }
185 
186     public String getCompactLogMessage( String descriptor )
187     {
188         StringBuilder result = new StringBuilder();
189         result.append( descriptor ).append( " classpath:" );
190         for ( String element : unmodifiableElements )
191         {
192             result.append( "  " );
193             if ( element != null )
194             {
195                 int pos = element.lastIndexOf( File.separatorChar );
196                 if ( pos >= 0 )
197                 {
198                     result.append( element.substring( pos + 1 ) );
199                 }
200                 else
201                 {
202                     result.append( element );
203                 }
204 
205             }
206             else
207             {
208                 result.append( element );
209             }
210         }
211         return result.toString();
212     }
213 
214     public Iterator<String> iterator()
215     {
216         return unmodifiableElements.iterator();
217     }
218 }