1 package org.apache.maven.surefire.booter;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
33
34
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 }