View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.shared.utils;
20  
21  import javax.annotation.Nonnull;
22  import javax.annotation.Nullable;
23  
24  import java.io.File;
25  import java.io.FileInputStream;
26  import java.io.IOException;
27  import java.io.InputStream;
28  import java.net.URL;
29  import java.util.Properties;
30  
31  /**
32   * Static utility methods for loading properties.
33   */
34  public class PropertyUtils {
35  
36      /**
37       * The constructor.
38       *
39       * @deprecated This is a utility class with only static methods. Don't create instances of it.
40       */
41      @Deprecated
42      public PropertyUtils() {}
43  
44      /**
45       * @param url the URL which should be used to load the properties
46       * @return the loaded properties
47       * @deprecated use {@link #loadOptionalProperties(java.net.URL)} instead. This method should not
48       *             be used as it suppresses exceptions silently when loading properties fails and returns {@code null}
49       *             instead of an empty {@code Properties} instance when the given {@code URL} is {@code null}.
50       */
51      @Deprecated
52      public static java.util.Properties loadProperties(@Nonnull URL url) {
53          try (InputStream in = url.openStream()) {
54              return loadProperties(in);
55          } catch (Exception e) {
56              // ignore
57          }
58          return null;
59      }
60  
61      /**
62       * @param file the file from which the properties will be loaded
63       * @return the loaded properties
64       * @deprecated use {@link #loadOptionalProperties(java.io.File)} instead. This method should not
65       *             be used as it suppresses exceptions silently when loading properties fails and returns {@code null}
66       *             instead of an empty {@code Properties} instance when the given {@code File} is {@code null}.
67       */
68      @Deprecated
69      public static Properties loadProperties(@Nonnull File file) {
70          try (InputStream in = new FileInputStream(file)) {
71              return loadProperties(in);
72          } catch (Exception e) {
73              // ignore
74          }
75          return null;
76      }
77  
78      /**
79       * Loads {@code Properties} from an {@code InputStream} and closes the stream.
80       * In a future release, this will no longer close the stream, so callers
81       * should close the stream themselves.
82       *
83       * @param is {@link InputStream}
84       * @return the loaded properties
85       * @deprecated use {@link #loadOptionalProperties(java.io.InputStream)} instead. This method
86       *             should not be used as it suppresses exceptions silently when loading properties fails.
87       */
88      @Deprecated
89      public static Properties loadProperties(@Nullable InputStream is) {
90          try {
91              Properties result = new Properties();
92              if (is != null) {
93                  try (InputStream in = is) {
94                      result.load(in);
95                  } catch (IOException e) {
96                      // ignore
97                  }
98              }
99              return result;
100         } catch (Exception e) {
101             // ignore
102         }
103         return null;
104     }
105 
106     /**
107      * Loads {@code Properties} from a given {@code URL}.
108      * <p>
109      * If the given {@code URL} is {@code null} or the properties can't be read, an empty properties object is returned.
110      * </p>
111      *
112      * @param url the {@code URL} of the properties resource to load or {@code null}
113      * @return the loaded properties or an empty {@code Properties} instance if properties fail to load
114      * @since 3.1.0
115      */
116     @Nonnull
117     public static Properties loadOptionalProperties(final @Nullable URL url) {
118 
119         Properties properties = new Properties();
120         if (url != null) {
121             try (InputStream in = url.openStream()) {
122                 properties.load(in);
123             } catch (IllegalArgumentException | IOException ex) {
124                 // ignore and return empty properties
125             }
126         }
127         return properties;
128     }
129 
130     /**
131      * Loads {@code Properties} from a {@code File}.
132      * <p>
133      * If the given {@code File} is {@code null} or the properties file can't be read, an empty properties object is
134      * returned.
135      * </p>
136      *
137      * @param file the {@code File} of the properties resource to load or {@code null}
138      * @return the loaded properties or an empty {@code Properties} instance if properties fail to load
139      * @since 3.1.0
140      */
141     @Nonnull
142     public static Properties loadOptionalProperties(final @Nullable File file) {
143         Properties properties = new Properties();
144         if (file != null) {
145             try (InputStream in = new FileInputStream(file)) {
146                 properties.load(in);
147             } catch (IllegalArgumentException | IOException ex) {
148                 // ignore and return empty properties
149             }
150         }
151 
152         return properties;
153     }
154 
155     /**
156      * Loads {@code Properties} from an {@code InputStream} and closes the stream.
157      * If the given {@code InputStream} is {@code null} or the properties can't be read, an empty properties object is
158      * returned. In a future release, this will no longer close the stream, so callers
159      * should close the stream themselves.
160      *
161      * @param inputStream the properties resource to load or {@code null}
162      * @return the loaded properties or an empty {@code Properties} instance if properties fail to load
163      * @since 3.1.0
164      */
165     @Nonnull
166     public static Properties loadOptionalProperties(final @Nullable InputStream inputStream) {
167 
168         Properties properties = new Properties();
169 
170         if (inputStream != null) {
171             try (InputStream in = inputStream) // reassign inputStream to autoclose
172             {
173                 properties.load(in);
174             } catch (IllegalArgumentException | IOException ex) {
175                 // ignore and return empty properties
176             }
177         }
178 
179         return properties;
180     }
181 }