Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* 
   * Licensed to Aduna under one or more contributor license agreements.  
   * See the NOTICE.txt file distributed with this work for additional 
   * information regarding copyright ownership. 
   *
   * Aduna licenses this file to you under the terms of the Aduna BSD 
   * License (the "License"); you may not use this file except in compliance 
   * with the License. See the LICENSE.txt file distributed with this work 
   * for the full License.
  *
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
  * implied. See the License for the specific language governing permissions
  * and limitations under the License.
  */
 package info.aduna.app.util;
 
 import java.io.File;
 
 
 public class ConfigurationUtil {

Load configuration settings from the specified file.

Parameters:
file the file to load from
Returns:
the contents of the file as a String, or null if the file did not exist
Throws:
java.io.IOException if the contents of the file could not be read due to an I/O problem
 
 	public static String loadConfigurationContents(File file)
 		throws IOException
 	{
 		String result = null;
 		if (file.exists()) {
 			result = IOUtil.readString(file);
 		}
 		return result;
 	}

Load configuration settings from a resource on the classpath.

Parameters:
resourceName the name of the resource
Returns:
the contents of the resources as a String, or null if the resource, nor its default, could be found
Throws:
java.io.IOException if the resource could not be read due to an I/O problem
 
 	public static String loadConfigurationContents(String resourceName)
 		throws IOException
 	{
 		String result = null;
 		InputStream in = ResourceUtil.getInputStream(getResourceName(resourceName));
 		if (in == null) {
 			in = ResourceUtil.getInputStream(getDefaultResourceName(resourceName));
 		}
 		if (in != null) {
 			result = IOUtil.readString(in);
 		}
 		return result;
 	}

Load configuration properties from the specified file.

Parameters:
file the file to load from
Returns:
the contents of the file as Properties, or null if the file did not exist
Throws:
java.io.IOException if the contents of the file could not be read due to an I/O problem
 
 	public static Properties loadConfigurationProperties(File fileProperties defaults)
 		throws IOException
 	{
 		Properties result = null;
 		if (file.exists()) {
 			result = IOUtil.readProperties(filedefaults);
 		}
 		else {
 			result = new Properties(defaults);
 		}
 		return result;
 	}

Load configuration properties from a resource on the classpath.

Parameters:
resourceName the name of the resource
Returns:
the contents of the resource as Properties
Throws:
java.io.IOException if the resource could not be read due to an I/O problem
	public static Properties loadConfigurationProperties(String resourceNameProperties defaults)
	{
		Properties result = null;
		String defaultResourceName = getDefaultResourceName(resourceName);
		Properties defaultResult = null;
		InputStream in = ResourceUtil.getInputStream(defaultResourceName);
		if (in != null) {
			defaultResult = IOUtil.readProperties(indefaults);
		}
		else {
			defaultResult = new Properties(defaults);
		}
		// load application-specific overrides
		in = ResourceUtil.getInputStream(getResourceName(resourceName));
		if (in != null) {
			result = IOUtil.readProperties(indefaultResult);
		}
		else {
			result = new Properties(defaultResult);
		}
		return result;
	}
	private static String getResourceName(String resourceName) {
		if (resourceName.startsWith("/")) {
			resourceName = resourceName.substring(1);
		}
		result.append(resourceName);
		return result.toString();
	}
	private static String getDefaultResourceName(String resourceName) {
		if (resourceName.startsWith("/")) {
			resourceName = resourceName.substring(1);
		}
		result.append(resourceName);
		return result.toString();
	}

Save configuration settings to a file.

Parameters:
contents the configuration settings
file the file to write to
Throws:
java.io.IOException if the settings could not be saved because of an I/O problem
	public static void saveConfigurationContents(String contentsFile file)
	{
		if (file.getParentFile().mkdirs() || file.getParentFile().canWrite()) {
			IOUtil.writeString(contentsfile);
		}
	}

Save configuration properties to a file.

Parameters:
props the configuration properties
file the file to write to
Throws:
java.io.IOException if the settings could not be saved because of an I/O problem
	public static void saveConfigurationProperties(Properties propsFile fileboolean includeDefaults)
	{
		if (file.getParentFile().mkdirs() || file.getParentFile().canWrite()) {
			IOUtil.writeProperties(propsfileincludeDefaults);
		}
	}
New to GrepCode? Check out our FAQ X