1 13 package info.magnolia.cms.beans.config; 14 15 import info.magnolia.cms.core.Content; 16 import info.magnolia.cms.core.HierarchyManager; 17 import info.magnolia.cms.util.ObservationUtil; 18 import info.magnolia.context.MgnlContext; 19 20 import java.util.ArrayList ; 21 import java.util.Collections ; 22 import java.util.HashSet ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Set ; 26 27 import javax.jcr.observation.EventIterator; 28 import javax.jcr.observation.EventListener; 29 30 import org.apache.commons.collections.CollectionUtils; 31 import org.slf4j.Logger; 32 import org.slf4j.LoggerFactory; 33 34 35 40 public abstract class ObservedManager { 41 42 45 protected Logger log = LoggerFactory.getLogger(getClass()); 46 47 50 private int reloadRequestCount = 0; 51 52 55 private boolean reloading = false; 56 57 60 private static final long SLEEP_MILLIS = 1000; 61 62 65 protected Set registeredUUIDs = new HashSet (); 66 67 71 public synchronized void register(Content node) { 72 if (node == null) { 73 log.warn("tried to register a not existing node!"); 74 return; 75 } 76 77 ObservationUtil.registerChangeListener(ContentRepository.CONFIG, node.getHandle(), new EventListener() { 78 79 public void onEvent(EventIterator events) { 80 reload(); 81 } 82 }); 83 84 try { 85 registeredUUIDs.add(node.getUUID()); 86 onRegister(node); 87 } 88 catch (Exception e) { 89 log.warn("Was not able to register [" + node.getHandle() + "]", e); 90 } 91 } 92 93 96 public synchronized void reload() { 97 if (this.reloading == true) { 99 log.debug("this manager waiting for reloading: [{}]", this.getClass().getName()); 100 this.reloadRequestCount++; 101 return; 102 } 103 setReloading(true); 104 this.reloadRequestCount = 0; 105 Reloader reloader = new Reloader(this, this.reloadRequestCount); 106 new Thread (reloader).start(); 107 } 108 109 113 protected final void reload(Content node) { 114 onRegister(node); 115 } 116 117 120 public final void clear() { 121 this.registeredUUIDs.clear(); 122 onClear(); 123 } 124 125 129 protected abstract void onRegister(Content node); 130 131 134 protected abstract void onClear(); 135 136 139 public boolean isReloading() { 140 return reloading; 141 } 142 143 147 protected void setReloading(boolean reloading) { 148 this.reloading = reloading; 149 } 150 151 154 protected int getReloadRequestCount() { 155 return reloadRequestCount; 156 } 157 158 164 private class Reloader implements Runnable { 165 166 169 private int lastReloadRequestCount = 0; 170 171 174 private ObservedManager observedManager; 175 176 181 protected Reloader(ObservedManager observedManager, int reloadRequestCount) { 182 this.observedManager = observedManager; 183 this.lastReloadRequestCount = reloadRequestCount; 184 } 185 186 189 public void run() { 190 while (true) { 191 try { 192 Thread.sleep(SLEEP_MILLIS); 193 } 194 catch (InterruptedException e) { 195 } 197 198 int currentReloadRequestCount = this.observedManager.getReloadRequestCount(); 200 if (currentReloadRequestCount > lastReloadRequestCount) { 201 lastReloadRequestCount = currentReloadRequestCount; 202 } 203 else { 204 this.observedManager.setReloading(false); 206 207 this.observedManager.onClear(); 209 210 HierarchyManager hm = MgnlContext.getSystemContext().getHierarchyManager(ContentRepository.CONFIG); 211 212 List uuids = new ArrayList (this.observedManager.registeredUUIDs); 214 215 for (Iterator iter = uuids.iterator(); iter.hasNext();) { 216 String uuid = (String ) iter.next(); 217 try { 218 Content node = hm.getContentByUUID(uuid); 219 this.observedManager.reload(node); 220 } 221 catch (Exception e) { 222 this.observedManager.registeredUUIDs.remove(uuid); 223 log.warn("can't reload the the node [" + uuid + "]"); 224 } 225 } 226 return; 227 } 228 } 229 } 230 } 231 } 232 | Popular Tags |