-
Notifications
You must be signed in to change notification settings - Fork 283
/
Observers.java
181 lines (161 loc) · 8.06 KB
/
Observers.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
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.weld.util;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.util.Set;
import javax.enterprise.inject.spi.AfterBeanDiscovery;
import javax.enterprise.inject.spi.AfterDeploymentValidation;
import javax.enterprise.inject.spi.AfterTypeDiscovery;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.BeforeBeanDiscovery;
import javax.enterprise.inject.spi.BeforeShutdown;
import javax.enterprise.inject.spi.EventContext;
import javax.enterprise.inject.spi.EventMetadata;
import javax.enterprise.inject.spi.ObserverMethod;
import javax.enterprise.inject.spi.ProcessAnnotatedType;
import javax.enterprise.inject.spi.ProcessBean;
import javax.enterprise.inject.spi.ProcessBeanAttributes;
import javax.enterprise.inject.spi.ProcessInjectionPoint;
import javax.enterprise.inject.spi.ProcessInjectionTarget;
import javax.enterprise.inject.spi.ProcessManagedBean;
import javax.enterprise.inject.spi.ProcessObserverMethod;
import javax.enterprise.inject.spi.ProcessProducer;
import javax.enterprise.inject.spi.ProcessProducerField;
import javax.enterprise.inject.spi.ProcessProducerMethod;
import javax.enterprise.inject.spi.ProcessSessionBean;
import javax.enterprise.inject.spi.ProcessSyntheticAnnotatedType;
import org.jboss.weld.bootstrap.SpecializationAndEnablementRegistry;
import org.jboss.weld.event.ContainerLifecycleEventObserverMethod;
import org.jboss.weld.event.EventMetadataAwareObserverMethod;
import org.jboss.weld.event.ObserverMethodImpl;
import org.jboss.weld.event.SyntheticObserverMethod;
import org.jboss.weld.logging.EventLogger;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.security.GetDeclaredMethodsAction;
import org.jboss.weld.util.collections.ImmutableSet;
import org.jboss.weld.util.reflection.Reflections;
/**
* @author pmuir
*/
public class Observers {
/*
* Contains all container lifecycle event types
*/
public static final Set<Class<?>> CONTAINER_LIFECYCLE_EVENT_CANONICAL_SUPERTYPES = ImmutableSet.of(BeforeBeanDiscovery.class, AfterTypeDiscovery.class,
AfterBeanDiscovery.class, AfterDeploymentValidation.class, BeforeShutdown.class, ProcessAnnotatedType.class, ProcessInjectionPoint.class,
ProcessInjectionTarget.class, ProcessProducer.class, ProcessBeanAttributes.class, ProcessBean.class, ProcessObserverMethod.class);
/*
* Contains only top superinterfaces of each chain of container lifecycle event types.
*/
public static final Set<Class<?>> CONTAINER_LIFECYCLE_EVENT_TYPES = ImmutableSet.<Class<?>> builder().addAll(CONTAINER_LIFECYCLE_EVENT_CANONICAL_SUPERTYPES)
.addAll(ProcessSyntheticAnnotatedType.class, ProcessSessionBean.class, ProcessManagedBean.class, ProcessProducerMethod.class,
ProcessProducerField.class, ProcessObserverMethod.class)
.build();
private static final String NOTIFY_METHOD_NAME = "notify";
private Observers() {
}
public static boolean isContainerLifecycleObserverMethod(ObserverMethod<?> method) {
return CONTAINER_LIFECYCLE_EVENT_TYPES.contains(Reflections.getRawType(method.getObservedType()))
|| (Object.class.equals(method.getObservedType()) && method instanceof ContainerLifecycleEventObserverMethod);
}
public static boolean isObserverMethodEnabled(ObserverMethod<?> method, BeanManagerImpl manager) {
if (method instanceof ObserverMethodImpl<?, ?>) {
Bean<?> declaringBean = Reflections.<ObserverMethodImpl<?, ?>> cast(method).getDeclaringBean();
return manager.getServices().get(SpecializationAndEnablementRegistry.class).isCandidateForLifecycleEvent(declaringBean);
}
return true;
}
/**
* Validates given external observer method.
*
* @param observerMethod the given observer method
* @param beanManager
* @param originalObserverMethod observer method replaced by given observer method (this parameter is optional)
*/
public static void validateObserverMethod(ObserverMethod<?> observerMethod, BeanManager beanManager, ObserverMethod<?> originalObserverMethod) {
Set<Annotation> qualifiers = observerMethod.getObservedQualifiers();
if (observerMethod.getBeanClass() == null) {
throw EventLogger.LOG.observerMethodsMethodReturnsNull("getBeanClass", observerMethod);
}
if (observerMethod.getObservedType() == null) {
throw EventLogger.LOG.observerMethodsMethodReturnsNull("getObservedType", observerMethod);
}
Bindings.validateQualifiers(qualifiers, beanManager, observerMethod, "ObserverMethod.getObservedQualifiers");
if (observerMethod.getReception() == null) {
throw EventLogger.LOG.observerMethodsMethodReturnsNull("getReception", observerMethod);
}
if (observerMethod.getTransactionPhase() == null) {
throw EventLogger.LOG.observerMethodsMethodReturnsNull("getTransactionPhase", observerMethod);
}
if (originalObserverMethod != null && (!observerMethod.getBeanClass().equals(originalObserverMethod.getBeanClass()))) {
throw EventLogger.LOG.beanClassMismatch(originalObserverMethod, observerMethod);
}
if (!(observerMethod instanceof SyntheticObserverMethod) && !hasNotifyOverriden(observerMethod.getClass(), observerMethod)) {
throw EventLogger.LOG.notifyMethodNotImplemented(observerMethod);
}
}
/**
* Determines whether the given observer method is either extension-provided or contains an injection point with {@link EventMetadata} type.
*/
public static boolean isEventMetadataRequired(ObserverMethod<?> observer) {
if (observer instanceof EventMetadataAwareObserverMethod) {
EventMetadataAwareObserverMethod<?> eventMetadataAware = (EventMetadataAwareObserverMethod<?>) observer;
return eventMetadataAware.isEventMetadataRequired();
} else {
return true;
}
}
/**
*
* @param observerMethod
* @param event
* @param metadata May be null
*/
public static <T> void notify(ObserverMethod<? super T> observerMethod, T event, EventMetadata metadata) {
observerMethod.notify(new EventContextImpl<>(event, metadata));
}
private static boolean hasNotifyOverriden(Class<?> clazz, ObserverMethod<?> observerMethod) {
if (clazz.isInterface()) {
return false;
}
for (Method method : AccessController.doPrivileged(new GetDeclaredMethodsAction(clazz))) {
if (NOTIFY_METHOD_NAME.equals(method.getName()) && method.getParameterTypes().length == 1) {
return true;
}
}
return clazz.getSuperclass() != null ? hasNotifyOverriden(clazz.getSuperclass(), observerMethod) : false;
}
static class EventContextImpl<T> implements EventContext<T> {
private final T event;
private final EventMetadata metadata;
EventContextImpl(T event, EventMetadata metadata) {
this.event = event;
this.metadata = metadata;
}
@Override
public T getEvent() {
return event;
}
@Override
public EventMetadata getMetadata() {
return metadata;
}
}
}