Documente Academic
Documente Profesional
Documente Cultură
There are several different ways to load a common property files. I list the pros & cons below.
We will discuss 2 – 4 because BEA EDocs provides a pretty good coverage on how to use Portal
Varpacks.1
1
BEA EDocs on how to use VarPacks.
http://edocs.bea.com/alui/devdoc/docs60/Customizing_the_Portal_UI/Using_Varpacks/PlumtreeDevDoc_Customizi
ng_VarPack_SampleUses.htm
1
1 A Custom Property File Loader (Java)
If you read the previous chapter on Logging, you remember that you can add application
environment variables in a web application’s configuration files. For Java platform, it is the
web.xml file. For .Net platform, it is the web.config file.
See my example “logging_and_common_property”.
/*
* Konfig.java
*/
package logging;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.Enumeration;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.apache.log4j.helpers.FileWatchdog;
/**
*
* @author rgao
2
*/
public class Konfig extends Properties {
private static boolean initialized = false;
private static Konfig instance;
private static Logger logger = Logger.getLogger(Konfig.class);
private KonfigWatchdog kdog;
3
KonfigWatchdog(String filename) {
super(filename);
}
In my example, the InitServlet class loads a specified global configuration file from the
web.xml. A singleton Konfig (subclass of java.util.Properties) is then constructed. The Konfig
class takes advantage of the FileWatchDog from the Log4J packages and automatically
refreshes itself when the property file is updated.
To access values of the configure file, use the following code snippets.
Konfig k = Konfig.getInstance();
Enumeration e = k.propertyNames();
while (e.hasMoreElements()) {
String name = e.nextElement().toString();
String val = k.getProperty(name);
out.println("<p>" + name + " = " + val + "</p>");
}
In a DotNet web application, it quite simple. You specify environment variables in the
web.config file within the <appSettings> node. When, your page-behind C# code retrieves the
name of the property file, you can create a wrapper class to represent your property file. That
way you can manipulate an object instead of individual name-value pairs of strings. In my
example, I uses a DataSet and a Xml configuration file.
<configuration>
<appSettings>
<add key="MyConfigFile" value="myconfig.xml" />
</appSettings>
<system.web>
…
</system.web>
</configuration>
Web.config
using System;
using System.Collections;
using AMS.Profile;
using System.Data;
using System.IO;
namespace logging
5
{
/// <summary>
/// MyGlobalConfig contains a DataSet for the configuration file.
/// </summary>
public class MyGlobalConfig
{
DataSet myconf;
public MyGlobalConfig(String PropertyFileName)
{
if (!File.Exists(PropertyFileName))
{
Console.WriteLine(PropertyFileName + " is not a valid
file.");
throw new Exception(PropertyFileName + " is not a
valid file error.");
}
Console.WriteLine("PropertyFileName is: " +
PropertyFileName);
Xml profile = new Xml (PropertyFileName);
this.myconf = profile.GetDataSet();
}
}
}
MyGlobalConfig.cs file
3
Jakarta Commons Configuration - http://jakarta.apache.org/commons/configuration/index.html
4
Jakarta Commons Lang - http://jakarta.apache.org/commons/lang/
5
Jakarta Commons Collection - http://jakarta.apache.org/commons/collections/
6