001: package com.jat.core.config;
002:
003: import java.io.IOException;
004: import java.util.Enumeration;
005: import java.util.Hashtable;
006: import java.util.Vector;
007:
008: import com.jat.util.StringUtil;
009:
010: /**
011: * <p>Title: JAT</p>
012: * <p>Description: This class is responsible to handle configuration information.</p>
013: * <p>Copyright: Copyright (c) 2004 -2005 Stefano Fratini (stefano.fratini@gmail.com)</p>
014: * <p>Distributed under the terms of the GNU Lesser General Public License, v2.1 or later</p>
015: * @author stf
016: * @version 1.1
017: */
018:
019: public class Config extends CGIniFile {
020:
021: /**
022: * Config Constructor
023: * @param filename name of the config file
024: */
025: protected Config(String _filename, boolean current)
026: throws IOException, Exception {
027: super (_filename);
028: if (current)
029: currentConfig_ = this ;
030: else {
031: configs_.put(getFormattedFilename(_filename), this );
032: }
033: }
034:
035: private static String getFormattedFilename(String _filename) {
036: String filename = StringUtil.replaceAll(_filename, "\\", "/");
037: filename = filename.substring(filename.lastIndexOf("/") + 1);
038: return filename;
039: }
040:
041: /**
042: * Get the current config object
043: * @return the current Config Object
044: */
045: public static Config getCurrent() {
046: return currentConfig_;
047: }
048:
049: /**
050: * Get the Config object called _name
051: * @param name the name of the configuration file
052: * @return the Config object
053: */
054: public static Config getConfig(String _name) {
055: return (Config) configs_.get(_name);
056: }
057:
058: /**
059: * Get all configuration file names
060: * @return the list of String of names
061: */
062: public static Enumeration configNames() {
063: return configs_.keys();
064: }
065:
066: /**
067: * Retreive current path from CLASSPATH
068: * Initialize configPath_
069: * set the unique Config instance of class BAProject
070: * @param _projectDirectory path of projet directory
071: * @param _configFiles vector of config files names
072: */
073: public static Config init(String _filename) throws Exception {
074: try {
075: configs_ = new Hashtable();
076: Config config = new Config(_filename, true);
077: config = new Config(_filename, false);
078: return currentConfig_;
079: } catch (IOException exception) {
080: throw new Exception("Config::init : exception = "
081: + exception + "<BR>");
082: }
083: }
084:
085: public void addFiles(Vector _configFiles) throws Exception {
086: for (int i = 0; i < _configFiles.size(); i++) {
087: String filename = (String) _configFiles.elementAt(i);
088: Config conf = new Config(filename, false);
089: this .getCurrent().addConfig(conf);
090: }
091: }
092:
093: /**
094: * add sections heys and value in the a config file from other config files
095: * @config Config a config file
096: *
097: */
098: public void addConfig(Config config) throws Exception {
099: try {
100: Hashtable sectionsHash = config.getAll();
101: Enumeration sectionsEnum = sectionsHash.keys();
102: while (sectionsEnum.hasMoreElements()) {
103: String sectionStr = (String) sectionsEnum.nextElement();
104: Hashtable keysHash = (Hashtable) sectionsHash
105: .get(sectionStr);
106: Enumeration keysEnum = keysHash.keys();
107: while (keysEnum.hasMoreElements()) {
108: String keyStr = (String) keysEnum.nextElement();
109: String value = config.getValue(sectionStr, keyStr);
110: this .getCurrent().addValue(sectionStr, keyStr,
111: value);
112: }
113: }
114: } catch (Exception exception) {
115: throw new Exception("Config::addConfig : error = "
116: + exception + "<BR>");
117: }
118: }
119:
120: public void save(String _filename) throws IOException {
121: if (this == getCurrent())
122: throw new IOException(
123: "Cannot save the current Config object. Please select a different Config object through the getConfig method.");
124: super .save(_filename);
125: }
126:
127: public void save() throws IOException {
128: this .save(this .getFilename());
129: }
130:
131: public synchronized void applyChanges() throws Exception {
132: Hashtable oldSections = getCurrent().getAll();
133: try {
134: getCurrent().removeAll();
135: for (Enumeration e = configs_.elements(); e
136: .hasMoreElements();) {
137: getCurrent().addConfig((Config) e.nextElement());
138: }
139: } catch (Exception ex) {
140: getCurrent().removeAll();
141: getCurrent().setAll(oldSections);
142: throw ex;
143: }
144: }
145:
146: /**
147: * Current Configuration
148: */
149: private static Config currentConfig_ = null;
150: /**
151: * List of file name and Config object
152: */
153: private static Hashtable configs_;
154: }
|