1 package org.columba.core.config; 19 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.util.Hashtable ; 23 import java.util.LinkedList ; 24 import java.util.List ; 25 import java.util.Map ; 26 import java.util.logging.Logger ; 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 63 public class Config implements IConfig { 64 65 private static final String CORE_STR = "core"; 67 static final Logger LOG = Logger.getLogger("org.columba.core.config"); 69 protected Map <String , Map <String , DefaultXmlConfig>> pluginList = new Hashtable <String , Map <String , DefaultXmlConfig>>(); 70 71 protected File path; 72 73 protected File optionsFile; 74 75 protected File toolsFile; 76 77 protected File viewsFile; 78 79 private static Config instance; 80 81 84 public Config(File thePath) { 85 if (thePath == null) { 86 thePath = DefaultConfigDirectory.getDefaultPath();; 87 } 88 89 this.path = thePath; 90 thePath.mkdir(); 91 optionsFile = new File (thePath, "options.xml"); toolsFile = new File (thePath, "external_tools.xml"); viewsFile = new File (thePath, "views.xml"); 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 final IShutdownManager shutdownManager = ShutdownManager.getInstance(); 107 108 shutdownManager.register(new Runnable () { 109 public void run() { 110 try { 111 save(); 112 } catch (final Exception 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 ("Must call Constructor first!"); 124 } 125 126 return instance; 127 } 128 129 134 public File getConfigDirectory() { 135 return path; 136 } 137 138 145 public void registerPlugin(final String moduleName, final String id, 146 final DefaultXmlConfig configPlugin) { 147 File directory; 148 149 if (moduleName.equals(CORE_STR)) { 150 directory = getConfigDirectory(); 151 } else { 152 directory = new File (getConfigDirectory(), moduleName); 153 } 154 155 final File destination = new File (directory, id); 156 157 if (!destination.exists()) { 158 final String hstr = "org/columba/" + moduleName + "/config/" + id; 160 try { 161 DiskIO.copyResource(hstr, destination); 162 } catch (final IOException e) { 163 } 164 } 165 166 if (!pluginList.containsKey(moduleName)) { 167 final Map <String , DefaultXmlConfig> map = new Hashtable <String , DefaultXmlConfig>(); 168 pluginList.put(moduleName, map); 169 } 170 171 addPlugin(moduleName, id, configPlugin); 172 173 configPlugin.load(); 175 } 176 177 184 public DefaultXmlConfig getPlugin(final String moduleName, final String id) { 185 if (pluginList.containsKey(moduleName)) { 186 final Map <String , 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 206 public void addPlugin(final String moduleName, final String id, 207 final DefaultXmlConfig configPlugin) { 208 final Map <String , DefaultXmlConfig> map = pluginList.get(moduleName); 209 210 if (map != null) { 211 map.put(id, configPlugin); 212 } 213 } 214 215 220 public List <DefaultXmlConfig> getPluginList() { 221 final List <DefaultXmlConfig> list = new LinkedList <DefaultXmlConfig>(); 222 223 for (String key : pluginList.keySet()) { 224 final Map <String , DefaultXmlConfig> map = pluginList.get(key); 226 227 if (map != null) { 228 for (String key2 : map.keySet()) { 229 final DefaultXmlConfig plugin = map.get(key2); 230 list.add(plugin); 231 } 232 } 233 } 234 235 return list; 236 } 237 238 241 public void save() throws Exception { 242 for (DefaultXmlConfig plugin : getPluginList()) { 243 if (plugin == null) { 244 continue; 245 } 246 247 plugin.save(); 248 } 249 } 250 251 254 protected void load() { 255 for (DefaultXmlConfig plugin : getPluginList()) { 256 if (plugin == null) { 257 continue; 258 } 259 260 plugin.load(); 261 } 262 } 263 264 267 public XmlElement get(final String name) { 268 final DefaultXmlConfig xml = getPlugin(CORE_STR, name + ".xml"); 269 270 return xml.getRoot(); 271 } 272 273 278 public OptionsXmlConfig getOptionsConfig() { 279 return (OptionsXmlConfig) getPlugin(CORE_STR, optionsFile.getName()); 280 } 281 282 286 public static File getDefaultConfigPath() { 287 if (OSInfo.isWindowsPlatform()) { 288 return new File ("config"); } 290 return new File (System.getProperty("user.home"), ".columba"); } 292 } | Popular Tags |