at.spardat.enterprise.util.Props Maven / Gradle / Ivy
/*******************************************************************************
* Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* s IT Solutions AT Spardat GmbH - initial API and implementation
*******************************************************************************/
//@(#) $Id: Props.java 2093 2007-11-28 14:23:36Z s3460 $
package at.spardat.enterprise.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;
import at.spardat.enterprise.exc.SysException;
/**
* This class is used by non-application-classes that want to access system-properties, but
* do not want to define them using -Dkey=value on the command line. Instead they
* may just specify the define
*
*
* -Denterprise.propertyFile=c:/javadev/myproject/config/ep.properties
*
*
* In this case, the properties are loaded from the filesystem. Alternatively, you
* may specify a path to load the file from the application-jar-file
*
*
* -Denterprise.propertyFile=/at/spardat/someProj/ep.properties
*
*
* by specifying a property-file that is loadable from the applications classloader. That
* classloader must be the same classloader the enterprise-classes are loaded from.
*
* Either way, all properties found in the file are inserted as system-properties before
* the getProperty-methods of this class are called.
*
* @author YSD, 28.09.2003
*/
public class Props {
static {
String enterprisePropSpec = System.getProperty("enterprise.propertyFile");
if (enterprisePropSpec != null) {
try {
/**
* 1) Assume that spec denotes a file in the file-system and try
* to load from filesystem
*/
File f = new File (enterprisePropSpec);
InputStream is = null;
Properties props = new Properties();
if (f.exists() && f.isFile()) {
/**
* load from file
*/
is = new FileInputStream (f);
} else {
/**
* 2) load properties from a property-file that is accessible from the classloader
* that is also used by enterprise.
*/
is = Props.class.getResourceAsStream(enterprisePropSpec);
}
if (is == null) {
throw new RuntimeException ("Cannot load property-file [-Denterprise.propertyFile=" + enterprisePropSpec + "]");
}
props.load(is);
try { is.close(); } catch (Exception ex) {}
/**
* Insert the loaded properties as system-properties
*/
Iterator iter = props.keySet().iterator();
while (iter.hasNext()) {
String key = (String) iter.next();
String val = props.getProperty(key);
System.setProperty(key, val);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
/**
* Returns a system-property for a given key or null if there is no such
* property set.
*
* @param key the key for the requested property
* @return the value of the property
*/
public static String getProperty (String key) {
return System.getProperty (key);
}
/**
* Returns a system-property for a given key and throws a RuntimeException
* if the property is not set.
*
* @param key the key for the requested property
* @return the value of the property
* @exception SysException if no property found
*/
public static String getPropertySafe (String key) {
String prop = System.getProperty (key);
if (prop == null) throw new SysException ("Property '" + key + "' required but not found");
return prop;
}
/**
* Returns the system-property for a given key. If there is no such property,
* a provided default-value is returned.
*
* @param key the property-key
* @param def the default-value that should be returned if the property with
* key is not found.
* @return value for the property.
*/
public static String getProperty (String key, String def) {
return System.getProperty (key, def);
}
}