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 import org.apache.maven.shared.utils.io.IOUtil; 33 34 /** 35 * 36 */ 37 public class PropertyUtils 38 { 39 40 /** 41 * The constructor. 42 */ 43 public PropertyUtils() 44 { 45 // should throw new IllegalAccessError( "Utility class" ); 46 } 47 48 /** 49 * @param url The URL which should be used to load the properties. 50 * 51 * @return The loaded properties. 52 * 53 * @deprecated As of 3.1.0, please use method {@link #loadOptionalProperties(java.net.URL)}. This method should not 54 * be used as it suppresses exceptions silently when loading properties fails and returns {@code null} instead of an 55 * empty {@code Properties} instance when the given {@code URL} is {@code null}. 56 */ 57 @Deprecated 58 public static java.util.Properties loadProperties( @Nonnull URL url ) 59 { 60 try 61 { 62 return loadProperties( url.openStream() ); 63 } 64 catch ( Exception e ) 65 { 66 // ignore 67 } 68 return null; 69 } 70 71 /** 72 * @param file The file from which the properties will be loaded. 73 * 74 * @return The loaded properties. 75 * 76 * @deprecated As of 3.1.0, please use method {@link #loadOptionalProperties(java.io.File)}. This method should not 77 * be used as it suppresses exceptions silently when loading properties fails and returns {@code null} instead of an 78 * empty {@code Properties} instance when the given {@code File} is {@code null}. 79 */ 80 @Deprecated 81 public static Properties loadProperties( @Nonnull File file ) 82 { 83 try 84 { 85 return loadProperties( new FileInputStream( file ) ); 86 } 87 catch ( Exception e ) 88 { 89 // ignore 90 } 91 return null; 92 } 93 94 /** 95 * @param is {@link InputStream} 96 * 97 * @return The loaded properties. 98 * 99 * @deprecated As of 3.1.0, please use method {@link #loadOptionalProperties(java.io.InputStream)}. This method 100 * should not be used as it suppresses exceptions silently when loading properties fails. 101 */ 102 @Deprecated 103 public static Properties loadProperties( @Nullable InputStream is ) 104 { 105 try 106 { 107 // to make this the same behaviour as the others we should really return null on any error 108 Properties result = new Properties(); 109 if ( is != null ) 110 { 111 try 112 { 113 result.load( is ); 114 } 115 catch ( IOException e ) 116 { 117 // ignore 118 } 119 } 120 return result; 121 } 122 catch ( Exception e ) 123 { 124 // ignore 125 } 126 finally 127 { 128 IOUtil.close( is ); 129 } 130 return null; 131 } 132 133 /** 134 * Loads {@code Properties} from a given {@code URL}. 135 * <p> 136 * If the given {@code URL} is not {@code null}, it is asserted to represent a valid and loadable properties 137 * resource. 138 * </p> 139 * 140 * @param url The {@code URL} of the properties resource to load or {@code null}. 141 * 142 * @return The loaded properties or an empty {@code Properties} instance if {@code url} is {@code null}. 143 * 144 * @since 3.1.0 145 */ 146 @Nonnull public static Properties loadOptionalProperties( final @Nullable URL url ) 147 { 148 InputStream in = null; 149 try 150 { 151 final Properties properties = new Properties(); 152 153 if ( url != null ) 154 { 155 in = url.openStream(); 156 properties.load( in ); 157 in.close(); 158 in = null; 159 } 160 161 return properties; 162 } 163 catch ( final IOException e ) 164 { 165 throw new AssertionError( e ); 166 } 167 finally 168 { 169 IOUtil.close( in ); 170 } 171 } 172 173 /** 174 * Loads {@code Properties} from a given {@code File}. 175 * <p> 176 * If the given {@code File} is not {@code null}, it is asserted to represent a valid and loadable properties 177 * resource. 178 * </p> 179 * 180 * @param file The {@code File} of the properties resource to load or {@code null}. 181 * 182 * @return The loaded properties or an empty {@code Properties} instance if {@code file} is {@code null}. 183 * 184 * @since 3.1.0 185 */ 186 @Nonnull public static Properties loadOptionalProperties( final @Nullable File file ) 187 { 188 InputStream in = null; 189 try 190 { 191 final Properties properties = new Properties(); 192 193 if ( file != null ) 194 { 195 in = new FileInputStream( file ); 196 properties.load( in ); 197 in.close(); 198 in = null; 199 } 200 201 return properties; 202 } 203 catch ( final IOException e ) 204 { 205 throw new AssertionError( e ); 206 } 207 finally 208 { 209 IOUtil.close( in ); 210 } 211 } 212 213 /** 214 * Loads {@code Properties} from a given {@code InputStream}. 215 * <p> 216 * If the given {@code InputStream} is not {@code null}, it is asserted to represent a valid and loadable properties 217 * resource. 218 * </p> 219 * 220 * @param inputStream The {@code InputStream} of the properties resource to load or {@code null}. 221 * 222 * @return The loaded properties or an empty {@code Properties} instance if {@code inputStream} is {@code null}. 223 * 224 * @since 3.1.0 225 */ 226 @Nonnull public static Properties loadOptionalProperties( final @Nullable InputStream inputStream ) 227 { 228 InputStream in = null; 229 try 230 { 231 final Properties properties = new Properties(); 232 233 if ( inputStream != null ) 234 { 235 in = inputStream; 236 properties.load( in ); 237 in.close(); 238 in = null; 239 } 240 241 return properties; 242 } 243 catch ( final IOException e ) 244 { 245 throw new AssertionError( e ); 246 } 247 finally 248 { 249 IOUtil.close( in ); 250 } 251 } 252 253 }