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