-
Notifications
You must be signed in to change notification settings - Fork 53
/
AliasDescriptor.java
executable file
·384 lines (322 loc) · 11.4 KB
/
AliasDescriptor.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
* Copyright (c) 2012, 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.utilities;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.locks.ReentrantLock;
import jakarta.inject.Named;
import org.glassfish.hk2.api.ActiveDescriptor;
import org.glassfish.hk2.api.HK2Loader;
import org.glassfish.hk2.api.Injectee;
import org.glassfish.hk2.api.ServiceHandle;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.hk2.utilities.general.GeneralUtilities;
/**
* An active descriptor class that serves as an alias for another descriptor.
* This alias can have only one contract that may or may not also be
* advertised by the host descriptor. An AliasDescriptor
* can have a different name from the host descriptor as well
*
* @author tbeerbower
* @param <T> The cache type
*/
public class AliasDescriptor<T> extends AbstractActiveDescriptor<T> {
/**
* This will be put in all Alias descriptors. The value
* will be ALIAS_FREE_DESCRIPTOR if the descritpor being
* aliased does not have a locator and service id. If
* the descriptor being aliased does have a locator and
* service id the value will be <locatorId>.<serviceId>
*/
public final static String ALIAS_METADATA_MARKER = "__AliasOf";
/**
* This is the value the metadata field ALIAS_METADATA_MARKER will
* take if the descriptor being aliased does not have a locator id
* or a service id
*/
public final static String ALIAS_FREE_DESCRIPTOR = "FreeDescriptor";
/**
* For serialization
*/
private static final long serialVersionUID = 2609895430798803508L;
private final ReentrantLock lock = new ReentrantLock();
/**
* The service locator.
*/
private ServiceLocator locator;
/**
* The descriptor that this descriptor will alias.
*/
private ActiveDescriptor<T> descriptor;
/**
* The contract type of this descriptor.
*/
private String contract;
/**
* The set of annotations for this descriptor.
*/
private Set<Annotation> qualifiers;
/**
* The set of qualifier names for this descriptor.
*/
private Set<String> qualifierNames;
/**
* Indicates whether or not this descriptor has been initialized.
*/
private boolean initialized = false;
// ----- Constants ------------------------------------------------------
/**
* Empty set of contracts used to construct this descriptor.
*/
private static final Set<Type> EMPTY_CONTRACT_SET = new HashSet<Type>();
/**
* Empty set of annotations used to construct this descriptor.
*/
private static final Set<Annotation> EMPTY_ANNOTATION_SET = new HashSet<Annotation>();
// ----- Constructors ---------------------------------------------------
/**
* For serialization
*/
public AliasDescriptor() {
super();
}
/**
* Construct an AliasDescriptor.
*
* @param locator the service locator
* @param descriptor the descriptor to be aliased
* @param contract the contact
* @param name the name
*/
public AliasDescriptor(ServiceLocator locator,
ActiveDescriptor<T> descriptor,
String contract,
String name) {
// pass in an empty contract set, an empty annotation set and a null
// scope since we are not really reified and we don't want to reify
// the given descriptor yet
super(EMPTY_CONTRACT_SET,
null,
name,
EMPTY_ANNOTATION_SET,
descriptor.getDescriptorType(),
descriptor.getDescriptorVisibility(),
descriptor.getRanking(),
descriptor.isProxiable(),
descriptor.isProxyForSameScope(),
descriptor.getClassAnalysisName(),
descriptor.getMetadata());
this.locator = locator;
this.descriptor = descriptor;
this.contract = contract;
addAdvertisedContract(contract);
super.setScope(descriptor.getScope());
super.addMetadata(ALIAS_METADATA_MARKER, getAliasMetadataValue(descriptor));
}
private static String getAliasMetadataValue(ActiveDescriptor<?> descriptor) {
Long locatorId = descriptor.getLocatorId();
Long serviceId = descriptor.getServiceId();
if (locatorId == null || serviceId == null) return ALIAS_FREE_DESCRIPTOR;
return locatorId + "." + serviceId;
}
// ----- ActiveDescriptor -----------------------------------------------
/* (non-Javadoc)
* @see org.glassfish.hk2.api.ActiveDescriptor#getImplementationClass()
*/
@Override
public Class<?> getImplementationClass() {
ensureInitialized();
return descriptor.getImplementationClass();
}
@Override
public Type getImplementationType() {
ensureInitialized();
return descriptor.getImplementationType();
}
/* (non-Javadoc)
* @see org.glassfish.hk2.api.ActiveDescriptor#create(org.glassfish.hk2.api.ServiceHandle)
*/
@Override
public T create(ServiceHandle<?> root) {
ensureInitialized();
return locator.getServiceHandle(descriptor).getService();
}
// ----- AbstractActiveDescriptor overrides -----------------------------
/* (non-Javadoc)
* @see org.glassfish.hk2.api.ActiveDescriptor#isReified()
*/
@Override
public boolean isReified() {
// always return true to get past the addActiveDescriptor checks
// even though the underlying descriptor may not be reified yet
return true;
}
/* (non-Javadoc)
* @see org.glassfish.hk2.api.ActiveDescriptor#getImplementation()
*/
@Override
public String getImplementation() {
return descriptor.getImplementation();
}
/* (non-Javadoc)
* @see org.glassfish.hk2.api.ActiveDescriptor#getContractTypes()
*/
@Override
public Set<Type> getContractTypes() {
ensureInitialized();
return super.getContractTypes();
}
/* (non-Javadoc)
* @see org.glassfish.hk2.api.ActiveDescriptor#getScopeAnnotation()
*/
@Override
public Class<? extends Annotation> getScopeAnnotation() {
ensureInitialized();
return descriptor.getScopeAnnotation();
}
/* (non-Javadoc)
* @see org.glassfish.hk2.api.ActiveDescriptor#getQualifierAnnotations()
*/
@Override
public Set<Annotation> getQualifierAnnotations() {
lock.lock();
try {
ensureInitialized();
if (qualifiers == null) {
qualifiers = new HashSet<Annotation>(descriptor.getQualifierAnnotations());
if (getName() != null) {
qualifiers.add(new NamedImpl(getName()));
}
}
return qualifiers;
} finally {
lock.unlock();
}
}
@Override
public Set<String> getQualifiers() {
lock.lock();
try {
if (qualifierNames != null) return qualifierNames;
qualifierNames = new HashSet<String>(descriptor.getQualifiers());
if (getName() != null) {
qualifierNames.add(Named.class.getName());
}
return qualifierNames;
} finally {
lock.unlock();
}
}
/* (non-Javadoc)
* @see org.glassfish.hk2.api.ActiveDescriptor#getInjectees()
*/
@Override
public List<Injectee> getInjectees() {
ensureInitialized();
return descriptor.getInjectees();
}
/* (non-Javadoc)
* @see org.glassfish.hk2.api.ActiveDescriptor#dispose(java.lang.Object, org.glassfish.hk2.api.ServiceHandle)
*/
@Override
public void dispose(T instance) {
ensureInitialized();
descriptor.dispose(instance);
}
// ----- accessors ------------------------------------------------
/**
* Get the descriptor being aliased.
*
* @return the descriptor
*/
public ActiveDescriptor<T> getDescriptor() {
return descriptor;
}
// ----- Utility methods ------------------------------------------------
/**
* Ensure that this descriptor has been initialized.
*/
@SuppressWarnings("unchecked")
private void ensureInitialized() {
lock.lock();
try {
if (!initialized) {
// reify the underlying descriptor if needed
if (!descriptor.isReified()) {
descriptor = (ActiveDescriptor<T>) locator.reifyDescriptor(descriptor);
}
if (contract == null) {
initialized = true;
return;
}
HK2Loader loader = descriptor.getLoader();
Type contractType = null;
try {
if (loader != null) {
contractType = loader.loadClass(contract);
}
else {
Class<?> ic = descriptor.getImplementationClass();
ClassLoader cl = null;
if (ic != null) {
cl = ic.getClassLoader();
}
if (cl == null) {
cl = ClassLoader.getSystemClassLoader();
}
contractType = cl.loadClass(contract);
}
}
catch (ClassNotFoundException e) {
// do nothing
}
super.addContractType(contractType);
initialized = true;
}
} finally {
lock.unlock();
}
}
@Override
public int hashCode() {
int retVal;
lock.lock();
try {
retVal = descriptor.hashCode();
} finally {
lock.unlock();
}
if (getName() != null) {
retVal ^= getName().hashCode();
}
if (contract != null) {
retVal ^= contract.hashCode();
}
return retVal;
}
@Override
public boolean equals(Object o) {
if (o == null) return false;
if (!(o instanceof AliasDescriptor)) return false;
AliasDescriptor<?> other = (AliasDescriptor<?>) o;
if (!other.descriptor.equals(descriptor)) return false;
if (!GeneralUtilities.safeEquals(other.getName(), getName())) return false;
return GeneralUtilities.safeEquals(other.contract, contract);
}
}