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.util.ArrayList;
27  import java.util.Collections;
28  import java.util.Iterator;
29  import java.util.List;
30  
31  /**
32   * An ordered list of classpath elements with set behaviour
33   *
34   * @author Kristian Rosenvold
35   */
36  public class Classpath
37  {
38      public static Classpath join( Classpath firstClasspath, Classpath secondClasspath )
39      {
40          Classpath joinedClasspath = new Classpath();
41          joinedClasspath.addElementsOfClasspath( firstClasspath );
42          joinedClasspath.addElementsOfClasspath( secondClasspath );
43          return joinedClasspath;
44      }
45  
46      private final List elements = new ArrayList();
47  
48      public Classpath()
49      {
50      }
51  
52      public Classpath( List elements )
53      {
54          this();
55          addElements( elements );
56      }
57  
58      public void addClassPathElementUrl( String path )
59      {
60          if ( path == null )
61          {
62              throw new IllegalArgumentException( "Null is not a valid class path element url." );
63          }
64          else if ( !elements.contains( path ) )
65          {
66              elements.add( path );
67          }
68      }
69  
70      private void addElements( List additionalElements )
71      {
72          for ( Iterator it = additionalElements.iterator(); it.hasNext(); )
73          {
74              String element = (String) it.next();
75              addClassPathElementUrl( element );
76          }
77      }
78  
79      private void addElementsOfClasspath( Classpath otherClasspath )
80      {
81          if ( otherClasspath != null )
82          {
83              addElements( otherClasspath.elements );
84          }
85      }
86  
87      public List getClassPath()
88      {
89          return Collections.unmodifiableList( elements );
90      }
91  
92      public List getAsUrlList()
93          throws MalformedURLException
94      {
95          List urls = new ArrayList();
96          for ( Iterator i = elements.iterator(); i.hasNext(); )
97          {
98              String url = (String) i.next();
99              File f = new File( url );
100             urls.add( UrlUtils.getURL( f ) );
101         }
102         return urls;
103     }
104 
105     public void writeToSystemProperty( String propertyName )
106     {
107         StringBuffer sb = new StringBuffer();
108         for ( Iterator i = elements.iterator(); i.hasNext(); )
109         {
110             sb.append( (String) i.next() ).append( File.pathSeparatorChar );
111         }
112         System.setProperty( propertyName, sb.toString() );
113     }
114 
115     public boolean equals( Object o )
116     {
117         if ( this == o )
118         {
119             return true;
120         }
121         if ( o == null || getClass() != o.getClass() )
122         {
123             return false;
124         }
125 
126         Classpath classpath = (Classpath) o;
127 
128         return !( elements != null ? !elements.equals( classpath.elements ) : classpath.elements != null );
129 
130     }
131 
132     public int hashCode()
133     {
134         return elements != null ? elements.hashCode() : 0;
135     }
136 }