View Javadoc

1   package org.alfresco.maven.plugin.amp.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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          // prevent instantiation
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         // This can also be done using InterpolationFilterReader,
106         // but it requires reparsing the file over and over until
107         // it doesn't change.
108 
109         String v = p.getProperty( k );
110         String ret = "";
111         int idx, idx2;
112 
113         while ( ( idx = v.indexOf( "${" ) ) >= 0 )
114         {
115             // append prefix to result
116             ret += v.substring( 0, idx );
117 
118             // strip prefix from original
119             v = v.substring( idx + 2 );
120 
121             // if no matching } then bail
122             if ( ( idx2 = v.indexOf( '}' ) ) < 0 )
123             {
124                 break;
125             }
126 
127             // strip out the key and resolve it
128             // resolve the key/value for the ${statement}
129             String nk = v.substring( 0, idx2 );
130             v = v.substring( idx2 + 1 );
131             String nv = p.getProperty( nk );
132 
133             // try global environment..
134             if ( nv == null )
135             {
136                 nv = System.getProperty( nk );
137             }
138 
139             // if the key cannot be resolved,
140             // leave it alone ( and don't parse again )
141             // else prefix the original string with the
142             // resolved property ( so it can be parsed further )
143             // taking recursion into account.
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 }