KickJava   Java API By Example, From Geeks To Geeks.

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


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 import info.magnolia.cms.util.ContentUtil;
18 import info.magnolia.cms.util.ObservationUtil;
19
20 import java.util.*;
21
22 import javax.jcr.PathNotFoundException;
23 import javax.jcr.RepositoryException;
24 import javax.jcr.observation.EventListener;
25 import javax.jcr.observation.EventIterator;
26
27 import org.apache.commons.lang.BooleanUtils;
28 import org.apache.commons.lang.StringUtils;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32
33 /**
34  * @author Sameer Charles
35  * @version 1.1
36  */

37 public final class Subscriber {
38
39     public static final String JavaDoc SUBSCRIBERS_NODE_NAME = "subscribers";
40
41     public static final String JavaDoc SUBSCRIBER_NODE_NAME = "subscriber"; //$NON-NLS-1$
42

43     public static final String JavaDoc DEFAULT_SUBSCRIBER_NAME = "default";
44
45     public static final String JavaDoc CONTEXT_NODE_NAME = "context";
46
47     /**
48      * Logger.
49      */

50     private static Logger log = LoggerFactory.getLogger(Subscriber.class);
51
52     private static Hashtable cachedContent = new Hashtable();
53
54     /**
55      * <code>true</code> if at least one subscriber is configured and enabled.
56      */

57     private static boolean subscribersEnabled;
58
59     private String JavaDoc name;
60
61     private boolean active;
62
63     private boolean requestConfirmation;
64
65     private String JavaDoc url;
66
67     private String JavaDoc senderURL;
68
69     private Map context;
70
71     /**
72      * Returns <code>true</code> if at least an enabled subscriber is configured, <code>false</code> if there are no
73      * subscriber or none of them is enabled
74      * @return <code>true</code> if at least an enabled subscriber exists
75      */

76     public static boolean isSubscribersEnabled() {
77         return subscribersEnabled;
78     }
79
80     /**
81      * constructor
82      */

83     private Subscriber() {
84     }
85
86     /**
87      * <p>
88      * reads listener config from the config repository and caches its content in to the hash table
89      * </p>
90      */

91     public static void init() {
92         load();
93         initObservation();
94     }
95
96     protected static void initObservation() {
97         EventListener listener = new EventListener(){
98             public void onEvent(EventIterator events) {
99                 reload();
100             };
101         };
102         
103         if(ContentUtil.getContent(ContentRepository.CONFIG, SUBSCRIBER_NODE_NAME)!= null){
104             ObservationUtil.registerChangeListener(ContentRepository.CONFIG, "/" + SUBSCRIBER_NODE_NAME, listener);
105         }
106
107         if(ContentUtil.getContent(ContentRepository.CONFIG, SUBSCRIBERS_NODE_NAME)!= null){
108             ObservationUtil.registerChangeListener(ContentRepository.CONFIG, "/" + SUBSCRIBERS_NODE_NAME, listener);
109         }
110     }
111
112     private static void load() {
113         Subscriber.cachedContent.clear();
114
115         log.info("Config : loading Subscribers"); //$NON-NLS-1$
116

117         Collection children = getSubscriberNodes();
118
119         if (children != null) {
120             Subscriber.cacheContent(children);
121         }
122         log.info("Config : Subscribers loaded"); //$NON-NLS-1$
123
}
124
125     protected static Collection getSubscriberNodes() {
126         Collection children = Collections.EMPTY_LIST;
127
128         try {
129             Content startPage;
130             try {
131                 startPage = ContentRepository.getHierarchyManager(ContentRepository.CONFIG).getContent(SUBSCRIBER_NODE_NAME);
132                 children = new ArrayList();
133                 children.add(startPage);
134             } catch (PathNotFoundException e) {
135                 startPage = ContentRepository.getHierarchyManager(ContentRepository.CONFIG).getContent(SUBSCRIBERS_NODE_NAME);
136                 children = startPage.getChildren(ItemType.CONTENTNODE);
137             }
138         }
139         catch (PathNotFoundException re) {
140             log.info("No subscribers configured"); //$NON-NLS-1$
141
}
142         catch (RepositoryException re) {
143             log.error("Config : Failed to load Subscribers"); //$NON-NLS-1$
144
log.error(re.getMessage(), re);
145         }
146         return children;
147     }
148
149     public static void reload() {
150         log.info("Config : re-loading Subscribers"); //$NON-NLS-1$
151
Subscriber.load();
152     }
153
154     /**
155      * Cache listener content from the config repository.
156      */

157     private static void cacheContent(Collection subs) {
158
159         Iterator list = subs.iterator();
160
161         // start by setting the subscribersEnabled property to false, will be reset when an active subscriber is found
162
subscribersEnabled = false;
163
164         while (list.hasNext()) {
165             Content c = (Content) list.next();
166             Subscriber si = new Subscriber();
167
168             si.url = c.getNodeData("URL").getString(); //$NON-NLS-1$
169

170             if (StringUtils.isEmpty(si.url)) {
171                 String JavaDoc address = c.getNodeData("address").getString(); //$NON-NLS-1$
172
String JavaDoc protocol = c.getNodeData("protocol").getString(); //$NON-NLS-1$
173

174                 log
175                     .warn("Deprecated: subscriber is missing the URL property. Please use URL instead of address and domain");
176
177                 if (StringUtils.isEmpty(protocol)) {
178                     protocol = "http";
179                     si.url = protocol + "://" + address;
180                 }
181             }
182
183             if (!si.url.endsWith("/")) {
184                 si.url = si.url + "/";
185             }
186
187             si.senderURL = c.getNodeData("senderURL").getString(); //$NON-NLS-1$
188
si.requestConfirmation = c.getNodeData("requestConfirmation").getBoolean(); //$NON-NLS-1$
189
si.name = c.getName();
190             
191             if(StringUtils.equals(si.name, SUBSCRIBER_NODE_NAME)){
192                 si.name = DEFAULT_SUBSCRIBER_NAME;
193             }
194
195             // don't use getBoolean since subscribers without an "active" node should be enabled by default
196
String JavaDoc activeString = c.getNodeData("active").getString(); //$NON-NLS-1$
197

198             if (StringUtils.isNotEmpty(activeString)) {
199                 si.active = BooleanUtils.toBoolean(activeString);
200             }
201             else {
202                 si.active = true;
203             }
204
205             if (si.active) {
206                 // at least one subscriber is enabled
207
subscribersEnabled = true;
208             }
209
210             // all context info
211
addContext(si, c);
212             
213             Subscriber.cachedContent.put(si.getName(), si);
214         }
215     }
216
217     /**
218      * Adds context datail to cache.
219      * @param subscriberInfo
220      * @param contentNode
221      */

222     private static void addContext(Subscriber subscriberInfo, Content contentNode) {
223         subscriberInfo.context = new Hashtable();
224         Content contextList = ContentUtil.getCaseInsensitive(contentNode, CONTEXT_NODE_NAME); //$NON-NLS-1$
225
if(contextList == null){
226             log.warn("subscriber has no context node defined");
227             return;
228         }
229         Iterator it = contextList.getChildren().iterator();
230         while (it.hasNext()) {
231             Content context = (Content) it.next();
232             Iterator contextDetails = context.getChildren().iterator();
233             List list = new ArrayList();
234             while (contextDetails.hasNext()) {
235                 Content map = (Content) contextDetails.next();
236                 list.add(map.getNodeData("subscribedURI").getString()); //$NON-NLS-1$
237
}
238             subscriberInfo.context.put(context.getName(), list);
239         }
240     }
241
242     /**
243      * Get list of all configured ip.
244      * @return Enumeration
245      */

246     public static Enumeration getList() {
247         return Subscriber.cachedContent.elements();
248     }
249
250     /**
251      * @return configured subscriber
252      **/

253     public static Subscriber getSubscriber() {
254         if(cachedContent.containsKey(DEFAULT_SUBSCRIBER_NAME)){
255             return (Subscriber) cachedContent.get(DEFAULT_SUBSCRIBER_NAME);
256         }
257         if(cachedContent.size() > 0){
258             return (Subscriber) cachedContent.entrySet().iterator().next();
259         }
260         return null;
261     }
262
263     /**
264      * Getter for <code>active</code>.
265      * @return Returns the active.
266      */

267     public boolean isActive() {
268         return this.active;
269     }
270
271     /**
272      * Getter for <code>requestConfirmation</code>.
273      * @return Returns the requestConfirmation.
274      */

275     public boolean getRequestConfirmation() {
276         return this.requestConfirmation;
277     }
278
279     /**
280      * @return name
281      */

282     public String JavaDoc getName() {
283         return this.name;
284     }
285
286     /**
287      * @return context details
288      */

289     public List getContext(String JavaDoc name) {
290         if (this.context.get(name) == null) {
291             return new ArrayList();
292         }
293         return (List) this.context.get(name);
294     }
295
296     /**
297      * Getter for <code>senderURL</code>.
298      * @return Returns the senderURL.
299      */

300     public String JavaDoc getSenderURL() {
301         return this.senderURL;
302     }
303
304     /**
305      * Returns the url of the subscriber, in the form <code>protocol://server:port/context/</code> (always with the
306      * leading "/")
307      * @return Returns the url.
308      */

309     public String JavaDoc getURL() {
310         return this.url;
311     }
312
313 }
314
Popular Tags