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.net.URL; 27 import java.util.Properties; 28 29 import javax.annotation.Nonnull; 30 import javax.annotation.Nullable; 31 32 /** 33 * Static utility methods for loading properties. 34 */ 35 public class PropertyUtils 36 { 37 38 /** 39 * The constructor. 40 * 41 * @deprecated This is a utility class with only static methods. Don't create instances of it. 42 */ 43 @Deprecated 44 public PropertyUtils() 45 { 46 } 47 48 /** 49 * @param url the URL which should be used to load the properties 50 * @return the loaded properties 51 * @deprecated use {@link #loadOptionalProperties(java.net.URL)} instead. This method should not 52 * be used as it suppresses exceptions silently when loading properties fails and returns {@code null} 53 * instead of an empty {@code Properties} instance when the given {@code URL} is {@code null}. 54 */ 55 @Deprecated 56 public static java.util.Properties loadProperties( @Nonnull URL url ) 57 { 58 try ( InputStream in = url.openStream() ) 59 { 60 return loadProperties( in ); 61 } 62 catch ( Exception e ) 63 { 64 // ignore 65 } 66 return null; 67 } 68 69 /** 70 * @param file the file from which the properties will be loaded 71 * @return the loaded properties 72 * @deprecated use {@link #loadOptionalProperties(java.io.File)} instead. This method should not 73 * be used as it suppresses exceptions silently when loading properties fails and returns {@code null} 74 * instead of an empty {@code Properties} instance when the given {@code File} is {@code null}. 75 */ 76 @Deprecated 77 public static Properties loadProperties( @Nonnull File file ) 78 { 79 try ( InputStream in = new FileInputStream( file ) ) 80 { 81 return loadProperties( in ); 82 } 83 catch ( Exception e ) 84 { 85 // ignore 86 } 87 return null; 88 } 89 90 /** 91 * Loads {@code Properties} from an {@code InputStream} and closes the stream. 92 * In a future release, this will no longer close the stream, so callers 93 * should close the stream themselves. 94 * 95 * @param is {@link InputStream} 96 * @return the loaded properties 97 * @deprecated use {@link #loadOptionalProperties(java.io.InputStream)} instead. This method 98 * should not be used as it suppresses exceptions silently when loading properties fails. 99 */ 100 @Deprecated 101 public static Properties loadProperties( @Nullable InputStream is ) 102 { 103 try 104 { 105 Properties result = new Properties(); 106 if ( is != null ) 107 { 108 try ( InputStream in = is ) 109 { 110 result.load( in ); 111 } 112 catch ( IOException e ) 113 { 114 // ignore 115 } 116 } 117 return result; 118 } 119 catch ( Exception e ) 120 { 121 // ignore 122 } 123 return null; 124 } 125 126 /** 127 * Loads {@code Properties} from a given {@code URL}. 128 * <p> 129 * If the given {@code URL} is {@code null} or the properties can't be read, an empty properties object is returned. 130 * </p> 131 * 132 * @param url the {@code URL} of the properties resource to load or {@code null} 133 * @return the loaded properties or an empty {@code Properties} instance if properties fail to load 134 * @since 3.1.0 135 */ 136 @Nonnull 137 public static Properties loadOptionalProperties( final @Nullable URL url ) 138 { 139 140 Properties properties = new Properties(); 141 if ( url != null ) 142 { 143 try ( InputStream in = url.openStream() ) 144 { 145 properties.load( in ); 146 } 147 catch ( IllegalArgumentException | IOException ex ) 148 { 149 // ignore and return empty properties 150 } 151 } 152 return properties; 153 } 154 155 /** 156 * Loads {@code Properties} from a {@code File}. 157 * <p> 158 * If the given {@code File} is {@code null} or the properties file can't be read, an empty properties object is 159 * returned. 160 * </p> 161 * 162 * @param file the {@code File} of the properties resource to load or {@code null} 163 * @return the loaded properties or an empty {@code Properties} instance if properties fail to load 164 * @since 3.1.0 165 */ 166 @Nonnull 167 public static Properties loadOptionalProperties( final @Nullable File file ) 168 { 169 Properties properties = new Properties(); 170 if ( file != null ) 171 { 172 try ( InputStream in = new FileInputStream( file ) ) 173 { 174 properties.load( in ); 175 } 176 catch ( IllegalArgumentException | IOException ex ) 177 { 178 // ignore and return empty properties 179 } 180 } 181 182 return properties; 183 184 } 185 186 /** 187 * Loads {@code Properties} from an {@code InputStream} and closes the stream. 188 * If the given {@code InputStream} is {@code null} or the properties can't be read, an empty properties object is 189 * returned. In a future release, this will no longer close the stream, so callers 190 * should close the stream themselves. 191 * 192 * @param inputStream the properties resource to load or {@code null} 193 * @return the loaded properties or an empty {@code Properties} instance if properties fail to load 194 * @since 3.1.0 195 */ 196 @Nonnull 197 public static Properties loadOptionalProperties( final @Nullable InputStream inputStream ) 198 { 199 200 Properties properties = new Properties(); 201 202 if ( inputStream != null ) 203 { 204 try ( InputStream in = inputStream ) // reassign inputStream to autoclose 205 { 206 properties.load( in ); 207 } 208 catch ( IllegalArgumentException | IOException ex ) 209 { 210 // ignore and return empty properties 211 } 212 } 213 214 return properties; 215 216 } 217 218 }