KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > util > Config


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: Config.java,v 1.17 2007/01/07 06:14:00 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.core.util;
23
24 import java.io.BufferedInputStream JavaDoc;
25 import java.io.BufferedOutputStream JavaDoc;
26 import java.io.File JavaDoc;
27 import java.io.FileInputStream JavaDoc;
28 import java.io.FileNotFoundException JavaDoc;
29 import java.io.FileOutputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.io.OutputStream JavaDoc;
33 import java.net.URL JavaDoc;
34 import java.util.Properties JavaDoc;
35 import java.util.logging.Level JavaDoc;
36 import java.util.logging.Logger JavaDoc;
37
38 import org.opensubsystems.core.error.OSSConfigException;
39
40 /**
41  * Class responsible for reading and saving of configuration files.
42  *
43  * @version $Id: Config.java,v 1.17 2007/01/07 06:14:00 bastafidli Exp $
44  * @author Miro Halas
45  * @code.reviewer Miro Halas
46  * @code.reviewed 1.9 2006/05/21 03:45:04 bastafidli
47  */

48 public class Config
49 {
50    // Configuration settings ///////////////////////////////////////////////////
51

52    /**
53     * Name of the property which can specify the configuration file to use.
54     */

55    public static final String JavaDoc CONFIG_FILE_NAME = "oss.config.file";
56
57    /**
58     * Name of the property which can specify the configuration file that a
59     * given configuration file depends on. If this property is found in the file
60     * being loaded, the dependent file is loaded first. Then the properties
61     * loaded from the dependent file are replaced with the properties which were
62     * redefined in the current file.
63     */

64    public static final String JavaDoc DEPENDENT_CONFIG_FILE_NAME = "oss.config.dependent.file";
65    
66    // Constants ////////////////////////////////////////////////////////////////
67

68    /**
69     * This value can be used to signal yes or true in the config file.
70     * Use number since this way we can just use equals and not equalsIgnoreCase.
71     */

72    public static final String JavaDoc VALUE_YES_TRUE = "1";
73
74    /**
75     * This value can be used to signal yes or true in the config file.
76     */

77    public static final String JavaDoc VALUE_YES_TRUE_BOOLEAN = Boolean.TRUE.toString();
78    
79    /**
80     * This value can be used to signal no or false in the config file but
81     * in general we should test on TRUE and if it is not TRUE then it is FALSE.
82     * Use number since this way we can just use equals and not equalsIgnoreCase.
83     */

84    public static final String JavaDoc VALUE_NO_FALSE = "0";
85
86    /**
87     * This value can be used to signal no or false in the config file but
88     */

89    public static final String JavaDoc VALUE_NO_FALSE_BOOLEAN = Boolean.FALSE.toString();
90
91    /**
92     * Default file name of configuration file.
93     */

94    public static final String JavaDoc DEFAULT_CONFIG_FILE_NAME = "oss.properties";
95
96    /**
97     * Lock used in synchronized sections.
98     */

99    private static final String JavaDoc IMPL_LOCK = "IMPL_LOCK";
100
101    /**
102     * Helper mutex to synchronize some methods.
103     */

104    protected String JavaDoc m_strMutex = "config.mutex";
105
106    // Member variables /////////////////////////////////////////////////////////
107

108    /**
109     * Property file containing settings for this application
110     * This property is not transient since it represents values which needs
111     * to be stored on a disk.
112     */

113    protected Properties JavaDoc m_prpProperties;
114    
115    /**
116     * Name of the property file containing application properties.
117     */

118    protected String JavaDoc m_strPropertyFileName;
119
120    /**
121     * If the property file name is not specified by s_strPropertyFileName,
122     * it can be specified by this URL as found on the system class path.
123     */

124    protected URL JavaDoc m_urlDefaultPropertyFile;
125    
126    /**
127     * Name of the configuration file actually used.
128     */

129    protected String JavaDoc m_strConfigFile;
130    
131    // Cached values ////////////////////////////////////////////////////////////
132

133    /**
134     * Logger for this class
135     */

136    private static Logger JavaDoc s_logger = Log.getInstance(Config.class);
137   
138    /**
139     * Reference to the DependencyChecker actually in use.
140     */

141    private static Config s_defaultInstance;
142    
143    // Constructors /////////////////////////////////////////////////////////////
144

145    /**
146     * Constructor to construct accessor to default property file.
147     */

148    public Config(
149    )
150    {
151       m_prpProperties = null;
152       m_strPropertyFileName = null;
153       m_urlDefaultPropertyFile = null;
154    }
155    
156    /**
157     * Public constructor to access specified property file.
158     *
159     * @param strPropertyFileName - name of the property file to use.
160     */

161    public Config(
162       String JavaDoc strPropertyFileName
163    )
164    {
165       m_prpProperties = null;
166       m_strPropertyFileName = strPropertyFileName;
167       m_urlDefaultPropertyFile = null;
168    }
169
170    /**
171     * Public constructor to use specified properties
172     *
173     * @param predefinedProperties - predefined properties which should be used
174     */

175    public Config(
176       Properties JavaDoc predefinedProperties
177    )
178    {
179       m_prpProperties = predefinedProperties;
180       m_strPropertyFileName = null;
181       m_urlDefaultPropertyFile = null;
182    }
183
184    // Static methods //////////////////////////////////////////////////////////
185

186    /**
187     * Get the default config instance.
188     *
189     * @return Config - actual instance
190     */

191    public static Config getInstance(
192    )
193    {
194       if (s_defaultInstance == null)
195       {
196          // Only if the default DependencyChecker wasn't set by other means create a new one
197
// Synchronize just for the creation
198
synchronized (IMPL_LOCK)
199          {
200             if (s_defaultInstance == null)
201             {
202                // Specify no name so that default name will be searched.
203
setInstance(new Config());
204             }
205          }
206       }
207       
208       return s_defaultInstance;
209    }
210    
211    /**
212     * Set default instance. This instance will be returned by getInstance
213     * method until it is changed.
214     *
215     * @param defaultInstance - new default Config instance
216     * @see #getInstance
217     */

218    public static void setInstance(
219       Config defaultInstance
220    )
221    {
222       if (GlobalConstants.ERROR_CHECKING)
223       {
224          assert defaultInstance != null
225                 : "Default config instance cannot be null";
226       }
227       
228       synchronized (IMPL_LOCK)
229       {
230          s_defaultInstance = defaultInstance;
231       }
232    }
233
234    // Accessors ////////////////////////////////////////////////////////////////
235

236    /**
237     * Load all properties from default configuration file or return the ones
238     * passed into the constructor.
239     *
240     * @return Properties - set of properties for this application.
241     * @throws IOException - for example the file was not found
242     */

243    public Properties JavaDoc getProperties(
244    ) throws IOException JavaDoc
245    {
246       if (m_prpProperties == null)
247       {
248          synchronized (m_strMutex)
249          {
250             InputStream JavaDoc isConfigFile = null;
251
252             try
253             {
254                if (m_strPropertyFileName == null)
255                {
256                   // Load the name of configuration file
257
m_strPropertyFileName = System.getProperty(CONFIG_FILE_NAME, "");
258                   if ((m_strPropertyFileName == null) || (m_strPropertyFileName.length() == 0))
259                   {
260                      m_strPropertyFileName = null;
261                      s_logger.config("Name of configuration file is not specified"
262                                      + " using property " + CONFIG_FILE_NAME
263                                      + ". Trying to load default configuration file"
264                                      + " from system classpath.");
265       
266                      isConfigFile = findConfigFile(DEFAULT_CONFIG_FILE_NAME);
267                   }
268                   else
269                   {
270                      s_logger.config(CONFIG_FILE_NAME + " = "
271                                      + m_strPropertyFileName);
272                      
273                      try
274                      {
275                         // Try to open the file first directly
276
isConfigFile = new FileInputStream JavaDoc(m_strPropertyFileName);
277                         m_strConfigFile = m_strPropertyFileName;
278                      }
279                      catch (FileNotFoundException JavaDoc fnfExc)
280                      {
281                         // Try to search for it
282
isConfigFile = findConfigFile(m_strPropertyFileName);
283                      }
284                   }
285                }
286                else
287                {
288                   s_logger.config("Name of configuration file" +
289                                   " is specified programatically = "
290                                   + m_strPropertyFileName);
291                      
292                   try
293                   {
294                      // Open the file
295
isConfigFile = new FileInputStream JavaDoc(m_strPropertyFileName);
296                      m_strConfigFile = m_strPropertyFileName;
297                   }
298                   catch (FileNotFoundException JavaDoc fnfExc)
299                   {
300                      // Try to search for it
301
isConfigFile = findConfigFile(m_strPropertyFileName);
302                   }
303                }
304    
305                BufferedInputStream JavaDoc bisConfigFile = null;
306                
307                // Load the properties
308
s_logger.config("Using configuration file " + m_strConfigFile);
309                m_prpProperties = new Properties JavaDoc();
310                try
311                {
312                   bisConfigFile = new BufferedInputStream JavaDoc(isConfigFile);
313                   m_prpProperties.load(bisConfigFile);
314                }
315                finally
316                {
317                   // Close the file
318
try
319                   {
320                      if (bisConfigFile != null)
321                      {
322                         bisConfigFile.close();
323                      }
324                   }
325                   catch (IOException JavaDoc ioeExc)
326                   {
327                      // Ignore this
328
s_logger.log(Level.WARNING,
329                                          "Failed to close buffer for configuration file.",
330                                          ioeExc);
331                   }
332                }
333             }
334             finally
335             {
336                try
337                {
338                   if (isConfigFile != null)
339                   {
340                      isConfigFile.close();
341                   }
342                }
343                catch (IOException JavaDoc ioeExc)
344                {
345                   // Ignore this
346
s_logger.log(Level.WARNING,
347                                       "Failed to close configuration file.",
348                                       ioeExc);
349                }
350             }
351             
352             // If we read any properties see if there was a dependent file
353
// specified and if so then read its settings and replace them
354
// with the ones redefined in this file
355
if (m_prpProperties != null)
356             {
357                String JavaDoc strDependentPropertyFile;
358                
359                strDependentPropertyFile = Config.getStringProperty(
360                                                     m_prpProperties,
361                                                     DEPENDENT_CONFIG_FILE_NAME,
362                                                     "",
363                                                     "Dependent property file",
364                                                     true);
365                if (strDependentPropertyFile.length() > 0)
366                {
367                   // A dependent property file was specified so load it first
368
// and then add on top of it the current properties
369
Config dependentFile;
370                   Properties JavaDoc prpSettings;
371                   
372                   dependentFile = new Config(strDependentPropertyFile);
373                   prpSettings = dependentFile.getPropertiesSafely();
374                   // Now replace the settings which were redefined in this file
375
prpSettings.putAll(m_prpProperties);
376                   // And the end result will be the current properties
377
m_prpProperties = prpSettings;
378                }
379             }
380          }
381       }
382       
383       return m_prpProperties;
384    }
385
386    /**
387     * Find and configuration file. The calling class is responsible for
388     * closing the input stream
389     *
390     * @param strConfigFileName - name of the configuration file
391     * @return InputStream - opened config file
392     * @throws IOException - an error has occured opening configuration file
393     * @throws FileNotFoundException - file cannot be found
394     */

395    protected InputStream JavaDoc findConfigFile(
396       String JavaDoc strConfigFileName
397    ) throws IOException JavaDoc,
398             FileNotFoundException JavaDoc
399    {
400       InputStream JavaDoc isConfigFile = null;
401
402       m_urlDefaultPropertyFile = ClassLoader.getSystemResource(strConfigFileName);
403       if (m_urlDefaultPropertyFile == null)
404       {
405          // if there was not found configuration file by using
406
// ClassLoader.getSystemResource(), try to use
407
// getClass().getClassLoader().getResource()
408
// This is here due to the fact that in J2EE server
409
// ClassLoader.getSystemResource() search the configuration file
410
// OUTSIDE of the packaged application (ear, war, jar) so that this
411
// file can override the configuration file which is packaged INSIDE
412
// the application which will be found belog with class loader
413
// for this class
414
m_urlDefaultPropertyFile = this.getClass().getClassLoader().getResource(
415                                                                         strConfigFileName);
416       }
417       if (m_urlDefaultPropertyFile == null)
418       {
419          throw new FileNotFoundException JavaDoc("Cannot find configuration file "
420                                          + strConfigFileName);
421       }
422       else
423       {
424          isConfigFile = m_urlDefaultPropertyFile.openStream();
425          m_strConfigFile = m_urlDefaultPropertyFile.toString();
426       }
427       
428       return isConfigFile;
429    }
430    
431    /**
432     * Load all properties from default configuration file or return the ones
433     * passed into the constructor. If this file doesn't exists, a new one will
434     * be created and empty property list will be returned.
435     *
436     * @return Properties - set of properties for this application.
437     */

438    public Properties JavaDoc getPropertiesSafely(
439    )
440    {
441       Properties JavaDoc prpSettings;
442       
443       try
444       {
445          prpSettings = getProperties();
446       }
447       catch (FileNotFoundException JavaDoc fnfeExc)
448       {
449          s_logger.log(Level.WARNING,
450                       "Cannot find default configuration file " + m_strPropertyFileName,
451                       fnfeExc);
452          // This will initialize it to defaults
453
prpSettings = new Properties JavaDoc();
454          m_prpProperties = prpSettings;
455          // This will cause it to create the default file further down
456
if ((m_strPropertyFileName == null)
457             || (m_strPropertyFileName.length() == 0))
458          {
459             setPropertyFileName(Config.DEFAULT_CONFIG_FILE_NAME);
460          }
461          else
462          {
463             setPropertyFileName(m_strPropertyFileName);
464          }
465       }
466       catch (IOException JavaDoc ioeExc)
467       {
468          s_logger.log(Level.CONFIG,
469                              "Error has occured while accessing default configuration file.",
470                              ioeExc);
471          // This will initialize it to defaults
472
prpSettings = new Properties JavaDoc();
473          m_prpProperties = prpSettings;
474          // This will cause it to create the default file further down
475
if ((m_strPropertyFileName == null)
476             || (m_strPropertyFileName.length() == 0))
477          {
478             setPropertyFileName(Config.DEFAULT_CONFIG_FILE_NAME);
479          }
480          else
481          {
482             setPropertyFileName(m_strPropertyFileName);
483             
484          }
485       }
486       
487       return prpSettings;
488    }
489    
490    /**
491     * Save the configuration to a file.
492     *
493     * @throws IOException - there was a problem saving configuration file.
494     * @throws FileNotFoundException - file cannot be found
495     */

496    public void save(
497    ) throws IOException JavaDoc,
498             FileNotFoundException JavaDoc
499    {
500       // Allow only one save a time, it is save to synchronize on properties
501
// since at this time they have to be initialized, if not, null pointer
502
// exception will be thrown
503
synchronized (m_prpProperties)
504       {
505          // Open the file
506
OutputStream JavaDoc osConfigFile = null;
507
508          try
509          {
510             if (m_strPropertyFileName != null)
511             {
512                // Open the file
513
osConfigFile = new FileOutputStream JavaDoc(m_strPropertyFileName);
514             }
515             else if (m_urlDefaultPropertyFile != null)
516             {
517                osConfigFile = new FileOutputStream JavaDoc(m_urlDefaultPropertyFile.getFile());
518             }
519             else
520             {
521                throw new FileNotFoundException JavaDoc("No configuration file defined.");
522             }
523    
524             BufferedOutputStream JavaDoc bosConfigFile = null;
525                     
526             // Store the properties
527
try
528             {
529                bosConfigFile = new BufferedOutputStream JavaDoc(osConfigFile);
530                
531                // TODO: Improve: Once this is invoked, all the comments from
532
// the original file are lost and the properties are in random
533
// order. Figure out how to save it so we don't mess up the
534
// comments and order/grouping of properties
535
m_prpProperties.store(bosConfigFile, "DO NOT MODIFY THIS FILE DIRECTLY.");
536             }
537             finally
538             {
539                // Close the file
540
try
541                {
542                   if (bosConfigFile != null)
543                   {
544                      bosConfigFile.close();
545                   }
546                }
547                catch (IOException JavaDoc ioeExc)
548                {
549                   // Ignore this
550
s_logger.log(Level.WARNING,
551                                       "Failed to close buffer for configuration file "
552                                       + m_strPropertyFileName, ioeExc);
553                }
554             }
555          }
556          finally
557          {
558             try
559             {
560                if (osConfigFile != null)
561                {
562                   osConfigFile.close();
563                }
564             }
565             catch (IOException JavaDoc ioeExc)
566             {
567                // Ignore this
568
s_logger.log(Level.WARNING,
569                                    "Failed to close configuration file "
570                                    + m_strPropertyFileName, ioeExc);
571             }
572          }
573       }
574    }
575    
576    /**
577     * Save the configuration to a file.
578     *
579     * @param fileConfig - file to save the properties to
580     * @param prpSettings - configuration settings to store to the file
581     * @throws IOException - there was a problem saving configuration file.
582     * @throws FileNotFoundException - file cannot be found
583     */

584    public static void save(
585       File JavaDoc fileConfig,
586       Properties JavaDoc prpSettings
587    ) throws IOException JavaDoc,
588             FileNotFoundException JavaDoc
589    {
590       // Open the file
591
OutputStream JavaDoc osConfigFile = null;
592
593       try
594       {
595          // Open the file
596
osConfigFile = new FileOutputStream JavaDoc(fileConfig);
597
598          BufferedOutputStream JavaDoc bosConfigFile = null;
599                  
600          // Load the properties
601
try
602          {
603             bosConfigFile = new BufferedOutputStream JavaDoc(osConfigFile);
604             
605             // TODO: Improve: Once this is invoked, all the comments from
606
// the original file are lost and the properties are in random
607
// order. Figure out how to save it so we don't mess up the
608
// comments and order/grouping of properties
609
prpSettings.store(bosConfigFile, "DO NOT MODIFY THIS FILE DIRECTLY.");
610          }
611          finally
612          {
613             // Close the file
614
try
615             {
616                if (bosConfigFile != null)
617                {
618                   bosConfigFile.close();
619                }
620             }
621             catch (IOException JavaDoc ioeExc)
622             {
623                // Ignore this
624
s_logger.log(Level.WARNING,
625                                    "Failed to close buffer for configuration file "
626                                    + fileConfig.getCanonicalPath(), ioeExc);
627             }
628          }
629       }
630       finally
631       {
632          try
633          {
634             if (osConfigFile != null)
635             {
636                osConfigFile.close();
637             }
638          }
639          catch (IOException JavaDoc ioeExc)
640          {
641             // Ignore this
642
s_logger.log(Level.WARNING,
643                                 "Failed to close configuration file "
644                                 + fileConfig.getCanonicalPath(), ioeExc);
645          }
646       }
647    }
648    
649    /**
650     * Get the name of the propety file.
651     *
652     * @return String - property file name or null if none set
653     */

654    public String JavaDoc getPropertyFileName(
655    )
656    {
657       return m_strPropertyFileName;
658    }
659    
660    /**
661     * Get full location of the property file. The file may be located in the file
662     * system or in jar file.
663     *
664     * @return String - full location of the property file including file name.
665     */

666    public String JavaDoc getFullPropertyFileName(
667    )
668    {
669       return m_strConfigFile;
670    }
671
672    /**
673     * Set the name of the propety file.
674     *
675     * @param strPropertyFileName - the strPropertyFileName to set
676     */

677    public void setPropertyFileName(
678       String JavaDoc strPropertyFileName
679    )
680    {
681       m_strPropertyFileName = strPropertyFileName;
682    }
683    
684    /**
685     * Test if specified value is true according to config file rules.
686     *
687     * @param strValue - value to test
688     * @return boolean - true if the value specifies boolean true
689     */

690    public static boolean isTrue(
691       String JavaDoc strValue
692    )
693    {
694       // We don't have to even do equalsIgnoreCase since this is a number.
695
return VALUE_YES_TRUE.equals(strValue)
696              || VALUE_YES_TRUE_BOOLEAN.equalsIgnoreCase(strValue);
697    }
698    
699    /**
700     * Retrieve integer property value of which should existin within a specified
701     * range.
702     *
703     * @param prpSettings - properties to retrieve the setting from
704     * @param strProperty - name of the property to retrieve
705     * @param iDefaultValue - default value to use if a valid value is not specified
706     * @param strDisplayName - user friendly name of the property
707     * @param iMinValue - inclusive minimal value of the range
708     * @param iMaxValue - inclusive maximal value of the range
709     * @return int - value of the property or default value if the value is not
710     * specified
711     */

712    public static int getIntPropertyInRange(
713       Properties JavaDoc prpSettings,
714       String JavaDoc strProperty,
715       int iDefaultValue,
716       String JavaDoc strDisplayName,
717       int iMinValue,
718       int iMaxValue
719    )
720    {
721       String JavaDoc strParam;
722       int iValue;
723       
724       // Read the property, use the name of the property as default value to
725
// detect if property is not set
726
strParam = prpSettings.getProperty(strProperty, strProperty);
727       if ((strParam.length() == 0) || (strParam.equals(strProperty)))
728       {
729          s_logger.config(strDisplayName + " is not set in property "
730                          + strProperty + ", using default value "
731                          + iDefaultValue);
732          iValue = iDefaultValue;
733       }
734       else
735       {
736          try
737          {
738             iValue = Integer.parseInt(strParam);
739             if ((iValue < iMinValue) || (iValue > iMaxValue))
740             {
741                s_logger.config("Value of " + strProperty
742                                + " property is outside of valid range ("
743                                + iMinValue + " - " + iMaxValue
744                                + "), using default value "
745                                + iDefaultValue);
746                iValue = iDefaultValue;
747             }
748          }
749          catch (NumberFormatException JavaDoc nfeExc)
750          {
751             s_logger.config("Value of " + strProperty
752                             + " property is incorrect ("
753                             + strParam + ", valid range is "
754                             + iMinValue + " - " + iMaxValue
755                             + "), using default value "
756                             + iDefaultValue);
757             iValue = iDefaultValue;
758          }
759       }
760       s_logger.config(strProperty + " = " + iValue);
761       
762       return iValue;
763    }
764
765    /**
766     * Retrieve string property value and if the value is not specified throw
767     * an exception.
768     *
769     * @param configFile - configuration file to retrieve the setting from
770     * @param strProperty - name of the property to retrieve
771     * @param strDisplayName - user friendly name of the property
772     * @return String - value of the property or default value if the value is not
773     * specified
774     * @throws OSSConfigException - value for the requested property is not specified
775     */

776    public static String JavaDoc getStringProperty(
777       Config configFile,
778       String JavaDoc strProperty,
779       String JavaDoc strDisplayName
780    ) throws OSSConfigException
781    {
782       Properties JavaDoc prpSettings;
783       String JavaDoc strParam;
784       String JavaDoc strValue;
785       
786       prpSettings = configFile.getPropertiesSafely();
787       strParam = prpSettings.getProperty(strProperty, strProperty);
788       if ((strParam.length() == 0) || (strParam.equals(strProperty)))
789       {
790          throw new OSSConfigException(strDisplayName + " is not set in property "
791                                       + strProperty + " in configuration file"
792                                       + configFile.getPropertyFileName());
793       }
794       else
795       {
796          strValue = strParam;
797       }
798       s_logger.config(strProperty + " = " + strValue);
799       
800       return strValue;
801    }
802
803    /**
804     * Retrieve string property value and if the value is not specified or it is
805     * empty throw an exception.
806     *
807     * @param prpSettings - properties to retrieve the setting from
808     * @param strProperty - name of the property to retrieve
809     * @param strDisplayName - user friendly name of the property
810     * @return String - value of the property
811     * @throws OSSConfigException - value for the requested property is not specified
812     */

813    public static String JavaDoc getStringProperty(
814       Properties JavaDoc prpSettings,
815       String JavaDoc strProperty,
816       String JavaDoc strDisplayName
817    ) throws OSSConfigException
818    {
819       return getStringProperty(prpSettings, strProperty, strDisplayName, false);
820    }
821
822    /**
823     * Retrieve string property value and if the value is not specified throw
824     * an exception.
825     *
826     * @param prpSettings - properties to retrieve the setting from
827     * @param strProperty - name of the property to retrieve
828     * @param strDisplayName - user friendly name of the property
829     * @param bAllowEmpty - if true then empty value is allowed
830     * @return String - value of the property
831     * @throws OSSConfigException - value for the requested property is not specified
832     */

833    public static String JavaDoc getStringProperty(
834       Properties JavaDoc prpSettings,
835       String JavaDoc strProperty,
836       String JavaDoc strDisplayName,
837       boolean bAllowEmpty
838    ) throws OSSConfigException
839    {
840       String JavaDoc strParam;
841       String JavaDoc strValue;
842       
843       strParam = prpSettings.getProperty(strProperty, strProperty);
844       if ((strParam.equals(strProperty))
845          || ((!bAllowEmpty) && (strParam.length() == 0)))
846       {
847          throw new OSSConfigException(strDisplayName + " is not set in property "
848                                       + strProperty);
849       }
850       else
851       {
852          strValue = strParam;
853       }
854       s_logger.config(strProperty + " = " + strValue);
855       
856       return strValue;
857    }
858
859    /**
860     * Retrieve string property value and if the property is not specified or it
861     * is specified as an empty value, use the default value instead.
862     *
863     * @param prpSettings - properties to retrieve the setting from
864     * @param strProperty - name of the property to retrieve
865     * @param strDefaultValue - default value to use if a valid value is not specified
866     * @param strDisplayName - user friendly name of the property
867     * @return String - value of the property or default value if the value is not
868     * specified
869     */

870    public static String JavaDoc getStringProperty(
871       Properties JavaDoc prpSettings,
872       String JavaDoc strProperty,
873       String JavaDoc strDefaultValue,
874       String JavaDoc strDisplayName
875    )
876    {
877       return getStringProperty(prpSettings, strProperty, strDefaultValue,
878                                strDisplayName, false);
879    }
880    
881    /**
882     * Retrieve string property value.
883     *
884     * @param prpSettings - properties to retrieve the setting from
885     * @param strProperty - name of the property to retrieve
886     * @param strDefaultValue - default value to use if a valid value is not specified
887     * @param strDisplayName - user friendly name of the property
888     * @param bAllowEmpty - if true then empty value is allowed
889     * @return String - value of the property or default value if the value is not
890     * specified
891     */

892    public static String JavaDoc getStringProperty(
893       Properties JavaDoc prpSettings,
894       String JavaDoc strProperty,
895       String JavaDoc strDefaultValue,
896       String JavaDoc strDisplayName,
897       boolean bAllowEmpty
898    )
899    {
900       String JavaDoc strParam;
901       String JavaDoc strValue;
902       
903       // Read the property, use the name of the property as default value to
904
// detect if property is not set
905
strParam = prpSettings.getProperty(strProperty, strProperty);
906       if ((strParam.equals(strProperty))
907          || ((!bAllowEmpty) && (strParam.length() == 0)))
908       {
909          s_logger.config(strDisplayName + " is not set in property "
910                          + strProperty + ", using default value "
911                          + strDefaultValue);
912          strValue = strDefaultValue;
913       }
914       else
915       {
916          strValue = strParam;
917       }
918       s_logger.config(strProperty + " = " + strValue);
919       
920       return strValue;
921    }
922
923    /**
924     * Retrieve boolean property value and if the value is not specified throw
925     * an exception.
926     *
927     * @param prpSettings - properties to retrieve the setting from
928     * @param strProperty - name of the property to retrieve
929     * @param strDisplayName - user friendly name of the property
930     * @return boolean - value of the property
931     * @throws OSSConfigException - value for the requested property is not specified
932     */

933    public static boolean getBooleanProperty(
934       Properties JavaDoc prpSettings,
935       String JavaDoc strProperty,
936       String JavaDoc strDisplayName
937    ) throws OSSConfigException
938    {
939       String JavaDoc strParam;
940       boolean bValue;
941       
942       strParam = prpSettings.getProperty(strProperty, strProperty);
943       if ((strParam.equals(strProperty)) || (strParam.length() == 0))
944       {
945          throw new OSSConfigException(strDisplayName + " is not set in property "
946                                       + strProperty);
947       }
948       else
949       {
950          bValue = Config.isTrue(strParam);
951       }
952       s_logger.config(strProperty + " = " + bValue);
953       
954       return bValue;
955    }
956
957    /**
958     * Retrieve boolean property value and if the value is not specified throw
959     * an exception.
960     *
961     * @param prpSettings - properties to retrieve the setting from
962     * @param strProperty - name of the property to retrieve
963     * @param bDefaultValue - default value to use if a valid value is not specified
964     * @param strDisplayName - user friendly name of the property
965     * @return boolean - value of the property or default value if the value is
966     * not specified
967     */

968    public static boolean getBooleanProperty(
969       Properties JavaDoc prpSettings,
970       String JavaDoc strProperty,
971       boolean bDefaultValue,
972       String JavaDoc strDisplayName
973    )
974    {
975       String JavaDoc strParam;
976       boolean bValue;
977       
978       strParam = prpSettings.getProperty(strProperty, strProperty);
979       if ((strParam.equals(strProperty)) || (strParam.length() == 0))
980       {
981          s_logger.config(strDisplayName + " is not set in property "
982                          + strProperty + ", using default value "
983                          + bDefaultValue);
984          bValue = bDefaultValue;
985       }
986       else
987       {
988          bValue = Config.isTrue(strParam);
989       }
990       s_logger.config(strProperty + " = " + bValue);
991       
992       return bValue;
993    }
994
995    /**
996     * Retrieve boolean property value and if the value is not specified throw
997     * an exception.
998     *
999     * @param prpSettings - properties to retrieve the setting from
1000    * @param strProperty - name of the property to retrieve
1001    * @param bDefaultValue - default value to use if a valid value is not specified
1002    * @param strDisplayName - user friendly name of the property
1003    * @return String - value of the property or default value if the value is
1004    * not specified
1005    */

1006   public static String JavaDoc getBooleanPropertyAsString(
1007      Properties JavaDoc prpSettings,
1008      String JavaDoc strProperty,
1009      boolean bDefaultValue,
1010      String JavaDoc strDisplayName
1011   )
1012   {
1013      String JavaDoc strReturn;
1014   
1015      if (getBooleanProperty(prpSettings, strProperty, bDefaultValue, strDisplayName))
1016      {
1017         strReturn = Boolean.TRUE.toString();
1018      }
1019      else
1020      {
1021         strReturn = Boolean.FALSE.toString();
1022      }
1023      
1024      return strReturn;
1025   }
1026}
1027
Popular Tags