001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.core.config;
019:
020: import java.io.File;
021: import java.io.IOException;
022: import java.util.Hashtable;
023: import java.util.LinkedList;
024: import java.util.List;
025: import java.util.Map;
026: import java.util.logging.Logger;
027:
028: import org.columba.api.shutdown.IShutdownManager;
029: import org.columba.core.base.OSInfo;
030: import org.columba.core.io.DiskIO;
031: import org.columba.core.shutdown.ShutdownManager;
032: import org.columba.core.xml.XmlElement;
033: import org.columba.core.xml.XmlIO;
034:
035: /**
036: * Main entrypoint for configuration management.
037: * <p>
038: * Stores a list of all xml files in a hashtable. Hashtable key is the name of
039: * the xml file. Value is {@link XmlIO} object.
040: * <p>
041: * Mail and Addressbook components are just wrappers, encapsulating this class.
042: * Using these wrapper classes, you don't need to specify the module name (for
043: * example: mail, or addressbook) manually.
044: * <p>
045: * Note that all configuration file have default templates in the /res directory
046: * in package org.columba.core.config. These default configuration files are
047: * copied into the users's configuration directory the first time Columba is
048: * started.
049: * <p>
050: * Config creates the top-level directory for Columba's configuration in
051: * ".columba", which usually resides in the user's home directory or on older
052: * Windows versions in Columba's program folder.
053: * <p>
054: * Saving and loading of all configuration files is handled here, too.
055: * <p>
056: *
057: * @see org.columba.mail.config.MailConfig
058: * @see org.columba.addressbook.config.AddressbookConfig
059: *
060: * @deprecated use XmlConfig instead
061: * @author fdietz
062: */
063: public class Config implements IConfig {
064:
065: private static final String CORE_STR = "core"; //$NON-NLS-1$
066:
067: static final Logger LOG = Logger
068: .getLogger("org.columba.core.config"); //$NON-NLS-1$
069:
070: protected Map<String, Map<String, DefaultXmlConfig>> pluginList = new Hashtable<String, Map<String, DefaultXmlConfig>>();
071:
072: protected File path;
073:
074: protected File optionsFile;
075:
076: protected File toolsFile;
077:
078: protected File viewsFile;
079:
080: private static Config instance;
081:
082: /**
083: * Creates a new configuration from the given directory.
084: */
085: public Config(File thePath) {
086: if (thePath == null) {
087: thePath = DefaultConfigDirectory.getDefaultPath();
088: ;
089: }
090:
091: this .path = thePath;
092: thePath.mkdir();
093: optionsFile = new File(thePath, "options.xml"); //$NON-NLS-1$
094: toolsFile = new File(thePath, "external_tools.xml"); //$NON-NLS-1$
095: viewsFile = new File(thePath, "views.xml"); //$NON-NLS-1$
096:
097: registerPlugin(CORE_STR, optionsFile.getName(),
098: new OptionsXmlConfig(optionsFile));
099: registerPlugin(CORE_STR, toolsFile.getName(),
100: new DefaultXmlConfig(toolsFile));
101: registerPlugin(CORE_STR, viewsFile.getName(),
102: new DefaultXmlConfig(viewsFile));
103:
104: // register at shutdown manager
105: // -> this will save all configuration data, when closing Columba
106: final IShutdownManager shutdownManager = ShutdownManager
107: .getInstance();
108:
109: shutdownManager.register(new Runnable() {
110: public void run() {
111: try {
112: save();
113: } catch (final Exception e) {
114: LOG.severe(e.getMessage());
115: }
116: }
117: });
118:
119: instance = this ;
120: }
121:
122: public static Config getInstance() {
123: if (instance == null) {
124: throw new RuntimeException("Must call Constructor first!");
125: }
126:
127: return instance;
128: }
129:
130: /*
131: * (non-Javadoc)
132: *
133: * @see org.columba.core.config.IConfig#getConfigDirectory()
134: */
135: public File getConfigDirectory() {
136: return path;
137: }
138:
139: /**
140: * Method registerPlugin.
141: *
142: * @param moduleName
143: * @param id
144: * @param configPlugin
145: */
146: public void registerPlugin(final String moduleName,
147: final String id, final DefaultXmlConfig configPlugin) {
148: File directory;
149:
150: if (moduleName.equals(CORE_STR)) {
151: directory = getConfigDirectory();
152: } else {
153: directory = new File(getConfigDirectory(), moduleName);
154: }
155:
156: final File destination = new File(directory, id);
157:
158: if (!destination.exists()) {
159: final String hstr = "org/columba/" + moduleName + "/config/" + id; //$NON-NLS-1$
160:
161: try {
162: DiskIO.copyResource(hstr, destination);
163: } catch (final IOException e) {
164: }
165: }
166:
167: if (!pluginList.containsKey(moduleName)) {
168: final Map<String, DefaultXmlConfig> map = new Hashtable<String, DefaultXmlConfig>();
169: pluginList.put(moduleName, map);
170: }
171:
172: addPlugin(moduleName, id, configPlugin);
173:
174: // load config-file from disk
175: configPlugin.load();
176: }
177:
178: /**
179: * Method getPlugin.
180: *
181: * @param moduleName
182: * @param id
183: * @return DefaultXmlConfig
184: */
185: public DefaultXmlConfig getPlugin(final String moduleName,
186: final String id) {
187: if (pluginList.containsKey(moduleName)) {
188: final Map<String, DefaultXmlConfig> map = pluginList
189: .get(moduleName);
190:
191: if (map.containsKey(id)) {
192: final DefaultXmlConfig plugin = map.get(id);
193:
194: return plugin;
195: }
196: }
197:
198: return null;
199: }
200:
201: /**
202: * Method addPlugin.
203: *
204: * @param moduleName
205: * @param id
206: * @param configPlugin
207: */
208: public void addPlugin(final String moduleName, final String id,
209: final DefaultXmlConfig configPlugin) {
210: final Map<String, DefaultXmlConfig> map = pluginList
211: .get(moduleName);
212:
213: if (map != null) {
214: map.put(id, configPlugin);
215: }
216: }
217:
218: /**
219: * Method getPluginList.
220: *
221: * @return List
222: */
223: public List<DefaultXmlConfig> getPluginList() {
224: final List<DefaultXmlConfig> list = new LinkedList<DefaultXmlConfig>();
225:
226: for (String key : pluginList.keySet()) {
227: // final String key = (String) keys.next();
228: final Map<String, DefaultXmlConfig> map = pluginList
229: .get(key);
230:
231: if (map != null) {
232: for (String key2 : map.keySet()) {
233: final DefaultXmlConfig plugin = map.get(key2);
234: list.add(plugin);
235: }
236: }
237: }
238:
239: return list;
240: }
241:
242: /**
243: * Method save.
244: */
245: public void save() throws Exception {
246: for (DefaultXmlConfig plugin : getPluginList()) {
247: if (plugin == null) {
248: continue;
249: }
250:
251: plugin.save();
252: }
253: }
254:
255: /**
256: * Loads all plugins and template plugins.
257: */
258: protected void load() {
259: for (DefaultXmlConfig plugin : getPluginList()) {
260: if (plugin == null) {
261: continue;
262: }
263:
264: plugin.load();
265: }
266: }
267:
268: /**
269: * @see org.columba.core.config.IConfig#get(java.lang.String)
270: */
271: public XmlElement get(final String name) {
272: final DefaultXmlConfig xml = getPlugin(CORE_STR, name + ".xml");
273:
274: return xml.getRoot();
275: }
276:
277: /**
278: * Method getOptionsMainInterface.config.
279: *
280: * @return OptionsXmlConfig
281: */
282: public OptionsXmlConfig getOptionsConfig() {
283: return (OptionsXmlConfig) getPlugin(CORE_STR, optionsFile
284: .getName());
285: }
286:
287: /**
288: * Returns the default configuration path. This value depends on the
289: * underlying operating system. This method must never return null.
290: */
291: public static File getDefaultConfigPath() {
292: if (OSInfo.isWindowsPlatform()) {
293: return new File("config"); //$NON-NLS-1$
294: }
295: return new File(System.getProperty("user.home"), ".columba"); //$NON-NLS-1$//$NON-NLS-2$
296: }
297: }
|