/
CapabilityServiceSupport.java
235 lines (210 loc) · 11.9 KB
/
CapabilityServiceSupport.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
/*
* Copyright The WildFly Authors
* SPDX-License-Identifier: Apache-2.0
*/
package org.jboss.as.controller.capability;
import java.util.Map;
import java.util.Optional;
import org.jboss.msc.service.ServiceName;
import org.wildfly.service.descriptor.BinaryServiceDescriptor;
import org.wildfly.service.descriptor.NullaryServiceDescriptor;
import org.wildfly.service.descriptor.QuaternaryServiceDescriptor;
import org.wildfly.service.descriptor.TernaryServiceDescriptor;
import org.wildfly.service.descriptor.UnaryServiceDescriptor;
/**
* Provides support for capability integration outside the management layer,
* in service implementations.
* <p>
* Note that use of this interface in no way creates a requirement on the
* referenced capability by the caller.
*
* @author Brian Stansberry
*/
public interface CapabilityServiceSupport extends CapabilityServiceDescriptorResolver {
/**
* Exception thrown when support for an unregistered capability is requested. This is a checked
* exception because {@code CapabilityServiceSupport} is used outside the management layer and
* the requirements for capability availability available in {@link org.jboss.as.controller.OperationContext}
* are not possible. So callers need to be aware of and handle non-existent capabilities.
*/
class NoSuchCapabilityException extends Exception {
static final long serialVersionUID = 1L;
public NoSuchCapabilityException(String message) {
super(message);
}
}
/**
* Gets whether a runtime capability with the given name is registered.
*
* @param capabilityName the name of the capability. Cannot be {@code null}
* @return {@code true} if there is a capability with the given name registered
*/
boolean hasCapability(String capabilityName);
/**
* Indicates whether a runtime capability with the given name and segments is registered.
*
* @param capabilityName the name of the capability. Cannot be {@code null}
* @parem segments the dynamic name segments of the capability. Cannot be {@code null}
* @return {@code true} if there is a capability with the given name registered
*/
default boolean hasCapability(String capabilityName, String... segments) {
return this.hasCapability(RuntimeCapability.buildDynamicCapabilityName(capabilityName, segments));
}
/**
* Indicates whether or not a runtime capability with the given descriptor is registered.
*
* @param descriptor the service descriptor of the requested capability
* @return {@code true} if there is a capability with the resolved name registered
*/
default boolean hasCapability(NullaryServiceDescriptor<?> descriptor) {
return this.hasCapability(descriptor.getName());
}
/**
* Indicates whether or not a runtime capability with the given descriptor and segment is registered.
*
* @param descriptor the service descriptor of the requested capability
* @param name the dynamic name segment of the requested capability.
* @return {@code true} if there is a capability with the resolved name registered
*/
default boolean hasCapability(UnaryServiceDescriptor<?> descriptor, String name) {
Map.Entry<String, String[]> segments = descriptor.resolve(name);
return this.hasCapability(segments.getKey(), segments.getValue());
}
/**
* Indicates whether or not a runtime capability with the given descriptor and segments is registered.
*
* @param descriptor the service descriptor of the requested capability
* @param parent the first dynamic name segment of the requested capability.
* @param child the second dynamic name segment of the requested capability.
* @return {@code true} if there is a capability with the resolved name registered
*/
default boolean hasCapability(BinaryServiceDescriptor<?> descriptor, String parent, String child) {
Map.Entry<String, String[]> segments = descriptor.resolve(parent, child);
return this.hasCapability(segments.getKey(), segments.getValue());
}
/**
* Indicates whether or not a runtime capability with the given descriptor and segments is registered.
*
* @param descriptor the service descriptor of the requested capability
* @param grandparent the first dynamic name segment of the requested capability.
* @param parent the second dynamic name segment of the requested capability.
* @param child the third dynamic name segment of the requested capability.
* @return {@code true} if there is a capability with the resolved name registered
*/
default boolean hasCapability(TernaryServiceDescriptor<?> descriptor, String grandparent, String parent, String child) {
Map.Entry<String, String[]> segments = descriptor.resolve(grandparent, parent, child);
return this.hasCapability(segments.getKey(), segments.getValue());
}
/**
* Indicates whether or not a runtime capability with the given descriptor and segments is registered.
*
* @param descriptor the service descriptor of the requested capability
* @param greatGrandparent the first dynamic name segment of the requested capability.
* @param grandparent the second dynamic name segment of the requested capability.
* @param parent the third dynamic name segment of the requested capability.
* @param child the fourth dynamic name segment of the requested capability.
* @return {@code true} if there is a capability with the resolved name registered
*/
default boolean hasCapability(QuaternaryServiceDescriptor<?> descriptor, String greatGrandparent, String grandparent, String parent, String child) {
Map.Entry<String, String[]> segments = descriptor.resolve(greatGrandparent, grandparent, parent, child);
return this.hasCapability(segments.getKey(), segments.getValue());
}
/**
* Gets the runtime API associated with a given capability, if there is one.
* @param capabilityName the name of the capability. Cannot be {@code null}
* @param apiType class of the java type that exposes the API. Cannot be {@code null}
* @param <T> the java type that exposes the API
* @return the runtime API. Will not return {@code null}
*
* @throws NoSuchCapabilityException if no matching capability is registered
* @throws java.lang.IllegalArgumentException if the capability does not provide a runtime API
* @throws java.lang.ClassCastException if the runtime API exposed by the capability cannot be cast to type {code T}
*/
<T> T getCapabilityRuntimeAPI(String capabilityName, Class<T> apiType) throws NoSuchCapabilityException;
/**
* Gets the runtime API associated with a given {@link RuntimeCapability#isDynamicallyNamed() dynamically named}
* capability, if there is one.
*
* @param capabilityBaseName the base name of the capability. Cannot be {@code null}
* @param dynamicPart the dynamic part of the capability name. Cannot be {@code null}
* @param apiType class of the java type that exposes the API. Cannot be {@code null}
* @param <T> the java type that exposes the API
* @return the runtime API. Will not return {@code null}
*
* @throws NoSuchCapabilityException if no matching capability is registered
* @throws java.lang.IllegalArgumentException if the capability does not provide a runtime API
* @throws java.lang.ClassCastException if the runtime API exposed by the capability cannot be cast to type {code T}
*/
<T> T getCapabilityRuntimeAPI(String capabilityBaseName, String dynamicPart, Class<T> apiType) throws NoSuchCapabilityException;
/**
* Gets the runtime API associated with a given capability, if there is one.
*
* @param capabilityName the name of the capability. Cannot be {@code null}
* @param apiType class of the java type that exposes the API. Cannot be {@code null}
* @param <T> the java type that exposes the API
*
* @return an Optional describing the value of the runtime API. If no matching capability is registered, the optional
* will be empty.
*
* @throws java.lang.IllegalArgumentException if the capability does not provide a runtime API
* @throws java.lang.ClassCastException if the runtime API exposed by the capability cannot be cast to type {code T}
*/
<T> Optional<T> getOptionalCapabilityRuntimeAPI(String capabilityName, Class<T> apiType);
/**
* Gets the runtime API associated with a given {@link RuntimeCapability#isDynamicallyNamed() dynamically named}
* capability, if there is one.
*
* @param capabilityBaseName the base name of the capability. Cannot be {@code null}
* @param dynamicPart the dynamic part of the capability name. Cannot be {@code null}
* @param apiType class of the java type that exposes the API. Cannot be {@code null}
* @param <T> the java type that exposes the API
*
* @return an Optional describing the value of the runtime API. If no matching capability is registered, the optional
* will be empty.
*
* @throws java.lang.IllegalArgumentException if the capability does not provide a runtime API
* @throws java.lang.ClassCastException if the runtime API exposed by the capability cannot be cast to type {code T}
*/
<T> Optional<T> getOptionalCapabilityRuntimeAPI(String capabilityBaseName, String dynamicPart, Class<T> apiType);
/**
* Gets the name of a service associated with a given capability. This method does not confirm that the
* capability is currently registered.
*
* @param capabilityName the name of the capability. Cannot be {@code null}
* @return the name of the service. Will not return {@code null}
*/
ServiceName getCapabilityServiceName(String capabilityName);
/**
* Gets the name of a service associated with a given {@link RuntimeCapability#isDynamicallyNamed() dynamically named}
* capability. This method does not confirm that the capability is currently registered.
*
* @param capabilityBaseName the base name of the capability. Cannot be {@code null}
* @param dynamicParts the dynamic parts of the capability name. Cannot be {@code null} Can be multiple if capability supports that
* @return the name of the service. Will not return {@code null}
*/
ServiceName getCapabilityServiceName(String capabilityBaseName, String ... dynamicParts);
@Override
default <T> ServiceName getCapabilityServiceName(NullaryServiceDescriptor<T> descriptor) {
return this.getCapabilityServiceName(descriptor.getName());
}
@Override
default <T> ServiceName getCapabilityServiceName(UnaryServiceDescriptor<T> descriptor, String name) {
Map.Entry<String, String[]> resolved = descriptor.resolve(name);
return this.getCapabilityServiceName(resolved.getKey(), resolved.getValue());
}
@Override
default <T> ServiceName getCapabilityServiceName(BinaryServiceDescriptor<T> descriptor, String parent, String child) {
Map.Entry<String, String[]> resolved = descriptor.resolve(parent, child);
return this.getCapabilityServiceName(resolved.getKey(), resolved.getValue());
}
@Override
default <T> ServiceName getCapabilityServiceName(TernaryServiceDescriptor<T> descriptor, String grandparent, String parent, String child) {
Map.Entry<String, String[]> resolved = descriptor.resolve(grandparent, parent, child);
return this.getCapabilityServiceName(resolved.getKey(), resolved.getValue());
}
@Override
default <T> ServiceName getCapabilityServiceName(QuaternaryServiceDescriptor<T> descriptor, String greatGrandparent, String grandparent, String parent, String child) {
Map.Entry<String, String[]> resolved = descriptor.resolve(greatGrandparent, grandparent, parent, child);
return this.getCapabilityServiceName(resolved.getKey(), resolved.getValue());
}
}