PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/messaging/src/main/java/org/jboss/as/messaging/BridgeAdd.java

https://github.com/tomathome/jboss-as
Java | 212 lines | 150 code | 29 blank | 33 comment | 30 complexity | b55921e492ddab11306f999ccf3d0460 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.messaging;
  23. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.ADD;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import java.util.Locale;
  27. import org.hornetq.api.core.client.HornetQClient;
  28. import org.hornetq.api.core.management.HornetQServerControl;
  29. import org.hornetq.core.config.BridgeConfiguration;
  30. import org.hornetq.core.config.Configuration;
  31. import org.hornetq.core.server.HornetQServer;
  32. import org.jboss.as.controller.AbstractAddStepHandler;
  33. import org.jboss.as.controller.AttributeDefinition;
  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.DescriptionProvider;
  39. import org.jboss.as.controller.descriptions.ModelDescriptionConstants;
  40. import org.jboss.dmr.ModelNode;
  41. import org.jboss.dmr.Property;
  42. import org.jboss.msc.service.ServiceController;
  43. import org.jboss.msc.service.ServiceRegistry;
  44. /**
  45. * Handler for adding a bridge.
  46. *
  47. * @author Brian Stansberry (c) 2011 Red Hat Inc.
  48. */
  49. public class BridgeAdd extends AbstractAddStepHandler implements DescriptionProvider {
  50. /**
  51. * Create an "add" operation using the existing model
  52. */
  53. public static ModelNode getAddOperation(final ModelNode address, ModelNode subModel) {
  54. final ModelNode operation = org.jboss.as.controller.operations.common.Util.getOperation(ADD, address, subModel);
  55. return operation;
  56. }
  57. public static final BridgeAdd INSTANCE = new BridgeAdd();
  58. private BridgeAdd() {}
  59. @Override
  60. protected void populateModel(ModelNode operation, ModelNode model) throws OperationFailedException {
  61. model.setEmptyObject();
  62. boolean hasStatic = operation.hasDefined(ConnectorRefsAttribute.BRIDGE_CONNECTORS.getName());
  63. boolean hasDiscGroup = operation.hasDefined(CommonAttributes.DISCOVERY_GROUP_NAME.getName());
  64. if (!hasStatic && !hasDiscGroup) {
  65. throw new OperationFailedException(new ModelNode().set(String.format("Operation must include parameter %s or parameter %s",
  66. ConnectorRefsAttribute.BRIDGE_CONNECTORS.getName(), CommonAttributes.DISCOVERY_GROUP_NAME.getName())));
  67. } else if (hasStatic && hasDiscGroup) {
  68. throw new OperationFailedException(new ModelNode().set(String.format("Operation cannot include both parameter %s and parameter %s",
  69. ConnectorRefsAttribute.BRIDGE_CONNECTORS.getName(), CommonAttributes.DISCOVERY_GROUP_NAME.getName())));
  70. }
  71. for (final AttributeDefinition attributeDefinition : CommonAttributes.BRIDGE_ATTRIBUTES) {
  72. if (hasDiscGroup && attributeDefinition == ConnectorRefsAttribute.BRIDGE_CONNECTORS) {
  73. continue;
  74. } else if (hasStatic && attributeDefinition == CommonAttributes.DISCOVERY_GROUP_NAME) {
  75. continue;
  76. }
  77. attributeDefinition.validateAndSet(operation, model);
  78. }
  79. }
  80. @Override
  81. protected void performRuntime(OperationContext context, ModelNode operation, ModelNode model, ServiceVerificationHandler verificationHandler, List<ServiceController<?>> newControllers) throws OperationFailedException {
  82. ServiceRegistry registry = context.getServiceRegistry(true);
  83. ServiceController<?> hqService = registry.getService(MessagingServices.JBOSS_MESSAGING);
  84. if (hqService != null) {
  85. // The original subsystem initialization is complete; use the control object to create the divert
  86. if (hqService.getState() != ServiceController.State.UP) {
  87. throw new IllegalStateException(String.format("Service %s is not in state %s, it is in state %s",
  88. MessagingServices.JBOSS_MESSAGING, ServiceController.State.UP, hqService.getState()));
  89. }
  90. final String name = PathAddress.pathAddress(operation.require(ModelDescriptionConstants.OP_ADDR)).getLastElement().getValue();
  91. BridgeConfiguration bridgeConfig = createBridgeConfiguration(name, model);
  92. HornetQServerControl serverControl = HornetQServer.class.cast(hqService.getValue()).getHornetQServerControl();
  93. createBridge(name, bridgeConfig, serverControl);
  94. }
  95. // else the initial subsystem install is not complete; MessagingSubsystemAdd will add a
  96. // handler that calls addBridgeConfigs
  97. }
  98. @Override
  99. public ModelNode getModelDescription(Locale locale) {
  100. return MessagingDescriptions.getBridgeAdd(locale);
  101. }
  102. static void addBridgeConfigs(final Configuration configuration, final ModelNode model) throws OperationFailedException {
  103. if (model.hasDefined(CommonAttributes.BRIDGE)) {
  104. final List<BridgeConfiguration> configs = configuration.getBridgeConfigurations();
  105. for (Property prop : model.get(CommonAttributes.BRIDGE).asPropertyList()) {
  106. configs.add(createBridgeConfiguration(prop.getName(), prop.getValue()));
  107. }
  108. }
  109. }
  110. static BridgeConfiguration createBridgeConfiguration(String name, ModelNode model) throws OperationFailedException {
  111. final String queueName = CommonAttributes.QUEUE_NAME.validateResolvedOperation(model).asString();
  112. final ModelNode forwardingNode = CommonAttributes.BRIDGE_FORWARDING_ADDRESS.validateResolvedOperation(model);
  113. final String forwardingAddress = forwardingNode.isDefined() ? forwardingNode.asString() : null;
  114. final ModelNode filterNode = CommonAttributes.FILTER.validateResolvedOperation(model);
  115. final String filterString = filterNode.isDefined() ? filterNode.asString() : null;
  116. final ModelNode transformerNode = CommonAttributes.TRANSFORMER_CLASS_NAME.validateResolvedOperation(model);
  117. final String transformerClassName = transformerNode.isDefined() ? transformerNode.asString() : null;
  118. final long retryInterval = CommonAttributes.RETRY_INTERVAL.validateResolvedOperation(model).asLong();
  119. final double retryIntervalMultiplier = CommonAttributes.RETRY_INTERVAL_MULTIPLIER.validateResolvedOperation(model).asDouble();
  120. final int reconnectAttempts = CommonAttributes.BRIDGE_RECONNECT_ATTEMPTS.validateResolvedOperation(model).asInt();
  121. final boolean useDuplicateDetection = CommonAttributes.BRIDGE_USE_DUPLICATE_DETECTION.validateResolvedOperation(model).asBoolean();
  122. final int confirmationWindowSize = CommonAttributes.CONFIRMATION_WINDOW_SIZE.validateResolvedOperation(model).asInt();
  123. final long clientFailureCheckPeriod = HornetQClient.DEFAULT_CLIENT_FAILURE_CHECK_PERIOD;
  124. final ModelNode discoveryNode = CommonAttributes.DISCOVERY_GROUP_NAME.validateResolvedOperation(model);
  125. final String discoveryGroupName = discoveryNode.isDefined() ? discoveryNode.asString() : null;
  126. List<String> staticConnectors = discoveryGroupName == null ? getStaticConnectors(model) : null;
  127. final boolean ha = CommonAttributes.HA.validateResolvedOperation(model).asBoolean();
  128. final String user = CommonAttributes.USER.validateResolvedOperation(model).asString();
  129. final String password = CommonAttributes.PASSWORD.validateResolvedOperation(model).asString();
  130. if (discoveryGroupName != null) {
  131. return new BridgeConfiguration(name, queueName, forwardingAddress, filterString, transformerClassName,
  132. retryInterval, retryIntervalMultiplier, reconnectAttempts, useDuplicateDetection,
  133. confirmationWindowSize, clientFailureCheckPeriod, discoveryGroupName, ha,
  134. user, password);
  135. } else {
  136. return new BridgeConfiguration(name, queueName, forwardingAddress, filterString, transformerClassName,
  137. retryInterval, retryIntervalMultiplier, reconnectAttempts, useDuplicateDetection,
  138. confirmationWindowSize, clientFailureCheckPeriod, staticConnectors, ha,
  139. user, password);
  140. }
  141. }
  142. private static List<String> getStaticConnectors(ModelNode model) {
  143. List<String> result = new ArrayList<String>();
  144. for (ModelNode connector : model.require(CommonAttributes.STATIC_CONNECTORS).asList()) {
  145. result.add(connector.asString());
  146. }
  147. return result;
  148. }
  149. static void createBridge(String name, BridgeConfiguration bridgeConfig, HornetQServerControl serverControl) {
  150. try {
  151. if (bridgeConfig.getDiscoveryGroupName() != null) {
  152. serverControl.createBridge(name, bridgeConfig.getQueueName(), bridgeConfig.getForwardingAddress(),
  153. bridgeConfig.getFilterString(), bridgeConfig.getTransformerClassName(), bridgeConfig.getRetryInterval(),
  154. bridgeConfig.getRetryIntervalMultiplier(), bridgeConfig.getReconnectAttempts(), bridgeConfig.isUseDuplicateDetection(),
  155. bridgeConfig.getConfirmationWindowSize(), HornetQClient.DEFAULT_CLIENT_FAILURE_CHECK_PERIOD,
  156. bridgeConfig.getDiscoveryGroupName(), true, bridgeConfig.isHA(), bridgeConfig.getUser(),
  157. bridgeConfig.getPassword());
  158. } else {
  159. boolean first = true;
  160. String connectors = "";
  161. for (String connector : bridgeConfig.getStaticConnectors()) {
  162. if (!first) {
  163. connectors += ",";
  164. } else {
  165. first = false;
  166. }
  167. connectors += connector;
  168. }
  169. serverControl.createBridge(name, bridgeConfig.getQueueName(), bridgeConfig.getForwardingAddress(),
  170. bridgeConfig.getFilterString(), bridgeConfig.getTransformerClassName(), bridgeConfig.getRetryInterval(),
  171. bridgeConfig.getRetryIntervalMultiplier(), bridgeConfig.getReconnectAttempts(), bridgeConfig.isUseDuplicateDetection(),
  172. bridgeConfig.getConfirmationWindowSize(), HornetQClient.DEFAULT_CLIENT_FAILURE_CHECK_PERIOD,
  173. connectors, false, bridgeConfig.isHA(), bridgeConfig.getUser(),
  174. bridgeConfig.getPassword());
  175. }
  176. } catch (RuntimeException e) {
  177. throw e;
  178. } catch (Exception e) {
  179. // TODO should this be an OFE instead?
  180. throw new RuntimeException(e);
  181. }
  182. }
  183. }