1 | |
package org.apache.maven.plugin.assembly.utils; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.maven.plugin.assembly.format.AssemblyFormattingException; |
23 | |
import org.codehaus.plexus.interpolation.InterpolationException; |
24 | |
import org.codehaus.plexus.interpolation.PropertiesBasedValueSource; |
25 | |
import org.codehaus.plexus.interpolation.StringSearchInterpolator; |
26 | |
import org.codehaus.plexus.util.IOUtil; |
27 | |
|
28 | |
import java.io.File; |
29 | |
import java.io.FileInputStream; |
30 | |
import java.io.FileNotFoundException; |
31 | |
import java.io.IOException; |
32 | |
import java.util.Enumeration; |
33 | |
import java.util.Properties; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public final class PropertyUtils |
40 | |
{ |
41 | |
private PropertyUtils() |
42 | 0 | { |
43 | |
|
44 | 0 | } |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
public static Properties getInterpolatedPropertiesFromFile( final File propfile, final boolean fail, |
58 | |
final boolean useSystemProps ) |
59 | |
throws IOException, AssemblyFormattingException |
60 | |
{ |
61 | |
Properties props; |
62 | |
|
63 | 6 | if ( useSystemProps ) |
64 | |
{ |
65 | 1 | props = new Properties( System.getProperties() ); |
66 | |
} |
67 | |
else |
68 | |
{ |
69 | 5 | props = new Properties(); |
70 | |
} |
71 | |
|
72 | 6 | if ( propfile.exists() ) |
73 | |
{ |
74 | 5 | final FileInputStream inStream = new FileInputStream( propfile ); |
75 | |
try |
76 | |
{ |
77 | 5 | props.load( inStream ); |
78 | |
} |
79 | |
finally |
80 | |
{ |
81 | 5 | IOUtil.close( inStream ); |
82 | 5 | } |
83 | 5 | } |
84 | 1 | else if ( fail ) |
85 | |
{ |
86 | 1 | throw new FileNotFoundException( propfile.toString() ); |
87 | |
} |
88 | |
|
89 | 5 | final StringSearchInterpolator interpolator = new StringSearchInterpolator(); |
90 | 5 | interpolator.addValueSource( new PropertiesBasedValueSource( props ) ); |
91 | |
|
92 | |
for ( @SuppressWarnings( "rawtypes" ) |
93 | 5 | final Enumeration n = props.propertyNames(); n.hasMoreElements(); ) |
94 | |
{ |
95 | 66 | final String key = (String) n.nextElement(); |
96 | 66 | String value = props.getProperty( key ); |
97 | |
try |
98 | |
{ |
99 | 66 | value = interpolator.interpolate( value ); |
100 | |
} |
101 | 0 | catch ( final InterpolationException e ) |
102 | |
{ |
103 | 0 | throw new AssemblyFormattingException( "Failed to interpolate property value: '" + value |
104 | |
+ "' for key: '" + key + "'. Reason: " + e.getMessage(), e ); |
105 | 66 | } |
106 | |
|
107 | 66 | props.setProperty( key, value ); |
108 | 66 | } |
109 | |
|
110 | 5 | return props; |
111 | |
} |
112 | |
|
113 | |
} |