aboutsummaryrefslogtreecommitdiff
path: root/src/share/classes/javax/management/event/FetchingEventForwarder.java
blob: 528775f1581bf1b33819078bd362156378404b05 (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
/*
 * Copyright 2007 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */

package javax.management.event;

import com.sun.jmx.event.EventBuffer;
import com.sun.jmx.remote.util.ClassLogger;
import java.io.IOException;
import java.util.List;
import javax.management.Notification;
import javax.management.remote.NotificationResult;
import javax.management.remote.TargetedNotification;

/**
 * This class is used by {@link FetchingEventRelay}. When
 * {@link FetchingEventRelay} calls {@link
 * EventClientDelegateMBean#addClient(String, Object[], String[])} to get a new
 * client identifier, it uses
 * this class name as the first argument to ask {@code EventClientDelegateMBean}
 * to create an object of this class.
 * Then {@code EventClientDelegateMBean} forwards client notifications
 * to this object.
 * When {@link FetchingEventRelay} calls
 * {@link EventClientDelegateMBean#fetchNotifications(String, long, int, long)}
 * to fetch notifications, the {@code EventClientDelegateMBean} will forward
 * the call to this object.
 */
public class FetchingEventForwarder implements EventForwarder {

    /**
     * Construct a new {@code FetchingEventForwarder} with the given
     * buffer size.
     * @param bufferSize the size of the buffer that will store notifications
     * until they have been fetched and acknowledged by the client.
     */
    public FetchingEventForwarder(int bufferSize) {
        if (logger.traceOn()) {
            logger.trace("Constructor", "buffer size is "+bufferSize);
        }

        buffer = new EventBuffer(bufferSize);
        this.bufferSize = bufferSize;
    }

    /**
     * Called by an {@link EventClientDelegateMBean} to forward a user call
     * {@link EventClientDelegateMBean#fetchNotifications(String, long, int, long)}.
     * A call of this method is considered to acknowledge reception of all
     * notifications whose sequence numbers are less the
     * {@code startSequenceNumber}, so all these notifications can be deleted
     * from this object.
     *
     * @param startSequenceNumber The first sequence number to
     * consider.
     * @param timeout The maximum waiting time in milliseconds.
     * If no notifications have arrived after this period of time, the call
     * will return with an empty list of notifications.
     * @param maxNotifs The maximum number of notifications to return.
     */
    public NotificationResult fetchNotifications(long startSequenceNumber,
            int maxNotifs, long timeout) {
        if (logger.traceOn()) {
            logger.trace("fetchNotifications",
                    startSequenceNumber+" "+
                    maxNotifs+" "+
                    timeout);
        }

        return buffer.fetchNotifications(startSequenceNumber,
                    timeout,
                    maxNotifs);
    }

    /**
     * {@inheritDoc}
     * In this implementation, the notification is stored in the local buffer
     * waiting for {@link #fetchNotifications fetchNotifications} to pick
     * it up.
     */
    public void forward(Notification n, Integer listenerId) throws IOException {
        if (logger.traceOn()) {
            logger.trace("forward", n+" "+listenerId);
        }

        buffer.add(new TargetedNotification(n, listenerId));
    }

    public void close() throws IOException {
        if (logger.traceOn()) {
            logger.trace("close", "");
        }

        buffer.close();
    }

    public void setClientId(String clientId) throws IOException {
        if (logger.traceOn()) {
            logger.trace("setClientId", clientId);
        }
        this.clientId = clientId;
    }

    /**
     * Sets a user specific list to save notifications in server side
     * before forwarding to an FetchingEventRelay in client side.
     * <P> This method should be called before any notification is
     * forwarded to this forwader.
     *
     * @param list a user specific list to save notifications
     */
    protected void setList(List<TargetedNotification> list) {
        if (logger.traceOn()) {
            logger.trace("setList", "");
        }

        if (clientId == null) {
            buffer = new EventBuffer(bufferSize, list);
        } else {
            throw new IllegalStateException();
        }
    }

    private EventBuffer buffer;
    private int bufferSize;
    private String clientId;

    private static final ClassLogger logger =
            new ClassLogger("javax.management.event", "FetchingEventForwarder");
}