Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/clra/visitor/Configuration.java


1   /*
2    * Copyright (c) Carnegie Lake Rowing Association 2002. All rights reserved.
3    * Distributed under the GPL license. See doc/COPYING.
4    * $RCSfile: Configuration.java,v $
5    * $Date: 2003/02/26 03:38:46 $
6    * $Revision: 1.6 $
7    */
8   
9   package com.clra.visitor;
10  
11  import com.clra.util.ConfigurationException;
12  import com.clra.util.DBConfiguration;
13  import java.io.InputStream;
14  import java.net.URL;
15  import java.util.Properties;
16  import org.apache.log4j.Category;
17  import org.apache.log4j.helpers.Loader;
18  
19  /**
20   * A collection of configurable properties used by this package.
21   *
22   * @author <a href="mailto:donaldzhu@sympatico.ca">Angela Yue</a>
23   * @version $Revision: 1.6 $ $Date: 2003/02/26 03:38:46 $
24   */
25  
26  public class Configuration {
27  
28    /** All methods are static */
29    private Configuration() {}
30  
31    private final static String base = Configuration.class.getName();
32    private final static Category theLog = Category.getInstance( base );
33  
34    /** Session SQL property-name prefix */
35    public final static String PN_PREFIX_SQL_SESSION = "visitor.sql.";
36  
37    /** SQL date format spec */
38    public final static String PN_SQL_DATE_FORMAT =
39            PN_PREFIX_SQL_SESSION + "dateformat." + DBConfiguration.DBTYPE;
40  
41    public final static String PN_PREFIX_SQL_01 = "visitor.sql.01.";
42  
43    public final static String PN_SQL_01 =
44            PN_PREFIX_SQL_01 + DBConfiguration.DBTYPE;
45  
46    public final static String PN_PREFIX_SQL_02 = "visitor.sql.02.";
47  
48    public final static String PN_SQL_02 =
49            PN_PREFIX_SQL_02 + DBConfiguration.DBTYPE;
50  
51    public final static String PN_PREFIX_SQL_03 = "visitor.sql.03.";
52  
53    public final static String PN_SQL_03 =
54            PN_PREFIX_SQL_03 + DBConfiguration.DBTYPE;
55  
56    public final static String PN_PREFIX_SQL_04 = "visitor.sql.04.";
57  
58    public final static String PN_SQL_04 =
59            PN_PREFIX_SQL_04 + DBConfiguration.DBTYPE;
60  
61    public final static String PN_PREFIX_SQL_05 = "visitor.sql.05.";
62  
63    public final static String PN_SQL_05 =
64            PN_PREFIX_SQL_05 + DBConfiguration.DBTYPE;          
65  
66    public final static String PN_PREFIX_SQL_06 = "visitor.sql.06.";
67  
68    public final static String PN_SQL_06 =
69            PN_PREFIX_SQL_06 + DBConfiguration.DBTYPE;          
70  
71    /** Property that holds the JNDI location of the ApplyMemberShip factory */
72    public final static String PN_APPLYMEMBERSHIP_HOME =
73            "applymembership.home";
74  
75    /** Name of file that holds default values for this package */
76    private final static String DEFAULTS_FILE =
77            "com/clra/visitor/visitor.properties";
78  
79    /** Default properties for this package */
80    private final static Properties defaultProperties = new Properties();
81    static {
82      InputStream is = null;
83      try {
84        URL url = Loader.getResource( DEFAULTS_FILE, Configuration.class );
85        is = url.openStream();
86        defaultProperties.load( is );
87        if ( theLog.isDebugEnabled() ) {
88          theLog.debug( "loaded properties from '" + DEFAULTS_FILE + "'" );
89          java.util.Enumeration _e = defaultProperties.propertyNames();
90          while ( _e.hasMoreElements() ) {
91            theLog.debug( "property: " + _e.nextElement() );
92          }
93        }
94      }
95      catch( Exception x ){
96        String msg = "unable to load default properties from '"
97                + DEFAULTS_FILE + "'";
98        theLog.fatal(msg,x);
99        throw new IllegalStateException( msg );
100     }
101     finally {
102       if ( is != null ) {
103         try { is.close(); } catch( Exception x ) {}
104         is = null;
105       }
106     } // finally
107   } // static
108 
109   /** Utility that looks up a default property by name */
110   private static String getDefaultProperty( String PN ) {
111     // Precondition
112     if ( PN == null || PN.trim().length()== 0 ) {
113       throw new IllegalArgumentException( "invalid property name" );
114     }
115 
116     String retVal = defaultProperties.getProperty( PN );
117     if ( retVal == null || retVal.trim().length() == 0 ) {
118       String msg = "invalid or missing value for '" + PN + "'";
119       theLog.error(msg);
120       retVal = null;
121     }
122     else {
123       retVal = retVal.trim();
124     }
125 
126     return retVal;
127   } // getDefaultProperty(String)
128 
129   /** Utility that looks up a System property or assigns a default value */
130   private static String getProperty( String PN, String DEFAULT ) {
131     // Preconditions
132     //theLog.debug( "PN == '" + PN + "'" );
133     //theLog.debug( "DEFAULT == '" + DEFAULT + "'" );
134     if ( PN == null || PN.trim().length() == 0 ) {
135       throw new IllegalArgumentException( "invalid property name" );
136     }
137     if ( DEFAULT != null && DEFAULT.trim().length() == 0 ) {
138       DEFAULT = null;
139     }
140       
141     String retVal = System.getProperty( PN );
142     if ( retVal == null || retVal.trim().length() == 0 ) {
143       retVal = DEFAULT;
144     }
145     if ( retVal == null || retVal.trim().length() == 0 ) {
146       String msg = "no property for '" + PN + "'";
147       theLog.fatal( msg );
148       throw new IllegalStateException( msg );
149     }
150     retVal = retVal.trim();
151 
152     return retVal;
153   } // getProperty(String,String)
154 
155   private static String DEFAULT_SQL_DATE_FORMAT = null;
156   public static String SQL_DATE_FORMAT = null;
157   static {
158     DEFAULT_SQL_DATE_FORMAT = getDefaultProperty( PN_SQL_DATE_FORMAT );
159     theLog.info("DEFAULT_SQL_DATE_FORMAT == '" + DEFAULT_SQL_DATE_FORMAT + "'");
160     SQL_DATE_FORMAT = getProperty(PN_SQL_DATE_FORMAT, DEFAULT_SQL_DATE_FORMAT);
161     theLog.info( "SQL_DATE_FORMAT == '" + SQL_DATE_FORMAT + "'" );
162   } // static
163 
164   private static String DEFAULT_SQL_01 = null;
165   static {
166     String tmp = defaultProperties.getProperty( PN_SQL_01 );
167     if ( tmp == null || tmp.trim().length() == 0 ) {
168       String msg = "invalid or missing value for '" + PN_SQL_01 + "'";
169       theLog.error(msg);
170     }
171     else {
172       DEFAULT_SQL_01 = tmp.trim();
173     }
174     theLog.info( "DEFAULT_SQL_01 == '" + DEFAULT_SQL_01 + "'" );
175   } // static
176 
177   public static String SQL_01 = null;
178   static {
179     SQL_01 = System.getProperty( PN_SQL_01 );
180     if ( SQL_01 == null || SQL_01.trim().length() == 0 ) {
181       SQL_01 = DEFAULT_SQL_01;
182     }
183     if ( SQL_01 == null || SQL_01.trim().length() == 0 ) {
184       String msg = "no property for '" + PN_SQL_01 + "'";
185       theLog.fatal( msg );
186       throw new IllegalStateException( msg );
187     }
188     theLog.info( "SQL_01 == '" + SQL_01 + "'" );
189   } // static
190 
191   public static String SQL_02 = null;
192   static {
193     String tmp = defaultProperties.getProperty( PN_SQL_02 );
194     if ( tmp != null ) {
195       tmp = tmp.trim();
196     }
197     theLog.debug( "DEFAULT_SQL_02 == " + tmp );
198     SQL_02 = System.getProperty( PN_SQL_02, tmp );
199     if ( SQL_02 == null || SQL_02.trim().length() == 0 ) {
200       String msg = "no property for '" + PN_SQL_02 + "'";
201       theLog.fatal( msg );
202       throw new IllegalStateException( msg );
203     }
204     theLog.info( "SQL_02 == '" + SQL_02 + "'" );
205   } // static
206 
207   public static String SQL_03 = null;
208   static {
209     String tmp = defaultProperties.getProperty( PN_SQL_03 );
210     if ( tmp != null ) {
211       tmp = tmp.trim();
212     }
213     theLog.debug( "DEFAULT_SQL_03 == " + tmp );
214     SQL_03 = System.getProperty( PN_SQL_03, tmp );
215     if ( SQL_03 == null || SQL_03.trim().length() == 0 ) {
216       String msg = "no property for '" + PN_SQL_03 + "'";
217       theLog.fatal( msg );
218       throw new IllegalStateException( msg );
219     }
220     theLog.info( "SQL_03 == '" + SQL_03 + "'" );
221   } // static
222 
223   public static String SQL_04 = null;
224   static {
225     String tmp = defaultProperties.getProperty( PN_SQL_04 );
226     if ( tmp != null ) {
227       tmp = tmp.trim();
228     }
229     theLog.debug( "DEFAULT_SQL_04 == " + tmp );
230     SQL_04 = System.getProperty( PN_SQL_04, tmp );
231     if ( SQL_04 == null || SQL_04.trim().length() == 0 ) {
232       String msg = "no property for '" + PN_SQL_04 + "'";
233       theLog.fatal( msg );
234       throw new IllegalStateException( msg );
235     }
236     theLog.info( "SQL_04 == '" + SQL_04 + "'" );
237   } // static
238 
239   public static String SQL_05 = null;
240   static {
241     String tmp = defaultProperties.getProperty( PN_SQL_05 );
242     if ( tmp != null ) {
243       tmp = tmp.trim();
244     }
245     theLog.debug( "DEFAULT_SQL_05 == " + tmp );
246     SQL_05 = System.getProperty( PN_SQL_05, tmp );
247     if ( SQL_05 == null || SQL_05.trim().length() == 0 ) {
248       String msg = "no property for '" + PN_SQL_05 + "'";
249       theLog.fatal( msg );
250       throw new IllegalStateException( msg );
251     }
252     theLog.info( "SQL_05 == '" + SQL_05 + "'" );
253   } // static
254 
255   public static String SQL_06 = null;
256   static {
257     String tmp = defaultProperties.getProperty( PN_SQL_06 );
258     if ( tmp != null ) {
259       tmp = tmp.trim();
260     }
261     theLog.debug( "DEFAULT_SQL_06 == " + tmp );
262     SQL_06 = System.getProperty( PN_SQL_06, tmp );
263     if ( SQL_06 == null || SQL_06.trim().length() == 0 ) {
264       String msg = "no property for '" + PN_SQL_06 + "'";
265       theLog.fatal( msg );
266       throw new IllegalStateException( msg );
267     }
268     theLog.info( "SQL_06 == '" + SQL_06 + "'" );
269   } // static
270 
271   private static String _DEFAULT_APPLYMEMBERSHIP_HOME = null;
272   private static String _APPLYMEMBERSHIP_HOME = null;
273   static {
274     _DEFAULT_APPLYMEMBERSHIP_HOME = getDefaultProperty( PN_APPLYMEMBERSHIP_HOME );
275     theLog.info(
276         "DEFAULT_APPLYMEMBERSHIP_HOME == '" + _DEFAULT_APPLYMEMBERSHIP_HOME + "'");
277     _APPLYMEMBERSHIP_HOME = getProperty(
278         PN_APPLYMEMBERSHIP_HOME, _DEFAULT_APPLYMEMBERSHIP_HOME );
279     theLog.info( "APPLYMEMBERSHIP_HOME == '" + _APPLYMEMBERSHIP_HOME + "'" );
280   } // static
281 
282   public static String APPLYMEMBERSHIP_HOME() { return _APPLYMEMBERSHIP_HOME; }
283 
284 } // Configuration
285 
286 /*
287  * $Log: Configuration.java,v $
288  * Revision 1.6  2003/02/26 03:38:46  rphall
289  * Added copyright and GPL license
290  *
291  * Revision 1.5  2003/02/19 22:09:17  rphall
292  * Removed gratuitous use of CLRA acronym
293  *
294  */
295