PageRenderTime 70ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/server/src/main/java/org/jboss/as/server/services/net/RemoteDestinationOutboundSocketBindingAddHandler.java

https://github.com/smcgowan/wildfly
Java | 145 lines | 90 code | 16 blank | 39 comment | 8 complexity | 04e6f0e2e6027be31caf0b2dfd15386b MD5 | raw file
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2011, Red Hat, Inc., and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.as.server.services.net;
  23. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.ADD;
  24. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.FIXED_SOURCE_PORT;
  25. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.HOST;
  26. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP;
  27. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP_ADDR;
  28. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.PORT;
  29. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SOURCE_INTERFACE;
  30. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SOURCE_PORT;
  31. import java.net.UnknownHostException;
  32. import java.util.List;
  33. import org.jboss.as.controller.AbstractAddStepHandler;
  34. import org.jboss.as.controller.OperationContext;
  35. import org.jboss.as.controller.OperationFailedException;
  36. import org.jboss.as.controller.PathAddress;
  37. import org.jboss.as.controller.ServiceVerificationHandler;
  38. import org.jboss.as.controller.descriptions.ModelDescriptionConstants;
  39. import org.jboss.as.network.NetworkInterfaceBinding;
  40. import org.jboss.as.network.OutboundSocketBinding;
  41. import org.jboss.as.network.SocketBindingManager;
  42. import org.jboss.dmr.ModelNode;
  43. import org.jboss.msc.service.ServiceBuilder;
  44. import org.jboss.msc.service.ServiceController;
  45. import org.jboss.msc.service.ServiceTarget;
  46. /**
  47. * Handles "add" operation for remote-destination outbound-socket-binding
  48. *
  49. * @author Jaikiran Pai
  50. */
  51. public class RemoteDestinationOutboundSocketBindingAddHandler extends AbstractAddStepHandler {
  52. static final RemoteDestinationOutboundSocketBindingAddHandler INSTANCE = new RemoteDestinationOutboundSocketBindingAddHandler();
  53. public static ModelNode getOperation(final ModelNode address, final ModelNode remoteDestinationOutboundSocketBinding) {
  54. final ModelNode addOperation = new ModelNode();
  55. addOperation.get(OP).set(ADD);
  56. addOperation.get(OP_ADDR).set(address);
  57. // destination host
  58. addOperation.get(HOST).set(remoteDestinationOutboundSocketBinding.get(HOST));
  59. // destination port
  60. addOperation.get(PORT).set(remoteDestinationOutboundSocketBinding.get(PORT));
  61. // (optional) source interface
  62. if (remoteDestinationOutboundSocketBinding.get(SOURCE_INTERFACE).isDefined()) {
  63. addOperation.get(SOURCE_INTERFACE).set(remoteDestinationOutboundSocketBinding.get(SOURCE_INTERFACE));
  64. }
  65. // (optional) source port
  66. if (remoteDestinationOutboundSocketBinding.get(SOURCE_PORT).isDefined()) {
  67. addOperation.get(SOURCE_PORT).set(remoteDestinationOutboundSocketBinding.get(SOURCE_PORT));
  68. }
  69. if (remoteDestinationOutboundSocketBinding.get(FIXED_SOURCE_PORT).isDefined()) {
  70. addOperation.get(FIXED_SOURCE_PORT).set(remoteDestinationOutboundSocketBinding.get(FIXED_SOURCE_PORT));
  71. }
  72. return addOperation;
  73. }
  74. @Override
  75. protected void populateModel(final ModelNode operation, final ModelNode model) throws OperationFailedException {
  76. model.get(ModelDescriptionConstants.HOST).set(operation.get(ModelDescriptionConstants.HOST));
  77. model.get(ModelDescriptionConstants.PORT).set(operation.get(ModelDescriptionConstants.PORT));
  78. if (operation.hasDefined(ModelDescriptionConstants.SOURCE_INTERFACE)) {
  79. model.get(ModelDescriptionConstants.SOURCE_INTERFACE).set(operation.get(ModelDescriptionConstants.SOURCE_INTERFACE));
  80. }
  81. if (operation.hasDefined(ModelDescriptionConstants.SOURCE_PORT)) {
  82. model.get(ModelDescriptionConstants.SOURCE_PORT).set(operation.get(ModelDescriptionConstants.SOURCE_PORT));
  83. }
  84. if (operation.hasDefined(FIXED_SOURCE_PORT)) {
  85. model.get(ModelDescriptionConstants.FIXED_SOURCE_PORT).set(operation.get(FIXED_SOURCE_PORT));
  86. }
  87. }
  88. @Override
  89. protected void performRuntime(final OperationContext context, final ModelNode operation, final ModelNode model,
  90. final ServiceVerificationHandler verificationHandler, final List<ServiceController<?>> serviceControllers) throws OperationFailedException {
  91. final PathAddress address = PathAddress.pathAddress(operation.get(OP_ADDR));
  92. final String outboundSocketName = address.getLastElement().getValue();
  93. final ServiceController<OutboundSocketBinding> outboundSocketBindingServiceController;
  94. try {
  95. outboundSocketBindingServiceController = this.installOutboundSocketBindingService(context, model, outboundSocketName);
  96. } catch (UnknownHostException e) {
  97. throw new RuntimeException(e);
  98. }
  99. serviceControllers.add(outboundSocketBindingServiceController);
  100. }
  101. public static ServiceController<OutboundSocketBinding> installOutboundSocketBindingService(final OperationContext context, final ModelNode model,
  102. final String outboundSocketName) throws OperationFailedException, UnknownHostException {
  103. final ServiceTarget serviceTarget = context.getServiceTarget();
  104. // destination host
  105. final String destinationHost = RemoteDestinationOutboundSocketBindingResourceDefinition.HOST.validateResolvedOperation(model).asString();
  106. // port
  107. final int destinationPort = RemoteDestinationOutboundSocketBindingResourceDefinition.PORT.validateResolvedOperation(model).asInt();
  108. // (optional) source interface
  109. final ModelNode sourceInterfaceModelNode = OutboundSocketBindingResourceDefinition.SOURCE_INTERFACE.validateResolvedOperation(model);
  110. final String sourceInterfaceName = sourceInterfaceModelNode.isDefined() ? sourceInterfaceModelNode.asString() : null;
  111. // (optional) source port
  112. final ModelNode sourcePortModelNode = OutboundSocketBindingResourceDefinition.SOURCE_PORT.validateResolvedOperation(model);
  113. final Integer sourcePort = sourcePortModelNode.isDefined() ? sourcePortModelNode.asInt() : null;
  114. // (optional) fixedSourcePort
  115. final ModelNode fixedSourcePortModelNode = OutboundSocketBindingResourceDefinition.FIXED_SOURCE_PORT.validateResolvedOperation(model);
  116. final boolean fixedSourcePort = fixedSourcePortModelNode.isDefined() ? fixedSourcePortModelNode.asBoolean() : false;
  117. // create the service
  118. final OutboundSocketBindingService outboundSocketBindingService = new RemoteDestinationOutboundSocketBindingService(outboundSocketName, destinationHost, destinationPort, sourcePort, fixedSourcePort);
  119. final ServiceBuilder<OutboundSocketBinding> serviceBuilder = serviceTarget.addService(OutboundSocketBinding.OUTBOUND_SOCKET_BINDING_BASE_SERVICE_NAME.append(outboundSocketName), outboundSocketBindingService);
  120. // if a source interface has been specified then add a dependency on it
  121. if (sourceInterfaceName != null) {
  122. serviceBuilder.addDependency(NetworkInterfaceService.JBOSS_NETWORK_INTERFACE.append(sourceInterfaceName), NetworkInterfaceBinding.class, outboundSocketBindingService.getSourceNetworkInterfaceBindingInjector());
  123. }
  124. // add a dependency on the socket binding manager
  125. serviceBuilder.addDependency(SocketBindingManager.SOCKET_BINDING_MANAGER, SocketBindingManager.class, outboundSocketBindingService.getSocketBindingManagerInjector());
  126. // install the service
  127. return serviceBuilder.setInitialMode(ServiceController.Mode.ON_DEMAND).install();
  128. }
  129. }