1 13 package info.magnolia.cms.beans.config; 14 15 import info.magnolia.cms.core.Content; 16 import info.magnolia.cms.core.ItemType; 17 18 import java.util.Collection ; 19 import java.util.Collections ; 20 import java.util.Hashtable ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 24 import javax.jcr.RepositoryException; 25 import javax.jcr.observation.Event; 26 import javax.jcr.observation.EventIterator; 27 import javax.jcr.observation.EventListener; 28 import javax.jcr.observation.ObservationManager; 29 30 import org.apache.commons.lang.StringUtils; 31 import org.slf4j.Logger; 32 import org.slf4j.LoggerFactory; 33 34 35 39 public final class Listener { 40 41 44 private static Logger log = LoggerFactory.getLogger(Listener.class); 45 46 private static final String CONFIG_PAGE = "server"; 48 private static Map cachedContent = new Hashtable (); 49 50 53 private Listener() { 54 } 56 57 60 public static void init() { 61 load(); 62 registerEventListener(); 63 } 64 65 68 public static void load() { 69 70 log.info("Config : loading Listener info"); 72 Collection children = Collections.EMPTY_LIST; 73 74 try { 75 76 Content startPage = ContentRepository.getHierarchyManager(ContentRepository.CONFIG).getContent(CONFIG_PAGE); 77 Content configNode = startPage.getContent("IPConfig"); 78 children = configNode.getChildren(ItemType.CONTENTNODE); 79 } 80 catch (RepositoryException re) { 81 log.error("Config : Failed to load Listener info"); log.error(re.getMessage(), re); 83 } 84 85 Listener.cachedContent.clear(); 86 Listener.cacheContent(children); 87 log.info("Config : Listener info loaded"); } 89 90 public static void reload() { 91 log.info("Config : re-loading Listener info"); Listener.load(); 93 } 94 95 98 private static void registerEventListener() { 99 100 log.info("Registering event listener for Listeners"); 102 try { 103 ObservationManager observationManager = ContentRepository 104 .getHierarchyManager(ContentRepository.CONFIG) 105 .getWorkspace() 106 .getObservationManager(); 107 108 observationManager.addEventListener(new EventListener() { 109 110 public void onEvent(EventIterator iterator) { 111 reload(); 113 } 114 }, Event.NODE_ADDED 115 | Event.NODE_REMOVED 116 | Event.PROPERTY_ADDED 117 | Event.PROPERTY_CHANGED 118 | Event.PROPERTY_REMOVED, "/" + CONFIG_PAGE + "/" + "IPConfig", true, null, null, false); } 120 catch (RepositoryException e) { 121 log.error("Unable to add event listeners for Listeners", e); } 123 } 124 125 128 private static void cacheContent(Collection listeners) { 129 130 Iterator ipList = listeners.iterator(); 131 while (ipList.hasNext()) { 132 Content c = (Content) ipList.next(); 133 try { 134 Map types = new Hashtable (); 135 Listener.cachedContent.put(c.getNodeData("IP").getString(), types); Iterator it = c.getContent("Access").getChildren().iterator(); while (it.hasNext()) { 138 Content type = (Content) it.next(); 139 types.put(type.getNodeData("Method").getString().toLowerCase(), StringUtils.EMPTY); } 141 } 142 catch (RepositoryException re) { 143 log.error("RepositoryException caught while loading listener configuration: " + re.getMessage(), re); } 145 } 146 } 147 148 154 public static Map getInfo(String key) throws Exception { 155 return (Hashtable ) Listener.cachedContent.get(key); 156 } 157 } 158 | Popular Tags |