summaryrefslogtreecommitdiff
path: root/qpid/java/management/eclipse-plugin/src/main/java/org/apache/qpid/management/ui/jmx/JMXServerRegistry.java
blob: 9a6d134d120167360ffc3f415440b057b204a25f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
package org.apache.qpid.management.ui.jmx;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import javax.management.ListenerNotFoundException;
import javax.management.MBeanInfo;
import javax.management.MBeanServerConnection;
import javax.management.Notification;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

import org.apache.qpid.management.ui.Constants;
import org.apache.qpid.management.ui.ManagedBean;
import org.apache.qpid.management.ui.ManagedServer;
import org.apache.qpid.management.ui.ServerRegistry;
import org.apache.qpid.management.ui.model.ManagedAttributeModel;
import org.apache.qpid.management.ui.model.NotificationInfoModel;
import org.apache.qpid.management.ui.model.NotificationObject;
import org.apache.qpid.management.ui.model.OperationDataModel;


public class JMXServerRegistry extends ServerRegistry
{
    private ObjectName _serverObjectName = null;
    private JMXConnector _jmxc = null;
    private MBeanServerConnection _mbsc = null;
    
    // When an mbean gets removed from mbean server, then the notification listener
    // will add that mbean in this list.
    private List<ManagedBean> _mbeansToBeRemoved = new ArrayList<ManagedBean>();
    
    // Map containing all managed beans and ampped with unique mbean name
    private HashMap<String, ManagedBean>   _mbeansMap    = new HashMap<String, ManagedBean>();
    // Map containing MBeanInfo for all mbeans and mapped with unique mbean name 
    private HashMap<String, MBeanInfo>     _mbeanInfoMap = new HashMap<String, MBeanInfo>();
    // Map containing attribute model for all mbeans and mapped with unique mbean name
    private HashMap<String, ManagedAttributeModel>    _attributeModelMap = new HashMap<String, ManagedAttributeModel>();
    // Map containing operation model for all mbeans and mapped with unique mbean name
    private HashMap<String, OperationDataModel>       _operationModelMap = new HashMap<String, OperationDataModel>();
    // Map containing NotificationInfo for all mbeans and mapped with unique mbean name
    private HashMap<String, List<NotificationInfoModel>> _notificationInfoMap = new HashMap<String, List<NotificationInfoModel>>();
    // Map containing all notifications sent for all mbeans, which are registered for notification
    private HashMap<String, List<NotificationObject>> _notificationsMap  = new HashMap<String, List<NotificationObject>>();
    // For mbeans which have subscribed for a notification type
    // mbean unique name mapped with notification map. Notification map contains list of notification type
    // mapped with notification name. Notification type list contains those notification types,
    // which are subscribed for notification.
    private HashMap<String, HashMap<String, List<String>>> _subscribedNotificationMap = new HashMap<String, HashMap<String, List<String>>>();
    
    // listener for registration or unregistratioj of mbeans on mbean server
    private ClientNotificationListener _notificationListener = null;
    // listener for server connection. Receives notification if server connection goes down
    private ClientListener _clientListener = null;
    
    public JMXServerRegistry(ManagedServer server) throws Exception
    {
        super(server);
        JMXServiceURL jmxUrl = new JMXServiceURL(server.getUrl());
        _jmxc = JMXConnectorFactory.connect(jmxUrl, null);
        _mbsc = _jmxc.getMBeanServerConnection();
        
        _clientListener = new ClientListener(server);
        _notificationListener = new ClientNotificationListener(server);
        
        _jmxc.addConnectionNotificationListener(_clientListener, null, null);       
        _serverObjectName = new ObjectName("JMImplementation:type=MBeanServerDelegate");
        _mbsc.addNotificationListener(_serverObjectName, _clientListener, null, null);
    }
    
    public MBeanServerConnection getServerConnection()
    {
        return _mbsc;
    }
    
