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 java.io.File; |
23 | |
import java.util.ArrayList; |
24 | |
import java.util.Iterator; |
25 | |
import java.util.List; |
26 | |
import java.util.Map; |
27 | |
import java.util.Properties; |
28 | |
import org.apache.maven.surefire.util.internal.StringUtils; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public class PropertiesWrapper |
34 | |
implements KeyValueSource |
35 | |
{ |
36 | |
private final Properties properties; |
37 | |
|
38 | |
public PropertiesWrapper( Properties properties ) |
39 | 6 | { |
40 | 6 | if ( properties == null ) |
41 | |
{ |
42 | 0 | throw new IllegalStateException( "Properties cannot be null" ); |
43 | |
} |
44 | 6 | this.properties = properties; |
45 | 6 | } |
46 | |
|
47 | |
public Properties getProperties() |
48 | |
{ |
49 | 0 | return properties; |
50 | |
} |
51 | |
|
52 | |
public void setAsSystemProperties() |
53 | |
{ |
54 | 0 | for ( Iterator i = properties.keySet().iterator(); i.hasNext(); ) |
55 | |
{ |
56 | 0 | String key = (String) i.next(); |
57 | |
|
58 | 0 | System.setProperty( key, properties.getProperty( key ) ); |
59 | 0 | } |
60 | 0 | } |
61 | |
|
62 | |
public String getProperty( String key ) |
63 | |
{ |
64 | 15 | return properties.getProperty( key ); |
65 | |
} |
66 | |
|
67 | |
public boolean getBooleanProperty( String propertyName ) |
68 | |
{ |
69 | 0 | final Boolean aBoolean = Boolean.valueOf( properties.getProperty( propertyName ) ); |
70 | 0 | return aBoolean.booleanValue(); |
71 | |
} |
72 | |
|
73 | |
public Boolean getBooleanObjectProperty( String propertyName ) |
74 | |
{ |
75 | 0 | return Boolean.valueOf( properties.getProperty( propertyName ) ); |
76 | |
} |
77 | |
|
78 | |
public File getFileProperty( String key ) |
79 | |
{ |
80 | 0 | final String property = getProperty( key ); |
81 | 0 | if ( property == null ) |
82 | |
{ |
83 | 0 | return null; |
84 | |
} |
85 | 0 | TypeEncodedValue typeEncodedValue = new TypeEncodedValue( File.class.getName(), property ); |
86 | 0 | return (File) typeEncodedValue.getDecodedValue(); |
87 | |
} |
88 | |
|
89 | |
public List getStringList( String propertyPrefix ) |
90 | |
{ |
91 | |
String value; |
92 | 4 | List result = new ArrayList(); |
93 | |
|
94 | 13 | for ( int i = 0; ( value = getProperty( propertyPrefix + i ) ) != null; i++ ) |
95 | |
{ |
96 | 9 | result.add( value ); |
97 | |
} |
98 | 4 | return result; |
99 | |
} |
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
public TypeEncodedValue getTypeEncodedValue( String key ) |
108 | |
{ |
109 | 0 | String typeEncoded = getProperty( key ); |
110 | 0 | if ( typeEncoded == null ) |
111 | |
{ |
112 | 0 | return null; |
113 | |
} |
114 | 0 | int typeSep = typeEncoded.indexOf( "|" ); |
115 | 0 | String type = typeEncoded.substring( 0, typeSep ); |
116 | 0 | String value = typeEncoded.substring( typeSep + 1 ); |
117 | 0 | return new TypeEncodedValue( type, value ); |
118 | |
} |
119 | |
|
120 | |
|
121 | |
public void setProperty( String key, File file ) |
122 | |
{ |
123 | 0 | if ( file != null ) |
124 | |
{ |
125 | 0 | setProperty( key, file.toString() ); |
126 | |
} |
127 | 0 | } |
128 | |
|
129 | |
public void setProperty( String key, Boolean aBoolean ) |
130 | |
{ |
131 | 0 | if ( aBoolean != null ) |
132 | |
{ |
133 | 0 | setProperty( key, aBoolean.toString() ); |
134 | |
} |
135 | 0 | } |
136 | |
|
137 | |
Classpath getClasspath( String prefix ) |
138 | |
{ |
139 | 3 | List elements = getStringList( prefix ); |
140 | 3 | return new Classpath( elements ); |
141 | |
} |
142 | |
|
143 | |
public void setClasspath( String prefix, Classpath classpath ) |
144 | |
{ |
145 | 2 | List classpathElements = classpath.getClassPath(); |
146 | 6 | for ( int i = 0; i < classpathElements.size(); ++i ) |
147 | |
{ |
148 | 4 | String element = (String) classpathElements.get( i ); |
149 | 4 | setProperty( prefix + i, element ); |
150 | |
} |
151 | 2 | } |
152 | |
|
153 | |
|
154 | |
public void setProperty( String key, String value ) |
155 | |
{ |
156 | 4 | if ( value != null ) |
157 | |
{ |
158 | 4 | properties.setProperty( key, value ); |
159 | |
} |
160 | 4 | } |
161 | |
|
162 | |
public void addList( List items, String propertyPrefix ) |
163 | |
{ |
164 | 1 | if ( items == null || items.size() == 0 ) |
165 | |
{ |
166 | 0 | return; |
167 | |
} |
168 | 1 | int i = 0; |
169 | 1 | for ( Iterator iterator = items.iterator(); iterator.hasNext(); ) |
170 | |
{ |
171 | 4 | Object item = iterator.next(); |
172 | 4 | if ( item == null ) |
173 | |
{ |
174 | 0 | throw new NullPointerException( propertyPrefix + i + " has null value" ); |
175 | |
} |
176 | |
|
177 | 4 | String[] stringArray = StringUtils.split( item.toString(), "," ); |
178 | |
|
179 | 9 | for ( int j = 0; j < stringArray.length; j++ ) |
180 | |
{ |
181 | 5 | properties.setProperty( propertyPrefix + i, stringArray[j] ); |
182 | 5 | i++; |
183 | |
} |
184 | |
|
185 | 4 | } |
186 | 1 | } |
187 | |
|
188 | |
public void copyTo( Map target ) |
189 | |
{ |
190 | 0 | Iterator iter = properties.keySet().iterator(); |
191 | |
Object key; |
192 | 0 | while ( iter.hasNext() ) |
193 | |
{ |
194 | 0 | key = iter.next(); |
195 | 0 | target.put( key, properties.get( key ) ); |
196 | |
} |
197 | 0 | } |
198 | |
} |