View Javadoc
1   package org.apache.maven.surefire.booter;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.FileInputStream;
24  import java.io.FileOutputStream;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.util.Map;
28  import java.util.Properties;
29  import java.util.concurrent.ConcurrentHashMap;
30  
31  /**
32   * @author Kristian Rosenvold
33   */
34  public class SystemPropertyManager
35  {
36  
37      /**
38       * Loads the properties, closes the stream
39       *
40       * @param inStream The stream to read from, will be closed
41       * @return The properties
42       * @throws java.io.IOException If something bad happens
43       */
44      public static PropertiesWrapper loadProperties( InputStream inStream )
45          throws IOException
46      {
47          try
48          {
49              Properties p = new Properties();
50              p.load( inStream );
51              Map<String, String> map = new ConcurrentHashMap<String, String>( p.size() );
52              // @todo use .stringPropertyNames() JDK6 instead of .keySet()
53              for ( Map.Entry<?, ?> entry : p.entrySet() )
54              {
55                  map.put( (String) entry.getKey(), (String) entry.getValue() );
56              }
57              return new PropertiesWrapper( map );
58          }
59          finally
60          {
61              close( inStream ); // @todo use try-with-resources JDK7, search in all code
62          }
63      }
64  
65      private static PropertiesWrapper loadProperties( File file )
66          throws IOException
67      {
68          return loadProperties( new FileInputStream( file ) );
69      }
70  
71      public static void setSystemProperties( File file )
72          throws IOException
73      {
74          PropertiesWrapper p = loadProperties( file );
75          p.setAsSystemProperties();
76      }
77  
78      public static File writePropertiesFile( Properties properties, File tempDirectory, String name,
79                                              boolean keepForkFiles )
80          throws IOException
81      {
82          File file = File.createTempFile( name, "tmp", tempDirectory );
83          if ( !keepForkFiles )
84          {
85              file.deleteOnExit();
86          }
87  
88          writePropertiesFile( file, name, properties );
89  
90          return file;
91      }
92  
93      public static void writePropertiesFile( File file, String name, Properties properties )
94          throws IOException
95      {
96          FileOutputStream out = new FileOutputStream( file );
97  
98          try
99          {
100             properties.store( out, name );
101         }
102         finally
103         {
104             out.close();
105         }
106     }
107 
108     public static void close( InputStream inputStream )
109     {
110         if ( inputStream == null )
111         {
112             return;
113         }
114 
115         try
116         {
117             inputStream.close();
118         }
119         catch ( IOException ex )
120         {
121             // ignore
122         }
123     }
124 
125 
126 }