    /**
     * removes all listeners from the mbean server. This is required when user
     * disconnects the Qpid server connection
     */
    public void closeServerConnection() throws Exception
    {
        try
        {
            if (_jmxc != null)
                _jmxc.removeConnectionNotificationListener(_clientListener);

            if (_mbsc != null)
                _mbsc.removeNotificationListener(_serverObjectName, _clientListener);

            // remove mbean notification listeners
            for (String mbeanName : _subscribedNotificationMap.keySet())
            {
                _mbsc.removeNotificationListener(new ObjectName(mbeanName), _notificationListener);
            }
        }
        catch (ListenerNotFoundException ex)
        {
            System.out.println(ex.toString());
        }
    }
    
    public ManagedBean getManagedObject(String uniqueName)
    {
        return _mbeansMap.get(uniqueName);
    }
    
    public void addManagedObject(ManagedBean mbean)
    {
        if (mbean.getType().endsWith(Constants.QUEUE) && !mbean.getName().startsWith("tmp_"))
        {
            addQueueMBean(mbean);
        }
        else if (mbean.getType().endsWith(Constants.EXCHANGE))
        {
            addExchangeMBean(mbean);
        }
        else if (mbean.getType().endsWith(Constants.CONNECTION))
        {
            addConnectionMBean(mbean);
        }
        
        _mbeansMap.put(mbean.getUniqueName(), mbean);
    }

    public void removeManagedObject(ManagedBean mbean)
    {
        if (mbean.getType().endsWith(Constants.QUEUE))
            removeQueueMBean(mbean);
        else if (mbean.getType().endsWith(Constants.EXCHANGE))
            removeExchangeMBean(mbean);
        else if (mbean.getType().endsWith(Constants.CONNECTION))
            removeConnectionMBean(mbean);
        
        _mbeansMap.remove(mbean.getUniqueName());
    }
    
    public void putMBeanInfo(ManagedBean mbean, MBeanInfo mbeanInfo)
    {
        _mbeanInfoMap.put(mbean.getUniqueName(), mbeanInfo);
    }    
    public MBeanInfo getMBeanInfo(ManagedBean mbean)
    {
        return _mbeanInfoMap.get(mbean.getUniqueName());
    }
    
    public List<ManagedBean> getMBeans()
    {
        return new ArrayList<ManagedBean>(_mbeansMap.values());
    }
    
    public void setNotificationInfo(ManagedBean mbean, List<NotificationInfoModel>value)
    {
        _notificationInfoMap.put(mbean.getUniqueName(), value);
    }
    
    public List<NotificationInfoModel> getNotificationInfo(ManagedBean mbean)
    {
        return _notificationInfoMap.get(mbean.getUniqueName());
    }
    
    public void addNotification(ObjectName objName, Notification notification)
    {
        List<NotificationObject> list = _notificationsMap.get(objName.toString());
        NotificationObject obj = new NotificationObject(notification.getSequenceNumber(),
                                                        new Date(notification.getTimeStamp()),
                                                        notification.getMessage(),
                                                        notification.getSource(),
                                                        notification.getType());
        
        if (list == null)
        {
            list = new ArrayList<NotificationObject>();
            _notificationsMap.put(objName.toString(), list);
        }
        
        list.add(obj);
    }
    
    public List<NotificationObject> getNotifications(ManagedBean mbean)
    {
        return _notificationsMap.get(mbean.getUniqueName());
    }
    
    public void clearNotifications(ManagedBean mbean)
    {
        if (_notificationsMap.containsKey(mbean.getUniqueName()))
            _notificationsMap.get(mbean.getUniqueName()).clear();
    }
    
    public void addNotificationListener(ManagedBean mbean, String name, String type)
    {
         HashMap<String, List<String>> map = _subscribedNotificationMap.get(mbean.getUniqueName());
        if (map == null)
        {
            map = new HashMap<String, List<String>>();
            _subscribedNotificationMap.put(mbean.getUniqueName(),map);
        }
        
        List<String> list = map.get(name);
        if (list == null)
        {
            list = new ArrayList<String>();
            map.put(name, list);
        }
        if (Constants.ALL.equals(type))
        {
            List<NotificationInfoModel> infoList = _notificationInfoMap.get(mbean.getUniqueName());
            for (NotificationInfoModel model : infoList)
            {                
                if (model.getName().equals(name))
                {
                    String[] types = model.getTypes();
                    for (int i = 0; i < types.length; i++)
                    {
                        list.add(types[i]);
                    }
                }
            }
        }
        else
        {
            list.add(type);
        }

        System.out.println("Subscribed for notification :" + mbean.getUniqueName());
    }
    
