1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.cms.beans.config;
35
36 import info.magnolia.cms.core.Content;
37 import info.magnolia.cms.core.DefaultContent;
38 import info.magnolia.cms.util.ObservationUtil;
39 import info.magnolia.cms.util.SystemContentWrapper;
40 import info.magnolia.context.MgnlContext;
41 import info.magnolia.repository.RepositoryConstants;
42
43 import java.util.ArrayList;
44 import java.util.HashMap;
45 import java.util.HashSet;
46 import java.util.Iterator;
47 import java.util.List;
48 import java.util.Map;
49 import java.util.Set;
50
51 import javax.jcr.ItemNotFoundException;
52 import javax.jcr.Node;
53 import javax.jcr.RepositoryException;
54 import javax.jcr.Session;
55 import javax.jcr.observation.EventIterator;
56 import javax.jcr.observation.EventListener;
57
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
60
61
62
63
64
65
66
67 public abstract class ObservedManager {
68 protected final Logger log = LoggerFactory.getLogger(getClass());
69
70
71
72
73 protected Set<String> registeredUUIDs = new HashSet<String>();
74 private Map<String, String> UUIDToPathMap = new HashMap<String, String>();
75
76
77
78
79
80
81 public synchronized void register(Content node) {
82 if (node == null) {
83 log.warn("Tried to register a non-existing node!");
84 return;
85 }
86
87 ObservationUtil.registerDeferredChangeListener(RepositoryConstants.CONFIG, node.getHandle(), new EventListener() {
88
89 @Override
90 public void onEvent(EventIterator events) {
91 reload();
92 }
93 }, 1000, 5000);
94
95 try {
96 registeredUUIDs.add(node.getUUID());
97 UUIDToPathMap.put(node.getUUID(), node.getHandle());
98 onRegister(new SystemContentWrapper(node));
99 } catch (Exception e) {
100 log.warn("Was not able to register [" + node.getHandle() + "]", e);
101 }
102 }
103
104
105
106
107 public synchronized void reload() {
108
109 onClear();
110
111 Session session = null;
112 try {
113 session = MgnlContext.getSystemContext().getJCRSession(RepositoryConstants.CONFIG);
114 } catch (RepositoryException e) {
115 throw new RuntimeException(e);
116 }
117
118
119 List<String> uuidsAndPaths = new ArrayList<String>(registeredUUIDs);
120
121 for (Iterator<String> iter = uuidsAndPaths.iterator(); iter.hasNext();) {
122 String uuid = iter.next();
123 try {
124 Node node = getNodeByIdentifierOrPath(session, uuid, UUIDToPathMap.get(uuid));
125 reload(new DefaultContent(node));
126 } catch (Exception e) {
127 registeredUUIDs.remove(uuid);
128 UUIDToPathMap.remove(uuid);
129 log.warn("Can't reload the node with uuid [" + uuid + "] and path [" + UUIDToPathMap.get(uuid) + "]");
130 }
131 }
132 return;
133 }
134
135
136
137
138
139
140
141 protected static Node getNodeByIdentifierOrPath(Session session, String uuid, String path) throws RepositoryException {
142 Node node = null;
143
144 try {
145 node = session.getNodeByIdentifier(uuid);
146 } catch (ItemNotFoundException e) {
147 node = session.getNode(path);
148 }
149
150 return node;
151 }
152
153
154
155
156
157
158 protected void reload(Content node) {
159 onRegister(node);
160 }
161
162
163
164
165 public void clear() {
166 this.registeredUUIDs.clear();
167 this.UUIDToPathMap.clear();
168 onClear();
169 }
170
171
172
173
174
175
176 protected abstract void onRegister(Content node);
177
178
179
180
181 protected abstract void onClear();
182
183 }