View Javadoc

1   // ========================================================================
2   // Copyright (c) 2009-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses.
12  // ========================================================================
13  
14  package org.eclipse.jetty.jmx;
15  
16  import java.lang.management.ManagementFactory;
17  import java.util.Map;
18  
19  import javax.management.MBeanServer;
20  import javax.management.ObjectName;
21  import javax.management.remote.JMXConnectorServer;
22  import javax.management.remote.JMXConnectorServerFactory;
23  import javax.management.remote.JMXServiceURL;
24  
25  import org.eclipse.jetty.util.component.AbstractLifeCycle;
26  import org.eclipse.jetty.util.log.Log;
27  import org.eclipse.jetty.util.thread.ShutdownThread;
28  
29  
30  /* ------------------------------------------------------------ */
31  /**
32   * AbstractLifeCycle wrapper for JMXConnector Server
33   */
34  public class ConnectorServer extends AbstractLifeCycle
35  {
36      JMXConnectorServer _connectorServer;
37      
38      /* ------------------------------------------------------------ */
39      /**
40       * Constructs connector server
41       * 
42       * @param serviceURL the address of the new connector server.
43       * The actual address of the new connector server, as returned 
44       * by its getAddress method, will not necessarily be exactly the same.
45       * @param name object name string to be assigned to connector server bean
46       * @throws Exception
47       */
48      public ConnectorServer(JMXServiceURL serviceURL, String name)
49          throws Exception
50      {
51          this(serviceURL, null, name);
52      }
53      
54      /* ------------------------------------------------------------ */
55      /**
56       * Constructs connector server
57       * 
58       * @param serviceURL the address of the new connector server.
59       * The actual address of the new connector server, as returned 
60       * by its getAddress method, will not necessarily be exactly the same.
61       * @param environment  a set of attributes to control the new connector
62       * server's behavior. This parameter can be null. Keys in this map must
63       * be Strings. The appropriate type of each associated value depends on
64       * the attribute. The contents of environment are not changed by this call. 
65       * @param name object name string to be assigned to connector server bean
66       * @throws Exception
67       */
68      public ConnectorServer(JMXServiceURL serviceURL, Map<String,?> environment, String name)
69           throws Exception
70      {
71          MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
72          _connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(serviceURL, environment, mbeanServer);
73          mbeanServer.registerMBean(_connectorServer,new ObjectName(name));
74      }
75  
76      /* ------------------------------------------------------------ */
77      /**
78       * @see org.eclipse.jetty.util.component.AbstractLifeCycle#doStart()
79       */
80      @Override
81      public void doStart()
82          throws Exception
83      {
84          _connectorServer.start();
85          ShutdownThread.register(0, this);       
86          
87          Log.info("JMX Remote URL: {}", _connectorServer.getAddress().toString());
88      }
89      
90      /* ------------------------------------------------------------ */
91      /**
92       * @see org.eclipse.jetty.util.component.AbstractLifeCycle#doStop()
93       */
94      @Override
95      public void doStop()
96          throws Exception
97      {
98          ShutdownThread.deregister(this);
99          _connectorServer.stop();
100     }
101 }