    public boolean hasSubscribedForNotifications(ManagedBean mbean, String name, String type)
    {
        if (_subscribedNotificationMap.containsKey(mbean.getUniqueName()))
        {
            HashMap<String, List<String>> map = _subscribedNotificationMap.get(mbean.getUniqueName());
            if (map.containsKey(name))
            {
                if (map.get(name).contains(type))
                {
                    return true;
                }
            }
        }
        return false;
    }
    
    public void removeNotificationListener(ManagedBean mbean, String name, String type) throws Exception
    {
        System.out.println("Removed notification listener :" + mbean.getUniqueName() + name +type);
        if (_subscribedNotificationMap.containsKey(mbean.getUniqueName()))
        {            
            HashMap<String, List<String>> map = _subscribedNotificationMap.get(mbean.getUniqueName());
            if (map.containsKey(name))
            {
                if (Constants.ALL.equals(type))
                {
                    map.remove(name);
                }
                else if (type != null)
                {
                    map.get(name).remove(type);
                }
            }
            
            JMXManagedObject jmxbean = (JMXManagedObject)mbean;
            _mbsc.removeNotificationListener(jmxbean.getObjectName(), _notificationListener);
        }
    }
    
    public void registerManagedObject(ObjectName objName)
    {
        JMXManagedObject managedObject = new JMXManagedObject(objName);
        
        managedObject.setServer(getManagedServer());
        addManagedObject(managedObject);
    }
    
    public void unregisterManagedObject(ObjectName objName)
    {
        ManagedBean mbean = _mbeansMap.get(objName.toString());
        _mbeansToBeRemoved.add(mbean);
    }

    public List<ManagedBean> getObjectsToBeRemoved()
    {
        if (_mbeansToBeRemoved.isEmpty())
            return null;
        else
        {
            List<ManagedBean> list = new CopyOnWriteArrayList<ManagedBean>(_mbeansToBeRemoved);
            _mbeansToBeRemoved.clear();
            return list;
        }
    }   
    
    public void setAttributeModel(ManagedBean mbean, ManagedAttributeModel value)
    {
        _attributeModelMap.put(mbean.getUniqueName(), value);
    }
    
    public ManagedAttributeModel getAttributeModel(ManagedBean mbean)
    {
        return _attributeModelMap.get(mbean.getUniqueName());
    }
    
    public void setOperationModel(ManagedBean mbean, OperationDataModel value)
    {
        _operationModelMap.put(mbean.getUniqueName(), value);
    }
    
    public OperationDataModel getOperationModel(ManagedBean mbean)
    {
        return _operationModelMap.get(mbean.getUniqueName());
    }
    
    public String[] getQueueNames(String virtualHostName)
    {
        List<ManagedBean> list = getQueues(virtualHostName);
        if (list == null)
            return null;
        
        String[] queues = new String[list.size()];
        int i = 0;
        for (ManagedBean mbean : list)
        {
            queues[i++] = mbean.getName();
        }
        return queues;
    }
    
    public String[] getExchangeNames(String virtualHostName)
    {
        List<ManagedBean> list = getExchanges(virtualHostName);
        if (list == null)
            return null;
        
        String[] exchanges = new String[list.size()];
        int i = 0;
        for (ManagedBean mbean : list)
        {
            exchanges[i++] = mbean.getName();
        }
        return exchanges;
    }
    
    public String[] getConnectionNames(String virtualHostName)
    {
        List<ManagedBean> list = getExchanges(virtualHostName);
        if (list == null)
            return null;
        
        String[] connections = new String[list.size()];
        int i = 0;
        for (ManagedBean mbean : list)
        {
            connections[i++] = mbean.getName();
        }
        return connections;
    }

    public ClientNotificationListener getNotificationListener()
    {
        return _notificationListener;
    }

    public ClientListener getClientListener()
    {
        return _clientListener;
    }
}