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.Collections;
27  import java.util.Iterator;
28  import java.util.List;
29  import org.apache.maven.surefire.util.UrlUtils;
30  
31  /**
32   * An ordered list of classpath elements with set behaviour
33   *
34   * @author Kristian Rosenvold
35   */
36  public class Classpath
37  {
38      private static final JdkReflector jdkReflector = new JdkReflector();
39  
40      public static Classpath join( Classpath firstClasspath, Classpath secondClasspath )
41      {
42          Classpath joinedClasspath = new Classpath();
43          joinedClasspath.addElementsOfClasspath( firstClasspath );
44          joinedClasspath.addElementsOfClasspath( secondClasspath );
45          return joinedClasspath;
46      }
47  
48      private final List elements;
49  
50      public Classpath()
51      {
52          this.elements = new ArrayList();
53      }
54  
55      public Classpath( Classpath other )
56      {
57          this.elements = new ArrayList( other.elements );
58      }
59  
60      public Classpath( List elements )
61      {
62          this();
63          addElements( elements );
64      }
65  
66      public void addClassPathElementUrl( String path )
67      {
68          if ( path == null )
69          {
70              throw new IllegalArgumentException( "Null is not a valid class path element url." );
71          }
72          else if ( !elements.contains( path ) )
73          {
74              elements.add( path );
75          }
76      }
77  
78      private void addElements( List additionalElements )
79      {
80          for ( Iterator it = additionalElements.iterator(); it.hasNext(); )
81          {
82              String element = (String) it.next();
83              addClassPathElementUrl( element );
84          }
85      }
86  
87      private void addElementsOfClasspath( Classpath otherClasspath )
88      {
89          if ( otherClasspath != null )
90          {
91              addElements( otherClasspath.elements );
92          }
93      }
94  
95      public List getClassPath()
96      {
97          return Collections.unmodifiableList( elements );
98      }
99  
100     public List getAsUrlList()
101         throws MalformedURLException
102     {
103         List urls = new ArrayList();
104         for ( Iterator i = elements.iterator(); i.hasNext(); )
105         {
106             String url = (String) i.next();
107             File f = new File( url );
108             urls.add( UrlUtils.getURL( f ) );
109         }
110         return urls;
111     }
112 
113     public void writeToSystemProperty( String propertyName )
114     {
115         StringBuffer sb = new StringBuffer();
116         for ( Iterator i = elements.iterator(); i.hasNext(); )
117         {
118             sb.append( (String) i.next() ).append( File.pathSeparatorChar );
119         }
120         System.setProperty( propertyName, sb.toString() );
121     }
122 
123     public boolean equals( Object o )
124     {
125         if ( this == o )
126         {
127             return true;
128         }
129         if ( o == null || getClass() != o.getClass() )
130         {
131             return false;
132         }
133 
134         Classpath classpath = (Classpath) o;
135 
136         return !( elements != null ? !elements.equals( classpath.elements ) : classpath.elements != null );
137 
138     }
139 
140     public ClassLoader createClassLoader( ClassLoader parent, boolean childDelegation, boolean enableAssertions,
141                                           String roleName )
142         throws SurefireExecutionException
143     {
144         try
145         {
146             List urls = getAsUrlList();
147             IsolatedClassLoader classLoader = new IsolatedClassLoader( parent, childDelegation, roleName );
148             for ( Iterator iter = urls.iterator(); iter.hasNext(); )
149             {
150                 URL url = (URL) iter.next();
151                 classLoader.addURL( url );
152             }
153             if ( parent != null )
154             {
155                 jdkReflector.invokeAssertionStatusMethod( parent, enableAssertions );
156             }
157             jdkReflector.invokeAssertionStatusMethod( classLoader, enableAssertions );
158             return classLoader;
159         }
160         catch ( MalformedURLException e )
161         {
162             throw new SurefireExecutionException( "When creating classloader", e );
163         }
164     }
165 
166 
167     public int hashCode()
168     {
169         return elements != null ? elements.hashCode() : 0;
170     }
171 }