-
Notifications
You must be signed in to change notification settings - Fork 53
/
ConfiguredByInjectionResolver.java
executable file
·180 lines (152 loc) · 6.36 KB
/
ConfiguredByInjectionResolver.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
/*
* Copyright (c) 2014, 2024 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.hk2.configuration.internal;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.inject.Singleton;
import org.glassfish.hk2.api.ActiveDescriptor;
import org.glassfish.hk2.api.DescriptorVisibility;
import org.glassfish.hk2.api.Injectee;
import org.glassfish.hk2.api.InjectionResolver;
import org.glassfish.hk2.api.ServiceHandle;
import org.glassfish.hk2.api.Visibility;
import org.glassfish.hk2.configuration.api.Configured;
/**
* @author jwells
*
*/
@Singleton
@Visibility(DescriptorVisibility.LOCAL)
public class ConfiguredByInjectionResolver implements
InjectionResolver<Configured> {
@Inject @Named(InjectionResolver.SYSTEM_RESOLVER_NAME)
private InjectionResolver<Inject> systemResolver;
@Inject
private ConfiguredByContext context;
private final ReentrantLock lock = new ReentrantLock();
private final ConcurrentHashMap<ActiveDescriptor<?>, BeanInfo> beanMap = new ConcurrentHashMap<ActiveDescriptor<?>, BeanInfo>();
private static String getParameterNameFromConstructor(Constructor<?> cnst, int position) {
Annotation paramAnnotations[] = cnst.getParameterAnnotations()[position];
Configured c = null;
for (Annotation anno : paramAnnotations) {
if (Configured.class.equals(anno.annotationType())) {
c = (Configured) anno;
break;
}
}
if (c == null) return null;
String key = c.value();
if (BeanUtilities.isEmpty(key)) {
throw new AssertionError("Not enough in @Configured annotation in constructor " + cnst + " at parameter index " + position);
}
return key;
}
private static String getParameterNameFromMethod(Method method, int position) {
Annotation paramAnnotations[] = method.getParameterAnnotations()[position];
Configured c = null;
for (Annotation anno : paramAnnotations) {
if (Configured.class.equals(anno.annotationType())) {
c = (Configured) anno;
break;
}
}
if (c == null) return null;
String key = c.value();
if (BeanUtilities.isEmpty(key)) {
throw new AssertionError("Not enough in @Configured annotation in method " + method + " at parameter index " + position);
}
return key;
}
/* (non-Javadoc)
* @see org.glassfish.hk2.api.InjectionResolver#resolve(org.glassfish.hk2.api.Injectee, org.glassfish.hk2.api.ServiceHandle)
*/
@Override
public Object resolve(Injectee injectee, ServiceHandle<?> root) {
lock.lock();
try {
ActiveDescriptor<?> injecteeParent = injectee.getInjecteeDescriptor();
if (injecteeParent == null) return systemResolver.resolve(injectee, root);
AnnotatedElement ae = injectee.getParent();
if (ae == null) return systemResolver.resolve(injectee, root);
String parameterName = null;
if (ae instanceof Field) {
parameterName = BeanUtilities.getParameterNameFromField((Field) ae, false);
}
else if (ae instanceof Constructor) {
parameterName = getParameterNameFromConstructor((Constructor<?>) ae, injectee.getPosition());
}
else if (ae instanceof Method){
parameterName = getParameterNameFromMethod((Method) ae, injectee.getPosition());
}
else {
return systemResolver.resolve(injectee, root);
}
if (parameterName == null) return systemResolver.resolve(injectee, root);
ActiveDescriptor<?> workingOn = context.getWorkingOn();
if (workingOn == null) return systemResolver.resolve(injectee, root);
BeanInfo beanInfo = beanMap.get(workingOn);
if (beanInfo == null) {
throw new IllegalStateException("Could not find a configuration bean for " + injectee + " with descriptor " + workingOn);
}
return BeanUtilities.getBeanPropertyValue(injectee.getRequiredType(), parameterName, beanInfo);
} finally {
lock.unlock();
}
}
/* (non-Javadoc)
* @see org.glassfish.hk2.api.InjectionResolver#isConstructorParameterIndicator()
*/
@Override
public boolean isConstructorParameterIndicator() {
return true;
}
/* (non-Javadoc)
* @see org.glassfish.hk2.api.InjectionResolver#isMethodParameterIndicator()
*/
@Override
public boolean isMethodParameterIndicator() {
return true;
}
/* package */ BeanInfo addBean(ActiveDescriptor<?> descriptor, Object bean, String type, Object metadata) {
lock.lock();
try {
BeanInfo retVal = new BeanInfo(type, descriptor.getName(), bean, metadata);
beanMap.put(descriptor, retVal);
return retVal;
} finally {
lock.unlock();
}
}
/* package */ void removeBean(ActiveDescriptor<?> descriptor) {
lock.lock();
try {
beanMap.remove(descriptor);
} finally {
lock.unlock();
}
}
@Override
public String toString() {
return "ConfiguredByInjectionResolver(" + System.identityHashCode(this) + ")";
}
}