View Javadoc

1   /* $Id: MBeanServerConnectionFactory.java 4184 2006-03-09 02:44:14Z stack-sf $
2    *
3    * (Created on Dec 12, 2005
4    *
5    * Copyright (C) 2005 Internet Archive.
6    *  
7    * This file is part of the Heritrix Cluster Controller (crawler.archive.org).
8    *  
9    * HCC is free software; you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser Public License as published by
11   * the Free Software Foundation; either version 2.1 of the License, or
12   * any later version.
13   * 
14   * Heritrix is distributed in the hope that it will be useful, 
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU Lesser Public License for more details.
18   * 
19   * You should have received a copy of the GNU Lesser Public License
20   * along with Heritrix; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 }