SDSU CS 580 Client-Server Programming
Fall Semester, 2002
Configuration Files
Previous    Lecture Notes Index    Next    
© 2002, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 17-Oct-02

Contents of Doc 17, Configuration Files


SDSU Java Library,

VisualWorks Application Developers Guide, pages 219-223

CommanlLineInterest class>>example

Doc 17, Configuration Files Slide # 2

Application Parameters & Configuration Files

Applications normal have configuration files to store

Some programs use environment variables

   cvs co assignment2

Command line program/servers/utilities have flags

   ls -la
   ps -aux

Doc 17, Configuration Files Slide # 3

Servers normally use configuration files & command line flags

Environment variables are uses much in servers (why?)

Doc 17, Configuration Files Slide # 4


Some systems have libraries to handle config files & command line arguments

JDK does not seem to have such classes

There should be a number of Java libraries that provide such support

sdsu Java library is one such library

Doc 17, Configuration Files Slide # 5


Configuration files
Command line arguments
Command Line argument

-flag value
-- (ignore rest of the command line )

File Formats
properties format
#A comment to the end of the line
key2=value2 with spaces
key3 with spaces=value3 #part of the value
sdsu.util.LabeledData format
#A comment to the end of the line,
key1 = value1;
key2='value2 with spaces';
'key3 with spaces'=value3; # a comment

Doc 17, Configuration Files Slide # 6
Simple Example

import sdsu.util.ProgramProperties;
public class ConfigurationExample {
   public static void main(String args[]) {
         ProgramProperties flags = 
            new ProgramProperties( args, "configurationFile");
         String nameValue = 
            flags.getString( "name" , "No name given");
         int size = flags.getInt( "size", 0);
         boolean switchOn = flags.containsKey( "s");
         System.out.println( " nameValue: " + nameValue);
         System.out.println( " size: " + size);
         System.out.println( " switchOn: " + switchOn);
      catch ( readParseProblem)
         System.err.println( "Program aborted on error " + 

File "configurationFile.labeledData"

Doc 17, Configuration Files Slide # 7
Sample Runs

java ConfigurationExample

nameValue: Roger
size: 12
switchOn: false

java ConfigurationExample -s -name Pete

nameValue: Pete
size: 12
switchOn: true

java ConfigurationExample -conf=otherFile

nameValue: Sam
size: 8
switchOn: true

Doc 17, Configuration Files Slide # 8


Command Line Arguments


Cenvironment class>>commandLine

Returns array of String of flags in order they were specified

Starting VisualWorks with:

   visual -port 5 -xyz 
CEnvironment commandLine returns

    #('visual' '' '-port' '5' '' '-xyz')

Doc 17, Configuration Files Slide # 9
CEnvironment class>> userEnvironment

Returns CEnvironment object with user’s environment variables

| user |
user :=CEnvironment userEnvironment. 
user at: 'CVSROOT'

returns value of CVSROOT environment variable

CEnvironment is a subclass of Dictionary

Doc 17, Configuration Files Slide # 10
Registering for Flags

To avoid parsing the command line array

Classes can register to be informed of individual flags

To register add method to CommanlLineInterest class
In dependencies-pragma protocol

Doc 17, Configuration Files Slide # 11

CommanlLineInterest class>>port: aTokenReadStream
   <triggerAtSystemEvent: #returnFromSnapshot option: '-port'>
   | port |
   port := aTokenReadStream next asNumber.
   SimpleDateServer port: port.
   “Now make SimpleDateServer>>port: set the port. 

aTokenReadStream next returns the value after the flag

CommanlLineInterest class>>port: is called on startup when flag -port exits on command line

Only supports -flag value options

Can have method triggered at

#earlySystemInstallation flags are handled from left to right

Then #returnFromSnapshot flags are handled from left to right

Doc 17, Configuration Files Slide # 12
Configuration Files

VisualWorks servers use configuration files

No system wide classes for supporting them

Too easy to create own system?

Copyright ©, All rights reserved.
2002 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.

Previous    visitors since 17-Oct-02    Next