View Javadoc

1   /* ClusterControllerClientManager
2    * 
3    * $Id: ClusterControllerClientManager.java 4141 2006-01-22 23:24:23Z dbernstein $
4    * 
5    * Created on Dec 15, 2005
6    *
7    * Copyright (C) 2005 Internet Archive.
8    * 
9    * This file is part of the Heritrix web crawler (crawler.archive.org).
10   * 
11   * Heritrix is free software; you can redistribute it and/or modify
12   * it under the terms of the GNU Lesser Public License as published by
13   * the Free Software Foundation; either version 2.1 of the License, or
14   * any later version.
15   * 
16   * Heritrix is distributed in the hope that it will be useful, 
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU Lesser Public License for more details.
20   * 
21   * You should have received a copy of the GNU Lesser Public License
22   * along with Heritrix; if not, write to the Free Software
23   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24   */
25  
26  package org.archive.hcc.client;
27  
28  import java.net.InetAddress;
29  import java.net.InetSocketAddress;
30  import java.util.logging.Level;
31  import java.util.logging.Logger;
32  
33  /***
34   * This class manages singleton instances of the ClusterControllerClient. Within
35   * a jvm there will be one instance of ClusterControllerClient for each jmx
36   * socket address (assuming that a ClusterControllerBean is running on that
37   * local or remote socket).
38   * 
39   * @author Daniel Bernstein (dbernstein@archive.org)
40   * 
41   */
42  public class ClusterControllerClientManager {
43      private static Logger log = Logger
44              .getLogger(ClusterControllerClientManager.class.getName());
45  
46      private static ClusterControllerClient defaultClient;
47  
48      /***
49       * Returns this singleton instance of the default cluster controller client.
50       * The host and jmx port can be set in the following command line options.
51       * -Dorg.archive.hcc.client.host={your host}
52       * -Dorg.archive.hcc.client.jmxPort={your port}
53       * 
54       * If you do not specify these values on the command line, the client will
55       * connect to the local jmx server on port 8849.
56       * 
57       * @return
58       */
59      public static ClusterControllerClient getDefaultClient() {
60          try {
61              if (defaultClient == null) {
62                  String host = System.getProperty(
63                          "org.archive.hcc.client.host",
64                          InetAddress.getLocalHost().getHostName());
65  
66                  int jmxPort = Integer.parseInt(System.getProperty(
67                          "org.archive.hcc.client.jmxPort",
68                          "8849"));
69                  InetSocketAddress address = new InetSocketAddress(host, jmxPort);
70                  defaultClient = new ClusterControllerClientImpl(address);
71              }
72  
73              return defaultClient;
74          } catch (Exception e) {
75              e.printStackTrace();
76              if (log.isLoggable(Level.SEVERE)) {
77                  log.severe(": " + e.getMessage());
78              }
79  
80              throw new RuntimeException(e);
81          }
82      }
83  
84      /***
85       * Removes the manager's reference to the default client for use with unit
86       * tests.
87       */
88      public static void resetDefaultClient() {
89          defaultClient = null;
90      }
91  
92  }