PageRenderTime 57ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/chorus-remoting/src/main/java/org/chorusbdd/chorus/remoting/jmx/remotingmanager/JmxRemotingManager.java

http://github.com/Chorus-bdd/Chorus
Java | 166 lines | 110 code | 24 blank | 32 comment | 10 complexity | 52043de048f4db105996b8821a1ef794 MD5 | raw file
Possible License(s): MIT
  1. /**
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 Chorus BDD Organisation.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. package org.chorusbdd.chorus.remoting.jmx.remotingmanager;
  25. import org.chorusbdd.chorus.annotations.ExecutionPriority;
  26. import org.chorusbdd.chorus.annotations.Scope;
  27. import org.chorusbdd.chorus.executionlistener.ExecutionListener;
  28. import org.chorusbdd.chorus.executionlistener.ExecutionListenerAdapter;
  29. import org.chorusbdd.chorus.handlerconfig.ConfigurableManager;
  30. import org.chorusbdd.chorus.logging.ChorusLog;
  31. import org.chorusbdd.chorus.logging.ChorusLogFactory;
  32. import org.chorusbdd.chorus.remoting.jmx.serialization.JmxInvokerResult;
  33. import org.chorusbdd.chorus.remoting.manager.RemotingConfigBean;
  34. import org.chorusbdd.chorus.remoting.manager.RemotingManager;
  35. import org.chorusbdd.chorus.remoting.manager.RemotingManagerConfig;
  36. import org.chorusbdd.chorus.results.ExecutionToken;
  37. import org.chorusbdd.chorus.results.FeatureToken;
  38. import org.chorusbdd.chorus.results.ScenarioToken;
  39. import org.chorusbdd.chorus.stepinvoker.StepInvoker;
  40. import java.util.*;
  41. /**
  42. * Created by nick on 30/08/2014.
  43. */
  44. public class JmxRemotingManager extends ConfigurableManager<RemotingConfigBean> implements RemotingManager {
  45. public static final String REMOTING_PROTOCOL = "jmx";
  46. private ChorusLog log = ChorusLogFactory.getLog(JmxRemotingManager.class);
  47. /**
  48. * Map: configName -> proxy
  49. */
  50. private final Map<String, ChorusHandlerJmxProxy> proxies = new HashMap<>();
  51. private final List<RemotingManagerConfig> remotingConfigs = new LinkedList<>();
  52. private Map<RemotingManagerConfig, List<StepInvoker>> remoteInvokersToUse = new HashMap<>();
  53. public JmxRemotingManager() {
  54. super(RemotingConfigBean.class);
  55. }
  56. @Override
  57. public void connect(String configName, Properties remotingProperties) {
  58. RemotingManagerConfig remotingConfig = getConfig(configName, remotingProperties, "remoting");
  59. ChorusHandlerJmxProxy proxy = getProxyForComponent(remotingConfig.getConfigName(), remotingConfig);
  60. List<StepInvoker> invokers = getRemoteStepInvokers(proxy);
  61. remoteInvokersToUse.put(remotingConfig, invokers);
  62. }
  63. @Override
  64. public List<StepInvoker> getStepInvokers() {
  65. List<StepInvoker> invokers = new LinkedList<>();
  66. for ( List<StepInvoker> l : remoteInvokersToUse.values()) {
  67. invokers.addAll(l);
  68. }
  69. return invokers;
  70. }
  71. private List<StepInvoker> getRemoteStepInvokers(ChorusHandlerJmxProxy proxy) {
  72. List<StepInvoker> invokers = new ArrayList<>();
  73. for (JmxInvokerResult r : proxy.getStepMetadata()) {
  74. StepInvoker invoker = RemoteStepInvoker.createRemoteStepInvoker(r, proxy);
  75. invokers.add(invoker);
  76. }
  77. return invokers;
  78. }
  79. private ChorusHandlerJmxProxy getProxyForComponent(String componentName, RemotingManagerConfig remotingConfig) {
  80. ChorusHandlerJmxProxy proxy = proxies.get(componentName);
  81. if ( proxy == null) {
  82. proxy = new ChorusHandlerJmxProxy(
  83. componentName,
  84. remotingConfig.getHost(),
  85. remotingConfig.getPort(),
  86. remotingConfig.getUserName(),
  87. remotingConfig.getPassword(),
  88. remotingConfig.getConnectionAttempts(),
  89. remotingConfig.getConnectionAttemptMillis()
  90. );
  91. proxies.put(componentName, proxy);
  92. remotingConfigs.add(remotingConfig);
  93. log.debug("Opened JMX connection to: " + componentName);
  94. }
  95. return proxy;
  96. }
  97. private void closeConnection(RemotingManagerConfig c) {
  98. ChorusHandlerJmxProxy proxy = proxies.remove(c.getConfigName());
  99. if ( proxy != null) {
  100. proxy.destroy();
  101. log.debug("Closed JMX connection to: " + c.getConfigName());
  102. }
  103. remotingConfigs.remove(c);
  104. remoteInvokersToUse.remove(c);
  105. }
  106. /**
  107. * Close all connections
  108. */
  109. public void closeAllConnections() {
  110. for(RemotingManagerConfig c : new ArrayList<>(remotingConfigs)) {
  111. closeConnection(c);
  112. }
  113. }
  114. @Override
  115. public ExecutionListener getExecutionListener() {
  116. return new RemotingManagerExecutionListener();
  117. }
  118. @ExecutionPriority(ExecutionPriority.REMOTING_MANAGER_PRIORITY)
  119. private class RemotingManagerExecutionListener extends ExecutionListenerAdapter {
  120. public void featureCompleted(ExecutionToken testExecutionToken, FeatureToken feature) {
  121. try {
  122. closeAllConnections();
  123. } catch (Throwable t) {
  124. log.error("Failed during destroyFeature jmx remoting manager", t);
  125. }
  126. }
  127. public void scenarioCompleted(ExecutionToken testExecutionToken, ScenarioToken scenario) {
  128. try {
  129. disposeForScope(Scope.SCENARIO);
  130. } catch (Throwable t) {
  131. log.error("Failed during destroyScenario() jmx remoting manager", t);
  132. }
  133. }
  134. private void disposeForScope(Scope scope) {
  135. for ( RemotingManagerConfig c : new ArrayList<>(remotingConfigs)) {
  136. if ( c.getScope() == scope) {
  137. closeConnection(c);
  138. }
  139. }
  140. }
  141. }
  142. }