Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
Nyla Solutions Global Java API provides support for basic application
utilities (application configuration, data encryption, debugger and text
processing).
* This class provides a central mechanism for applications to access
* key/value property settings and encrypted passwords.
*
* There are several ways to set to specify the
* configuration property file location.
*
Add file config.properties to CLASSPATH.
*This file will be loaded as a Java resource bundle.
*
Add the JVM argument -Dconfig.properties where the value is equal to
*the location of configuration file.
*
*Example:
*-Dconfig.properties=/dev/configurations/files/system/config.properties
* There are methods to get the String value property such as Config.getProperty(key) method
* or get an expected property value of a type such as Integer, Boolean, etc.
*
* JVM argument system properties can also by accessed by adding the
* following to the configuration file;
*
* nyla.solutions.global.util.Config.mergeSystemProperties=true
*
*
* Values in the System properties can be set with values for the configuration by using the following
* solutions.global.util.Config.setSystemProperties=true
*
* It also supports formatting several property values into a single property
* by the added the following property;
*
*
* nyla.solutions.global.util.Config.useFormatting=true
*
* Example
* machineName=localhost
* host=${machineName}.mycompany.com
*
*
* By default the configuration is read only once when the
* application is initialized. Add the following to the
* configuration property file to always reload the property whenever
* a getProperty... method is called.
*
* nyla.solutions.global.util.Config.alwaysReloadProperties=true
*
*
* @author Gregory Green
*/
public class Config
{
public static final String RESOURCE_BUNDLE_NAME = "config";
/**
* SMP_PROPERTY_FILE
*/
public static final String SYS_PROPERTY = "config.properties";
public static final String DEFAULT_PROP_FILE_NAME = SYS_PROPERTY;
/**
* Property may reference properties in example ${prop.name}+somethingElse
* @param property the property
* @return the formatted value
* @throws ConfigException when format exception occur
*/
public static String interpret(String property)
{
try
{
property = Cryption.interpret(property);
if(property != null && property.indexOf(Styles.DEFAULT_PREFIX) > -1)
{
property = Text.format(property, Config.getProperties());
}
}
catch (FormatException e)
{
throw new ConfigException("Format exception for \""+property+"\"",e);
}
return Cryption.interpret(property);
}// --------------------------------------------------------
/**
* @return the system property file
*/
private static String getSystemPropertyFile()
{
String file = System.getProperty(SYS_PROPERTY);
if (file == null || file.length() == 0)
{
try
{
// file = (String) new InitialContext().lookup(SYS_PROPERTY);
}
catch (Throwable e)
{
throw new SystemException(e);
}
}
return file;
}// -----------------------------------------------------------
/**
* @return resource bundle name
*/
static String getBundleName()
{
return RESOURCE_BUNDLE_NAME;
}// -------------------------------------------------------------
/**
* Load the configuration properties from the properties file.
*
*
*
* Caller must test to ensure that properties is Non-null.
*
* @throws IllegalArgumentException Translates an IOException from reading
*
* the properties file into a run time exception.
*/
private static synchronized void loadProperties()
{
// If multiple threads are waiting to invoke this method only allow
// the first one to do so. The rest should just return since the first
// thread through took care of loading the properties.
try
{
String file = getSystemPropertyFile();
if (file != null && file.length() > 0)
{
// System.out.println("CONFIG: LOADING CONFIG properties from "+
// file);
FileInputStream fis = new FileInputStream(file);
try
{
properties = new Properties();
// Load the properties object from the properties file
properties.load(fis);
// System.out.println("CONFIG: FINISHED LOADING CONFIG properties from "+
// file);
configSourceLocation = file;
}
catch (Exception e)
{
e.printStackTrace();
throw new ConfigException(e.toString());
}
finally
{
if (fis != null)
fis.close(); // Always close the file, even on exception
}
}
else
{
properties = new Properties();
String bundleName = getBundleName();
// try to get properties from resource bundle
ResourceBundle rb = ResourceBundle.getBundle(bundleName);
URL url = Config.class.getResource(
bundleName + ".properties");
if(url != null)
configSourceLocation = url.toString();
else
configSourceLocation = bundleName + ".properties";
Enumeration> keys = rb.getKeys();
Object k = null;
while (keys.hasMoreElements())
{
k = keys.nextElement();
properties.put(k, rb.getString(k + ""));
}
}// end els load from resource bundle
String reloadBool = properties.getProperty(Config.class.getName()
+ ".alwaysReload");
if (reloadBool == null || reloadBool.length() == 0)
{
// throw new ConfigException("System property
// "+Constants.ALWAY_RELOAD_PROP+" is not set.");
alwaysReload = false;
}
else
{
alwaysReload = Boolean.valueOf(reloadBool).booleanValue();
}
//Merge SystemProperteis
mergeSystemProperties = Boolean.valueOf(properties.getProperty(Config.class.getName()
+ ".mergeSystemProperties", "false")).booleanValue();
if(mergeSystemProperties)
{
//add system properties
properties.putAll(System.getProperties());
}
// process formatting
String propName = Config.class.getName() + ".useFormatting";
String useFormattingText = properties.getProperty(propName);
// System.out.println("CONFIG: "+propName+"="+useFormattingText);
if (useFormattingText == null || useFormattingText.length() == 0)
{
// throw new ConfigException("System property
// "+Constants.ALWAY_RELOAD_PROP+" is not set.");
useFormatting = false;
}
else
{
useFormatting = Boolean.valueOf(useFormattingText)
.booleanValue();
}
if (useFormatting)
{
// System.out.println("CONFIG: FORMATTING MAP CONFIG properties ");
// format map (note this can be an expensive operation)
Text.formatMap(properties);
// System.out.println("CONFIG: FORMATTED MAP CONFIG properties ");
}
propName = Config.class.getName() + ".setSystemProperties";
setSystemProperties = Boolean.valueOf(properties.getProperty(propName,"false")).booleanValue();
//Set system properties with values from configuration
if(setSystemProperties)
{
Set