KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > beans > config > Listener


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

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 JavaDoc;
19 import java.util.Collections JavaDoc;
20 import java.util.Hashtable JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
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 /**
36  * @author Sameer Charles
37  * @version 1.1
38  */

39 public final class Listener {
40
41     /**
42      * Logger.
43      */

44     private static Logger log = LoggerFactory.getLogger(Listener.class);
45
46     private static final String JavaDoc CONFIG_PAGE = "server"; //$NON-NLS-1$
47

48     private static Map JavaDoc cachedContent = new Hashtable JavaDoc();
49
50     /**
51      * Utility class, don't instantiate.
52      */

53     private Listener() {
54         // unused
55
}
56
57     /**
58      * Reads listener config from the config repository and caches its content in to the hash table.
59      */

60     public static void init() {
61         load();
62         registerEventListener();
63     }
64
65     /**
66      * Reads listener config from the config repository and caches its content in to the hash table.
67      */

68     public static void load() {
69
70         log.info("Config : loading Listener info"); //$NON-NLS-1$
71

72         Collection JavaDoc 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"); //$NON-NLS-1$
82
log.error(re.getMessage(), re);
83         }
84
85         Listener.cachedContent.clear();
86         Listener.cacheContent(children);
87         log.info("Config : Listener info loaded"); //$NON-NLS-1$
88
}
89
90     public static void reload() {
91         log.info("Config : re-loading Listener info"); //$NON-NLS-1$
92
Listener.load();
93     }
94
95     /**
96      * Register an event listener: reload cache configuration when something changes.
97      */

98     private static void registerEventListener() {
99
100         log.info("Registering event listener for Listeners"); //$NON-NLS-1$
101

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 everything
112
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); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
119
}
120         catch (RepositoryException e) {
121             log.error("Unable to add event listeners for Listeners", e); //$NON-NLS-1$
122
}
123     }
124
125     /**
126      * Cache listener content from the config repository.
127      */

128     private static void cacheContent(Collection JavaDoc listeners) {
129
130         Iterator JavaDoc ipList = listeners.iterator();
131         while (ipList.hasNext()) {
132             Content c = (Content) ipList.next();
133             try {
134                 Map JavaDoc types = new Hashtable JavaDoc();
135                 Listener.cachedContent.put(c.getNodeData("IP").getString(), types); //$NON-NLS-1$
136
Iterator JavaDoc it = c.getContent("Access").getChildren().iterator(); //$NON-NLS-1$
137
while (it.hasNext()) {
138                     Content type = (Content) it.next();
139                     types.put(type.getNodeData("Method").getString().toLowerCase(), StringUtils.EMPTY); //$NON-NLS-1$
140
}
141             }
142             catch (RepositoryException re) {
143                 log.error("RepositoryException caught while loading listener configuration: " + re.getMessage(), re); //$NON-NLS-1$
144
}
145         }
146     }
147
148     /**
149      * Get access info of the requested IP.
150      * @param key IP tp be checked
151      * @return Hashtable containing Access info
152      * @throws Exception
153      */

154     public static Map JavaDoc getInfo(String JavaDoc key) throws Exception JavaDoc {
155         return (Hashtable JavaDoc) Listener.cachedContent.get(key);
156     }
157 }
158
Popular Tags