PropertiesUtils.java
001 /*
002  *
003  * Licensed to the Apache Software Foundation (ASF) under one
004  * or more contributor license agreements.  See the NOTICE file
005  * distributed with this work for additional information
006  * regarding copyright ownership.  The ASF licenses this file
007  * to you under the Apache License, Version 2.0 (the
008  * "License"); you may not use this file except in compliance
009  * with the License.  You may obtain a copy of the License at
010  *
011  *   http://www.apache.org/licenses/LICENSE-2.0
012  *
013  * Unless required by applicable law or agreed to in writing,
014  * software distributed under the License is distributed on an
015  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016  * KIND, either express or implied.  See the License for the
017  * specific language governing permissions and limitations
018  * under the License.
019  *
020  */
021 package org.apache.qpid.util;
022 
023 import org.slf4j.Logger;
024 import org.slf4j.LoggerFactory;
025 
026 import java.io.File;
027 import java.io.FileInputStream;
028 import java.io.IOException;
029 import java.io.InputStream;
030 import java.net.URL;
031 import java.util.Iterator;
032 import java.util.Properties;
033 
034 /**
035  * PropertiesHelper defines some static methods which are useful when working with properties
036  * files.
037  *
038  <p><table id="crc"><caption>CRC Card</caption>
039  <tr><th> Responsibilities <th> Collaborations
040  <tr><td> Read properties from an input stream
041  <tr><td> Read properties from a file
042  <tr><td> Read properties from a URL
043  <tr><td> Read properties given a path to a file
044  <tr><td> Trim any whitespace from property values
045  </table>
046  */
047 public class PropertiesUtils
048 {
049     /** Used for logging. */
050     private static final Logger log = LoggerFactory.getLogger(PropertiesUtils.class);
051 
052     /**
053      * Get properties from an input stream.
054      *
055      @param is The input stream.
056      *
057      @return The properties loaded from the input stream.
058      *
059      @throws IOException If the is an I/O error reading from the stream.
060      */
061     public static Properties getProperties(InputStream isthrows IOException
062     {
063         log.debug("getProperties(InputStream): called");
064 
065         // Create properties object laoded from input stream
066         Properties properties = new Properties();
067 
068         properties.load(is);
069 
070         return properties;
071     }
072 
073     /**
074      * Get properties from a file.
075      *
076      @param file The file.
077      *
078      @return The properties loaded from the file.
079      *
080      @throws IOException If there is an I/O error reading from the file.
081      */
082     public static Properties getProperties(File filethrows IOException
083     {
084         log.debug("getProperties(File): called");
085 
086         // Open the file as an input stream
087         InputStream is = new FileInputStream(file);
088 
089         // Create properties object loaded from the stream
090         Properties properties = getProperties(is);
091 
092         // Close the file
093         is.close();
094 
095         return properties;
096     }
097 
098     /**
099      * Get properties from a url.
100      *
101      @param url The URL.
102      *
103      @return The properties loaded from the url.
104      *
105      @throws IOException If there is an I/O error reading from the URL.
106      */
107     public static Properties getProperties(URL urlthrows IOException
108     {
109         log.debug("getProperties(URL): called");
110 
111         // Open the URL as an input stream
112         InputStream is = url.openStream();
113 
114         // Create properties object loaded from the stream
115         Properties properties = getProperties(is);
116 
117         // Close the url
118         is.close();
119 
120         return properties;
121     }
122 
123     /**
124      * Get properties from a path name. The path name may refer to either a file or a URL.
125      *
126      @param pathname The path name.
127      *
128      @return The properties loaded from the file or URL.
129      *
130      @throws IOException If there is an I/O error reading from the URL or file named by the path.
131      */
132     public static Properties getProperties(String pathnamethrows IOException
133     {
134         log.debug("getProperties(String): called");
135 
136         // Check that the path is not null
137         if (pathname == null)
138         {
139             return null;
140         }
141 
142         // Check if the path is a URL
143         if (isURL(pathname))
144         {
145             // The path is a URL
146             return getProperties(new URL(pathname));
147         }
148         else
149         {
150             // Assume the path is a file name
151             return getProperties(new File(pathname));
152         }
153     }
154 
155     /**
156      * Trims whitespace from property values. This method returns a new set of properties
157      * the same as the properties specified as an argument but with any white space removed by
158      * the {@link java.lang.String#trim} method.
159      *
160      @param properties The properties to trim whitespace from.
161      *
162      @return The white space trimmed properties.
163      */
164     public static Properties trim(Properties properties)
165     {
166         Properties trimmedProperties = new Properties();
167 
168         // Loop over all the properties
169         for (Iterator i = properties.keySet().iterator(); i.hasNext();)
170         {
171             String next = (Stringi.next();
172             String nextValue = properties.getProperty(next);
173 
174             // Trim the value if it is not null
175             if (nextValue != null)
176             {
177                 nextValue.trim();
178             }
179 
180             // Store the trimmed value in the trimmed properties
181             trimmedProperties.setProperty(next, nextValue);
182         }
183 
184         return trimmedProperties;
185     }
186 
187     /**
188      * Helper method. Guesses whether a string is a URL or not. A String is considered to be a url if it begins with
189      * http:, ftp:, or uucp:.
190      *
191      @param name The string to test for being a URL.
192      *
193      @return True if the string is a URL and false if not.
194      */
195     private static boolean isURL(String name)
196     {
197         return (name.toLowerCase().startsWith("http:"|| name.toLowerCase().startsWith("ftp:")
198                 || name.toLowerCase().startsWith("uucp:"));
199     }
200 }