/src/com/googlecode/jmxtrans/model/Server.java

http://jmxtrans.googlecode.com/ · Java · 314 lines · 189 code · 43 blank · 82 comment · 23 complexity · 78515550a87ebf9af4a342c11de1c9b0 MD5 · raw file

  1. package com.googlecode.jmxtrans.model;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.apache.commons.lang.builder.EqualsBuilder;
  5. import org.apache.commons.lang.builder.HashCodeBuilder;
  6. import org.codehaus.jackson.annotate.JsonIgnore;
  7. import org.codehaus.jackson.annotate.JsonPropertyOrder;
  8. import org.codehaus.jackson.map.annotate.JsonSerialize;
  9. import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12. import com.googlecode.jmxtrans.util.DatagramSocketFactory;
  13. import com.googlecode.jmxtrans.util.JmxConnectionFactory;
  14. import com.googlecode.jmxtrans.util.PropertyResolver;
  15. import com.googlecode.jmxtrans.util.SocketFactory;
  16. import com.googlecode.jmxtrans.util.ValidationException;
  17. /**
  18. * Represents a jmx server that we want to connect to.
  19. * This also stores the queries that we want to execute against the server.
  20. * @author jon
  21. */
  22. @JsonSerialize(include=Inclusion.NON_NULL)
  23. @JsonPropertyOrder(value={"alias", "host", "port", "username", "password", "cronExpression", "numQueryThreads", "protocolProviderPackages"})
  24. public class Server {
  25. private static final Logger log = LoggerFactory.getLogger(Server.class);
  26. private static final String FRONT = "service:jmx:rmi:///jndi/rmi://";
  27. private static final String BACK = "/jmxrmi";
  28. public static final String SOCKET_FACTORY_POOL = SocketFactory.class.getSimpleName();
  29. public static final String JMX_CONNECTION_FACTORY_POOL = JmxConnectionFactory.class.getSimpleName();
  30. public static final String DATAGRAM_SOCKET_FACTORY_POOL = DatagramSocketFactory.class.getSimpleName();
  31. private JmxProcess jmxProcess;
  32. private String alias;
  33. private String host;
  34. private String port;
  35. private String username;
  36. private String password;
  37. private String protocolProviderPackages;
  38. private String url;
  39. private String cronExpression;
  40. private Integer numQueryThreads;
  41. private List<Query> queries = new ArrayList<Query>();
  42. public Server() { }
  43. /** */
  44. public Server(String host, String port) {
  45. this.host = host;
  46. this.port = port;
  47. }
  48. /** */
  49. public Server(String host, String port, Query query) throws ValidationException {
  50. this.host = host;
  51. this.port = port;
  52. this.addQuery(query);
  53. }
  54. /**
  55. * The parent container in json
  56. */
  57. public void setJmxProcess(JmxProcess jmxProcess) {
  58. this.jmxProcess = jmxProcess;
  59. }
  60. /**
  61. * The parent container in json
  62. */
  63. @JsonIgnore
  64. public JmxProcess getJmxProcess() {
  65. return this.jmxProcess;
  66. }
  67. /**
  68. * Some writers (GraphiteWriter) use the alias in generation of the unique key which references
  69. * this server.
  70. */
  71. public void setAlias(String alias) {
  72. this.alias = PropertyResolver.resolveProps(alias);
  73. }
  74. /**
  75. * Some writers (GraphiteWriter) use the alias in generation of the unique key which references
  76. * this server.
  77. */
  78. public String getAlias() {
  79. return this.alias;
  80. }
  81. /** */
  82. public void setHost(String host) {
  83. this.host = PropertyResolver.resolveProps(host);
  84. }
  85. /** */
  86. public String getHost() {
  87. return this.host;
  88. }
  89. /** */
  90. public void setPort(String port) {
  91. this.port = PropertyResolver.resolveProps(port);
  92. }
  93. /** */
  94. public String getPort() {
  95. return this.port;
  96. }
  97. /** */
  98. public void setUsername(String username) {
  99. this.username = PropertyResolver.resolveProps(username);
  100. }
  101. /** */
  102. public String getUsername() {
  103. return this.username;
  104. }
  105. /** */
  106. public void setPassword(String password) {
  107. this.password = PropertyResolver.resolveProps(password);
  108. }
  109. /** */
  110. public String getPassword() {
  111. return this.password;
  112. }
  113. /**
  114. * Won't add the same query (determined by equals()) 2x.
  115. */
  116. public void setQueries(List<Query> queries) throws ValidationException {
  117. for (Query q : queries) {
  118. this.addQuery(q);
  119. }
  120. }
  121. /** */
  122. public List<Query> getQueries() {
  123. return this.queries;
  124. }
  125. /**
  126. * Adds a query. Won't add the same query (determined by equals()) 2x.
  127. */
  128. public void addQuery(Query q) throws ValidationException {
  129. if (!this.queries.contains(q)) {
  130. this.queries.add(q);
  131. } else {
  132. log.debug("Skipped duplicate query: " + q + " for server: " + this);
  133. }
  134. }
  135. /**
  136. * The jmx url to connect to. If null, it builds this from host/port with a standard configuration. Other
  137. * JVM's may want to set this value.
  138. */
  139. @JsonIgnore
  140. public String getUrl() {
  141. if (this.url == null) {
  142. if ((this.host == null) || (this.port == null)) {
  143. throw new RuntimeException("url is null and host or port is null. cannot construct url dynamically.");
  144. }
  145. this.url = FRONT + this.host + ":" + this.port + BACK;
  146. }
  147. return this.url;
  148. }
  149. public void setUrl(String url) {
  150. this.url = PropertyResolver.resolveProps(url);
  151. }
  152. /**
  153. * If there are queries and results that have been executed,
  154. * this is just a shortcut to get all the Results.
  155. *
  156. * @return null if there are no queries or empty list if there are no results.
  157. */
  158. @JsonIgnore
  159. public List<Result> getResults() {
  160. List<Query> queries = this.getQueries();
  161. List<Result> results = null;
  162. if (queries != null) {
  163. results = new ArrayList<Result>();
  164. for (Query q : queries) {
  165. List<Result> tmp = q.getResults();
  166. if (tmp != null) {
  167. results.addAll(tmp);
  168. }
  169. }
  170. }
  171. return results;
  172. }
  173. /** */
  174. @JsonIgnore
  175. public boolean isQueriesMultiThreaded() {
  176. return (this.numQueryThreads != null) && (this.numQueryThreads > 0);
  177. }
  178. /**
  179. * The number of query threads for this server.
  180. */
  181. public void setNumQueryThreads(Integer numQueryThreads) {
  182. this.numQueryThreads = numQueryThreads;
  183. }
  184. /**
  185. * The number of query threads for this server.
  186. */
  187. public Integer getNumQueryThreads() {
  188. return this.numQueryThreads;
  189. }
  190. /**
  191. * Each server can set a cronExpression for the scheduler.
  192. * If the cronExpression is null, then the job is run immediately
  193. * and once. Otherwise, it is added to the scheduler for immediate
  194. * execution and run according to the cronExpression.
  195. */
  196. public void setCronExpression(String cronExpression) {
  197. this.cronExpression = cronExpression;
  198. }
  199. /**
  200. * Each server can set a cronExpression for the scheduler.
  201. * If the cronExpression is null, then the job is run immediately
  202. * and once. Otherwise, it is added to the scheduler for immediate
  203. * execution and run according to the cronExpression.
  204. */
  205. public String getCronExpression() {
  206. return this.cronExpression;
  207. }
  208. /** */
  209. @Override
  210. public String toString() {
  211. return "Server [host=" + this.host + ", port=" + this.port + ", url=" + this.url + ", cronExpression="
  212. + this.cronExpression + ", numQueryThreads=" + this.numQueryThreads + "]";
  213. }
  214. /** */
  215. @Override
  216. public boolean equals(Object o) {
  217. if (o == null) {
  218. return false;
  219. }
  220. if (o == this) {
  221. return true;
  222. }
  223. if (o.getClass() != this.getClass()) {
  224. return false;
  225. }
  226. if (!(o instanceof Server)) {
  227. return false;
  228. }
  229. Server other = (Server)o;
  230. return new EqualsBuilder()
  231. .append(this.getHost(), other.getHost())
  232. .append(this.getPort(), other.getPort())
  233. .append(this.getNumQueryThreads(), other.getNumQueryThreads())
  234. .append(this.getCronExpression(), other.getCronExpression())
  235. .append(this.getAlias(), other.getAlias())
  236. .append(this.getUsername(), other.getUsername())
  237. .append(this.getPassword(), other.getPassword()).isEquals();
  238. }
  239. /** */
  240. @Override
  241. public int hashCode() {
  242. return new HashCodeBuilder(13, 21)
  243. .append(this.getHost())
  244. .append(this.getPort())
  245. .append(this.getNumQueryThreads())
  246. .append(this.getCronExpression())
  247. .append(this.getAlias())
  248. .append(this.getUsername())
  249. .append(this.getPassword()).toHashCode();
  250. }
  251. /**
  252. * This is some obtuse shit for enabling weblogic support.
  253. *
  254. * http://download.oracle.com/docs/cd/E13222_01/wls/docs90/jmx/accessWLS.html
  255. *
  256. * You'd set this to: weblogic.management.remote
  257. */
  258. public String getProtocolProviderPackages() {
  259. return protocolProviderPackages;
  260. }
  261. /**
  262. * This is some obtuse shit for enabling weblogic support.
  263. *
  264. * http://download.oracle.com/docs/cd/E13222_01/wls/docs90/jmx/accessWLS.html
  265. *
  266. * You'd set this to: weblogic.management.remote
  267. */
  268. public void setProtocolProviderPackages(String protocolProviderPackages) {
  269. this.protocolProviderPackages = protocolProviderPackages;
  270. }
  271. }