summaryrefslogtreecommitdiff
path: root/java/management/eclipse-plugin/src/main/java/org/apache/qpid/management/ui/jmx/JMXServerRegistry.java
blob: 717f781334c24aab6e17cf2753a99afe4e262936 (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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
/*
 *
 * 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 static org.apache.qpid.management.ui.Constants.ALL;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import javax.management.MBeanInfo;
import javax.management.MBeanServerConnection;
import javax.management.Notification;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;

import org.apache.qpid.management.common.JMXConnnectionFactory;
import org.apache.qpid.management.ui.ApplicationRegistry;
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;
    private String _securityMechanism = null;
    
    private List<String> _usersList;
    // 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 mapped 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);
        
        _jmxc = JMXConnnectionFactory.getJMXConnection(
                ApplicationRegistry.timeout, server.getHost(),
                server.getPort(), server.getUser(), server.getPassword());
        
        _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;
    }


    public String getSecurityMechanism()
    {
        return _securityMechanism;
    }

    /**
     * removes all listeners from the mbean server. This is required when user
     * disconnects the Qpid server connection
     */
    public void closeServerConnection() throws IOException
    {
        if(isServerConnectionClosed())
        {
            //connection was already closed
            return;
        }
        
        try
        {
            //remove the listener from the JMXConnector
            if (_jmxc != null && _clientListener != null)
            {
                _jmxc.removeConnectionNotificationListener(_clientListener);
            }
        }
        catch (Exception e)
        {
            //ignore
        }

        try
        {
            //remove the listener from the MBeanServerDelegate MBean
            if (_mbsc != null && _clientListener != null)
            {
                _mbsc.removeNotificationListener(_serverObjectName, _clientListener);
            }
        }
        catch (Exception e)
        {
            //ignore
        }

        if (_mbsc != null && _clientListener != null)
        {
            //remove any listeners from the Qpid MBeans
            for (String mbeanName : _subscribedNotificationMap.keySet())
            {
                try
                {
                    _mbsc.removeNotificationListener(new ObjectName(mbeanName), _notificationListener);
                }
                catch (Exception e)
                {
                    //ignore
                }
            }
        }

        //close the JMXConnector
        if (_jmxc != null)
        {
            _jmxc.close();
        }
        
        serverConnectionClosed();
    }
    
    public ManagedBean getManagedObject(String uniqueName)
    {
        return _mbeansMap.get(uniqueName);
    }
    
    public void addManagedObject(ManagedBean mbean)
    {
        if (mbean.isQueue())
        {
            addQueueMBean(mbean);
        }
        else if (mbean.isExchange())
        {
            addExchangeMBean(mbean);
        }
        else if (mbean.isConnection())
        {
            addConnectionMBean(mbean);
        }
        else if (mbean.isVirtualHostManager())
        {
            addVirtualHostManagerMBean(mbean);
        }
        
        addVirtualHost(mbean.getVirtualHostName());
        _mbeansMap.put(mbean.getUniqueName(), mbean);
    }

    public void removeManagedObject(ManagedBean mbean)
    {
        if (mbean == null)
        {
            return;
        }
        
        _mbeansMap.remove(mbean.getUniqueName());
        
        if (mbean.isQueue())
        {
            removeQueueMBean(mbean);
        }
        else if (mbean.isExchange())
        {
            removeExchangeMBean(mbean);
        }
        else if (mbean.isConnection())
        {
            removeConnectionMBean(mbean);
        }
        else if (mbean.isVirtualHostManager())
        {
            removeVirtualHostManagerMBean(mbean);
        }
    }
    
    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);
    }
    
    /**
     * Returns all the notification objects for a given mbean. If mbean is null, it returns
     * notification objects for all the mbeans.
     */
    public List<NotificationObject> getNotifications(ManagedBean mbean)
    {
        if (mbean == null)
        {
            List<NotificationObject> totalList = new ArrayList<NotificationObject>();
            for (List<NotificationObject> list : _notificationsMap.values())
            {
                totalList.addAll(list);
            }
            return totalList;
        }
        else
        {
            return _notificationsMap.get(mbean.getUniqueName());
        }
    }
    
    public List<NotificationObject> getNotifications(String virtualhost)
    {
        List<NotificationObject> vhostNotificationsList = new ArrayList<NotificationObject>();

        //iterate over all the notification lists for mbeans with subscribed notifications
        for (List<NotificationObject> list : _notificationsMap.values())
        {
            if(list == null || list.isEmpty())
            {
                continue;
            }
            
            //Check the source vhost of the first notification
            NotificationObject notification  = list.get(0);
            
            if (notification != null)
            {
                String sourceVhost = notification.getSourceVirtualHost();
                if(sourceVhost != null)
                {
                    if(sourceVhost.equalsIgnoreCase(virtualhost))
                    {
                        //If it matches, add the entire list as they are from the same vhost (same source mbean)
                        vhostNotificationsList.addAll(list);
                    }
                }
            }
        }

        return vhostNotificationsList;
    }
    
    public void clearNotifications(ManagedBean mbean, List<NotificationObject> list)
    {
        if (mbean == null)
        {
            if (list == null || list.isEmpty())
            {
                // All notifications of all mbeans to be cleared
                _notificationsMap.clear();
            }
            else
            {
                // Clear the selected notifications
                for (NotificationObject obj : list)
                {
                    mbean = _mbeansMap.get(obj.getSource().toString());
                    List<NotificationObject> nList = _notificationsMap.get(mbean.getUniqueName());
                    if (nList != null && !nList.isEmpty())
                    {
                        nList.remove(obj);
                    }
                }
            }
        }
        else 
        {
            if (list == null || list.isEmpty())
            {
                // All notifications of this mbean to be cleared
                List<NotificationObject> nList = _notificationsMap.get(mbean.getUniqueName());
                if (nList != null && !nList.isEmpty())
                {
                    nList.clear();
                }
            }
            else
            {
                // Clear the selected notifications
                for (NotificationObject obj : list)
                {
                    List<NotificationObject> nList = _notificationsMap.get(mbean.getUniqueName());
                    if (nList != null && !nList.isEmpty())
                    {
                        nList.remove(obj);
                    }
                }
            }
        }
    }
    
    
    
    /**
     * Adds notification name and type to the map. The map contains all the notification names,
     * subscribed for an mbean.
     * @param mbean
     * @param name
     * @param type
     */
    public void addNotificationListener(ManagedBean mbean, String name, String type)
    {
        // Get the subscribed notifications map for given mbean. If map is null then create a new one. 
        HashMap<String, List<String>> map = _subscribedNotificationMap.get(mbean.getUniqueName());
        if (map == null)
        {
            map = new HashMap<String, List<String>>();
            _subscribedNotificationMap.put(mbean.getUniqueName(),map);
        }
        
        // Get the list of notification types for given notification name. If null, then create a new list.
        List<String> list = map.get(name);
        if (list == null)
        {
            list = new ArrayList<String>();
            map.put(name, list);
        }
        // Now add the notification type to the list
        if (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());
    }
    
    /**
     * Checks if the given notification name and type are subscribed for the mbean.
     */
    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;
    }
    
    /**
     * Clears the notification name and type information from the subscribed notifications map
     * and removes the listener from mbeanserver connection
     * @param mbean
     * @param name
     * @param type
     * @throws Exception
     */
    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()))
        {            
            // get the notifications map. This map contains the notification name mapped with the notification types
            HashMap<String, List<String>> map = _subscribedNotificationMap.get(mbean.getUniqueName());
            if (map.containsKey(name))
            {
                if (ALL.equals(type))
                {
                    map.remove(name);
                }
                else if (type != null)
                {
                    map.get(name).remove(type);
                    if (map.get(name).isEmpty())
                    {
                        map.remove(name);
                    }
                }
            }
            if (map.size() == 0)
            {
                _subscribedNotificationMap.remove(mbean.getUniqueName());
            }
            
            JMXManagedObject jmxbean = (JMXManagedObject)mbean;
            _mbsc.removeNotificationListener(jmxbean.getObjectName(), _notificationListener);
        }
    }
    
    /**
     * When the mbean registration request is received from the mbean server, then the client listener
     * can use this method.  It will add the mbean to a list, which will be used to add the mbean to
     * the registry and gui
     * @param objName
     */
    public void registerManagedObject(ObjectName objName)
    {
        JMXManagedObject managedObject = new JMXManagedObject(objName);       
        managedObject.setServer(getManagedServer());
        addManagedObject(managedObject);
    }
    
    /**
     * When mbean unregistration notification is received from the mbean server, then client listener
     * can invoke this method. It will add the mbean to the list of mbeans to be removed from registry
     * @param objName
     */
    public void unregisterManagedObject(ObjectName objName)
    {
        ManagedBean mbean = _mbeansMap.get(objName.toString());
        // Check if mbean was not available in the map. It can happen if mbean unregistration
        // notification is received and the mbean is not added in the map.
        if (mbean != null)
        {
            removeManagedObject(mbean);
            _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 List<String> getQueueNames(String virtualHostName)
    {
        List<ManagedBean> list = getQueues(virtualHostName);
        if (list == null)
            return null;
        
        List<String> queueNames = new ArrayList<String>();
        for (ManagedBean mbean : list)
        {
            queueNames.add(mbean.getName());
        }
        return queueNames;
    }
    
    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 void setUserList(List<String> list)
    {
        _usersList = list;
        Collections.sort(_usersList);
    }
    
    public List<String> getUsernames()
    {
        return _usersList;
    }

    public ClientNotificationListener getNotificationListener()
    {
        return _notificationListener;
    }

    public ClientListener getClientListener()
    {
        return _clientListener;
    }
}