NoKideen wrote:
On Tuesday 27 September 2005 15:36, Seak, Teng-Fong wrote:
My webapp needs some application string variables for
configuration. For the moment, I hard-code them as class static
properties and compiled. But I'd like to know if there's any method to
define such variables in a text file, something like the global.asa in
ASP where we could simply write something like this withing the
application_onstart subroutine:
application("myvar") = "my value"
any better idea than this one ? without modification web.xml :-D
this is example to to read
-------------------------------------------------
import lib.ConfLineSeparator;
...
...
ConfLineSeparator c = new ConfLineSeparator("/whereis/thefile/file.conf");
out.println(c.getConf("dbPwd","VALUEifMissing"));
...
...
this is example data file /whereis/thefile/file.conf
----------------------------------------------------------
#CONFig Text Mode,
#Place many Configuration here
#Format is : property=value or property = value
#there is no SPACE, space is WASTE of String :-p
#value is value not "value" or 'value'
#all Chars are case sensitive
#Add # char to comment
# Db Conf
dbPwd=secret
Try this API Class file
-----------------------------------------------------
package lib;
import java.io.*;
import java.util.Vector;
public class ConfLineSeparator {
String[] all;
int i=0;
String currentProp, currentVal;
Vector p;
Vector v;
public ConfLineSeparator(String file) {
all = new String[2000]; //maximum
p = new Vector();
v = new Vector();
readFile(file);
processNow();
}
public void readFile(String file) {
debug(" Reading "+file);
try {
i=0;
FileInputStream fstream = new FileInputStream(file);
DataInputStream in = new DataInputStream(fstream);
while (in.available() !=0){
i++;
in.readLine();
}
in.close();
all = new String[i];
i=0;
fstream = new FileInputStream(file);
in = new DataInputStream(fstream);
while (in.available() !=0){
all[i++] = in.readLine();
debug(" Reading Got this data "+all[i-1]);
}
in.close();
} catch (Exception e) {
debug(" Reading Exception "+e.getMessage());
}
}
public void processNow() {
// trim it
for (int z=0;z < all.length; z++) {
all[z] = all[z].trim();
}
i=0;
for (int z=0;z < all.length; z++) {
if (all[z].startsWith("#")) {
} else if (all[z].startsWith("#end")) {
return;
} else if (all[z].indexOf('=')==-1) {
} else if (all[z]==null || all[z]=="") {
} else {
try {
p.add(i,new
String(all[z].substring(0,all[z].indexOf('=')).trim()));
v.add(i,new
String(all[z].substring(all[z].indexOf('=')+1,all[z].length()).trim()));
} catch (Exception e) {
}
debug((i) + " => "+p.get(i)+" value_is
"+v.get(i));
i++;
}
}
}
public String getConf(String prop, String IfValueNotFound) {
try {
int tmp = p.indexOf(prop);
if (tmp >=0 && tmp<v.size())
return (String) v.get(tmp);
else
return IfValueNotFound;
} catch (Exception e) {
//e.printStackTrace();
return IfValueNotFound;
}
}
public void debug(String x) {
//System.out.println(x);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
Use commons.configuration. Works beautifully and has the advantage that
you don't need to change your deployment descriptor or server.xml if you
want to change the configuration. With commons-configuration you can
also reload and write the properties, store them in xml, a database or a
simple text file.
Jilles
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]