|
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 |
| package org.objectledge.cache.impl; |
|
30 |
| |
|
31 |
| import java.io.ByteArrayInputStream; |
|
32 |
| import java.io.ByteArrayOutputStream; |
|
33 |
| import java.io.IOException; |
|
34 |
| import java.io.ObjectInputStream; |
|
35 |
| import java.io.ObjectOutputStream; |
|
36 |
| |
|
37 |
| import org.objectledge.cache.NoLongerValidException; |
|
38 |
| import org.objectledge.cache.Refreshable; |
|
39 |
| import org.objectledge.cache.spi.CacheFactorySPI; |
|
40 |
| import org.objectledge.cache.spi.ConfigurableMap; |
|
41 |
| import org.objectledge.cache.spi.DistributedMap; |
|
42 |
| import org.objectledge.notification.Notification; |
|
43 |
| import org.objectledge.notification.NotificationReceiver; |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| public class DistributedMapImpl |
|
52 |
| extends DelegateMap |
|
53 |
| implements DistributedMap, ConfigurableMap, |
|
54 |
| NotificationReceiver |
|
55 |
| { |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| public static final String CHANNEL_NAME = "ledge.cache:1.0"; |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| private Notification notification; |
|
65 |
| |
|
66 |
| |
|
67 |
| private String channel; |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
828
| public DistributedMapImpl()
|
|
75 |
| { |
|
76 |
828
| super();
|
|
77 |
| } |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
782
| public void configure(CacheFactorySPI caching, String name, String config)
|
|
85 |
| { |
|
86 |
782
| attach(caching.getNotification(), name);
|
|
87 |
| } |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
828
| public void attach(Notification notification, String name)
|
|
95 |
| { |
|
96 |
828
| this.notification = notification;
|
|
97 |
828
| channel = CHANNEL_NAME+"/"+name;
|
|
98 |
| |
|
99 |
| |
|
100 |
| } |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
0
| public void detach()
|
|
106 |
| { |
|
107 |
0
| if(notification != null)
|
|
108 |
| { |
|
109 |
0
| notification.removeReceiver(channel, this);
|
|
110 |
0
| notification = null;
|
|
111 |
| } |
|
112 |
| } |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
0
| public void updated(Object key)
|
|
120 |
| { |
|
121 |
0
| notify(true, key);
|
|
122 |
| } |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
0
| public Object put(Object key, Object value)
|
|
130 |
| { |
|
131 |
0
| notify(false, key);
|
|
132 |
0
| return delegate.put(key, value);
|
|
133 |
| } |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
| |
|
138 |
0
| public Object remove(Object key)
|
|
139 |
| { |
|
140 |
0
| notify(false, key);
|
|
141 |
0
| return delegate.remove(key);
|
|
142 |
| } |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
| |
|
147 |
| |
|
148 |
| |
|
149 |
0
| public void receive(String channel, byte[] message)
|
|
150 |
| { |
|
151 |
0
| boolean update;
|
|
152 |
0
| Object key;
|
|
153 |
0
| try
|
|
154 |
| { |
|
155 |
0
| ByteArrayInputStream bais = new ByteArrayInputStream(message);
|
|
156 |
0
| ObjectInputStream is = new ObjectInputStream(bais);
|
|
157 |
0
| update = is.readBoolean();
|
|
158 |
0
| key = is.readObject();
|
|
159 |
| } |
|
160 |
| catch(Exception e) |
|
161 |
| { |
|
162 |
0
| throw new RuntimeException("failed to decode message", e);
|
|
163 |
| } |
|
164 |
0
| if(update)
|
|
165 |
| { |
|
166 |
0
| Object value = delegate.get(key);
|
|
167 |
0
| if(value != null)
|
|
168 |
| { |
|
169 |
0
| if(value instanceof Refreshable)
|
|
170 |
| { |
|
171 |
0
| try
|
|
172 |
| { |
|
173 |
0
| ((Refreshable)value).refresh();
|
|
174 |
0
| return;
|
|
175 |
| } |
|
176 |
| catch(NoLongerValidException e) |
|
177 |
| { |
|
178 |
0
| delegate.remove(key);
|
|
179 |
| } |
|
180 |
| } |
|
181 |
| } |
|
182 |
| } |
|
183 |
| |
|
184 |
0
| delegate.remove(key);
|
|
185 |
| } |
|
186 |
| |
|
187 |
| |
|
188 |
| |
|
189 |
0
| private void notify(boolean update, Object key)
|
|
190 |
| { |
|
191 |
0
| byte[] message;
|
|
192 |
0
| try
|
|
193 |
| { |
|
194 |
0
| ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
195 |
0
| ObjectOutputStream os = new ObjectOutputStream(baos);
|
|
196 |
0
| os.writeBoolean(update);
|
|
197 |
0
| os.writeObject(key);
|
|
198 |
0
| os.flush();
|
|
199 |
0
| message = baos.toByteArray();
|
|
200 |
| } |
|
201 |
| catch(IOException e) |
|
202 |
| { |
|
203 |
0
| throw new RuntimeException("failed to encode requeuest", e);
|
|
204 |
| } |
|
205 |
0
| notification.sendNotification(channel, message, false);
|
|
206 |
| } |
|
207 |
| } |