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 Classpath getClasspath( String prefix )
123 {
124 List<String> elements = getStringList( prefix );
125 return new Classpath( elements );
126 }
127
128 public void setClasspath( String prefix, Classpath classpath )
129 {
130 List classpathElements = classpath.getClassPath();
131 for ( int i = 0, size = classpathElements.size(); i < size; ++i )
132 {
133 String element = (String) classpathElements.get( i );
134 setProperty( prefix + i, element );
135 }
136 }
137
138
139 public void setProperty( String key, String value )
140 {
141 if ( value != null )
142 {
143 properties.put( key, value );
144 }
145 }
146
147 public void addList( List items, String propertyPrefix )
148 {
149 if ( items != null && !items.isEmpty() )
150 {
151 int i = 0;
152 for ( Object item : items )
153 {
154 if ( item == null )
155 {
156 throw new NullPointerException( propertyPrefix + i + " has null value" );
157 }
158
159 String[] stringArray = StringUtils.split( item.toString(), "," );
160
161 for ( String aStringArray : stringArray )
162 {
163 properties.put( propertyPrefix + i, aStringArray );
164 i++;
165 }
166 }
167 }
168 }
169
170 public void copyTo( Map<Object, Object> target )
171 {
172 target.putAll( properties );
173 }
174 }