KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > util > AbsConfigFile


1  /*
2   * Enhydra Java Application Server Project
3   *
4   * The contents of this file are subject to the Enhydra Public License
5   * Version 1.1 (the "License"); you may not use this file except in
6   * compliance with the License. You may obtain a copy of the License on
7   * the Enhydra web site ( http://www.enhydra.org/ ).
8   *
9   * Software distributed under the License is distributed on an "AS IS"
10   * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11   * the License for the specific terms governing rights and limitations
12   * under the License.
13   *
14   * The Initial Developer of the Enhydra Application Server is Lutris
15   * Technologies, Inc. The Enhydra Application Server and portions created
16   * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17   * All Rights Reserved.
18   *
19   * Contributor(s):
20   *
21   * $Id: AbsConfigFile.java,v 1.1 2005/07/13 11:09:06 slobodan Exp $
22   */

23 package org.enhydra.util;
24
25 import java.io.File JavaDoc;
26 import java.io.FileNotFoundException JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.OutputStream JavaDoc;
30 import java.util.Hashtable JavaDoc;
31 import java.util.Vector JavaDoc;
32
33 import com.lutris.util.Config;
34 import com.lutris.util.ConfigException;
35 import com.lutris.util.KeywordValueException;
36 import com.lutris.util.KeywordValueTable;
37
38    /**
39     * AbsConfigFile is abstract class which contains methods used to manipulate
40     * application's configuration file to read its configuration parameters.
41     *
42     * @see Config, ConfigFileInterface
43     * @author Tanja Jovanovic
44     * @version 1.0
45     */

