-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
JGroupsTransportResourceDefinition.java
275 lines (249 loc) · 14.3 KB
/
JGroupsTransportResourceDefinition.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
/*
* JBoss, Home of Professional Open Source.
* Copyright 2012, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.as.clustering.infinispan.subsystem;
import java.util.Set;
import org.jboss.as.clustering.controller.DefaultableCapabilityReference;
import org.jboss.as.clustering.controller.ResourceDescriptor;
import org.jboss.as.clustering.controller.SimpleResourceRegistration;
import org.jboss.as.clustering.controller.ResourceServiceHandler;
import org.jboss.as.clustering.controller.SimpleAliasEntry;
import org.jboss.as.clustering.controller.transform.SimpleAttributeConverter;
import org.jboss.as.clustering.controller.transform.SimpleAttributeConverter.Converter;
import org.jboss.as.clustering.controller.transform.SimpleRejectAttributeChecker;
import org.jboss.as.clustering.controller.transform.SimpleRejectAttributeChecker.Rejecter;
import org.jboss.as.clustering.infinispan.InfinispanLogger;
import org.jboss.as.clustering.jgroups.subsystem.ChannelResourceDefinition;
import org.jboss.as.clustering.jgroups.subsystem.JGroupsSubsystemResourceDefinition;
import org.jboss.as.controller.AttributeDefinition;
import org.jboss.as.controller.CapabilityReferenceRecorder;
import org.jboss.as.controller.ModelVersion;
import org.jboss.as.controller.PathAddress;
import org.jboss.as.controller.PathElement;
import org.jboss.as.controller.SimpleAttributeDefinitionBuilder;
import org.jboss.as.controller.capability.RuntimeCapability;
import org.jboss.as.controller.client.helpers.MeasurementUnit;
import org.jboss.as.controller.registry.AttributeAccess;
import org.jboss.as.controller.registry.ManagementResourceRegistration;
import org.jboss.as.controller.registry.Resource.NoSuchResourceException;
import org.jboss.as.controller.transform.TransformationContext;
import org.jboss.as.controller.transform.description.ResourceTransformationDescriptionBuilder;
import org.jboss.dmr.ModelNode;
import org.jboss.dmr.ModelType;
import org.wildfly.clustering.jgroups.spi.JGroupsRequirement;
/**
* Resource description for the addressable resource and its alias
*
* /subsystem=infinispan/cache-container=X/transport=jgroups
*
* @author Richard Achmatowicz (c) 2011 Red Hat Inc.
*/
public class JGroupsTransportResourceDefinition extends TransportResourceDefinition {
static final PathElement LEGACY_PATH = pathElement("TRANSPORT");
static final PathElement PATH = pathElement("jgroups");
enum Capability implements org.jboss.as.clustering.controller.Capability {
TRANSPORT_CHANNEL_FACTORY("org.wildfly.clustering.infinispan.transport.channel-factory"),
;
private final RuntimeCapability<Void> definition;
Capability(String name) {
this.definition = RuntimeCapability.Builder.of(name, true).build();
}
@Override
public RuntimeCapability<Void> getDefinition() {
return this.definition;
}
@Override
public RuntimeCapability<?> resolve(PathAddress address) {
return this.definition.fromBaseCapability(address.getParent().getLastElement().getValue());
}
}
enum Attribute implements org.jboss.as.clustering.controller.Attribute {
CHANNEL("channel", ModelType.STRING, new DefaultableCapabilityReference(Capability.TRANSPORT_CHANNEL_FACTORY, JGroupsRequirement.CHANNEL_FACTORY)),
LOCK_TIMEOUT("lock-timeout", ModelType.LONG, new ModelNode(240000L)),
;
private final AttributeDefinition definition;
Attribute(String name, ModelType type, CapabilityReferenceRecorder reference) {
this.definition = createBuilder(name, type, null)
.setAllowExpression(false)
.setCapabilityReference(reference)
.build();
}
Attribute(String name, ModelType type, ModelNode defaultValue) {
this.definition = createBuilder(name, type, defaultValue).build();
}
@Override
public AttributeDefinition getDefinition() {
return this.definition;
}
}
@Deprecated
enum ExecutorAttribute implements org.jboss.as.clustering.controller.Attribute {
TRANSPORT("executor"),
;
private final AttributeDefinition definition;
ExecutorAttribute(String name) {
this.definition = createBuilder(name, ModelType.STRING, null).setAllowExpression(false).setDeprecated(InfinispanModel.VERSION_3_0_0.getVersion()).build();
}
@Override
public AttributeDefinition getDefinition() {
return this.definition;
}
}
@Deprecated
enum DeprecatedAttribute implements org.jboss.as.clustering.controller.Attribute {
CLUSTER("cluster", ModelType.STRING, null, InfinispanModel.VERSION_3_0_0),
STACK("stack", ModelType.STRING, null, InfinispanModel.VERSION_3_0_0),
;
private final AttributeDefinition definition;
DeprecatedAttribute(String name, ModelType type, ModelNode defaultValue, InfinispanModel deprecation) {
this.definition = createBuilder(name, type, defaultValue).setDeprecated(deprecation.getVersion()).build();
}
@Override
public AttributeDefinition getDefinition() {
return this.definition;
}
}
static SimpleAttributeDefinitionBuilder createBuilder(String name, ModelType type, ModelNode defaultValue) {
return new SimpleAttributeDefinitionBuilder(name, type)
.setAllowExpression(true)
.setRequired(false)
.setDefaultValue(defaultValue)
.setFlags(AttributeAccess.Flag.RESTART_RESOURCE_SERVICES)
.setMeasurementUnit((type == ModelType.LONG) ? MeasurementUnit.MILLISECONDS : null)
;
}
@SuppressWarnings("deprecation")
static void buildTransformation(ModelVersion version, ResourceTransformationDescriptionBuilder parent) {
ResourceTransformationDescriptionBuilder builder = InfinispanModel.VERSION_4_0_0.requiresTransformation(version) ? parent.addChildRedirection(PATH, LEGACY_PATH) : parent.addChildResource(PATH);
if (InfinispanModel.VERSION_3_0_0.requiresTransformation(version)) {
// We need to reject if we cannot determine the underlying stack via the jgroups subsystem
Rejecter stackRejecter = new Rejecter() {
@Override
public boolean reject(PathAddress address, String name, ModelNode value, ModelNode model, TransformationContext context) {
if (value.isDefined()) return false;
PathAddress rootAddress = address.subAddress(0, address.size() - 3);
PathAddress subsystemAddress = rootAddress.append(JGroupsSubsystemResourceDefinition.PATH);
ModelNode subsystemModel = context.readResourceFromRoot(subsystemAddress).getModel();
String channelName = null;
if (model.hasDefined(Attribute.CHANNEL.getName())) {
ModelNode channel = model.get(Attribute.CHANNEL.getName());
if (channel.getType() == ModelType.STRING) {
channelName = channel.asString();
}
} else if (subsystemModel.hasDefined(JGroupsSubsystemResourceDefinition.Attribute.DEFAULT_CHANNEL.getName())) {
ModelNode defaultChannel = subsystemModel.get(JGroupsSubsystemResourceDefinition.Attribute.DEFAULT_CHANNEL.getName());
if (defaultChannel.getType() == ModelType.STRING) {
channelName = defaultChannel.asString();
}
}
if (channelName == null) return true;
String stackName = null;
PathAddress channelAddress = subsystemAddress.append(ChannelResourceDefinition.pathElement(channelName));
try {
ModelNode channel = context.readResourceFromRoot(channelAddress).getModel();
if (channel.hasDefined(ChannelResourceDefinition.Attribute.STACK.getName())) {
ModelNode stack = channel.get(ChannelResourceDefinition.Attribute.STACK.getName());
if (stack.getType() == ModelType.STRING) {
stackName = stack.asString();
}
} else if (subsystemModel.hasDefined(JGroupsSubsystemResourceDefinition.Attribute.DEFAULT_STACK.getName())) {
ModelNode defaultStack = subsystemModel.get(JGroupsSubsystemResourceDefinition.Attribute.DEFAULT_STACK.getName());
if (defaultStack.getType() == ModelType.STRING) {
stackName = defaultStack.asString();
}
}
} catch (NoSuchResourceException e) {
// Ignore
}
return (stackName == null);
}
@Override
public String getRejectedMessage(Set<String> attributes) {
return InfinispanLogger.ROOT_LOGGER.indeterminiteStack();
}
};
// Lookup the stack via the jgroups channel resource, if necessary
Converter stackConverter = new Converter() {
@Override
public void convert(PathAddress address, String name, ModelNode value, ModelNode model, TransformationContext context) {
if (!value.isDefined()) {
PathAddress rootAddress = address.subAddress(0, address.size() - 3);
PathAddress subsystemAddress = rootAddress.append(JGroupsSubsystemResourceDefinition.PATH);
ModelNode subsystemModel = context.readResourceFromRoot(subsystemAddress).getModel();
String channelName = null;
if (model.hasDefined(Attribute.CHANNEL.getName())) {
ModelNode channel = model.get(Attribute.CHANNEL.getName());
if (channel.getType() == ModelType.STRING) {
channelName = channel.asString();
}
} else if (subsystemModel.hasDefined(JGroupsSubsystemResourceDefinition.Attribute.DEFAULT_CHANNEL.getName())) {
ModelNode defaultChannel = subsystemModel.get(JGroupsSubsystemResourceDefinition.Attribute.DEFAULT_CHANNEL.getName());
if (defaultChannel.getType() == ModelType.STRING) {
channelName = defaultChannel.asString();
}
}
if (channelName != null) {
PathAddress channelAddress = subsystemAddress.append(ChannelResourceDefinition.pathElement(channelName));
try {
ModelNode channel = context.readResourceFromRoot(channelAddress).getModel();
if (channel.hasDefined(ChannelResourceDefinition.Attribute.STACK.getName())) {
ModelNode stack = channel.get(ChannelResourceDefinition.Attribute.STACK.getName());
if (stack.getType() == ModelType.STRING) {
value.set(stack.asString());
}
} else if (subsystemModel.hasDefined(JGroupsSubsystemResourceDefinition.Attribute.DEFAULT_STACK.getName())) {
ModelNode defaultStack = subsystemModel.get(JGroupsSubsystemResourceDefinition.Attribute.DEFAULT_STACK.getName());
if (defaultStack.getType() == ModelType.STRING) {
value.set(defaultStack.asString());
}
}
} catch (NoSuchResourceException e) {
// Ignore
}
}
}
}
};
builder.getAttributeBuilder()
.addRejectCheck(new SimpleRejectAttributeChecker(stackRejecter), DeprecatedAttribute.STACK.getDefinition())
.setValueConverter(new SimpleAttributeConverter(stackConverter), DeprecatedAttribute.STACK.getDefinition())
.addRename(Attribute.CHANNEL.getDefinition(), DeprecatedAttribute.CLUSTER.getDefinition().getName())
.end();
}
}
JGroupsTransportResourceDefinition() {
super(PATH);
}
@Override
public void register(ManagementResourceRegistration parentRegistration) {
ManagementResourceRegistration registration = parentRegistration.registerSubModel(this);
parentRegistration.registerAlias(LEGACY_PATH, new SimpleAliasEntry(registration));
ResourceDescriptor descriptor = new ResourceDescriptor(this.getResourceDescriptionResolver())
.addAttributes(Attribute.class)
.addAttributes(ExecutorAttribute.class)
.addAttributes(DeprecatedAttribute.class)
.addCapabilities(Capability.class)
.addCapabilities(CLUSTERING_CAPABILITIES.values())
;
ResourceServiceHandler handler = new JGroupsTransportServiceHandler();
new SimpleResourceRegistration(descriptor, handler).register(registration);
}
}