1 package org.apache.maven.plugin.surefire;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.Collections;
26 import java.util.Enumeration;
27 import java.util.Iterator;
28 import java.util.LinkedHashSet;
29 import java.util.Map;
30 import java.util.Properties;
31 import org.apache.maven.plugin.logging.Log;
32 import org.apache.maven.surefire.booter.KeyValueSource;
33
34
35
36
37 public class SurefireProperties
38 extends Properties
39 implements KeyValueSource
40 {
41 private final LinkedHashSet<Object> items = new LinkedHashSet<Object>();
42
43 public SurefireProperties()
44 {
45 }
46
47 public SurefireProperties( Properties source )
48 {
49 if ( source != null )
50 {
51 this.putAll( source );
52 }
53 }
54
55 @Override
56 public synchronized Object put( Object key, Object value )
57 {
58 items.add( key );
59 return super.put( key, value );
60 }
61
62 @Override
63 public synchronized Object remove( Object key )
64 {
65 items.remove( key );
66 return super.remove( key );
67 }
68
69 @Override
70 public synchronized void clear()
71 {
72 items.clear();
73 super.clear();
74 }
75
76 public synchronized Enumeration<Object> keys()
77 {
78 return Collections.enumeration( items );
79 }
80
81 public void copyProperties( Properties source )
82 {
83 if ( source != null )
84 {
85
86 for ( Object key : source.keySet() )
87 {
88 Object value = source.get( key );
89 put( key, value );
90 }
91 }
92 }
93
94 private Iterable<Object> getStringKeySet()
95 {
96
97
98 return keySet();
99 }
100
101 public void showToLog( org.apache.maven.plugin.logging.Log log, String setting )
102 {
103 for ( Object key : getStringKeySet() )
104 {
105 String value = getProperty( (String) key );
106 log.debug( "Setting " + setting + " [" + key + "]=[" + value + "]" );
107 }
108 }
109
110 public void verifyLegalSystemProperties( org.apache.maven.plugin.logging.Log log )
111 {
112 for ( Object key : getStringKeySet() )
113 {
114 if ( "java.library.path".equals( key ) )
115 {
116 log.warn(
117 "java.library.path cannot be set as system property, use <argLine>-Djava.library.path=...<argLine> instead" );
118 }
119 }
120 }
121
122
123 public void copyToSystemProperties()
124 {
125
126
127 for ( Object o : items )
128 {
129 String key = (String) o;
130 String value = getProperty( key );
131
132 System.setProperty( key, value );
133 }
134 }
135
136 static SurefireProperties calculateEffectiveProperties( Properties systemProperties, File systemPropertiesFile,
137 Map<String, String> systemPropertyVariables,
138 Properties userProperties, Log log )
139 {
140 SurefireProperties result = new SurefireProperties();
141 result.copyProperties( systemProperties );
142
143 if ( systemPropertiesFile != null )
144 {
145 Properties props = new SurefireProperties();
146 try
147 {
148 InputStream fis = new FileInputStream( systemPropertiesFile );
149 props.load( fis );
150 fis.close();
151 }
152 catch ( IOException e )
153 {
154 String msg = "The system property file '" + systemPropertiesFile.getAbsolutePath() + "' can't be read.";
155 if ( log.isDebugEnabled() )
156 {
157 log.warn( msg, e );
158 }
159 else
160 {
161 log.warn( msg );
162 }
163 }
164
165 result.copyProperties( props );
166 }
167
168 copyProperties( result, systemPropertyVariables );
169 copyProperties( result, systemPropertyVariables );
170
171
172
173
174
175
176 result.copyProperties( userProperties );
177 return result;
178 }
179
180 public static void copyProperties( Properties target, Map<String, String> source )
181 {
182 if ( source != null )
183 {
184 for ( String key : source.keySet() )
185 {
186 String value = source.get( key );
187
188 if ( value != null )
189 {
190 target.setProperty( key, value );
191 }
192 }
193 }
194 }
195
196 public void copyTo( Map target )
197 {
198 Iterator iter = keySet().iterator();
199 Object key;
200 while ( iter.hasNext() )
201 {
202 key = iter.next();
203
204 target.put( key, get( key ) );
205 }
206 }
207
208
209 }