View Javadoc

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