Quantcast

reading property files in java (advanced usage)

$tinkle

Expert on blowing
Feb 12, 2003
14,591
6
i need to create a property file which will have changes to an applications functionality (re-deployment will be tight-looped & don't have time for proper code changes).

what i'd like to do is have similar properties, which are made unique by a sequence number, or suffix.

ex:
Code:
NAME1        = george
ADDRESS1     = "1600 penn ave"
PHONE1       = 202-456-1414
APPLICATION1 = foo1.sh

NAME2        = gordon
ADDRESS2     = "10 downing st"
PHONE2       = 01-233-34-34
APPLICATION2 = foo2.sh

NAME3        = muffinMan
ADDRESS3     = "drury lane"
PHONE3       = 703-256-8000
APPLICATION3 = foo3.sh
then changes to the top-level application would include changing a GUI properties file (or config b/c it's another language altogether), and then my properties file would be read in kind to associate the proper black box code (indicated by APPLICATION#).

i want to keep this as flexible as possible, so anyone could enter out-of-sequence values & it would not break, so i'd like the suffix to be treated as a wild-card, so i can build my vector of <name>, <address>, <phone>, <app>.

any ideas or alternative approaches? only rule is that changes must only be made to property file, not code.
 

$tinkle

Expert on blowing
Feb 12, 2003
14,591
6
very kludgie, but i think this'll work in building the tuples:

Code:
private Vector< String > names        = new Vector< String >();
private Vector< String > addresses    = new Vector< String >();
private Vector< String > phones       = new Vector< String >();
private Vector< String > applications = new Vector< String >();


try {
   for (int i=1;; i++) {
      String suffix = new String( Integer.valueOf( i ) );
      StringTokenizer st = new StringTokenizer( prop.getProperty( "NAME"+suffix );
      names.add( st.nextToken() );
      st = new StringTokenizer( prop.getProperty( "ADDRESS"+suffix );
      addresses.add( st.nextToken() );
      st = new StringTokenizer( prop.getProperty( "PHONE"+suffix );
      phones.add( st.nextToken() );
      st = new StringTokenizer( prop.getProperty( "APPLICATION"+suffix );
      applications.add( st.nextToken() );
   }
} catch (NoSuchElementException nsee) {
   // do nothing, we're done (i toldja this was kludgie)
}
i really don't like this for long term, as the list must be sequenced ordinally from 1, and any gaps in sequences will keep the rest from being populated (i.e., if i have to take APPLICATION9 offline, i had better hotswap a stub or else APPLICATION10 - APPLICATIONn won't be associated).

this is to say nothing about using exception handling as expected flow

quick show of hands: who gives a schyte?