1 package org.apache.maven.surefire.booter;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 import org.apache.maven.surefire.api.util.TempFileManager;
32
33
34
35
36 public class SystemPropertyManager
37 {
38
39
40
41
42
43
44
45
46 public static PropertiesWrapper loadProperties( InputStream inStream )
47 throws IOException
48 {
49 try ( final InputStream stream = inStream )
50 {
51 Properties p = new Properties();
52 p.load( stream );
53 Map<String, String> map = new ConcurrentHashMap<>( p.size() );
54 for ( String key : p.stringPropertyNames() )
55 {
56 map.put( key, p.getProperty( key ) );
57 }
58 return new PropertiesWrapper( map );
59 }
60 }
61
62 private static PropertiesWrapper loadProperties( File file )
63 throws IOException
64 {
65 return loadProperties( new FileInputStream( file ) );
66 }
67
68 public static void setSystemProperties( File file )
69 throws IOException
70 {
71 PropertiesWrapper p = loadProperties( file );
72 p.setAsSystemProperties();
73 }
74
75 public static File writePropertiesFile( Properties properties, File tempDirectory, String name,
76 boolean keepForkFiles )
77 throws IOException
78 {
79 File file = TempFileManager.instance( tempDirectory ).createTempFile( name, "tmp" );
80 if ( !keepForkFiles )
81 {
82 file.deleteOnExit();
83 }
84
85 writePropertiesFile( file, name, properties );
86
87 return file;
88 }
89
90 public static void writePropertiesFile( File file, String name, Properties properties )
91 throws IOException
92 {
93 try ( FileOutputStream out = new FileOutputStream( file ) )
94 {
95 properties.store( out, name );
96 }
97 }
98 }