-
Notifications
You must be signed in to change notification settings - Fork 97
/
InstancesService.java
267 lines (235 loc) · 9.43 KB
/
InstancesService.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
* JBoss, Home of Professional Open Source.
* Copyright 2011, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.as.pojo.service;
import org.jboss.as.pojo.BeanState;
import org.jboss.as.pojo.descriptor.BeanMetaDataConfig;
import org.jboss.logging.Logger;
import org.jboss.msc.service.DuplicateServiceException;
import org.jboss.msc.service.Service;
import org.jboss.msc.service.ServiceBuilder;
import org.jboss.msc.service.ServiceController;
import org.jboss.msc.service.ServiceName;
import org.jboss.msc.service.ServiceRegistry;
import org.jboss.msc.service.ServiceTarget;
import org.jboss.msc.service.StartContext;
import org.jboss.msc.service.StartException;
import org.jboss.msc.service.StopContext;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Available instances per type
*
* @author <a href="mailto:ales.justin@jboss.org">Ales Justin</a>
*/
@SuppressWarnings({"SynchronizationOnLocalVariableOrMethodParameter"})
final class InstancesService implements Service<Set<Object>> {
private static final Logger log = Logger.getLogger(InstancesService.class);
private final Class<?> type;
private final Set<Object> instances = new HashSet<Object>(); // we do own locking, per type (service is also per type)
private static Map<TypeBeanStateKey, Set<Object>> beans = new HashMap<TypeBeanStateKey, Set<Object>>();
private static Map<TypeBeanStateKey, List<Callback>> incallbacks = new HashMap<TypeBeanStateKey, List<Callback>>();
private static Map<TypeBeanStateKey, List<Callback>> uncallbacks = new HashMap<TypeBeanStateKey, List<Callback>>();
private InstancesService(Class<?> type) {
this.type = type;
}
/**
* Add bean instance.
*
* @param registry the service registry
* @param target the service target
* @param state the bean state
* @param bean the bean
* @throws StartException for any error
*/
static void addInstance(ServiceRegistry registry, ServiceTarget target, BeanState state, Object bean) throws StartException {
addInstance(registry, target, state, bean.getClass(), bean);
}
/**
* Remove bean instance.
*
* @param registry the service registry
* @param state the bean state
* @param bean the bean
*/
static void removeInstance(ServiceRegistry registry, BeanState state, Object bean) {
removeInstance(registry, state, bean.getClass(), bean);
}
/**
* Add incallback.
*
* @param callback the callback
*/
static void addIncallback(Callback callback) {
addCallback(incallbacks, callback);
}
/**
* Add uncallback.
*
* @param callback the callback
*/
static void addUncallback(Callback callback) {
addCallback(uncallbacks, callback);
}
/**
* Remove incallback.
*
* @param callback the callback
*/
static void removeIncallback(Callback callback) {
removeCallback(incallbacks, callback);
}
/**
* Remove uncallback.
*
* @param callback the callback
*/
static void removeUncallback(Callback callback) {
removeCallback(uncallbacks, callback);
}
private static void addCallback(Map<TypeBeanStateKey, List<Callback>> map, Callback callback) {
final Class<?> type = callback.getType();
synchronized (type) {
if (map == incallbacks) {
try {
callback.dispatch(); // check all previous
} catch (Throwable t) {
log.warn("Error invoking incallback: " + callback, t);
}
}
TypeBeanStateKey key = new TypeBeanStateKey(type, callback.getState());
List<Callback> callbacks = map.get(key);
if (callbacks == null) {
callbacks = new ArrayList<Callback>();
map.put(key, callbacks);
}
callbacks.add(callback);
}
}
private static void removeCallback(Map<TypeBeanStateKey, List<Callback>> map, Callback callback) {
final Class<?> type = callback.getType();
synchronized (type) {
TypeBeanStateKey key = new TypeBeanStateKey(type, callback.getState());
List<Callback> callbacks = map.get(key);
if (callbacks != null) {
callbacks.remove(callback);
if (callbacks.isEmpty())
map.remove(key);
}
if (map == uncallbacks) {
try {
callback.dispatch(); // try all remaining
} catch (Throwable t) {
log.warn("Error invoking uncallback: " + callback, t);
}
}
}
}
private static void invokeCallbacks(Map<TypeBeanStateKey, List<Callback>> map, BeanState state, final Class<?> clazz, Object bean) {
synchronized (clazz) {
TypeBeanStateKey key = new TypeBeanStateKey(clazz, state);
List<Callback> callbacks = map.get(key);
if (callbacks != null) {
for (Callback c : callbacks) {
try {
c.dispatch(bean);
} catch (Throwable t) {
log.warn("Error invoking callback: " + c, t);
}
}
}
}
}
private static void addInstance(ServiceRegistry registry, ServiceTarget target, BeanState state, final Class<?> clazz, Object bean) throws StartException {
if (clazz == null)
return;
ServiceName name = BeanMetaDataConfig.toInstancesName(clazz, state);
ServiceBuilder<Set<Object>> builder = target.addService(name, new InstancesService(clazz));
InstancesService service = putIfAbsent(registry, name, builder);
synchronized (clazz) {
service.instances.add(bean);
TypeBeanStateKey key = new TypeBeanStateKey(clazz, state);
if (beans.containsKey(key) == false)
beans.put(key, service.instances);
invokeCallbacks(incallbacks, state, clazz, bean);
}
addInstance(registry, target, state, clazz.getSuperclass(), bean);
Class<?>[] ifaces = clazz.getInterfaces();
for (Class<?> iface : ifaces)
addInstance(registry, target, state, iface, bean);
}
private static InstancesService putIfAbsent(ServiceRegistry registry, ServiceName name, ServiceBuilder builder) throws StartException {
for (; ; ) {
try {
ServiceController sc = registry.getService(name);
if (sc == null) {
sc = builder.install();
}
return (InstancesService) sc.getService();
} catch (DuplicateServiceException ignored) {
} catch (Exception e) {
throw new StartException(e);
}
}
}
private static void removeInstance(ServiceRegistry registry, BeanState state, final Class<?> clazz, Object bean) {
if (clazz == null)
return;
ServiceController controller = registry.getService(BeanMetaDataConfig.toInstancesName(clazz, state));
if (controller != null) {
InstancesService service = (InstancesService) controller.getService();
synchronized (clazz) {
service.instances.remove(bean);
invokeCallbacks(uncallbacks, state, clazz, bean);
if (service.instances.isEmpty()) {
beans.remove(new TypeBeanStateKey(clazz, state));
controller.setMode(ServiceController.Mode.REMOVE);
}
}
}
removeInstance(registry, state, clazz.getSuperclass(), bean);
Class<?>[] ifaces = clazz.getInterfaces();
for (Class<?> iface : ifaces)
removeInstance(registry, state, iface, bean);
}
@Override
public void start(StartContext context) throws StartException {
}
@Override
public void stop(StopContext context) {
}
@Override
public Set<Object> getValue() throws IllegalStateException, IllegalArgumentException {
synchronized (type) {
return Collections.unmodifiableSet(instances);
}
}
static Set<Object> getBeans(Class<?> type, BeanState state) {
TypeBeanStateKey key = new TypeBeanStateKey(type, state);
Set<Object> objects = beans.get(key);
return (objects != null) ? objects : Collections.emptySet();
}
}