/
DecorationHelper.java
127 lines (107 loc) · 5.34 KB
/
DecorationHelper.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
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc. and/or its affiliates, 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.bean.proxy;
import org.jboss.weld.context.SerializableContextualInstanceImpl;
import org.jboss.weld.exceptions.WeldException;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.serialization.spi.ContextualStore;
import org.jboss.weld.util.reflection.Reflections;
import org.jboss.weld.util.reflection.SecureReflections;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.Decorator;
import javax.enterprise.inject.spi.InjectionPoint;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
import static org.jboss.weld.logging.messages.BeanMessage.PROXY_INSTANTIATION_BEAN_ACCESS_FAILED;
import static org.jboss.weld.logging.messages.BeanMessage.PROXY_INSTANTIATION_FAILED;
/**
* @author Marius Bogoevici
* @author Ales Justin
*/
public class DecorationHelper<T> {
private static ThreadLocal<Stack<DecorationHelper<?>>> helperStackHolder = new ThreadLocal<Stack<DecorationHelper<?>>>() {
@Override
protected Stack<DecorationHelper<?>> initialValue() {
return new Stack<DecorationHelper<?>>();
}
};
private final Class<T> proxyClassForDecorator;
private final TargetBeanInstance targetBeanInstance;
private T originalInstance;
private T previousDelegate;
private int counter;
private BeanManagerImpl beanManager;
private final ContextualStore contextualStore;
private final Bean<?> bean;
List<Decorator<?>> decorators;
public DecorationHelper(TargetBeanInstance originalInstance, Bean<?> bean, Class<T> proxyClassForDecorator, BeanManagerImpl beanManager, ContextualStore contextualStore, List<Decorator<?>> decorators) {
this.originalInstance = Reflections.cast(originalInstance.getInstance());
this.targetBeanInstance = originalInstance;
this.beanManager = beanManager;
this.contextualStore = contextualStore;
this.decorators = new LinkedList<Decorator<?>>(decorators);
this.proxyClassForDecorator = proxyClassForDecorator;
this.bean = bean;
counter = 0;
}
public static void push(DecorationHelper<?> helper) {
helperStackHolder.get().push(helper);
}
public static DecorationHelper<?> peek() {
return helperStackHolder.get().peek();
}
public static void pop() {
final Stack<DecorationHelper<?>> stack = helperStackHolder.get();
stack.pop();
if (stack.isEmpty()) {
helperStackHolder.remove();
}
}
public T getNextDelegate(InjectionPoint injectionPoint, CreationalContext<?> creationalContext) {
if (counter == decorators.size()) {
previousDelegate = originalInstance;
return originalInstance;
} else {
T proxy = createProxy(injectionPoint, creationalContext);
previousDelegate = proxy;
return proxy;
}
}
private T createProxy(InjectionPoint injectionPoint, CreationalContext<?> creationalContext) {
try {
T proxy = SecureReflections.newInstance(proxyClassForDecorator);
TargetBeanInstance newTargetBeanInstance = new TargetBeanInstance(targetBeanInstance);
Decorator<Object> decorator = Reflections.cast(decorators.get(counter++));
DecoratorProxyMethodHandler methodHandler = createMethodHandler(injectionPoint, creationalContext, decorator);
newTargetBeanInstance.setInterceptorsHandler(methodHandler);
ProxyFactory.setBeanInstance(proxy, newTargetBeanInstance, bean);
return proxy;
} catch (InstantiationException e) {
throw new WeldException(PROXY_INSTANTIATION_FAILED, e, this);
} catch (IllegalAccessException e) {
throw new WeldException(PROXY_INSTANTIATION_BEAN_ACCESS_FAILED, e, this);
}
}
public DecoratorProxyMethodHandler createMethodHandler(InjectionPoint injectionPoint, CreationalContext<?> creationalContext, Decorator<Object> decorator) {
Object decoratorInstance = beanManager.getReference(injectionPoint, decorator, creationalContext);
assert previousDelegate != null : "previousDelegate should have been set when calling beanManager.getReference(), but it wasn't!";
SerializableContextualInstanceImpl<Decorator<Object>, Object> serializableContextualInstance = new SerializableContextualInstanceImpl<Decorator<Object>, Object>(decorator, decoratorInstance, null, contextualStore);
return new DecoratorProxyMethodHandler(serializableContextualInstance, previousDelegate);
}
}