View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.surefire.booter;
20  
21  import java.io.File;
22  import java.io.FileInputStream;
23  import java.io.FileOutputStream;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.util.Map;
27  import java.util.Properties;
28  import java.util.concurrent.ConcurrentHashMap;
29  
30  import org.apache.maven.surefire.api.util.TempFileManager;
31  
32  /**
33   * @author Kristian Rosenvold
34   */
35  public class SystemPropertyManager {
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) throws IOException {
45          try (InputStream stream = inStream) {
46              Properties p = new Properties();
47              p.load(stream);
48              Map<String, String> map = new ConcurrentHashMap<>(p.size());
49              for (String key : p.stringPropertyNames()) {
50                  map.put(key, p.getProperty(key));
51              }
52              return new PropertiesWrapper(map);
53          }
54      }
55  
56      private static PropertiesWrapper loadProperties(File file) throws IOException {
57          return loadProperties(new FileInputStream(file));
58      }
59  
60      public static void setSystemProperties(File file) throws IOException {
61          PropertiesWrapper p = loadProperties(file);
62          p.setAsSystemProperties();
63      }
64  
65      public static File writePropertiesFile(
66              Properties properties, File tempDirectory, String name, boolean keepForkFiles) throws IOException {
67          File file = TempFileManager.instance(tempDirectory).createTempFile(name, "tmp");
68          if (!keepForkFiles) {
69              file.deleteOnExit();
70          }
71  
72          writePropertiesFile(file, name, properties);
73  
74          return file;
75      }
76  
77      public static void writePropertiesFile(File file, String name, Properties properties) throws IOException {
78          try (FileOutputStream out = new FileOutputStream(file)) {
79              properties.store(out, name);
80          }
81      }
82  }