/
JChannelFactoryServiceConfigurator.java
158 lines (137 loc) · 7.04 KB
/
JChannelFactoryServiceConfigurator.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
/*
* 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.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Supplier;
import org.jboss.as.clustering.controller.CapabilityServiceNameProvider;
import org.jboss.as.clustering.controller.CommonRequirement;
import org.jboss.as.clustering.controller.ResourceServiceConfigurator;
import org.jboss.as.clustering.jgroups.JChannelFactory;
import org.jboss.as.clustering.jgroups.logging.JGroupsLogger;
import org.jboss.as.controller.OperationContext;
import org.jboss.as.controller.OperationFailedException;
import org.jboss.as.controller.PathAddress;
import org.jboss.as.controller.registry.Resource;
import org.jboss.as.network.SocketBindingManager;
import org.jboss.as.server.ServerEnvironment;
import org.jboss.as.server.ServerEnvironmentService;
import org.jboss.dmr.ModelNode;
import org.jboss.msc.Service;
import org.jboss.msc.service.ServiceBuilder;
import org.jboss.msc.service.ServiceController;
import org.jboss.msc.service.ServiceTarget;
import org.jgroups.protocols.TP;
import org.jgroups.stack.Protocol;
import org.wildfly.clustering.jgroups.spi.ChannelFactory;
import org.wildfly.clustering.jgroups.spi.ProtocolConfiguration;
import org.wildfly.clustering.jgroups.spi.ProtocolStackConfiguration;
import org.wildfly.clustering.jgroups.spi.RelayConfiguration;
import org.wildfly.clustering.jgroups.spi.TransportConfiguration;
import org.wildfly.clustering.service.CompositeDependency;
import org.wildfly.clustering.service.Dependency;
import org.wildfly.clustering.service.ServiceConfigurator;
import org.wildfly.clustering.service.ServiceSupplierDependency;
import org.wildfly.clustering.service.SupplierDependency;
/**
* Builder for a service that provides a {@link ChannelFactory} for creating channels.
* @author Paul Ferraro
*/
public class JChannelFactoryServiceConfigurator extends CapabilityServiceNameProvider implements ResourceServiceConfigurator, ProtocolStackConfiguration {
private final PathAddress address;
private volatile boolean statisticsEnabled;
private volatile SupplierDependency<TransportConfiguration<? extends TP>> transport = null;
private volatile List<SupplierDependency<ProtocolConfiguration<? extends Protocol>>> protocols = null;
private volatile SupplierDependency<RelayConfiguration> relay = null;
private volatile SupplierDependency<SocketBindingManager> socketBindingManager;
private volatile Supplier<ServerEnvironment> environment;
public JChannelFactoryServiceConfigurator(PathAddress address) {
super(StackResourceDefinition.Capability.JCHANNEL_FACTORY, address);
this.address = address;
}
@Override
public ServiceBuilder<?> build(ServiceTarget target) {
ServiceBuilder<?> builder = target.addService(this.getServiceName());
Consumer<ChannelFactory> factory = new CompositeDependency(this.transport, this.relay, this.socketBindingManager).register(builder).provides(this.getServiceName());
this.environment = builder.requires(ServerEnvironmentService.SERVICE_NAME);
for (Dependency dependency : this.protocols) {
dependency.register(builder);
}
Service service = Service.newInstance(factory, new JChannelFactory(this));
return builder.setInstance(service).setInitialMode(ServiceController.Mode.ON_DEMAND);
}
@Override
public ServiceConfigurator configure(OperationContext context, ModelNode model) throws OperationFailedException {
this.statisticsEnabled = StackResourceDefinition.Attribute.STATISTICS_ENABLED.resolveModelAttribute(context, model).asBoolean();
Resource resource = context.readResourceFromRoot(this.address, false);
Iterator<Resource.ResourceEntry> transports = resource.getChildren(TransportResourceDefinition.WILDCARD_PATH.getKey()).iterator();
if (!transports.hasNext()) {
throw JGroupsLogger.ROOT_LOGGER.transportNotDefined(this.getName());
}
this.transport = new ServiceSupplierDependency<>(new SingletonProtocolServiceNameProvider(this.address, transports.next().getPathElement()));
Set<Resource.ResourceEntry> entries = resource.getChildren(ProtocolResourceDefinition.WILDCARD_PATH.getKey());
this.protocols = new ArrayList<>(entries.size());
for (Resource.ResourceEntry entry : entries) {
this.protocols.add(new ServiceSupplierDependency<>(new ProtocolServiceNameProvider(this.address, entry.getPathElement())));
}
this.relay = resource.hasChild(RelayResourceDefinition.PATH) ? new ServiceSupplierDependency<>(new SingletonProtocolServiceNameProvider(this.address, RelayResourceDefinition.PATH)) : null;
this.socketBindingManager = new ServiceSupplierDependency<>(CommonRequirement.SOCKET_BINDING_MANAGER.getServiceName(context));
return this;
}
@Override
public String getName() {
return this.address.getLastElement().getValue();
}
@Override
public TransportConfiguration<? extends TP> getTransport() {
return this.transport.get();
}
@Override
public List<ProtocolConfiguration<? extends Protocol>> getProtocols() {
List<ProtocolConfiguration<? extends Protocol>> protocols = new ArrayList<>(this.protocols.size());
for (Supplier<ProtocolConfiguration<? extends Protocol>> protocolValue : this.protocols) {
ProtocolConfiguration<? extends Protocol> protocol = protocolValue.get();
protocols.add(protocol);
}
return protocols;
}
@Override
public String getNodeName() {
return this.environment.get().getNodeName();
}
@Override
public Optional<RelayConfiguration> getRelay() {
return (this.relay != null) ? Optional.of(this.relay.get()) : Optional.empty();
}
@Override
public boolean isStatisticsEnabled() {
return this.statisticsEnabled;
}
@Override
public SocketBindingManager getSocketBindingManager() {
return this.socketBindingManager.get();
}
}