• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
RE: docRoot webresources path string
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: docRoot webresources path string


  • Subject: RE: docRoot webresources path string
  • From: Elizabeth Lynch <email@hidden>
  • Date: Tue, 3 Jun 2003 23:39:34 +0100

Jonathan

You asked me to give you an example of how to set up config files which can be read in at runtime to provide different parameters to your app. So here goes - and I've copied to the list as well in the hope others will find it useful.


***** Using configuration files with WebObjects

This is written with code examples for WO 5.2; I have used more or less the same code from 4.0 onwards. The configuration file can be XML or plist format. To be honest, the plist format is easier to edit.

The basic approach is to supply a command line parameter on application start up; eg, -config values.xml. This is loaded into a dictionary by the application constructor. Values are made available to other classes by referencing application().valueForKey("key").


*** Loading the configuration dictionary (code)

public class Application extends WOApplication {

    public static void main(String argv[]) {
        WOApplication.main(argv, Application.class);
    }

    protected NSDictionary configMap;

    public Application() {
        super();
        System.out.println("Welcome to " + this.name() + " !");
        /* ** put your initialization code in here ** */
        String filePath = NSProperties.stringForKey("config");

if (null != filePath) {
try {
//File file = new File(filePath);
//configMap = (NSDictionary) NSPropertyListSerialization.propertyListFromString(PLUtilities.stringFro mFile(file));
WOXMLDecoder xDecoder = WOXMLDecoder.decoder();
configMap = (NSDictionary)xDecoder.decodeRootObject(NSData.dataWithContentsOfFile(fi lePath));
}
catch (Exception e) {
System.out.println("Unable to load dictionary: " + filePath);
}
}

/*WOXMLCoder xCoder = WOXMLCoder.coder();
xCoder.setXmlDeclaration();
NSData configData = new NSData(xCoder.encodeRootObjectForKey(configMap, "config"));
configData.writeToFile("/tmp/config.xml");*/


    }

    public Object handleQueryWithUnboundKey(String key) {
        if (configMap != null) {
            if (configMap.objectForKey(key) != null)
                return configMap.objectForKey(key);
        }
        //return super.handleQueryWithUnboundKey(key);
        return null;
    }

}


*** Loading the configuration dictionary (commentary)

The Application constructor gets the property value for the key config, which should be a full path to your xml/plist file. The code example is for the xml version, but the code required to support a plist file is present but commented out.

If a file path is present, we load it. The plist version refers to a trivial code snippet (not supplied) to load a file into a string.

The easiest way to create this file is to insert the code required to construct the correct dictionary here, and uncomment the next couple of lines. These simply write out a dictionary to a file. Once this file is created, it is relatively simple to modify it by hand.

The next method allows you to respond to valueForKey() requests that don't match existing KeyValueCoding accessors or instance variables in your Application class. These are simply looked up in the dictionary and returned.

An example of using this might be:

        String logfile = (String)valueForKey("logfile");
        if (logfile != "")
            statisticsStore().setLogFile(logfile, (double)7);
        System.out.println("Log file: " + statisticsStore().logFile());



By the way, on a completely different subject, if anyone knows how to make log files rollover correctly, please let us know! :-)


Liz --- Liz Lynch www.plsys.co.uk P&L Software


> -----Original Message-----
> From: email@hidden
> [mailto:email@hidden]On Behalf Of Jonathan
> Fleming
> Sent: 03 June 2003 06:30
> To: email@hidden; email@hidden
> Subject: docRoot webresources path string
>
>
> how can I dynamically / progmatically change the string of a documentRoot
> directory when I build an app for install. at the moment I have have to
> change the path manually just before I do the split install build.
>
> Thanks


> Jonathan :^s
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.

  • Prev by Date: Re: Internal Data Type for BOOL
  • Next by Date: Re: Encryption Code Problem
  • Previous by thread: Re: docRoot webresources path string
  • Next by thread: Combining 2 Fields
  • Index(es):
    • Date
    • Thread