1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.archive.hcc.util.jmx;
24
25 import java.io.IOException;
26 import java.net.InetSocketAddress;
27 import java.net.MalformedURLException;
28 import java.util.HashMap;
29 import java.util.Map;
30 import java.util.Properties;
31 import java.util.logging.Level;
32 import java.util.logging.Logger;
33
34 import javax.management.MBeanServerConnection;
35 import javax.management.remote.JMXConnector;
36 import javax.management.remote.JMXConnectorFactory;
37 import javax.management.remote.JMXServiceURL;
38
39 import org.archive.hcc.util.SmartPropertiesResolver;
40
41 public class MBeanServerConnectionFactory {
42
43 private static Logger log = Logger.getLogger(MBeanServerConnectionFactory.class.getName());
44 private static Properties properties = null;
45 static {
46 try{
47 properties = SmartPropertiesResolver.getProperties("hcc.properties");
48 }catch(Throwable t){
49 log.warning("hcc.properties not found.");
50 properties = new Properties();
51 }
52
53
54
55 }
56 /***
57 * Creates a new MBeanServerConnection on the specified port.
58 *
59 * @param address
60 * @return
61 * @throws IOException
62 */
63 public static MBeanServerConnection createConnection(
64 InetSocketAddress address) throws IOException {
65 JMXConnector jmxc = JMXConnectorFactory.connect(
66 createJMXServiceUrl(address),
67 formatCredentials(
68 properties.getProperty("heritrix.jmx.username", "controlRole"),
69 properties.getProperty("heritrix.jmx.password", "letmein")));
70
71 MBeanServerConnection mbeanServerConnection = jmxc
72 .getMBeanServerConnection();
73
74 if (log.isLoggable(Level.INFO)) {
75 log.info("successfully created mbeanServerConnection on "
76 + address);
77 }
78 return mbeanServerConnection;
79 }
80
81 protected static JMXServiceURL createJMXServiceUrl(InetSocketAddress address) {
82 try {
83 String hostport = address.getHostName() + ":" + address.getPort();
84
85 String serviceUrl = "service:jmx:rmi://" + hostport
86 + "/jndi/rmi://" + hostport + "/jmxrmi";
87
88
89 if (log.isLoggable(Level.INFO)) {
90 log.info("service url: " + serviceUrl);
91 }
92
93 return new JMXServiceURL(serviceUrl);
94 } catch (MalformedURLException e) {
95 e.printStackTrace();
96 return null;
97 }
98
99 }
100
101 protected static Map<String, Object> formatCredentials(
102 String username,
103 String password) {
104 String[] creds = new String[] { username, password };
105 Map<String, Object> env = new HashMap<String, Object>(1);
106 env.put(JMXConnector.CREDENTIALS, creds);
107 return env;
108 }
109 }