View Javadoc

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