1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
34
35 public class SystemPropertyManager {
36
37
38
39
40
41
42
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 }