PageRenderTime 21ms CodeModel.GetById 13ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/googlecode/jmxtrans/util/JmxConnectionFactory.java

http://jmxtrans.googlecode.com/
Java | 57 lines | 33 code | 10 blank | 14 comment | 0 complexity | 65b7aeec6bca836a2ad6ef7095a84a82 MD5 | raw file
 1package com.googlecode.jmxtrans.util;
 2
 3import java.io.IOException;
 4
 5import javax.management.remote.JMXConnector;
 6
 7import org.apache.commons.pool.BaseKeyedPoolableObjectFactory;
 8import org.slf4j.Logger;
 9import org.slf4j.LoggerFactory;
10
11import com.googlecode.jmxtrans.model.Server;
12
13/**
14 * Allows us to pool connections to remote jmx servers.
15 */
16public class JmxConnectionFactory extends BaseKeyedPoolableObjectFactory {
17
18	@SuppressWarnings("unused")
19	private static final Logger log = LoggerFactory.getLogger(JmxConnectionFactory.class);
20
21	/** constructor */
22	public JmxConnectionFactory() {}
23
24	/**
25	 * Creates the connection.
26	 */
27	@Override
28	public Object makeObject(Object key) throws Exception {
29		Server server = (Server) key;
30		return JmxUtils.getServerConnection(server);
31	}
32
33	/**
34	 * Closes the connection.
35	 */
36	@Override
37	public void destroyObject(Object key, Object obj) throws Exception {
38		JMXConnector conn = (JMXConnector) obj;
39		conn.close();
40	}
41
42	/**
43	 * Validates that the connection is good.
44	 */
45	@Override
46	public boolean validateObject(Object key, Object obj) {
47		JMXConnector conn = (JMXConnector) obj;
48		boolean result = false;
49		try {
50			conn.getConnectionId();
51			result = true;
52		} catch (IOException ex) {
53			// ignored
54		}
55		return result;
56	}
57}