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 | |
|
40 | |
public final class PropertyUtils |
41 | |
{ |
42 | |
private PropertyUtils() |
43 | 0 | { |
44 | |
|
45 | 0 | } |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
public static Properties getInterpolatedPropertiesFromFile( File propfile, boolean fail, boolean useSystemProps ) |
56 | |
throws IOException, AssemblyFormattingException |
57 | |
{ |
58 | |
Properties props; |
59 | |
|
60 | 12 | if ( useSystemProps ) |
61 | |
{ |
62 | 2 | props = new Properties( System.getProperties() ); |
63 | |
} |
64 | |
else |
65 | |
{ |
66 | 10 | props = new Properties(); |
67 | |
} |
68 | |
|
69 | 12 | if ( propfile.exists() ) |
70 | |
{ |
71 | 10 | FileInputStream inStream = new FileInputStream( propfile ); |
72 | |
try |
73 | |
{ |
74 | 10 | props.load( inStream ); |
75 | |
} |
76 | |
finally |
77 | |
{ |
78 | 10 | IOUtil.close( inStream ); |
79 | 10 | } |
80 | |
} |
81 | 2 | else if ( fail ) |
82 | |
{ |
83 | 2 | throw new FileNotFoundException( propfile.toString() ); |
84 | |
} |
85 | |
|
86 | 10 | StringSearchInterpolator interpolator = new StringSearchInterpolator(); |
87 | 10 | interpolator.addValueSource( new PropertiesBasedValueSource( props ) ); |
88 | |
|
89 | 10 | for ( Enumeration n = props.propertyNames(); n.hasMoreElements(); ) |
90 | |
{ |
91 | 128 | String key = (String) n.nextElement(); |
92 | 128 | String value = props.getProperty( key ); |
93 | |
try |
94 | |
{ |
95 | 128 | value = interpolator.interpolate( value ); |
96 | |
} |
97 | 0 | catch ( InterpolationException e ) |
98 | |
{ |
99 | 0 | throw new AssemblyFormattingException( "Failed to interpolate property value: '" + value + "' for key: '" + key + "'. Reason: " + e.getMessage(), e ); |
100 | 128 | } |
101 | |
|
102 | 128 | props.setProperty( key, value ); |
103 | |
} |
104 | |
|
105 | 10 | return props; |
106 | |
} |
107 | |
|
108 | |
} |