KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > config > ConfigurationManager


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25 package org.snipsnap.config;
26
27 import org.snipsnap.notification.Consumer;
28 import org.snipsnap.notification.Message;
29 import org.snipsnap.notification.MessageService;
30 import org.snipsnap.snip.Snip;
31 import org.snipsnap.container.Components;
32 import org.radeox.util.logging.Logger;
33
34 import java.util.HashMap JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.Map JavaDoc;
37 import java.io.ByteArrayInputStream JavaDoc;
38 import java.io.IOException JavaDoc;
39
40 /**
41  * Manages configurations of this web application (different contexts)
42  *
43  * @author Matthias L. Jugel
44  * @version $Id: ConfigurationManager.java 1606 2004-05-17 10:56:18Z leo $
45  */

46 public class ConfigurationManager implements Consumer {
47   private static ConfigurationManager configManager = null;
48
49   // local configuration map
50
private Map JavaDoc configMap;
51   private Map JavaDoc prefixMap;
52
53   public static ConfigurationManager getInstance() {
54     if (null == configManager) {
55       configManager = new ConfigurationManager();
56     }
57     return configManager;
58   }
59
60   private ConfigurationManager() {
61     configMap = new HashMap JavaDoc();
62     prefixMap = new HashMap JavaDoc();
63   }
64
65   public Configuration addConfiguration(String JavaDoc oid, Configuration config) {
66     if(config.isInstalled()) {
67       MessageService service = (MessageService)Components.getComponent(MessageService.class);
68       service.register(this);
69     }
70     configMap.put(oid, config);
71     prefixMap.put(config.getPrefix(), oid);
72     return config;
73   }
74
75   public void removeConfiguration(String JavaDoc oid) {
76     Configuration config = (Configuration)configMap.get(oid);
77     if(config != null) {
78       configMap.remove(oid);
79       prefixMap.remove(config);
80     }
81   }
82
83   public Configuration getConfiguration(String JavaDoc oid) {
84     return (Configuration) configMap.get(oid);
85   }
86
87   public String JavaDoc checkForPrefix(String JavaDoc prefix) {
88     return (String JavaDoc)prefixMap.get(prefix);
89   }
90
91   public Iterator JavaDoc getOids() {
92     return configMap.keySet().iterator();
93   }
94
95   public void consume(Message messsage) {
96     if(Message.SNIP_MODIFIED.equals(messsage.getType())) {
97       Snip snip = (Snip)messsage.getValue();
98       if("SnipSnap/config".equals(snip.getName())) {
99         String JavaDoc appOid = snip.getApplication();
100         Configuration config = getConfiguration(appOid);
101         try {
102           Logger.log("reloading config for: "+appOid);
103           config.load(new ByteArrayInputStream JavaDoc(snip.getContent().getBytes()));
104         } catch (IOException JavaDoc e) {
105           System.err.println("ConfigurationManager: unable to reload configuration: "+e);
106           e.printStackTrace();
107         }
108       }
109     }
110   }
111 }
112
Popular Tags