001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: * $Id: AbsConfigFile.java,v 1.2 2006-06-15 13:40:47 sinisa Exp $
022: */
023: package org.enhydra.util;
024:
025: import java.io.File;
026: import java.io.FileNotFoundException;
027: import java.io.FileOutputStream;
028: import java.io.IOException;
029: import java.io.OutputStream;
030: import java.util.Hashtable;
031: import java.util.Vector;
032:
033: import com.lutris.util.Config;
034: import com.lutris.util.ConfigException;
035: import com.lutris.util.KeywordValueException;
036: import com.lutris.util.KeywordValueTable;
037:
038: /**
039: * AbsConfigFile is abstract class which contains methods used to manipulate
040: * application's configuration file to read its configuration parameters.
041: *
042: * @see Config, ConfigFileInterface
043: * @author Tanja Jovanovic
044: * @version 1.0
045: */
046:
047: public abstract class AbsConfigFile implements ConfigFileInterface {
048:
049: /**
050: * The configuration object to which this configuration file belongs to.
051: */
052: protected Config config;
053:
054: /**
055: * Order in which are configuration parameters added.
056: */
057: protected Vector order;
058:
059: /**
060: * Comments of configuration parameters.
061: */
062: protected Hashtable comments;
063:
064: /**
065: * The associated file (if any).
066: */
067: protected File file = null;
068:
069: /**
070: * JNDI Adapter used for reading application's parameters.
071: */
072: protected JNDIAdapter jndiAdapt = null;
073: /**
074: * Parameters from configuration file whose values are jndi names of resources.
075: */
076: protected Hashtable jndiParameterNames = null;
077:
078: /**
079: * Default constructor for an empty configuration file.
080: */
081: public AbsConfigFile() {
082: config = new Config();
083: order = new Vector();
084: comments = new Hashtable();
085: jndiParameterNames = new Hashtable();
086: }
087:
088: /**
089: * Constructor from an InputStream.
090: * @param inputStream The input stream from which to parse the config file.
091: * @exception ConfigException
092: */
093: /*
094: public AbsConfigFile (InputStream inputStream) throws ConfigException {
095: }
096: */
097:
098: /**
099: * 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 file) throws ConfigException, IOException {
106: this ();
107:
108: // tj 25.01.2004.
109: // this(new FileInputStream(configFile));
110: this .file = file;
111: try {
112: readJndi();
113: } catch (Exception e) {
114: }
115: config.setConfigFile(this );
116: }
117:
118: /**
119: * Constructor from a KeywordValueTable.
120: * @param kvt A KeywordValueTable from which to populate the configuration file.
121: * @exception ConfigException
122: */
123: public AbsConfigFile(KeywordValueTable kvt) throws ConfigException {
124: config = new Config(kvt);
125: order = new Vector();
126: comments = new Hashtable();
127: jndiParameterNames = new Hashtable();
128: }
129:
130: /**
131: * Reads application configuration parameters using JNDI Context.
132: * Subclasses should override this method.
133: */
134:
135: protected void readJndi() throws ConfigException {
136: }
137:
138: /**
139: * Returns the Config object representing the config data in the file.
140: * @return The Config object for this ConfigFile.
141: */
142: public Config getConfig() {
143: return config;
144: }
145:
146: /**
147: * Returns the comment associated with a given key, or <code>null</code> if
148: * there is no comment. Pass in <code>ConfigFileInterface.TRAILING_COMMENT</code> to get
149: * the trailing comment.
150: * @param key the key to look up.
151: * @return the associated comment or <code>null</code>
152: */
153: public String getComment(String key) {
154: return (String) comments.get(key);
155: }
156:
157: /**
158: * Adds an entry to the configuration.
159: * @param key The config element name.
160: * @param values A string array of values.
161: * @param comment A string containing a properly commented configuration file
162: * comment.
163: * @exception KeywordValueException
164: */
165: public void addEntry(String key, String[] values, String comment)
166: throws KeywordValueException {
167:
168: // Don't add an actual config entry for the trailing comment
169: if (!key.equals(TRAILING_COMMENT)) {
170: config.set(key, values);
171: try {
172: if (jndiAdapt != null) {
173: String jndiName = JNDIAdapter
174: .makeContextString(key);
175: String jndiValue = JNDIAdapter
176: .makeStringFromStrings(values);
177: jndiAdapt.set(jndiName + "[]", jndiValue);
178: }
179: } catch (Exception ex) {
180: System.err
181: .println("Error in addEntry method of AbsConfigFile");
182: }
183: if (!order.contains(key)) {
184: order.addElement(key);
185: }
186: }
187: comments.put(key, comment);
188: }
189:
190: /**
191: * Adds an entry to the configuration.
192: * @param key The config element name.
193: * @param value A String value.
194: * @param comment A string containing a properly commented configuration file
195: * comment.
196: * @exception KeywordValueException
197: */
198: public void addEntry(String kkey, String value, String comment)
199: throws KeywordValueException {
200: // Don't add an actual config entry for the trailing comment
201: if (!kkey.equals(TRAILING_COMMENT)) {
202: config.set(kkey, value);
203: try {
204: if (jndiAdapt != null) {
205: String jndiName = JNDIAdapter
206: .makeContextString(kkey);
207: jndiAdapt.set(jndiName, value);
208: }
209: } catch (Exception ex) {
210: System.err
211: .println("Error in addEntry method of AbsConfigFile");
212: }
213: if (!order.contains(kkey)) {
214: order.addElement(kkey);
215: }
216: }
217: comments.put(kkey, comment);
218: }
219:
220: /**
221: * Removes an entry from the configuration.
222: * @param key The config element name.
223: * @exception KeywordValueException
224: */
225: public void removeEntry(String key) throws KeywordValueException {
226: // There is no config entry for the trailing comment
227: if (!key.equals(TRAILING_COMMENT)) {
228: config.remove(key);
229: if (jndiAdapt != null) {
230: String jndiName = JNDIAdapter.makeContextString(key);
231: try {
232: jndiAdapt.remove(jndiName);
233: } catch (Exception ex) {
234: System.err
235: .println("Error in removeEntry method of AbsConfigFile 1");
236: }
237: try {
238: jndiAdapt.remove(jndiName + "[]");
239: } catch (Exception ex) {
240: System.err
241: .println("Error in removeEntry method of AbsConfigFile 2");
242: }
243: }
244:
245: order.removeElement(key);
246: }
247: comments.remove(key);
248: }
249:
250: /**
251: * Gets the associated file. If no file is associated with this config, <code>null</code> is
252: * returned.
253: * @return associated file
254: */
255: public File getFile() {
256: return file;
257: }
258:
259: /**
260: * Sets the configuration file. This method can be useful in case when in
261: * construction of ConfigFile object is not defined associated file. After the
262: * file is set, Configuration parameters are read by using JNDI Context.
263: * @param file given reference to configuration file represented as File object.
264: */
265: public void setFile(File file) {
266: this .file = file;
267: try {
268: readJndi();
269: } catch (Exception e) {
270: }
271: }
272:
273: /**
274: * Writes this config to the associated configuration file. If no file is
275: * associated with this config, throws a <code>FileNotFoundException</code>.
276: * @exception IOException
277: * @exception FileNotFoundException
278: */
279: public void write() throws IOException, FileNotFoundException {
280: if (file == null) {
281: throw new FileNotFoundException(
282: "No file associated with this object");
283: }
284: FileOutputStream out = new FileOutputStream(file);
285: write(out);
286: out.close();
287: }
288:
289: /**
290: * Writes out a configuration file to the OutputStream specified.
291: * @param outputStream The output stream on which to write the config file.
292: */
293: public abstract void write(OutputStream outputStream);
294: }
|