46
47 public abstract class AbsConfigFile implements ConfigFileInterface{
48
49    /**
50     * The configuration object to which this configuration file belongs to.
51     */

52    protected Config config;
53
54   /**
55    * Order in which are configuration parameters added.
56    */

57   protected Vector JavaDoc order;
58
59   /**
60    * Comments of configuration parameters.
61    */

62   protected Hashtable JavaDoc comments;
63
64   /**
65    * The associated file (if any).
66    */

67   protected File JavaDoc file = null;
68   
69   /**
70    * JNDI Adapter used for reading application's parameters.
71    */

72  protected JNDIAdapter jndiAdapt = null;
73   /**
74    * Parameters from configuration file whose values are jndi names of resources.
75    */

76  protected Hashtable JavaDoc jndiParameterNames = null;
77
78  /**
79   * Default constructor for an empty configuration file.
80   */

81   public AbsConfigFile () {
82     config = new Config();
83     order = new Vector JavaDoc();
84     comments = new Hashtable JavaDoc();
85     jndiParameterNames = new Hashtable JavaDoc();
86   }
87
88 /**
89  * Constructor from an InputStream.
90  * @param inputStream The input stream from which to parse the config file.
91  * @exception ConfigException
92  */

93 /*
94   public AbsConfigFile (InputStream inputStream) throws ConfigException {
95   }
96 */

97
98 /**
99  * Constructor from a File. Allows to later write back the configuration to the
100  * same file.
101  * @param file The local file to parse.
102  * @exception IOException
103  * @exception ConfigException
104  */

105   public AbsConfigFile (File JavaDoc file) throws ConfigException, IOException JavaDoc {
106    this();
107    
108 // tj 25.01.2004.
109
// this(new FileInputStream(configFile));
110
this.file = file;
111    try {
112     readJndi();
113    } catch (Exception JavaDoc e){}
114    config.setConfigFile(this);
115   }
116
117 /**
118  * Constructor from a KeywordValueTable.
119  * @param kvt A KeywordValueTable from which to populate the configuration file.
120  * @exception ConfigException
121  */

122   public AbsConfigFile(KeywordValueTable kvt) throws ConfigException {
123     config = new Config(kvt);
124     order = new Vector JavaDoc();
125     comments = new Hashtable JavaDoc();
126     jndiParameterNames = new Hashtable JavaDoc();
127   }
128
129 /**
130  * Reads application configuration parameters using JNDI Context.
131  * Subclasses should override this method.
132  */

133
134  protected void readJndi() throws ConfigException {}
135
136 /**
137  * Returns the Config object representing the config data in the file.
138  * @return The Config object for this ConfigFile.
139  */

140   public Config getConfig() {
141     return config;
142   }
143
144 /**
145  * Returns the comment associated with a given key, or <code>null</code> if
146  * there is no comment. Pass in <code>ConfigFileInterface.TRAILING_COMMENT</code> to get
147  * the trailing comment.
148  * @param key the key to look up.
149  * @return the associated comment or <code>null</code>
150  */

151   public String JavaDoc getComment(String JavaDoc key) {
152     return (String JavaDoc)comments.get(key);
153   }
154
155 /**
156  * Adds an entry to the configuration.
157  * @param key The config element name.
158  * @param values A string array of values.
159  * @param comment A string containing a properly commented configuration file
160  * comment.
161  * @exception KeywordValueException
162  */

163   public void addEntry(String JavaDoc key, String JavaDoc[] values, String JavaDoc comment)
164     throws KeywordValueException {
165
166 // Don't add an actual config entry for the trailing comment
167
if (!key.equals(TRAILING_COMMENT)) {
168       config.set(key, values);
169       try {
170         if (jndiAdapt != null) {
171           String JavaDoc jndiName = JNDIAdapter.makeContextString(key);
172           String JavaDoc jndiValue = JNDIAdapter.makeStringFromStrings(values);
173           jndiAdapt.set(jndiName+"[]", jndiValue);
174         }
175       }
176       catch (Exception JavaDoc ex){
177         System.err.println("Error in addEntry method of AbsConfigFile");
178       }
179       if (!order.contains(key)) {
180         order.addElement(key);
181       }
182     }
183     comments.put(key, comment);
184   }
185
186 /**
187  * Adds an entry to the configuration.
188  * @param key The config element name.
189  * @param value A String value.
190  * @param comment A string containing a properly commented configuration file
191  * comment.
192  * @exception KeywordValueException
193  */

194   public void addEntry(String JavaDoc kkey, String JavaDoc value, String JavaDoc comment)
195     throws KeywordValueException {
196 // Don't add an actual config entry for the trailing comment
197
if (!kkey.equals(TRAILING_COMMENT)) {
198      config.set(kkey, value);
199       try {
200         if (jndiAdapt != null) {
201           String JavaDoc jndiName = JNDIAdapter.makeContextString(kkey);
202           jndiAdapt.set(jndiName, value);
203         }
204       }
205       catch (Exception JavaDoc ex){
206         System.err.println("Error in addEntry method of AbsConfigFile");
207       }
208      if (!order.contains(kkey)) {
209        order.addElement(kkey);
210      }
211     }
212     comments.put(kkey, comment);
213   }
214
215 /**
216  * Removes an entry from the configuration.
217  * @param key The config element name.
218  * @exception KeywordValueException
219  */

220   public void removeEntry(String JavaDoc key) throws KeywordValueException {
221 // There is no config entry for the trailing comment
222
if (!key.equals(TRAILING_COMMENT)) {
223       config.remove(key);
224       if (jndiAdapt != null) {
225         String JavaDoc jndiName = JNDIAdapter.makeContextString(key);
226         try {
227           jndiAdapt.remove(jndiName);
228         }
229         catch (Exception JavaDoc ex){
230           System.err.println("Error in removeEntry method of AbsConfigFile 1");
231         }
232         try {
233           jndiAdapt.remove(jndiName+"[]");
234         }
235         catch (Exception JavaDoc ex){
236           System.err.println("Error in removeEntry method of AbsConfigFile 2");
237         }
238       }
239       
240       order.removeElement(key);
241     }
242     comments.remove(key);
243   }
244
245 /**
246  * Gets the associated file. If no file is associated with this config, <code>null</code> is
247  * returned.
248  * @return associated file
249  */

250   public File JavaDoc getFile() {
251     return file;
252   }
253
254 /**
255  * Sets the configuration file. This method can be useful in case when in
256  * construction of ConfigFile object is not defined associated file. After the
257  * file is set, Configuration parameters are read by using JNDI Context.
258  * @param file given reference to configuration file represented as File object.
259  */

260   public void setFile(File JavaDoc file) {
261     this.file = file;
262     try {
263       readJndi();
264     } catch (Exception JavaDoc e){}
265   }
266
267 /**
268  * Writes this config to the associated configuration file. If no file is
269  * associated with this config, throws a <code>FileNotFoundException</code>.
270  * @exception IOException
271  * @exception FileNotFoundException
272  */

273   public void write() throws IOException JavaDoc, FileNotFoundException JavaDoc {
274     if (file == null) {
275         throw new FileNotFoundException JavaDoc("No file associated with this object");
276     }
277     FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(file);
278     write(out);
279     out.close();
280   }
281
282 /**
283  * Writes out a configuration file to the OutputStream specified.
284  * @param outputStream The output stream on which to write the config file.
285  */

286   public abstract void write(OutputStream JavaDoc outputStream);
287 }
Popular Tags