KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > util > PropertiesUtils


1 /*
2  * $Id: PropertiesUtils.java 4259 2006-12-14 03:12:07Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.util;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.Properties JavaDoc;
20
21 import org.apache.commons.lang.StringUtils;
22 import org.mule.config.i18n.Message;
23 import org.mule.config.i18n.Messages;
24
25 import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList;
26
27 /**
28  * <code>PropertiesHelper</code> is a utility class for manipulating and filtering
29  * property Maps.
30  */

31 // @ThreadSafe
32
public class PropertiesUtils
33 {
34     // @GuardedBy(itself)
35
private static final List JavaDoc maskedProperties = new CopyOnWriteArrayList();
36
37     static
38     {
39         // When printing property lists mask password fields
40
// Users can register their own fields to mask
41
registerMaskedPropertyName("password");
42     }
43
44     /**
45      * Register a property name for masking. This will prevent certain values from
46      * leaking e.g. into debugging output or logfiles.
47      *
48      * @param name the key of the property to be masked.
49      * @throws IllegalArgumentException is name is null or empty.
50      */

51     public static void registerMaskedPropertyName(String JavaDoc name)
52     {
53         if (StringUtils.isNotEmpty(name))
54         {
55             maskedProperties.add(name);
56         }
57         else
58         {
59             throw new IllegalArgumentException JavaDoc("Cannot mask empty property name.");
60         }
61     }
62
63     /**
64      * Returns the String representation of the property value or a masked String if
65      * the property key has been registered previously via
66      * {@link #registerMaskedPropertyName(String)}.
67      *
68      * @param property a key/value pair
69      * @return String of the property value or a "masked" String that hides the
70      * contents.
71      */

72     public static String JavaDoc maskedPropertyValue(Map.Entry JavaDoc property)
73     {
74         if (maskedProperties.contains(property.getKey()))
75         {
76             return ("*****");
77         }
78         else
79         {
80             return property.getValue().toString();
81         }
82     }
83
84     /**
85      * Read in the properties from a properties file. The file may be on the file
86      * system or the classpath.
87      *
88      * @param fileName - The name of the properties file
89      * @param callingClass - The Class which is calling this method. This is used to
90      * determine the classpath.
91      * @return a java.util.Properties object containing the properties.
92      */

93     public static synchronized Properties JavaDoc loadProperties(String JavaDoc fileName, final Class JavaDoc callingClass)
94         throws IOException JavaDoc
95     {
96         InputStream JavaDoc is = IOUtils.getResourceAsStream(fileName, callingClass,
97         /* tryAsFile */true, /* tryAsUrl */false);
98         if (is == null)
99         {
100             Message error = new Message(Messages.CANT_LOAD_X_FROM_CLASSPATH_FILE, fileName);
101             throw new IOException JavaDoc(error.toString());
102         }
103
104         try
105         {
106             Properties JavaDoc props = new Properties JavaDoc();
107             props.load(is);
108             return props;
109         }
110         finally
111         {
112             is.close();
113         }
114     }
115
116     public static String JavaDoc removeXmlNamespacePrefix(String JavaDoc eleName)
117     {
118         int i = eleName.indexOf(':');
119         return (i == -1 ? eleName : eleName.substring(i + 1, eleName.length()));
120     }
121
122     public static String JavaDoc removeNamespacePrefix(String JavaDoc eleName)
123     {
124         int i = eleName.lastIndexOf('.');
125         return (i == -1 ? eleName : eleName.substring(i + 1, eleName.length()));
126     }
127
128     public static Map JavaDoc removeNamespaces(Map JavaDoc properties)
129     {
130         HashMap JavaDoc props = new HashMap JavaDoc(properties.size());
131         Map.Entry JavaDoc entry;
132         for (Iterator JavaDoc iter = properties.entrySet().iterator(); iter.hasNext();)
133         {
134             entry = (Map.Entry JavaDoc)iter.next();
135             props.put(removeNamespacePrefix((String JavaDoc)entry.getKey()), entry.getValue());
136
137         }
138         return props;
139     }
140
141     /**
142      * Will create a map of properties where the names have a prefix
143      *
144      * @param props the source set of properties
145      * @param prefix the prefix to filter on
146      * @return and new Map containing the filtered list of properties or an empty map
147      * if no properties matched the prefix
148      * @deprecated use void getPropertiesWithPrefix(Map props, String prefix, Map
149      * newProps)
150      */

151     public static Map JavaDoc getPropertiesWithPrefix(Map JavaDoc props, String JavaDoc prefix)
152     {
153         Map JavaDoc newProps = new HashMap JavaDoc();
154         for (Iterator JavaDoc iterator = props.entrySet().iterator(); iterator.hasNext();)
155         {
156             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iterator.next();
157             Object JavaDoc key = entry.getKey();
158             if (key.toString().startsWith(prefix))
159             {
160                 newProps.put(key, entry.getValue());
161             }
162         }
163         return newProps;
164     }
165
166     /**
167      * Will create a map of properties where the names have a prefix Allows the
168      * callee to supply the target map so a comarator can be set
169      *
170      * @param props the source set of properties
171      * @param prefix the prefix to filter on
172      * @param newProps return map containing the filtered list of properties or an
173      * empty map if no properties matched the prefix
174      */

175     public static void getPropertiesWithPrefix(Map JavaDoc props, String JavaDoc prefix, Map JavaDoc newProps)
176     {
177         for (Iterator JavaDoc iterator = props.entrySet().iterator(); iterator.hasNext();)
178         {
179             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iterator.next();
180             Object JavaDoc key = entry.getKey();
181             if (key.toString().startsWith(prefix))
182             {
183                 newProps.put(key, entry.getValue());
184             }
185         }
186     }
187
188     public static Map JavaDoc getPropertiesWithoutPrefix(Map JavaDoc props, String JavaDoc prefix)
189     {
190         Map JavaDoc newProps = new HashMap JavaDoc();
191         for (Iterator JavaDoc iterator = props.entrySet().iterator(); iterator.hasNext();)
192         {
193             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iterator.next();
194             Object JavaDoc key = entry.getKey();
195             if (!key.toString().startsWith(prefix))
196             {
197                 newProps.put(key, entry.getValue());
198             }
199         }
200         return newProps;
201     }
202
203     public static Properties JavaDoc getPropertiesFromQueryString(String JavaDoc query)
204     {
205         Properties JavaDoc props = new Properties JavaDoc();
206
207         if (query == null)
208         {
209             return props;
210         }
211
212         query = new StringBuffer JavaDoc(query.length() + 1).append('&').append(query).toString();
213
214         int x = 0;
215         while ((x = addProperty(query, x, props)) != -1)
216         {
217             // run
218
}
219
220         return props;
221     }
222
223     private static int addProperty(String JavaDoc query, int start, Properties JavaDoc properties)
224     {
225         int i = query.indexOf('&', start);
226         int i2 = query.indexOf('&', i + 1);
227         String JavaDoc pair;
228         if (i > -1 && i2 > -1)
229         {
230             pair = query.substring(i + 1, i2);
231         }
232         else if (i > -1)
233         {
234             pair = query.substring(i + 1);
235         }
236         else
237         {
238             return -1;
239         }
240         int eq = pair.indexOf('=');
241
242         if (eq <= 0)
243         {
244             String JavaDoc key = pair;
245             String JavaDoc value = StringUtils.EMPTY;
246             properties.setProperty(key, value);
247         }
248         else
249         {
250             String JavaDoc key = pair.substring(0, eq);
251             String JavaDoc value = (eq == pair.length() ? StringUtils.EMPTY : pair.substring(eq + 1));
252             properties.setProperty(key, value);
253         }
254         return i2;
255     }
256
257     /**
258      * @deprecated Use {@link MapUtils#toString(Map, boolean)} instead
259      */

260     public static String JavaDoc propertiesToString(Map JavaDoc props, boolean newline)
261     {
262         return MapUtils.toString(props, newline);
263     }
264
265 }
266
Popular Tags