-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
ForkProtocolRuntimeResourceRegistration.java
141 lines (127 loc) · 7.13 KB
/
ForkProtocolRuntimeResourceRegistration.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
/*
* JBoss, Home of Professional Open Source.
* Copyright 2014, 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.jgroups.subsystem;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import org.jboss.as.clustering.controller.RuntimeResourceRegistration;
import org.jboss.as.clustering.jgroups.subsystem.ProtocolMetricsHandler.Attribute;
import org.jboss.as.clustering.jgroups.subsystem.ProtocolMetricsHandler.FieldType;
import org.jboss.as.controller.OperationContext;
import org.jboss.as.controller.OperationFailedException;
import org.jboss.as.controller.PathAddress;
import org.jboss.as.controller.SimpleAttributeDefinitionBuilder;
import org.jboss.as.controller.descriptions.ModelDescriptionConstants;
import org.jboss.as.controller.descriptions.OverrideDescriptionProvider;
import org.jboss.as.controller.registry.ManagementResourceRegistration;
import org.jboss.as.controller.registry.Resource;
import org.jboss.dmr.ModelNode;
import org.jboss.modules.ModuleLoadException;
import org.jboss.msc.service.ServiceController;
import org.jboss.msc.service.ServiceRegistry;
import org.jgroups.Channel;
import org.jgroups.protocols.FORK;
import org.jgroups.protocols.TP;
import org.jgroups.stack.Protocol;
import org.wildfly.clustering.jgroups.spi.ChannelFactory;
import org.wildfly.clustering.jgroups.spi.JGroupsRequirement;
import org.wildfly.clustering.jgroups.spi.ProtocolConfiguration;
import org.wildfly.clustering.jgroups.spi.ProtocolStackConfiguration;
/**
* Operation handler for registration of fork protocol runtime resources.
* @author Paul Ferraro
*/
public class ForkProtocolRuntimeResourceRegistration implements RuntimeResourceRegistration, ProtocolMetricsHandler.ProtocolLocator {
@Override
public Protocol findProtocol(OperationContext context) throws ClassNotFoundException, ModuleLoadException {
PathAddress address = context.getCurrentAddress();
String channelName = address.getElement(address.size() - 3).getValue();
String forkName = address.getElement(address.size() - 2).getValue();
String protocolName = address.getElement(address.size() - 1).getValue();
ServiceRegistry registry = context.getServiceRegistry(false);
ServiceController<?> controller = registry.getService(JGroupsRequirement.CHANNEL.getServiceName(context, channelName));
if (controller != null) {
Channel channel = (Channel) controller.getValue();
if (channel != null) {
FORK fork = (FORK) channel.getProtocolStack().findProtocol(FORK.class);
if (fork != null) {
controller = registry.getService(JGroupsRequirement.CHANNEL_FACTORY.getServiceName(context, channelName));
if (controller != null) {
ChannelFactory factory = (ChannelFactory) controller.getValue();
if (factory != null) {
ProtocolStackConfiguration configuration = factory.getProtocolStackConfiguration();
ProtocolConfiguration<? extends TP> transport = configuration.getTransport();
if (transport.getName().equals(protocolName)) {
Class<? extends Protocol> protocolClass = transport.createProtocol(configuration).getClass();
return channel.getProtocolStack().findProtocol(protocolClass);
}
for (ProtocolConfiguration<? extends Protocol> protocol : configuration.getProtocols()) {
if (protocol.getName().equals(protocolName)) {
Class<? extends Protocol> protocolClass = protocol.createProtocol(configuration).getClass();
return fork.get(forkName).getProtocolStack().findProtocol(protocolClass);
}
}
}
}
}
}
}
return null;
}
@Override
public void register(OperationContext context) throws OperationFailedException {
Resource resource = context.readResource(PathAddress.EMPTY_ADDRESS);
ManagementResourceRegistration registration = context.getResourceRegistrationForUpdate();
String protocolName = context.getCurrentAddressValue();
String moduleName = ProtocolResourceDefinition.Attribute.MODULE.resolveModelAttribute(context, resource.getModel()).asString();
Class<? extends Protocol> protocolClass = ChannelRuntimeResourceRegistration.findProtocolClass(context, protocolName, moduleName);
final Map<String, Attribute> attributes = ProtocolMetricsHandler.findProtocolAttributes(protocolClass);
OverrideDescriptionProvider provider = new OverrideDescriptionProvider() {
@Override
public Map<String, ModelNode> getAttributeOverrideDescriptions(Locale locale) {
Map<String, ModelNode> result = new HashMap<>();
for (Attribute attribute : attributes.values()) {
ModelNode value = new ModelNode();
value.get(ModelDescriptionConstants.DESCRIPTION).set(attribute.getDescription());
result.put(attribute.getName(), value);
}
return result;
}
@Override
public Map<String, ModelNode> getChildTypeOverrideDescriptions(Locale locale) {
return Collections.emptyMap();
}
};
ManagementResourceRegistration protocolRegistration = registration.registerOverrideModel(protocolName, provider);
ProtocolMetricsHandler handler = new ProtocolMetricsHandler(this);
for (Attribute attribute : attributes.values()) {
String name = attribute.getName();
FieldType type = FieldType.valueOf(attribute.getType());
protocolRegistration.registerMetric(new SimpleAttributeDefinitionBuilder(name, type.getModelType()).setStorageRuntime().build(), handler);
}
}
@Override
public void unregister(OperationContext context) {
context.getResourceRegistrationForUpdate().unregisterOverrideModel(context.getCurrentAddressValue());
}
}