1 package org.alfresco.maven.plugin.amp.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.codehaus.plexus.util.IOUtil;
23
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.FileNotFoundException;
27 import java.io.IOException;
28 import java.util.Enumeration;
29 import java.util.Properties;
30
31 /***
32 * @author <a href="mailto:kenney@neonics.com">Kenney Westerhof</a>
33 * @version $Id: PropertyUtils.java 565036 2007-08-12 10:26:14Z snicoll $
34 * @todo this is duplicated from the resources plugin - migrate to plexus-util
35 */
36 public final class PropertyUtils
37 {
38 private PropertyUtils()
39 {
40
41 }
42
43 /***
44 * Reads a property file, resolving all internal variables.
45 *
46 * @param propfile The property file to load
47 * @param fail wheter to throw an exception when the file cannot be loaded or to return null
48 * @param useSystemProps wheter to incorporate System.getProperties settings into the returned Properties object.
49 * @return the loaded and fully resolved Properties object
50 * @throws IOException if an error failed while loading the properties
51 */
52 public static Properties loadPropertyFile( File propfile, boolean fail, boolean useSystemProps )
53 throws IOException
54 {
55 Properties props = new Properties();
56
57 if ( useSystemProps )
58 {
59 props = new Properties( System.getProperties() );
60 }
61
62 if ( propfile.exists() )
63 {
64 FileInputStream inStream = new FileInputStream( propfile );
65 try
66 {
67 props.load( inStream );
68 }
69 finally
70 {
71 IOUtil.close( inStream );
72 }
73 }
74 else if ( fail )
75 {
76 throw new FileNotFoundException( propfile.toString() );
77 }
78
79 for ( Enumeration n = props.propertyNames(); n.hasMoreElements(); )
80 {
81 String k = (String) n.nextElement();
82 props.setProperty( k, PropertyUtils.getPropertyValue( k, props ) );
83 }
84
85 return props;
86 }
87
88
89 /***
90 * Retrieves a property value, replacing values like ${token}
91 * using the Properties to look them up.
92 * <p/>
93 * It will leave unresolved properties alone, trying for System
94 * properties, and implements reparsing (in the case that
95 * the value of a property contains a key), and will
96 * not loop endlessly on a pair like
97 * test = ${test}.
98 *
99 * @param k the token
100 * @param p the properties containing the filter values
101 * @return the value
102 */
103 private static String getPropertyValue( String k, Properties p )
104 {
105
106
107
108
109 String v = p.getProperty( k );
110 String ret = "";
111 int idx, idx2;
112
113 while ( ( idx = v.indexOf( "${" ) ) >= 0 )
114 {
115
116 ret += v.substring( 0, idx );
117
118
119 v = v.substring( idx + 2 );
120
121
122 if ( ( idx2 = v.indexOf( '}' ) ) < 0 )
123 {
124 break;
125 }
126
127
128
129 String nk = v.substring( 0, idx2 );
130 v = v.substring( idx2 + 1 );
131 String nv = p.getProperty( nk );
132
133
134 if ( nv == null )
135 {
136 nv = System.getProperty( nk );
137 }
138
139
140
141
142
143
144 if ( nv == null || nv.equals( k ) )
145 {
146 ret += "${" + nk + "}";
147 }
148 else
149 {
150 v = nv + v;
151 }
152 }
153 return ret + v;
154 }
155 }