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 public abstract class ObservedManager {
66 protected final Logger log = LoggerFactory.getLogger(getClass());
67
68
69
70
71 protected Set<String> registeredUUIDs = new HashSet<String>();
72 private Map<String, String> UUIDToPathMap = new HashMap<String, String>();
73
74
75
76
77
78
79 public synchronized void register(Content node) {
80 if (node == null) {
81 log.warn("Tried to register a non-existing node!");
82 return;
83 }
84
85 ObservationUtil.registerDeferredChangeListener(RepositoryConstants.CONFIG, node.getHandle(), new EventListener() {
86
87 @Override
88 public void onEvent(EventIterator events) {
89 reload();
90 }
91 }, 1000, 5000);
92
93 try {
94 registeredUUIDs.add(node.getUUID());
95 UUIDToPathMap.put(node.getUUID(), node.getHandle());
96 onRegister(new SystemContentWrapper(node));
97 } catch (Exception e) {
98 log.warn("Was not able to register [{}]", node.getHandle(), e);
99 }
100 }
101
102
103
104
105 public synchronized void reload() {
106
107 onClear();
108
109 Session session = null;
110 try {
111 session = MgnlContext.getSystemContext().getJCRSession(RepositoryConstants.CONFIG);
112 } catch (RepositoryException e) {
113 throw new RuntimeException(e);
114 }
115
116
117 List<String> uuidsAndPaths = new ArrayList<String>(registeredUUIDs);
118
119 for (Iterator<String> iter = uuidsAndPaths.iterator(); iter.hasNext();) {
120 String uuid = iter.next();
121 try {
122 Node node = getNodeByIdentifierOrPath(session, uuid, UUIDToPathMap.get(uuid));
123 reload(new DefaultContent(node));
124 } catch (Exception e) {
125 registeredUUIDs.remove(uuid);
126 UUIDToPathMap.remove(uuid);
127 log.warn("Can't reload the node with uuid [{}] and path [{}]", uuid, UUIDToPathMap.get(uuid));
128 }
129 }
130 return;
131 }
132
133
134
135
136
137
138
139 protected static Node getNodeByIdentifierOrPath(Session session, String uuid, String path) throws RepositoryException {
140 Node node = null;
141
142 try {
143 node = session.getNodeByIdentifier(uuid);
144 } catch (ItemNotFoundException e) {
145 node = session.getNode(path);
146 }
147
148 return node;
149 }
150
151
152
153
154 protected void reload(Content node) {
155 onRegister(node);
156 }
157
158
159
160
161 public void clear() {
162 this.registeredUUIDs.clear();
163 this.UUIDToPathMap.clear();
164 onClear();
165 }
166
167
168
169
170 protected abstract void onRegister(Content node);
171
172
173
174
175 protected abstract void onClear();
176
177 }