View Javadoc

1   // ========================================================================
2   // Copyright (c) Webtide LLC
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.monitor;
15  
16  import java.io.IOException;
17  import java.util.HashSet;
18  import java.util.Set;
19  
20  import javax.management.MBeanServerConnection;
21  
22  import org.eclipse.jetty.monitor.jmx.MonitorAction;
23  import org.eclipse.jetty.monitor.jmx.MonitorTask;
24  import org.eclipse.jetty.monitor.jmx.ServiceConnection;
25  import org.eclipse.jetty.xml.XmlConfiguration;
26  
27  /* ------------------------------------------------------------ */
28  /**
29   * JMXMonitor
30   *
31   * Performs monitoring of the values of the attributes of MBeans
32   * and executes specified actions as well as sends notifications
33   * of the specified events that have occurred.
34   */
35  public class JMXMonitor
36  {
37      private static JMXMonitor __monitor = new JMXMonitor();
38      
39      private String _serverUrl;
40      private ServiceConnection _serviceConnection;
41  
42      private Set<MonitorAction> _actions = new HashSet<MonitorAction>();
43  
44      /* ------------------------------------------------------------ */
45      /**
46       * Constructs a JMXMonitor instance. Used for XML Configuration.
47       * 
48       * !! DO NOT INSTANTIATE EXPLICITLY !!
49       */
50      public JMXMonitor() {}
51  
52      /* ------------------------------------------------------------ */
53      /**
54       * Adds monitor actions to the monitor
55       *
56       * @param actions monitor actions to add
57       * @return true if successful
58       */
59      public boolean addActions(MonitorAction... actions)
60      {
61          return getInstance().add(actions);
62      }
63  
64      /* ------------------------------------------------------------ */
65      /**
66       * Removes monitor actions from the monitor
67       * 
68       * @param actions monitor actions to remove
69       * @return true if successful
70       */
71      public boolean removeActions(MonitorAction... actions)
72      {
73          return getInstance().remove(actions);
74      }
75  
76      /* ------------------------------------------------------------ */
77      /**
78       * Sets the JMX server URL
79       *
80       * @param url URL of the JMX server
81       */
82      public void setUrl(String url)
83       {
84          getInstance().set(url);
85      }
86      
87      public MBeanServerConnection getConnection()
88          throws IOException
89      {
90          return getInstance().get();
91      }
92  
93      public static JMXMonitor getInstance()
94      {
95          return __monitor;
96      }
97         
98      public static boolean addMonitorActions(MonitorAction... actions)
99      {
100         return getInstance().add(actions);
101     }
102 
103     public static boolean removeMonitorActions(MonitorAction... actions)
104     {
105         return getInstance().remove(actions);
106     }
107     
108     public static void setServiceUrl(String url)
109     {
110         getInstance().set(url);
111     }
112     
113     /* ------------------------------------------------------------ */
114     /**
115      * Retrieves a connection to JMX service
116      *
117      * @return server connection
118      * @throws IOException
119      */
120     public static MBeanServerConnection getServiceConnection()
121         throws IOException
122     {
123         return getInstance().getConnection();
124     }
125 
126     public static void main(final String args[]) throws Exception
127     {
128         XmlConfiguration.main(args);
129     }
130     
131     private synchronized boolean add(MonitorAction... actions)
132     {
133         boolean result = true;
134 
135         for (MonitorAction action : actions)
136         {
137             if (!_actions.add(action))
138             {
139                 result = false;
140             }
141             else
142             {
143                 MonitorTask.schedule(action);
144             }
145         }
146         
147         return result;
148     }
149     
150     private synchronized boolean remove(MonitorAction... actions)
151     {
152         boolean result = true;
153 
154         for (MonitorAction action : actions)
155         {
156             if (!_actions.remove(action))
157             {
158                 result = false;
159             }
160 
161             MonitorTask.cancel(action);
162         }
163         
164         return result;
165     }
166     
167     private synchronized void set(String url)
168     {
169         _serverUrl = url;
170 
171         if (_serviceConnection != null)
172         {
173             _serviceConnection.disconnect();
174             _serviceConnection = null;
175         }
176     }
177     
178     private synchronized MBeanServerConnection get()
179         throws IOException
180     {
181         if (_serviceConnection == null)
182         {
183             _serviceConnection = new ServiceConnection(_serverUrl);
184             _serviceConnection.connect();
185         }
186         
187         return _serviceConnection.getConnection();
188     }
189 }