KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > presumo > util > config > PropertyFileConfiguration


1 /**
2  * This file is part of Presumo.
3  *
4  * Presumo is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * Presumo is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Presumo; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  *
19  * Copyright 2001 Dan Greff
20  */

21 package com.presumo.util.config;
22
23 import java.io.File JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26
27 import java.util.Enumeration JavaDoc;
28 import java.util.Properties JavaDoc;
29 import java.util.Vector JavaDoc;
30
31 /**
32  * Jms Configuration wrapper based on property files.
33  */

34 public class PropertyFileConfiguration extends Configuration
35 {
36
37   private static final String JavaDoc JMS_BASEDIR_PROPERTY = "jmsbasedir";
38   private static final String JavaDoc SYSTEM_PROPERTIES_NAME = "JmsConfig";
39   private static final String JavaDoc SUFFIX = ".properties";
40   private static final String JavaDoc CONFIG_SUBDIRECTORY = "config/";
41   
42   private String JavaDoc baseDir;
43   private String JavaDoc configDir;
44   private String JavaDoc mainPropFile;
45
46
47     ///////////////////////////////////////////////////////////////////////////
48
// Constructors //
49
///////////////////////////////////////////////////////////////////////////
50

51   PropertyFileConfiguration()
52   {
53     baseDir = System.getProperty(JMS_BASEDIR_PROPERTY, "./");
54     
55     if (! baseDir.endsWith("/"))
56       baseDir = baseDir + "/";
57     
58     configDir = baseDir + CONFIG_SUBDIRECTORY;
59     mainPropFile = configDir + SYSTEM_PROPERTIES_NAME + SUFFIX;
60   }
61
62     ///////////////////////////////////////////////////////////////////////////
63
// Public Methods //
64
///////////////////////////////////////////////////////////////////////////
65

66   /**
67    * Used to retrieve the preferences object for the entire system.
68    *
69    * @return The preference object, or null if an error occurred.
70    */

71   public Preferences getSystemPreferences()
72   {
73     PropertyPreferenceWrapper systemPreferences = null;
74  
75     try {
76       FileInputStream JavaDoc fis = new FileInputStream JavaDoc(mainPropFile);
77       try {
78         Properties JavaDoc props = new Properties JavaDoc();
79         props.load(fis);
80         systemPreferences = new PropertyPreferenceWrapper(props);
81       } finally {
82         fis.close();
83       }
84     } catch (IOException JavaDoc ioe) {
85       //
86
// TODO:: log error.
87
System.err.println("An exception occurred while retrieving"+
88                          "Jms system properties:\n" + ioe.toString());
89     }
90          
91     return systemPreferences;
92   }
93
94   /**
95    * Used to retrieve the preferences object for the specified subsystem.
96    *
97    * @return The preference object if found, or null if an error occurred
98    * or it was not found.
99    */

100   public Preferences getSubsystemPreferences(String JavaDoc subsystem)
101   {
102     PropertyPreferenceWrapper subPreferences = null;
103  
104     try {
105       FileInputStream JavaDoc fis = new FileInputStream JavaDoc(configDir+subsystem+SUFFIX);
106       try {
107         Properties JavaDoc props = new Properties JavaDoc();
108         props.load(fis);
109         subPreferences = new PropertyPreferenceWrapper(props);
110       } finally {
111         fis.close();
112       }
113     } catch (IOException JavaDoc ioe) {
114       System.err.println("An exception occurred while retrieving"+
115                          "Jms subsystem properties:\n" + ioe.toString());
116     }
117          
118     return subPreferences;
119   }
120
121   /**
122    * List all subsystems containing configuration.
123    */

124   public Enumeration JavaDoc getSubsystems()
125   {
126     String JavaDoc [] files = new File JavaDoc(configDir).list();
127     
128     Vector JavaDoc retval = new Vector JavaDoc(files.length);
129     for (int i=0; i < files.length; i++) {
130       if (files[i].endsWith(SUFFIX) &&
131           ! ( files[i].startsWith(SYSTEM_PROPERTIES_NAME)) )
132       {
133         retval.add(files[i].substring(0, files[i].length() - SUFFIX.length()));
134       }
135     }
136     return retval.elements();
137   }
138   
139     /////////////////////////////////////////////////////////////////////////
140
// Begin Inner Class PropertyPreferenceWrapper //
141
/////////////////////////////////////////////////////////////////////////
142

143   public class PropertyPreferenceWrapper implements Preferences
144   {
145     
146     private final Properties JavaDoc props;
147     
148     PropertyPreferenceWrapper(Properties JavaDoc props)
149     {
150       this.props = props;
151     }
152     
153     public String JavaDoc get(String JavaDoc key)
154     {
155       return props.getProperty(key);
156     }
157     
158     public String JavaDoc get(String JavaDoc key, String JavaDoc defaultVal)
159     {
160       return props.getProperty(key, defaultVal);
161     }
162     
163     public Enumeration JavaDoc names()
164     {
165       return props.propertyNames();
166     }
167     
168     public String JavaDoc toString()
169     {
170       return super.toString() + props.toString();
171     }
172   }
173     /////////////////////////////////////////////////////////////////////////
174
// End Inner Class PropertyPreferenceWrapper //
175
/////////////////////////////////////////////////////////////////////////
176

177
178 }
179
Popular Tags