/
ClassBean.java
140 lines (123 loc) · 4.57 KB
/
ClassBean.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
/*
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.jersey.inject.weld.internal.bean;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Set;
import javax.enterprise.context.Dependent;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.InjectionTarget;
import javax.ws.rs.RuntimeType;
import org.glassfish.jersey.inject.weld.internal.injector.JerseyInjectionTarget;
import org.glassfish.jersey.internal.inject.ClassBinding;
/**
* Creates an implementation of {@link javax.enterprise.inject.spi.Bean} interface using Jersey's {@link ClassBinding}. Binding
* provides the information about the bean also called {@link javax.enterprise.inject.spi.BeanAttributes} information and
* {@link JerseyInjectionTarget} provides the contextual part of the bean because implements
* {@link javax.enterprise.context.spi.Contextual} with Jersey injection extension (is able to inject into JAX-RS/Jersey specified
* annotation).
* <p>
* Inject example:
* <pre>
* AbstractBinder {
* @Override
* protected void configure() {
* bind(MyBean.class)
* .to(MyBean.class)
* .in(Singleton.class);
* }
* }
* </pre>
* Register example:
* <pre>
* @Path("/")
* public class MyResource {
* @Inject
* private MyBean myBean;
* }
* </pre>
*
* @author Petr Bouda
*/
class ClassBean<T> extends JerseyBean<T> {
private final ClassBinding<T> binding;
private InjectionTarget<T> injectionTarget;
/**
* Creates a new Jersey-specific {@link javax.enterprise.inject.spi.Bean} instance.
* @param runtimeType {@link RuntimeType} type information of the bean source.
* @param binding the binding information.
*/
ClassBean(RuntimeType runtimeType, ClassBinding<T> binding) {
super(runtimeType, binding);
this.binding = binding;
}
@Override
public Class<? extends Annotation> getScope() {
/*
* Resource class without the Scope annotation should registered as a RequestScoped.
*/
if (binding.getScope() == null && BeanHelper.isResourceClass(binding.getService())) {
return RequestScoped.class;
}
return binding.getScope() == null ? Dependent.class : transformScope(binding.getScope());
}
@Override
@SuppressWarnings("unchecked")
public T create(CreationalContext<T> context) {
T instance = injectionTarget.produce(context);
injectionTarget.inject(instance, context);
injectionTarget.postConstruct(instance);
return instance;
}
@Override
public void destroy(T instance, CreationalContext<T> context) {
injectionTarget.preDestroy(instance);
injectionTarget.dispose(instance);
context.release();
}
@Override
public Set<Type> getTypes() {
Set<Type> contracts = super.getTypes();
contracts.addAll(Arrays.asList(binding.getService().getInterfaces()));
return contracts;
}
@Override
public Class<?> getBeanClass() {
return binding.getService();
}
@Override
public Set<InjectionPoint> getInjectionPoints() {
return injectionTarget.getInjectionPoints();
}
/**
* Lazy set of an injection target because to create fully functional injection target needs already created bean.
*
* @param injectionTarget {@link javax.enterprise.context.spi.Contextual} information belonging to this bean.
*/
void setInjectionTarget(InjectionTarget<T> injectionTarget) {
this.injectionTarget = injectionTarget;
}
@Override
public String toString() {
return "ClassBean{" + getBeanClass() + "(" + getRutimeType() + ")}";
}
public InjectionTarget<T> getInjectionTarget() {
return injectionTarget;
}
}