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
32
33
34 public class SystemPropertyManager
35 {
36
37
38
39
40
41
42
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
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 );
62 }
63 }
64
65 private static PropertiesWrapper loadProperties( File file )
66 throws IOException
67 {
68 return loadProperties( new FileInputStream( file ) );
69 }
70
71
72 public static void setSystemProperties( File file )
73 throws IOException
74 {
75 PropertiesWrapper p = loadProperties( file );
76 p.setAsSystemProperties();
77 }
78
79 public static File writePropertiesFile( Properties properties, File tempDirectory, String name,
80 boolean keepForkFiles )
81 throws IOException
82 {
83 File file = File.createTempFile( name, "tmp", tempDirectory );
84 if ( !keepForkFiles )
85 {
86 file.deleteOnExit();
87 }
88
89 writePropertiesFile( file, name, properties );
90
91 return file;
92 }
93
94 public static void writePropertiesFile( File file, String name, Properties properties )
95 throws IOException
96 {
97 FileOutputStream out = new FileOutputStream( file );
98
99 try
100 {
101 properties.store( out, name );
102 }
103 finally
104 {
105 out.close();
106 }
107 }
108
109 public static void close( InputStream inputStream )
110 {
111 if ( inputStream == null )
112 {
113 return;
114 }
115
116 try
117 {
118 inputStream.close();
119 }
120 catch ( IOException ex )
121 {
122
123 }
124 }
125
126
127 }