View Javadoc

1   package org.apache.maven.plugin.surefire;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
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   * A properties implementation that preserves insertion order.
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              //noinspection unchecked
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          //noinspection unchecked
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         //noinspection unchecked
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         // We used to take all of our system properties and dump them in with the
172         // user specified properties for SUREFIRE-121, causing SUREFIRE-491.
173         // Not gonna do THAT any more... instead, we only propagate those system properties
174         // that have been explicitly specified by the user via -Dkey=value on the CLI
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                 //java Properties does not accept null value
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             //noinspection unchecked
204             target.put( key, get( key ) );
205         }
206     }
207 
208 
209 }