1/*
2 * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24// JDK
25import java.util.Vector;
26
27// JMX
28import javax.management.NotificationListener;
29import javax.management.Notification;
30
31public class SimpleListener implements NotificationListener {
32    private boolean received = false;
33    private String type = null;
34    private Object handback = null;
35    private Vector<Object> handbacks = new Vector<Object>();
36    private int nbrec = 0;
37
38    public synchronized void handleNotification(Notification notification,
39                                                Object handback) {
40        Utils.debug(Utils.DEBUG_STANDARD,
41            "SimpleListener::handleNotification :" + notification);
42        try {
43            received = true;
44            type = notification.getType();
45            this.handback = handback;
46            handbacks.add(handback);
47            nbrec++;
48            notify();
49        } catch(Exception e) {
50            System.out.println("(ERROR) SimpleListener::handleNotification :"
51                        + " Caught exception "
52                        + e) ;
53        }
54    }
55
56    public synchronized boolean isNotificationReceived() {
57        boolean ret = received;
58        reset();
59        return ret;
60    }
61
62    public synchronized Object[] waitForMultiNotifications(int nb) {
63        while(true) {
64            if(nbrec < nb) {
65                Utils.debug(Utils.DEBUG_STANDARD,
66                            "SimpleListener::waitForMultiNotifications wait");
67                try {
68                    wait();
69                } catch(InterruptedException ie) {
70                    // OK : we wait for being interrupted
71                }
72                Utils.debug(Utils.DEBUG_STANDARD,
73                            "SimpleListener::waitForMultiNotifications wait over");
74            }
75            else
76            break;
77        }
78        Object[] ret = handbacks.toArray();
79        reset();
80        return ret;
81    }
82
83    private void reset() {
84        received = false;
85        handback = null;
86        handbacks.removeAllElements();
87        type = null;
88    }
89
90    public synchronized Object waitForNotificationHB() {
91        while(true) {
92            if(!received) {
93                Utils.debug(Utils.DEBUG_STANDARD,
94                    "SimpleListener::waitForNotificationHB wait");
95                try {
96                    wait();
97                } catch(InterruptedException ie) {
98                    // OK : we wait for being interrupted
99                }
100                Utils.debug(Utils.DEBUG_STANDARD,
101                    "SimpleListener::waitForNotificationHB received");
102            }
103            else
104                break;
105        }
106        Object ret = handback;
107        reset();
108        return ret;
109    }
110
111    public synchronized String waitForNotification() {
112        while(true) {
113            if(!received) {
114                Utils.debug(Utils.DEBUG_STANDARD,
115                    "SimpleListener::waitForNotification wait");
116                try {
117                    wait();
118                } catch(InterruptedException ie) {
119                    // OK : we wait for being interrupted
120                }
121                Utils.debug(Utils.DEBUG_STANDARD,
122                    "SimpleListener::waitForNotification received");
123            }
124            else
125                break;
126        }
127        String ret = type;
128        reset();
129        return ret;
130    }
131}
132