KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > config > Config


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.core.config;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.util.Hashtable JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27
28 import org.columba.api.shutdown.IShutdownManager;
29 import org.columba.core.base.OSInfo;
30 import org.columba.core.io.DiskIO;
31 import org.columba.core.shutdown.ShutdownManager;
32 import org.columba.core.xml.XmlElement;
33 import org.columba.core.xml.XmlIO;
34
35 /**
36  * Main entrypoint for configuration management.
37  * <p>
38  * Stores a list of all xml files in a hashtable. Hashtable key is the name of
39  * the xml file. Value is {@link XmlIO} object.
40  * <p>
41  * Mail and Addressbook components are just wrappers, encapsulating this class.
42  * Using these wrapper classes, you don't need to specify the module name (for
43  * example: mail, or addressbook) manually.
44  * <p>
45  * Note that all configuration file have default templates in the /res directory
46  * in package org.columba.core.config. These default configuration files are
47  * copied into the users's configuration directory the first time Columba is
48  * started.
49  * <p>
50  * Config creates the top-level directory for Columba's configuration in
51  * ".columba", which usually resides in the user's home directory or on older
52  * Windows versions in Columba's program folder.
53  * <p>
54  * Saving and loading of all configuration files is handled here, too.
55  * <p>
56  *
57  * @see org.columba.mail.config.MailConfig
58  * @see org.columba.addressbook.config.AddressbookConfig
59  *
60  * @deprecated use XmlConfig instead
61  * @author fdietz
62  */

