1 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 37 public final class Subscriber { 38 39 public static final String SUBSCRIBERS_NODE_NAME = "subscribers"; 40 41 public static final String SUBSCRIBER_NODE_NAME = "subscriber"; 43 public static final String DEFAULT_SUBSCRIBER_NAME = "default"; 44 45 public static final String CONTEXT_NODE_NAME = "context"; 46 47 50 private static Logger log = LoggerFactory.getLogger(Subscriber.class); 51 52 private static Hashtable cachedContent = new Hashtable(); 53 54 57 private static boolean subscribersEnabled; 58 59 private String name; 60 61 private boolean active; 62 63 private boolean requestConfirmation; 64 65 private String url; 66 67 private String senderURL; 68 69 private Map context; 70 71 76 public static boolean isSubscribersEnabled() { 77 return subscribersEnabled; 78 } 79 80 83 private Subscriber() { 84 } 85 86 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"); 117 Collection children = getSubscriberNodes(); 118 119 if (children != null) { 120 Subscriber.cacheContent(children); 121 } 122 log.info("Config : Subscribers loaded"); } 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"); } 142 catch (RepositoryException re) { 143 log.error("Config : Failed to load Subscribers"); log.error(re.getMessage(), re); 145 } 146 return children; 147 } 148 149 public static void reload() { 150 log.info("Config : re-loading Subscribers"); Subscriber.load(); 152 } 153 154 157 private static void cacheContent(Collection subs) { 158 159 Iterator list = subs.iterator(); 160 161 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(); 170 if (StringUtils.isEmpty(si.url)) { 171 String address = c.getNodeData("address").getString(); String protocol = c.getNodeData("protocol").getString(); 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(); si.requestConfirmation = c.getNodeData("requestConfirmation").getBoolean(); si.name = c.getName(); 190 191 if(StringUtils.equals(si.name, SUBSCRIBER_NODE_NAME)){ 192 si.name = DEFAULT_SUBSCRIBER_NAME; 193 } 194 195 String activeString = c.getNodeData("active").getString(); 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 subscribersEnabled = true; 208 } 209 210 addContext(si, c); 212 213 Subscriber.cachedContent.put(si.getName(), si); 214 } 215 } 216 217 222 private static void addContext(Subscriber subscriberInfo, Content contentNode) { 223 subscriberInfo.context = new Hashtable(); 224 Content contextList = ContentUtil.getCaseInsensitive(contentNode, CONTEXT_NODE_NAME); 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()); } 238 subscriberInfo.context.put(context.getName(), list); 239 } 240 } 241 242 246 public static Enumeration getList() { 247 return Subscriber.cachedContent.elements(); 248 } 249 250 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 267 public boolean isActive() { 268 return this.active; 269 } 270 271 275 public boolean getRequestConfirmation() { 276 return this.requestConfirmation; 277 } 278 279 282 public String getName() { 283 return this.name; 284 } 285 286 289 public List getContext(String name) { 290 if (this.context.get(name) == null) { 291 return new ArrayList(); 292 } 293 return (List) this.context.get(name); 294 } 295 296 300 public String getSenderURL() { 301 return this.senderURL; 302 } 303 304 309 public String getURL() { 310 return this.url; 311 } 312 313 } 314 | Popular Tags |