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