PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/interpreter/chorus-remoting/src/main/java/org/chorusbdd/chorus/remoting/manager/RemotingConfigBean.java

http://github.com/Chorus-bdd/Chorus
Java | 220 lines | 169 code | 25 blank | 26 comment | 12 complexity | 6ee2eb8e9104347f8e0c2305051b8f4c 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.manager;
  25. import org.chorusbdd.chorus.annotations.Scope;
  26. import org.chorusbdd.chorus.handlerconfig.configproperty.ConfigProperty;
  27. import org.chorusbdd.chorus.handlerconfig.configproperty.ConfigValidator;
  28. import org.chorusbdd.chorus.handlerconfig.configproperty.ConfigValidatorException;
  29. /**
  30. * Remoting config
  31. */
  32. public class RemotingConfigBean implements RemotingManagerConfig {
  33. private String protocol;
  34. private String configName;
  35. private String host;
  36. private int port;
  37. private int connectionAttempts;
  38. private int connectionAttemptMillis;
  39. private Scope scope;
  40. private String userName;
  41. private String password;
  42. @Override
  43. public String getConfigName() {
  44. return configName;
  45. }
  46. public void setConfigName(String configName) {
  47. this.configName = configName;
  48. }
  49. @ConfigProperty(
  50. name="connection",
  51. description="A shorthand way of setting protocol host and port properties delimited by colon, e.g. jmx:myHost:myPort",
  52. validationPattern = "jmx:\\S+:\\d+",
  53. mandatory = false,
  54. order = 5
  55. )
  56. public void setConnection(String connection) {
  57. String[] vals = String.valueOf(connection).split(":");
  58. if (vals.length != 3) {
  59. throw new ConfigValidatorException(
  60. "Could not parse remoting property 'connection', " +
  61. "expecting a value in the form protocol:host:port"
  62. );
  63. }
  64. setProtocol(vals[0]);
  65. setHost(vals[1]);
  66. setPort(Integer.parseInt(vals[2]));
  67. }
  68. @Override
  69. public String getProtocol() {
  70. return protocol;
  71. }
  72. @ConfigProperty(
  73. name="protocol",
  74. description="Protocol to make connection (only JMX supported at present)",
  75. defaultValue = "jmx",
  76. validationPattern = "jmx",
  77. order = 10
  78. )
  79. public void setProtocol(String protocol) {
  80. this.protocol = protocol;
  81. }
  82. @Override
  83. public String getHost() {
  84. return host;
  85. }
  86. @ConfigProperty(
  87. name="host",
  88. description="host where remote component is running",
  89. mandatory = false, //instead can set 'connection' property
  90. order = 20
  91. )
  92. public void setHost(String host) {
  93. this.host = host;
  94. }
  95. @Override
  96. public int getPort() {
  97. return port;
  98. }
  99. @ConfigProperty(
  100. name="port",
  101. description="port on which remote component's jmx service is listening for connections",
  102. validationPattern = "\\d+",
  103. mandatory = false, //instead can set 'connection' property
  104. order = 30
  105. )
  106. public void setPort(int port) {
  107. this.port = port;
  108. }
  109. @Override
  110. public int getConnectionAttempts() {
  111. return connectionAttempts;
  112. }
  113. @ConfigProperty(
  114. name="connectionAttempts",
  115. description="Number of times to attempt connection",
  116. defaultValue = "40",
  117. validationPattern = "\\d+",
  118. order = 40
  119. )
  120. public void setConnectionAttempts(int connectionAttempts) {
  121. this.connectionAttempts = connectionAttempts;
  122. }
  123. @Override
  124. public int getConnectionAttemptMillis() {
  125. return connectionAttemptMillis;
  126. }
  127. @ConfigProperty(
  128. name="connectionAttemptMillis",
  129. description="Wait time between each connection attempt",
  130. defaultValue = "250",
  131. validationPattern = "\\d+",
  132. order = 50
  133. )
  134. public void setConnectionAttemptMillis(int connectionAttemptMillis) {
  135. this.connectionAttemptMillis = connectionAttemptMillis;
  136. }
  137. @Override
  138. public Scope getScope() {
  139. return scope;
  140. }
  141. @ConfigProperty(
  142. name="scope",
  143. description="Whether the remoting connection is closed at the end of the scenario or at the end of the feature. " +
  144. "This will be set automatically to FEATURE for connections established during 'Feature-Start:' if not provided, otherwise Scenario",
  145. defaultValue = "SCENARIO",
  146. order = 60
  147. )
  148. public void setScope(Scope scope) {
  149. this.scope = scope;
  150. }
  151. @Override
  152. public String getUserName() {
  153. return this.userName;
  154. }
  155. @ConfigProperty(
  156. name="userName",
  157. description = "User Name to provide if remote component's JMX server requires authentication",
  158. mandatory = false,
  159. order = 70
  160. )
  161. public void setUserName(String userName) { this.userName = userName; }
  162. @Override
  163. public String getPassword() {
  164. return this.password;
  165. }
  166. @ConfigProperty(
  167. name="password",
  168. description = "Password to provide if remote component's JMX server requires authentication",
  169. mandatory = false,
  170. order = 80
  171. )
  172. public void setPassword(String password) { this.password = password; }
  173. @ConfigValidator
  174. public void validate() {
  175. if (host == null) throw new ConfigValidatorException("host property must be set");
  176. if (port < 0) throw new ConfigValidatorException("port must be set and > 0");
  177. if (connectionAttempts < 0) throw new ConfigValidatorException("connectionAttempts must be set and > 0");
  178. if (connectionAttemptMillis < 0) throw new ConfigValidatorException("connectionAttemptMillis must be set and > 0");
  179. if ( userName != null && password == null) throw new ConfigValidatorException("If userName is set, then password must also be provided");
  180. }
  181. @Override
  182. public String toString() {
  183. return "RemotingConfigBean{" +
  184. "protocol='" + protocol + '\'' +
  185. ", configName='" + configName + '\'' +
  186. ", host='" + host + '\'' +
  187. ", port=" + port +
  188. ", connectionAttempts=" + connectionAttempts +
  189. ", connectionAttemptMillis=" + connectionAttemptMillis +
  190. ", userName=" + userName +
  191. ", password=" + password == null ? "not set" : "*******" +
  192. ", scope=" + scope +
  193. '}';
  194. }
  195. }