1 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 ; 35 import java.util.Iterator ; 36 import java.util.Map ; 37 import java.io.ByteArrayInputStream ; 38 import java.io.IOException ; 39 40 46 public class ConfigurationManager implements Consumer { 47 private static ConfigurationManager configManager = null; 48 49 private Map configMap; 51 private Map 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 (); 62 prefixMap = new HashMap (); 63 } 64 65 public Configuration addConfiguration(String 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 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 oid) { 84 return (Configuration) configMap.get(oid); 85 } 86 87 public String checkForPrefix(String prefix) { 88 return (String )prefixMap.get(prefix); 89 } 90 91 public Iterator 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 appOid = snip.getApplication(); 100 Configuration config = getConfiguration(appOid); 101 try { 102 Logger.log("reloading config for: "+appOid); 103 config.load(new ByteArrayInputStream (snip.getContent().getBytes())); 104 } catch (IOException e) { 105 System.err.println("ConfigurationManager: unable to reload configuration: "+e); 106 e.printStackTrace(); 107 } 108 } 109 } 110 } 111 } 112 | Popular Tags |