PageRenderTime 46ms CodeModel.GetById 15ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 0ms

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