Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.jboss.remoting.samples.chat.utility;
  
  import java.util.*;
  import java.io.*;
  import  javax.servlet.*;

Title: chat.utility.Parameters

Description: Manages parameters to the application.

A parameter's value is taken from:

  1. the primary source, which is
    • the command line if the program is standalone
    • the web.conf file if the program is a servlet
    or, if the parameter is not given in the primary source,
  2. a configuration file property with the same name, or, if there is no such configuration file property
  3. the default value given in this file, or, if a default value is not given in this file,
  4. null

Copyright: Copyright (c) 2004

Author(s):
Ron Sigal
Version:
1.0
 
 
 public class Parameters
 {
   static Hashtable parameterValues = new Hashtable();
   static Hashtable primaryParameterValues = new Hashtable();
   static Hashtable defaultParameterValues = new Hashtable();
 
   static
   {
       .put("serverUri""http://localhost:8000"); // URI of chat server
       .put("clientUri""http://localhost:8002"); // URI used by ChatReceiver's
   }


  
initParameters() puts parameters into a Hashtable.

For each element of args of the form "string1=string2", the value "string2" is a parameter with key "string1".

Parameters:
<code>args: an array of Strings. These are meant to be the command line parameters.
 
   public static void initParameters(String[] args)
   {
     // get command line parameters
     for (int i = 0; i < args.lengthi++)
     {
       int mark = args[i].indexOf('=');
       if (mark >= 0)
       {
         .put(args[i].substring(0, mark), args[i].substring(mark + 1));
       }
     }
 
   }


  
initParameters() puts parameters into a Hashtable.

Parameters:
<code>servletConfig/code>: the primary source of parameters
 
   public static void initParameters(ServletConfig servletConfig)
   {
     Enumeration e = servletConfig.getInitParameterNames();
 
     while (e.hasMoreElements())
     {
       String key = (Stringe.nextElement();
       .put(keyservletConfig.getInitParameter(key));
     }
 
   }


  
getSecondaryParameterSources()
  • reads parameters from a parameter file named chat.conf, if it exists,
  • gets default parameter values, and
  • merges these with the parameter values from the primary source
 
   private static void getSecondaryParameterSources()
 {
     // check if chat.conf parameter file has a non-standard location
     String configurationFilePath = "chat.conf";
 
     if (.containsKey("chat.conf"))
       configurationFilePath = (String.get("chat.conf");
    File configurationFile = new File(configurationFilePath);
    // get properties from the configuration file
    if (configurationFile.exists())
    {
      try
      {
        FileInputStream fis = new FileInputStream(configurationFile);
        Properties properties = new Properties();
        properties.load(fis);
        Enumeration e = properties.propertyNames();
        while (e.hasMoreElements())
        {
          String key = (Stringe.nextElement();
          .put(keyproperties.getProperty(key));
          ..println(key + ":" + properties.getProperty(key));
        }
      }
      catch (FileNotFoundException fnfe)
      {
        ..println("configuration file not found: " + configurationFilePath);
        ..println("using default properties");
      }
      catch (IOException ioe)
      {
        ..println("error reading configuration file: " + configurationFilePath);
        ..println("using default properties");
        ..println(ioe.getMessage());
      }
      String debug = getParameter("debug");
      if (debug.charAt(0) == 'y')
      {
        ..println("properties:");
        Iterator it = .keySet().iterator();
        while (it.hasNext())
        {
          String key = (Stringit.next();
          ..println("  " + key + ": " + .get(key));
        }
      }
    }
    // override properties from configuration file with command line parameters
    while (it.hasNext())
    {
      Object key = it.next();
    }
  }



  
getParameter() returns stored parameter values

Parameters:
<code>name name of parameter whose value is requested
Returns:
if name is a key in parameters, returns the value associated with name. Otherwise, returns null.
  public static String getParameter(String name)
  {
     return getParameter(namenull);
  }
  
  
  
getParameter() returns stored parameter values

Parameters:
<code>name name of parameter whose value is requested
Returns:
if name is a key in parameters, returns the value associated with name. Otherwise, returns null.
  public static String getParameter(String nameString defaultValue)
  {
    String value = null;
    value = (String.get(name);
    if (value == null)
      value = System.getProperty(name);
    if (value == null)
      value = (String.get(name);
    
    if (value == null)
       value = defaultValue;
    return value;
  }

  
main() implements unit tests.
  public static void main(String[] args)
  {
    initParameters(args);
    ..println("clientUri: " + getParameter("clientUri"));
    ..println("serverUri: " + getParameter("serverUri"));
  }
New to GrepCode? Check out our FAQ X