63 public class Config implements IConfig {
64
65     private static final String JavaDoc CORE_STR = "core"; //$NON-NLS-1$
66

67     static final Logger JavaDoc LOG = Logger.getLogger("org.columba.core.config"); //$NON-NLS-1$
68

69     protected Map JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, DefaultXmlConfig>> pluginList = new Hashtable JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, DefaultXmlConfig>>();
70
71     protected File JavaDoc path;
72
73     protected File JavaDoc optionsFile;
74
75     protected File JavaDoc toolsFile;
76
77     protected File JavaDoc viewsFile;
78
79     private static Config instance;
80
81     /**
82      * Creates a new configuration from the given directory.
83      */

84     public Config(File JavaDoc thePath) {
85         if (thePath == null) {
86             thePath = DefaultConfigDirectory.getDefaultPath();;
87         }
88
89         this.path = thePath;
90         thePath.mkdir();
91         optionsFile = new File JavaDoc(thePath, "options.xml"); //$NON-NLS-1$
92
toolsFile = new File JavaDoc(thePath, "external_tools.xml"); //$NON-NLS-1$
93
viewsFile = new File JavaDoc(thePath, "views.xml"); //$NON-NLS-1$
94

95
96         registerPlugin(CORE_STR, optionsFile.getName(), new OptionsXmlConfig(
97                 optionsFile));
98         registerPlugin(CORE_STR, toolsFile.getName(), new DefaultXmlConfig(
99                 toolsFile));
100         registerPlugin(CORE_STR, viewsFile.getName(), new DefaultXmlConfig(
101                 viewsFile));
102
103
104         // register at shutdown manager
105
// -> this will save all configuration data, when closing Columba
106
final IShutdownManager shutdownManager = ShutdownManager.getInstance();
107
108         shutdownManager.register(new Runnable JavaDoc() {
109             public void run() {
110                 try {
111                     save();
112                 } catch (final Exception JavaDoc e) {
113                     LOG.severe(e.getMessage());
114                 }
115             }
116         });
117
118         instance = this;
119     }
120
121     public static Config getInstance() {
122         if (instance == null) {
123             throw new RuntimeException JavaDoc("Must call Constructor first!");
124         }
125
126         return instance;
127     }
128
129     /*
130      * (non-Javadoc)
131      *
132      * @see org.columba.core.config.IConfig#getConfigDirectory()
133      */

134     public File JavaDoc getConfigDirectory() {
135         return path;
136     }
137
138     /**
139      * Method registerPlugin.
140      *
141      * @param moduleName
142      * @param id
143      * @param configPlugin
144      */

145     public void registerPlugin(final String JavaDoc moduleName, final String JavaDoc id,
146             final DefaultXmlConfig configPlugin) {
147         File JavaDoc directory;
148
149         if (moduleName.equals(CORE_STR)) {
150             directory = getConfigDirectory();
151         } else {
152             directory = new File JavaDoc(getConfigDirectory(), moduleName);
153         }
154
155         final File JavaDoc destination = new File JavaDoc(directory, id);
156
157         if (!destination.exists()) {
158             final String JavaDoc hstr = "org/columba/" + moduleName + "/config/" + id; //$NON-NLS-1$
159

160             try {
161                 DiskIO.copyResource(hstr, destination);
162             } catch (final IOException JavaDoc e) {
163             }
164         }
165
166         if (!pluginList.containsKey(moduleName)) {
167             final Map JavaDoc<String JavaDoc, DefaultXmlConfig> map = new Hashtable JavaDoc<String JavaDoc, DefaultXmlConfig>();
168             pluginList.put(moduleName, map);
169         }
170
171         addPlugin(moduleName, id, configPlugin);
172
173         // load config-file from disk
174
configPlugin.load();
175     }
176
177     /**
178      * Method getPlugin.
179      *
180      * @param moduleName
181      * @param id
182      * @return DefaultXmlConfig
183      */

184     public DefaultXmlConfig getPlugin(final String JavaDoc moduleName, final String JavaDoc id) {
185         if (pluginList.containsKey(moduleName)) {
186             final Map JavaDoc<String JavaDoc, DefaultXmlConfig> map = pluginList
187                     .get(moduleName);
188
189             if (map.containsKey(id)) {
190                 final DefaultXmlConfig plugin = map.get(id);
191
192                 return plugin;
193             }
194         }
195
196         return null;
197     }
198
199     /**
200      * Method addPlugin.
201      *
202      * @param moduleName
203      * @param id
204      * @param configPlugin
205      */

206     public void addPlugin(final String JavaDoc moduleName, final String JavaDoc id,
207             final DefaultXmlConfig configPlugin) {
208         final Map JavaDoc<String JavaDoc, DefaultXmlConfig> map = pluginList.get(moduleName);
209
210         if (map != null) {
211             map.put(id, configPlugin);
212         }
213     }
214
215     /**
216      * Method getPluginList.
217      *
218      * @return List
219      */

220     public List JavaDoc<DefaultXmlConfig> getPluginList() {
221         final List JavaDoc<DefaultXmlConfig> list = new LinkedList JavaDoc<DefaultXmlConfig>();
222
223         for (String JavaDoc key : pluginList.keySet()) {
224             // final String key = (String) keys.next();
225
final Map JavaDoc<String JavaDoc, DefaultXmlConfig> map = pluginList.get(key);
226
227             if (map != null) {
228                 for (String JavaDoc key2 : map.keySet()) {
229                     final DefaultXmlConfig plugin = map.get(key2);
230                     list.add(plugin);
231                 }
232             }
233         }
234
235         return list;
236     }
237
238     /**
239      * Method save.
240      */

241     public void save() throws Exception JavaDoc {
242         for (DefaultXmlConfig plugin : getPluginList()) {
243             if (plugin == null) {
244                 continue;
245             }
246
247             plugin.save();
248         }
249     }
250
251     /**
252      * Loads all plugins and template plugins.
253      */

254     protected void load() {
255         for (DefaultXmlConfig plugin : getPluginList()) {
256             if (plugin == null) {
257                 continue;
258             }
259
260             plugin.load();
261         }
262     }
263
264     /**
265      * @see org.columba.core.config.IConfig#get(java.lang.String)
266      */

267     public XmlElement get(final String JavaDoc name) {
268         final DefaultXmlConfig xml = getPlugin(CORE_STR, name + ".xml");
269
270         return xml.getRoot();
271     }
272
273     /**
274      * Method getOptionsMainInterface.config.
275      *
276      * @return OptionsXmlConfig
277      */

278     public OptionsXmlConfig getOptionsConfig() {
279         return (OptionsXmlConfig) getPlugin(CORE_STR, optionsFile.getName());
280     }
281
282     /**
283      * Returns the default configuration path. This value depends on the
284      * underlying operating system. This method must never return null.
285      */

286     public static File JavaDoc getDefaultConfigPath() {
287         if (OSInfo.isWindowsPlatform()) {
288             return new File JavaDoc("config"); //$NON-NLS-1$
289
}
290         return new File JavaDoc(System.getProperty("user.home"), ".columba"); //$NON-NLS-1$//$NON-NLS-2$
291
}
292 }
Popular Tags