You can create classes and objects in struts just like you can with any 
other application.  Attached is a file with a few classes I use to teach 
students about the Properties class in java.util.  Change the value in 
Folder (line 138, if you are not using Windows).  PropertiesManager creates 
properties files from which the locations of other properties files is 
managed.  It also uses default properties, etc.

Micael

At 12:07 AM 9/22/2002 -0700, you wrote:

>Hello,
>
>This is a very "newbie" question I'm sure, so this might not be the 
>appropriate forum.  Maybe it belongs in the Tomcat forum?  Not sure.
>
>Pretty basic objective.... I just want to be able to put application 
>settings (things like path names, integer values, etc) in a *.properties 
>file and access those properties from within my tomcat/struts(1.1) 
>application.  I see quite a bit of talk about 
>ApplicationResources.properties, but it seems like people are only using 
>that to store and retrieve messages.  I'd rather not mix my messages with 
>my settings.  How do you do it?  I'm aware of the java.util.properties 
>class.... but I don't really know how to efficiently use it within the 
>application servlet context (I don't want to reload it with every 
>request)..... and I wouldn't know when to load it into application 
>scope.....   anyway, you see that I don't have a clue.  I can think of 
>plenty of ways to do it, but I would like know the most common/accepted 
>method(s).  So any tips would be much appreciated :)
>
>Thanks!
>
>
>
>
>
>---------------------------------
>Do you Yahoo!?
>New DSL Internet Access from SBC & Yahoo!
/* tccjava.toolbox.io.properties.PropertiesManager December 15, 2001
 *
 * Copyright 2001 Swords and Ploughshares, Ltd. All Rights Reserved.
 *
 * This software is the proprietary information of Swords and Ploughshares, Ltd.
 * Use is subject to license terms. */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.StringTokenizer;public class Demo {
    public static void main(String [] params) throws IOException, 
FileNotFoundException {
        new File(Folder.STORE).mkdirs();
        PropertiesManager factory = new PropertiesManager();

        factory.setDefaultPropertiesFileLocation("databank", Folder.STORE +  
File.separator + "databank_default.properties");
        factory.setApplicationPropertiesFileLocation("databank", Folder.STORE + 
File.separator + "databank_application.properties");

        factory.setDefaultFile("databank");
        factory.setApplicationFile("databank");
        factory.setDefaultProperty("databank", "double", "trouble");
        factory.setDefaultProperty("databank", "tickle", "ooooo");
        factory.setApplicationProperty("databank", "itch", "aaaaa");

        factory.setDefaultPropertiesFileLocation("test", Folder.STORE +  
File.separator + "portal_default.properties");
        factory.setApplicationPropertiesFileLocation("test", Folder.STORE +  
File.separator + "portal_application.properties");

        factory.setDefaultFile("test");
        factory.setApplicationFile("test");

        factory.setDefaultProperty("test", "pickels", "chomp");
        factory.setDefaultProperty("test", "main", "man");
        factory.setDefaultProperty("test", "double", "bubble");
        factory.setApplicationProperty("test", "radishes", "crunch");
        factory.setApplicationProperty("test", "double", "elbuob");
        factory.setApplicationProperty("test", "main", "street");
        factory.setApplicationProperty("test", "check", "mark");

        System.out.println("DEF FILE set app      " + 
factory.getApplicationProperty("test", "app"));
        System.out.println("DEF FILE set tickle   " + 
factory.getDefaultProperty("test", "tickle"));
        System.out.println("APP FILE set itch     " + 
factory.getApplicationProperty("test", "itch"));
        System.out.println("DEF FILE factory main " + 
factory.getDefaultProperty("test", "main"));
        System.out.println("APP FILE              " + 
factory.getApplicationProperty("test", "main"));
        System.out.println("APP AND DEF FILE      " + 
factory.getApplicationPropertyWithDefaults("test", "main"));
        System.out.println("APP AND DEF FILE      " + 
factory.getApplicationPropertyWithDefaults("test", "radishes"));

        /////////////////////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////

        System.out.println();
        System.out.println("DEF LOC          " + 
factory.getDefaultPropertiesFileLocation("databank"));
        System.out.println("APP LOC          " + 
factory.getApplicationPropertiesFileLocation("databank"));
        System.out.println();

        Properties defaultFile2 = new Properties();
        Properties applicationFile2 = new Properties();
        Properties applicationFileAndDefaultFile2 = new Properties();

        System.out.println("DEF FILE         " + (defaultFile2 = 
factory.getDefaultProperties("databank")));
        System.out.println("APP FILE         " + (applicationFile2 = 
factory.getApplicationProperties("databank")));
        System.out.println("APP AND DEF FILE " + (applicationFileAndDefaultFile2 = 
factory.getApplicationPropertiesWithDefaults("databank")));
        System.out.println("N.B.:\n\tThe application property file does not have the 
property\n"); /////////////////////////////////////////////////////////////////////////
        System.out.println("DEF PROP check   " + defaultFile2.getProperty("check"));
        System.out.println("APP PROP         " + 
applicationFile2.getProperty("check"));
        System.out.println("APP AND DEF PROP " + 
applicationFileAndDefaultFile2.getProperty("check"));
        System.out.println("N.B.:\n\tThe default file does not have the property\n"); 
/////////////////////////////////////////////////////////////////////////
        System.out.println("DEF PROP double  " + 
defaultFile2.getProperty("double-check"));
        System.out.println("APP PROP         " + 
applicationFile2.getProperty("double-check"));
        System.out.println("APP AND DEF PROP " + 
applicationFileAndDefaultFile2.getProperty("double-check"));
        System.out.println("N.B.:\n\tThe files have conflicting values\n"); 
/////////////////////////////////////////////////////////////////////////
        System.out.println("DEF PROP main    " + defaultFile2.getProperty("main"));
        System.out.println("APP PROP         " + applicationFile2.getProperty("main"));
        System.out.println("APP AND DEF PROP " + 
applicationFileAndDefaultFile2.getProperty("main"));

        System.out.println("DEF FILE set app      " + 
factory.getApplicationProperty("test", "app"));
        System.out.println("DEF FILE set tickle   " + 
factory.getDefaultProperty("test", "tickle"));
        System.out.println("APP FILE set itch     " + 
factory.getApplicationProperty("test", "itch"));
        System.out.println("DEF FILE factory main " + 
factory.getDefaultProperty("test", "main"));
        System.out.println("APP FILE              " + 
factory.getApplicationProperty("test", "main"));
        System.out.println("APP AND DEF FILE      " + 
factory.getApplicationPropertyWithDefaults("test", "main"));

        System.out.println("DEF FILE         " + (defaultFile2 = 
factory.getDefaultProperties("databank")));

        /////////////////////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////

        System.out.println();
        System.out.println("DEF LOC          " + 
factory.getDefaultPropertiesFileLocation("test"));
        System.out.println("APP LOC          " + 
factory.getApplicationPropertiesFileLocation("test"));
        System.out.println();

        Properties defaultFile = new Properties();
        Properties applicationFile = new Properties();
        Properties applicationFileAndDefaultFile = new Properties();

        System.out.println("DEF FILE         " + (defaultFile = 
factory.getDefaultProperties("test")));
        System.out.println("APP FILE         " + (applicationFile = 
factory.getApplicationProperties("test")));
        System.out.println("APP AND DEF FILE " + (applicationFileAndDefaultFile = 
factory.getApplicationPropertiesWithDefaults("test")));
        System.out.println("\\n\\tThe application property file does not have the 
property\\n"); 
/////////////////////////////////////////////////////////////////////////
        System.out.println("DEF PROP check   " + defaultFile.getProperty("check"));
        System.out.println("APP PROP         " + applicationFile.getProperty("check"));
        System.out.println("APP AND DEF PROP " + 
applicationFileAndDefaultFile.getProperty("check"));
        System.out.println("\\n\\tThe default file does not have the property\\n"); 
/////////////////////////////////////////////////////////////////////////
        System.out.println("DEF PROP double  " + 
defaultFile.getProperty("double-check"));
        System.out.println("APP PROP         " + 
applicationFile.getProperty("double-check"));
        System.out.println("APP AND DEF PROP " + 
applicationFileAndDefaultFile.getProperty("double-check"));
        System.out.println("\\n\\tThe files have conflicting values\\n"); 
/////////////////////////////////////////////////////////////////////////
        System.out.println("DEF PROP main    " + defaultFile.getProperty("main"));
        System.out.println("APP PROP         " + applicationFile.getProperty("main"));
        System.out.println("APP AND DEF PROP " + 
applicationFileAndDefaultFile.getProperty("main"));
        System.out.println();

        System.out.println("DEF FILE         " + (defaultFile2 = 
factory.getDefaultProperties("databank")));

        /////////////////////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////

        System.exit(0);
    }
}


/* tccjava December 31, 2001/* tccjava December 31, 2001
 *
 * Copyright 2001 Swords and Ploughshares, Ltd. All Rights Reserved.
 *
 * This software is the proprietary information of Swords and Ploughshares, Ltd.
 * Use is subject to license terms. */

/** This class holds the STORE of the JDK presently being created
  * @author Micael Padraig Og mac Grene
  * @STORE 1.1 January 4, 2002 Made the class uninstantiable and final.
  * @since Version 1.0 December 31, 2001 */
final class Folder {
    public static final String STORE = "C:\\STORE";
} ///;-)



/** This class handles everything you need with respect to properties
  * files.
  * <br><br>
  * There are property files for various applications, including default
  * and application property files, and there are property files keeping
  * track of where the property files for the applications are.
  * <br><br>
  * The class sets up a master properties file called
  * "location.properties," where the addresses of properties files,
  * default and application files, for applications are stored.
  * The class also adds to and changes properties for default and
  * application properties files for applications.  Further,
  * the class allows you to obtain: (1) default properties,
  * (2) application properites, or  (3) both default and application
  * properties.
  * @author  Micael Padraig Og mac Grene
  * @STORE 1.2 July 28, 2001
  * @since   1.0*/
class PropertiesManager {

    /** This gives a constant for the LOCATION of the properties
      * file holding the LOCATIONs of other properties file. */
    public static final String LOCATION = Folder.STORE +  File.separator + 
"location.properties";

    /** A properties object for general use. */
    private Properties       properties;

    public PropertiesManager() {
        try {
            new File(LOCATION).createNewFile();
        } catch (Exception e) {
        }
    }

    /** This method sets a property file holding the LOCATION of
      * the default properties files for applications.
      * @param app The name of the application.
      * @param filePath The filePath you want the LOCATION of the default property 
file to have.
      * @throws java.lang.IllegalArgumentException
      * @throws java.io.FileNotFound
      * @throws java.io.IOException */
    public void setDefaultPropertiesFileLocation(String app, String filePath) throws 
FileNotFoundException, IllegalArgumentException, IOException {
        if(filePath.endsWith("_default.properties")) {
            // TODO -- TODO -- TODO -- TODO
            // This is not creating the file path.
            String path = Path.name(filePath);
            File file = new File(Path.name(LOCATION));
            if(file.exists()) {
                FileInputStream input  = new FileInputStream(file);
                properties = new Properties();
                properties.load(input);
            } else {
                properties = new Properties();
            }
            properties.setProperty(app + "_default", path);
            FileOutputStream output   = new FileOutputStream(file);
            properties.store(output, " -- Properties Manager File -- ");
            output.close();
        } else {
            throw new IllegalArgumentException("You need to use a properties file 
ending with the properties name \"<app>_default.properties\"");
        }
    }


    /** This method gets the the LOCATION of the default properties file for
      * an application from a property file holding that information.
      * @param app The name of the application.
      * @throws java.lang.IllegalArgumentException
      * @throws java.io.FileNotFound*/
    public String getDefaultPropertiesFileLocation(String app) throws 
FileNotFoundException, IllegalArgumentException, IOException {
        File file = new File(Path.name(LOCATION));
        if(file.exists()) {
           FileInputStream input = new FileInputStream(file);
            properties = new Properties();
            properties.load(input);
            input.close();
        } else {
            throw new IllegalArgumentException("The manager.properties file is 
missing");
        }
        return properties.getProperty(app + "_default");

    }

    /** This method sets a property file holding the LOCATION of
      * the application properties files for applications.
      * @param app The name of the application.
      * @param filePath The filePath you want the LOCATION of the application property 
file to have.
      * @throws java.lang.IllegalArgumentException
      * @throws java.io.FileNotFound */
    public void setApplicationPropertiesFileLocation(String app, String filePath) 
throws FileNotFoundException, IllegalArgumentException, IOException {
        if(filePath.endsWith("_application.properties")) {
            String path = Path.name(filePath);
            File file = new File(Path.name(LOCATION));
            if(file.exists()) {
                FileInputStream input    = new FileInputStream(file);
                properties = new Properties();
                properties.load(input);
            } else {
                properties = new Properties();
            }
            properties.setProperty(app + "_application", path);
            FileOutputStream output = new FileOutputStream(file);
            properties.store(output, " -- Properties Manager File -- ");
            output.close();
        } else {
            throw new IllegalArgumentException("You need to use a properties file 
ending with the properties name \"<app>_application.properties\"");
        }
    }

    /** This method gets the the LOCATION of the application properties file for
      * an application from a property file holding that information.
      * @param app The name of the application.
      * @throws java.lang.IllegalArgumentException
      * @throws java.io.FileNotFound*/
    public String getApplicationPropertiesFileLocation(String app) throws 
FileNotFoundException, IllegalArgumentException, IOException {
        File file = new File(Path.name(LOCATION));
        if(file.exists()) {
           FileInputStream input = new FileInputStream(file);
            properties = new Properties();
            properties.load(input);
            input.close();
        } else {
            throw new IllegalArgumentException("The manager.properties file is 
missing");
        }
        return properties.getProperty(app + "_application");
    }

    /** This method creates the default properties file for an application
      * @param app The name of the application.
      * @throws java.lang.IllegalArgumentException
      * @throws java.io.FileNotFound*/
    public void setDefaultFile(String app) throws FileNotFoundException, 
IllegalArgumentException, IOException {
        File file = new File(Path.name(getDefaultPropertiesFileLocation(app)));
        if(file.exists()) {
            FileInputStream input = new FileInputStream(file);
            properties = new Properties();
            properties.load(input);
            input.close();
        } else {
            properties = new Properties();
        }
        FileOutputStream output = new FileOutputStream(file);
        properties.store(output, " -- " + app.toUpperCase() + " Default File -- ");
        output.close();
    }

    /** This method creates the application properties file for an application.
      * @param app The name of the application.
      * @throws java.lang.IllegalArgumentException
      * @throws java.io.FileNotFound*/
    public void setApplicationFile(String app) throws FileNotFoundException, 
IllegalArgumentException, IOException {
        File file = new File(Path.name(getApplicationPropertiesFileLocation(app)));
        if(file.exists()) {
            FileInputStream input = new FileInputStream(file);
            properties = new Properties();
            properties.load(input);
            input.close();
        } else {
            properties = new Properties();
        }
        FileOutputStream output = new FileOutputStream(file);
        properties.store(output, " -- " + app.toUpperCase() + " Application File -- ");
        output.close();
    }

    /** This method sets the default properties file for an application
      * @param app The name of the application.
      * @param key The properties name.
      * @param value The properties value.
      * @throws java.lang.IllegalArgumentException
      * @throws java.io.FileNotFound */
    public void setDefaultProperty(String app, String key, String value) throws 
FileNotFoundException, IllegalArgumentException, IOException {
        File file = new File(getDefaultPropertiesFileLocation(app));
        properties = getDefaultProperties(app);
        properties.setProperty(key, value);
        FileOutputStream output = new FileOutputStream(file);
        properties.store(output, " -- " + app + " Default Properties -- ");
        output.close();
    }

    /** This method gets a property from the default properties file for an 
application.
      * @param app The name of the application.
      * @param key The properties name.
      * @throws java.lang.IllegalArgumentException
      * @throws java.io.FileNotFound */
    public String getDefaultProperty(String app, String key) throws 
FileNotFoundException, IllegalArgumentException, IOException {
        String path = getDefaultPropertiesFileLocation(app);
        if(path != null) {
            path = Path.name(path);
            File file = new File(path);
            if(file.exists()) {
                FileInputStream input    = new FileInputStream(file);
                properties = new Properties();
                properties.load(input);
                input.close();
            } else {
                throw new IllegalArgumentException(app + " default property file does 
not exist");
            }
        } else {
            throw new IllegalArgumentException(app + " has not registered a default 
property file with the PropertyManager input module.");
        }
        return properties.getProperty(key);
    }

    /** This method gets the properties from the default properties file for an 
application
      * @param app The name of the application.
      * @throws java.lang.IllegalArgumentException
      * @throws java.io.FileNotFound */
    public Properties getDefaultProperties(String app) throws FileNotFoundException, 
IllegalArgumentException, IOException {
        String path = getDefaultPropertiesFileLocation(app);
        if(path != null) {
            path = Path.name(path);
            File file = new File(path);
            if(file.exists()) {
                FileInputStream input    = new FileInputStream(file);
                properties = new Properties();
                properties.load(input);
                input.close();
            } else {
                throw new IllegalArgumentException(app + " default property file does 
not exist");
            }
        } else {
            throw new IllegalArgumentException(app + " has not registered a default 
property file with the PropertyManager input module.");
        }
        return properties;
    }

    /** This method adds a property to the application properties file for an 
application.
      * @param app The name of the application.
      * @param key The properties name.
      * @param value The properties value.
      * @throws java.lang.IllegalArgumentException
      * @throws java.io.FileNotFound */
    public void setApplicationProperty(String app, String key, String value) throws 
FileNotFoundException, IllegalArgumentException, IOException {
        File file = new File(getApplicationPropertiesFileLocation(app));
        properties = getApplicationProperties(app);
        properties.setProperty(key, value);
        FileOutputStream output = new FileOutputStream(file);
        properties.store(output, " -- " + app + " Application Properties -- ");
        output.close();
    }

    /** This method gets a particular property from the application properties file for
      *an application.
      * @param app The name of the application.
      * @param key The properties name.
      * @throws java.lang.IllegalArgumentException
      * @throws java.io.FileNotFound */
    public String getApplicationProperty(String app, String key) throws 
FileNotFoundException, IllegalArgumentException, IOException {
        String path = getApplicationPropertiesFileLocation(app);
        if(path != null) {
            path = Path.name(path);
            File file = new File(path);
            if(file.exists()) {
                FileInputStream input    = new FileInputStream(file);
                properties = new Properties();
                properties.load(input);
                input.close();
            } else {
                throw new IllegalArgumentException(app + " app property file does not 
exist");
            }
        } else {
            throw new IllegalArgumentException(app + " has not registered a default 
property file with the PropertyManager input module.");
        }
        return properties.getProperty(key);
    }

    /** This method gets a particular property from both the application properties
      * file and the default properties file for an application, with the application
      * properties taking precedence, i.e. overriding the same default properties.
      * @param app The name of the application.
      * @param key The properties name.
      * @throws java.lang.IllegalArgumentException
      * @throws java.io.FileNotFound */
    public String getApplicationPropertyWithDefaults(String app, String key) throws 
FileNotFoundException, IllegalArgumentException, IOException {
        Properties defaultproperties = getDefaultProperties(app);
        String path = getApplicationPropertiesFileLocation(app);
        if(path != null) {
            path = Path.name(path);
            File file = new File(path);
            if(file.exists()) {
                FileInputStream input    = new FileInputStream(file);
                properties = new Properties(defaultproperties);
                properties.load(input);
                input.close();
            } else {
                throw new IllegalArgumentException(app + " app property file does not 
exist");
            }
        } else {
            throw new IllegalArgumentException(app + " has not registered a default 
property file with the PropertyManager input module.");
        }
        return properties.getProperty(key);
    }

    /** This method gets the properties from the application properties file
      * for an application.
      * @param app The name of the application.
      * @throws java.lang.IllegalArgumentException
      * @throws java.io.FileNotFound */
    public Properties getApplicationProperties(String app) throws 
FileNotFoundException, IllegalArgumentException, IOException {
        String path = getApplicationPropertiesFileLocation(app);
        if(path != null) {
            path = Path.name(path);
            File file = new File(path);
            if(file.exists()) {
                FileInputStream input    = new FileInputStream(file);
                properties = new Properties();
                properties.load(input);
                input.close();
            } else {
                throw new IllegalArgumentException(app + " app property file does not 
exist");
            }
        } else {
            throw new IllegalArgumentException(app + " has not registered a default 
property file with the PropertyManager input module.");
        }
        return properties;
    }

    /** This method gets the properties from both the application properties file
      * and the default properties file for an application, with the application
      * properties taking precedence.
      * @param app The name of the application.
      * @throws java.lang.IllegalArgumentException
      * @throws java.io.FileNotFound */
    public Properties getApplicationPropertiesWithDefaults(String app) throws 
FileNotFoundException, IllegalArgumentException, IOException {
        Properties defaultproperties = getDefaultProperties(app);
        String path = getApplicationPropertiesFileLocation(app);
        if(path != null) {
            path = Path.name(path);
            File file = new File(path);
            if(file.exists()) {
                FileInputStream input    = new FileInputStream(file);
                properties = new Properties(defaultproperties);
                properties.load(input);
                input.close();
            } else {
                throw new IllegalArgumentException(app + " app property file does not 
exist");
            }
        } else {
            throw new IllegalArgumentException(app + " has not registered a default 
property file with the PropertyManager input module.");
        }
        return properties;
    }

    /** This method is a helper method that replaces one string for another in a given
      * String object.
      * @param content The String object to be worked on.
      * @param before The string in the String object to be replaced throughout.
      * @param after The string to act as the replacement throughout the String
      * object */
    private static String replace(String content, String before, String after) {
        int position = content.indexOf(before);
        while (position > -1) {
            content  = content.substring(0, position) + after + 
content.substring(position + before.length());
            position = content.indexOf(before, position + after.length());
        }
        return content;
    }
} ///;-)

/* tccjava.toolbox.io.properties.PropertiesManager December 15, 2001
 *
 * Copyright 2001 Swords and Ploughshares, Ltd. All Rights Reserved.
 *
 * This software is the proprietary information of Swords and Ploughshares, Ltd.
 * Use is subject to license terms. */

/** This class makes sure a classpath is platform neutral.
  * <code>Path</code> takes a <code>String</code> object representation
  * of a classpath (with or without a file name) and replaces the four
  * symbols / \ ; and : with their platform neutral <code>File</code> class
  * fields.
  * @STORE 1.2: 21 April         2001
  * @author    Micael Padraig Og mac Grene
  * @since     Version 1.2*/
final class Path {

    /** The method <code>name</code> makes a String object classpath platform
      * neutral.
      * @param path: A <code>String</code> object representation of a classpath
      * @return A <code>String</code> object representation of a platform neutral
      * classpath. */
    public static String name(String path) {
         replace(path, "\\", File.separator);
         replace(path, "/",    File.separator);
         replace(path, ";",    File.pathSeparator);
         replace(path, ":",    File.pathSeparator);
        return path;
    }

    /** This method is a helper method that replaces one string for another in a given
      * String object.
      * @param content The String object to be worked on.
      * @param before The string in the String object to be replaced throughout.
      * @param after The string to act as the replacement throughout the String
      * object */
    private static String replace(String content, String before, String after) {

        /* This finds the beginning of the first occurence of the string called
         * "before" in the String object called "content". */
        int cursor = content.indexOf(before);
        while (cursor > -1) {
            /* This replaces the string called "before" with the string called 
"after".*/
            content    = content.substring(0, cursor) + after + 
content.substring(cursor + before.length());

            /* This finds the beginning of the next occurence of the string called
             * "before" in the String object called "content". */
            cursor = content.indexOf(before, cursor + after.length());
        }
        /* This returns the results of replacing instances of strings identical to
         * the string called "before" with instances of strings identical to the
         * string called "after"in the String object called "content". */
        return content;
    }
} ///;-)

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to