-
Notifications
You must be signed in to change notification settings - Fork 53
/
CrossOverDescriptor.java
executable file
·168 lines (133 loc) · 4.55 KB
/
CrossOverDescriptor.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
/*
* Copyright (c) 2015, 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.extras.hk2bridge.internal;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Set;
import java.util.concurrent.locks.ReentrantLock;
import org.glassfish.hk2.api.ActiveDescriptor;
import org.glassfish.hk2.api.Injectee;
import org.glassfish.hk2.api.ServiceHandle;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.hk2.extras.ExtrasUtilities;
import org.glassfish.hk2.utilities.AbstractActiveDescriptor;
/**
* @author jwells
*
*/
public class CrossOverDescriptor<T> extends AbstractActiveDescriptor<T> {
private final ReentrantLock lock = new ReentrantLock();
private final ServiceLocator remoteLocator;
private final ActiveDescriptor<T> remote;
private boolean remoteReified;
public CrossOverDescriptor(ServiceLocator local, ActiveDescriptor<T> localService) {
super(localService);
this.remoteLocator = local;
this.remote = localService;
remoteReified = localService.isReified();
if (remoteReified) {
setScopeAsAnnotation(remote.getScopeAsAnnotation());
}
else {
setScope(remote.getScope());
}
addMetadata(ExtrasUtilities.HK2BRIDGE_LOCATOR_ID, Long.toString(local.getLocatorId()));
addMetadata(ExtrasUtilities.HK2BRIDGE_SERVICE_ID, Long.toString(localService.getServiceId()));
}
/**
* This method returns true if this descriptor has been reified
* (class loaded). If this method returns false then the other methods
* in this interface will throw an IllegalStateException. Once this
* method returns true it may be
*
* @return true if this descriptor has been reified, false otherwise
*/
public boolean isReified() {
return true;
}
private void checkState() {
lock.lock();
try {
if (remoteReified) return;
remoteReified = true;
if (remote.isReified()) return;
remoteLocator.reifyDescriptor(remote);
} finally {
lock.unlock();
}
}
@Override
public Class<?> getImplementationClass() {
checkState();
return remote.getImplementationClass();
}
@Override
public Type getImplementationType() {
checkState();
return remote.getImplementationType();
}
@Override
public void setImplementationType(Type t) {
checkState();
throw new AssertionError("Can not set type of remove descriptor");
}
@Override
public Set<Type> getContractTypes() {
checkState();
return remote.getContractTypes();
}
@Override
public Annotation getScopeAsAnnotation() {
checkState();
return remote.getScopeAsAnnotation();
}
@Override
public Class<? extends Annotation> getScopeAnnotation() {
checkState();
return remote.getScopeAnnotation();
}
@Override
public Set<Annotation> getQualifierAnnotations() {
checkState();
return remote.getQualifierAnnotations();
}
@Override
public List<Injectee> getInjectees() {
checkState();
return remote.getInjectees();
}
@Override
public Long getFactoryServiceId() {
checkState();
return remote.getFactoryServiceId();
}
@Override
public Long getFactoryLocatorId() {
checkState();
return remote.getFactoryLocatorId();
}
@Override
public T create(ServiceHandle<?> root) {
checkState();
return remoteLocator.getService(remote, root);
}
@Override
public void dispose(T instance) {
checkState();
remote.dispose(instance);
}
}