View Javadoc
1   package org.apache.maven.shared.utils;
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 java.io.File;
23  import java.io.FileInputStream;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.util.Properties;
27  
28  import javax.annotation.Nonnull;
29  import javax.annotation.Nullable;
30  
31  import org.apache.maven.shared.utils.io.IOUtil;
32  
33  /**
34   * 
35   */
36  public class PropertyUtils
37  {
38  
39      /**
40       * The constructor.
41       */
42      public PropertyUtils()
43      {
44          // should throw new IllegalAccessError( "Utility class" );
45      }
46  
47      /**
48       * @param url The URL which should be used to load the properties.
49       * @return The loaded properties.
50       */
51      public static java.util.Properties loadProperties( @Nonnull java.net.URL url )
52      {
53          try
54          {
55              return loadProperties( url.openStream() );
56          }
57          catch ( Exception e )
58          {
59              // ignore
60          }
61          return null;
62      }
63  
64      /**
65       * @param file The file from which the properties will be loaded.
66       * @return The loaded properties.
67       */
68      public static Properties loadProperties( @Nonnull File file )
69      {
70          try
71          {
72              return loadProperties( new FileInputStream( file ) );
73          }
74          catch ( Exception e )
75          {
76              // ignore
77          }
78          return null;
79      }
80  
81      /**
82       * @param is {@link InputStream}
83       * @return The loaded properties.
84       */
85      public static Properties loadProperties( @Nullable InputStream is )
86      {
87          try
88          {
89              // to make this the same behaviour as the others we should really return null on any error
90              Properties result = new Properties();
91              if ( is != null )
92              {
93                  try
94                  {
95                      result.load( is );
96                  }
97                  catch ( IOException e )
98                  {
99                      // ignore
100                 }
101             }
102             return result;
103         }
104         catch ( Exception e )
105         {
106             // ignore
107         }
108         finally
109         {
110             IOUtil.close( is );
111         }
112         return null;
113     }
114 
115 }