View Javadoc
1   package org.codehaus.plexus.util;
2   
3   /*
4    * Copyright The Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.Properties;
20  import java.io.File;
21  import java.io.FileInputStream;
22  import java.io.InputStream;
23  import java.io.IOException;
24  import java.net.URL;
25  
26  /**
27   * Static methods to create Properties loaded from various sources.
28   *
29   * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
30   * @author <a href="mailto:mmaczka@interia.pl">Michal Maczka</a>
31   * @version $Id$
32   */
33  public class PropertyUtils
34  {
35  
36      public static Properties loadProperties( final URL url )
37          throws IOException
38      {
39          if ( url == null )
40          {
41              throw new NullPointerException( "url" );
42          }
43  
44          return loadProperties( url.openStream() );
45      }
46  
47      public static Properties loadProperties( final File file )
48          throws IOException
49      {
50          if ( file == null )
51          {
52              throw new NullPointerException( "file" );
53          }
54  
55          return loadProperties( new FileInputStream( file ) );
56      }
57  
58      public static Properties loadProperties( final InputStream is )
59          throws IOException
60      {
61          InputStream in = is;
62          try
63          {
64              final Properties properties = new Properties();
65  
66              // Make sure the properties stream is valid
67              if ( in != null )
68              {
69                  properties.load( in );
70                  in.close();
71                  in = null;
72              }
73  
74              return properties;
75          }
76          finally
77          {
78              IOUtil.close( in );
79          }
80      }
81  
82  }