jdk/src/java.management/share/classes/com/sun/jmx/remote/internal/ClientNotifForwarder.java
author martin
Thu, 30 Oct 2014 07:31:41 -0700
changeset 28059 e576535359cc
parent 25859 3317bb8137f4
child 29273 f32302d0ba47
permissions -rw-r--r--
8067377: My hobby: caning, then then canning, the the can-can Summary: Fix ALL the stutters! Reviewed-by: rriggs, mchung, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 14912
diff changeset
     2
 * Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2624
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2624
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2624
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2624
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2624
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package com.sun.jmx.remote.internal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.NotSerializableException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.concurrent.Executor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
2615
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
    36
import java.security.AccessControlContext;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.security.auth.Subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.management.Notification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.management.NotificationListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.management.NotificationFilter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.management.MBeanServerNotification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.management.InstanceNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.management.ListenerNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.management.remote.NotificationResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.management.remote.TargetedNotification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import com.sun.jmx.remote.util.ClassLogger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import com.sun.jmx.remote.util.EnvHelp;
14912
e6fc057a8011 6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents: 5506
diff changeset
    54
import java.rmi.UnmarshalException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
public abstract class ClientNotifForwarder {
2615
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
    58
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
    59
    private final AccessControlContext acc;
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
    60
24871
224e298c3978 8044865: Fix raw and unchecked lint warnings in management-related code
sjiang
parents: 23706
diff changeset
    61
    public ClientNotifForwarder(Map<String, ?> env) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        this(null, env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private static int threadId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /* An Executor that allows at most one executing and one pending
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
       Runnable.  It uses at most one thread -- as soon as there is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
       no pending Runnable the thread can exit.  Another thread is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
       created as soon as there is a new pending Runnable.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
       Executor is adapted for use in a situation where each Runnable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
       usually schedules up another Runnable.  On return from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
       first one, the second one is immediately executed.  So this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
       just becomes a complicated way to write a while loop, but with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
       the advantage that you can replace it with another Executor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
       for instance one that you are using to execute a bunch of other
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
       unrelated work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
       You might expect that a java.util.concurrent.ThreadPoolExecutor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
       with corePoolSize=0 and maximumPoolSize=1 would have the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
       behavior, but it does not.  A ThreadPoolExecutor only creates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
       a new thread when a new task is submitted and the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
       existing threads is < corePoolSize.  This can never happen when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
       corePoolSize=0, so new threads are never created.  Surprising,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
       but there you are.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private static class LinearExecutor implements Executor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        public synchronized void execute(Runnable command) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            if (this.command != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                throw new IllegalArgumentException("More than one command");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            this.command = command;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            if (thread == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                thread = new Thread() {
2615
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
    94
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
    95
                    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                            Runnable r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                            synchronized (LinearExecutor.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                                if (LinearExecutor.this.command == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                                    thread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                                    r = LinearExecutor.this.command;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                                    LinearExecutor.this.command = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                            r.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                thread.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                thread.setName("ClientNotifForwarder-" + ++threadId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                thread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        private Runnable command;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        private Thread thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1243
diff changeset
   122
    public ClientNotifForwarder(ClassLoader defaultClassLoader, Map<String, ?> env) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        maxNotifications = EnvHelp.getMaxFetchNotifNumber(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        timeout = EnvHelp.getFetchTimeout(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        /* You can supply an Executor in which the remote call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
           fetchNotifications will be made.  The Executor's execute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
           method reschedules another task, so you must not use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
           an Executor that executes tasks in the caller's thread.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        Executor ex = (Executor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            env.get("jmx.remote.x.fetch.notifications.executor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (ex == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            ex = new LinearExecutor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        else if (logger.traceOn())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            logger.trace("ClientNotifForwarder", "executor is " + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        this.defaultClassLoader = defaultClassLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        this.executor = ex;
2615
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   139
        this.acc = AccessController.getContext();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
   143
     * Called to fetch notifications from a server.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    abstract protected NotificationResult fetchNotifs(long clientSequenceNumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                                                      int maxNotifications,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                                                      long timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            throws IOException, ClassNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    abstract protected Integer addListenerForMBeanRemovedNotif()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        throws IOException, InstanceNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    abstract protected void removeListenerForMBeanRemovedNotif(Integer id)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        throws IOException, InstanceNotFoundException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
               ListenerNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Used to send out a notification about lost notifs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    abstract protected void lostNotifs(String message, long number);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public synchronized void addNotificationListener(Integer listenerID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                                        ObjectName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                                        NotificationListener listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                                        NotificationFilter filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                                        Object handback,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                                        Subject delegationSubject)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            throws IOException, InstanceNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (logger.traceOn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            logger.trace("addNotificationListener",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                         "Add the listener "+listener+" at "+name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        infoList.put(listenerID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                     new ClientListenerInfo(listenerID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                                            name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                                            listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                                            filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                                            handback,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                                            delegationSubject));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        init(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    public synchronized Integer[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        removeNotificationListener(ObjectName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                                   NotificationListener listener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        throws ListenerNotFoundException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        beforeRemove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (logger.traceOn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            logger.trace("removeNotificationListener",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                         "Remove the listener "+listener+" from "+name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        List<Integer> ids = new ArrayList<Integer>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        List<ClientListenerInfo> values =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                new ArrayList<ClientListenerInfo>(infoList.values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        for (int i=values.size()-1; i>=0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            ClientListenerInfo li = values.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            if (li.sameAs(name, listener)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                ids.add(li.getListenerID());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                infoList.remove(li.getListenerID());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (ids.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            throw new ListenerNotFoundException("Listener not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        return ids.toArray(new Integer[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public synchronized Integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        removeNotificationListener(ObjectName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                                   NotificationListener listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                                   NotificationFilter filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                                   Object handback)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            throws ListenerNotFoundException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        if (logger.traceOn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            logger.trace("removeNotificationListener",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                         "Remove the listener "+listener+" from "+name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        beforeRemove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        Integer id = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        List<ClientListenerInfo> values =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                new ArrayList<ClientListenerInfo>(infoList.values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        for (int i=values.size()-1; i>=0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            ClientListenerInfo li = values.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            if (li.sameAs(name, listener, filter, handback)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                id=li.getListenerID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                infoList.remove(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        if (id == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            throw new ListenerNotFoundException("Listener not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        return id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    public synchronized Integer[] removeNotificationListener(ObjectName name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if (logger.traceOn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            logger.trace("removeNotificationListener",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                         "Remove all listeners registered at "+name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        List<Integer> ids = new ArrayList<Integer>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        List<ClientListenerInfo> values =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                new ArrayList<ClientListenerInfo>(infoList.values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        for (int i=values.size()-1; i>=0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            ClientListenerInfo li = values.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            if (li.sameAs(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                ids.add(li.getListenerID());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                infoList.remove(li.getListenerID());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        return ids.toArray(new Integer[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * Called when a connector is doing reconnection. Like <code>postReconnection</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * this method is intended to be called only by a client connector:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * <code>RMIConnector</code> and <code>ClientIntermediary</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * Call this method will set the flag beingReconnection to <code>true</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * and the thread used to fetch notifis will be stopped, a new thread can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * created only after the method <code>postReconnection</code> is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * It is caller's responsiblity to not re-call this method before calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * <code>postReconnection</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    public synchronized ClientListenerInfo[] preReconnection() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        if (state == TERMINATED || beingReconnected) { // should never
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            throw new IOException("Illegal state.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        final ClientListenerInfo[] tmp =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            infoList.values().toArray(new ClientListenerInfo[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        beingReconnected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        infoList.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        return tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * Called after reconnection is finished.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * This method is intended to be called only by a client connector:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * <code>RMIConnector</code> and <code>ClientIntermediary</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    public synchronized void postReconnection(ClientListenerInfo[] listenerInfos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        if (state == TERMINATED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        while (state == STOPPING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            } catch (InterruptedException ire) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                IOException ioe = new IOException(ire.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                EnvHelp.initCause(ioe, ire);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        final boolean trace = logger.traceOn();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        final int len   = listenerInfos.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        for (int i=0; i<len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            if (trace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                logger.trace("addNotificationListeners",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                             "Add a listener at "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                             listenerInfos[i].getListenerID());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            infoList.put(listenerInfos[i].getListenerID(), listenerInfos[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        beingReconnected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
1243
f5d91fb6df32 6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents: 1004
diff changeset
   341
        if (currentFetchThread == Thread.currentThread() ||
f5d91fb6df32 6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents: 1004
diff changeset
   342
              state == STARTING || state == STARTED) { // doing or waiting reconnection
f5d91fb6df32 6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents: 1004
diff changeset
   343
              // only update mbeanRemovedNotifID
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                mbeanRemovedNotifID = addListenerForMBeanRemovedNotif();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    "Failed to register a listener to the mbean " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    "server: the client will not do clean when an MBean " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    "is unregistered";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                if (logger.traceOn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    logger.trace("init", msg, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            }
1243
f5d91fb6df32 6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents: 1004
diff changeset
   355
        } else {
f5d91fb6df32 6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents: 1004
diff changeset
   356
              while (state == STOPPING) {
f5d91fb6df32 6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents: 1004
diff changeset
   357
                  try {
f5d91fb6df32 6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents: 1004
diff changeset
   358
                      wait();
f5d91fb6df32 6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents: 1004
diff changeset
   359
                  } catch (InterruptedException ire) {
f5d91fb6df32 6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents: 1004
diff changeset
   360
                      IOException ioe = new IOException(ire.toString());
f5d91fb6df32 6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents: 1004
diff changeset
   361
                      EnvHelp.initCause(ioe, ire);
f5d91fb6df32 6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents: 1004
diff changeset
   362
                      throw ioe;
f5d91fb6df32 6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents: 1004
diff changeset
   363
                  }
f5d91fb6df32 6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents: 1004
diff changeset
   364
              }
f5d91fb6df32 6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents: 1004
diff changeset
   365
f5d91fb6df32 6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents: 1004
diff changeset
   366
              if (listenerInfos.length > 0) { // old listeners are re-added
f5d91fb6df32 6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents: 1004
diff changeset
   367
                  init(true); // not update clientSequenceNumber
f5d91fb6df32 6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents: 1004
diff changeset
   368
              } else if (infoList.size() > 0) { // only new listeners added during reconnection
f5d91fb6df32 6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents: 1004
diff changeset
   369
                  init(false); // need update clientSequenceNumber
f5d91fb6df32 6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents: 1004
diff changeset
   370
              }
f5d91fb6df32 6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents: 1004
diff changeset
   371
          }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    public synchronized void terminate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        if (state == TERMINATED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        if (logger.traceOn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            logger.trace("terminate", "Terminating...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        if (state == STARTED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
           infoList.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        setState(TERMINATED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
2615
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   390
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   391
    // -------------------------------------------------
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   392
    // private classes
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   393
    // -------------------------------------------------
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    //
2615
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   395
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    private class NotifFetcher implements Runnable {
2615
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   397
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   398
        private volatile boolean alreadyLogged = false;
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   399
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   400
        private void logOnce(String msg, SecurityException x) {
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   401
            if (alreadyLogged) return;
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   402
            // Log only once.
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   403
            logger.config("setContextClassLoader",msg);
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   404
            if (x != null) logger.fine("setContextClassLoader", x);
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   405
            alreadyLogged = true;
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   406
        }
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   407
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   408
        // Set new context class loader, returns previous one.
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   409
        private final ClassLoader setContextClassLoader(final ClassLoader loader) {
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   410
            final AccessControlContext ctxt = ClientNotifForwarder.this.acc;
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   411
            // if ctxt is null, log a config message and throw a
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   412
            // SecurityException.
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   413
            if (ctxt == null) {
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   414
                logOnce("AccessControlContext must not be null.",null);
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   415
                throw new SecurityException("AccessControlContext must not be null");
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   416
            }
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   417
            return AccessController.doPrivileged(
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   418
                new PrivilegedAction<ClassLoader>() {
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   419
                    public ClassLoader run() {
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   420
                        try {
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   421
                            // get context class loader - may throw
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   422
                            // SecurityException - though unlikely.
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   423
                            final ClassLoader previous =
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   424
                                Thread.currentThread().getContextClassLoader();
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   425
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   426
                            // if nothing needs to be done, break here...
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   427
                            if (loader == previous) return previous;
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   428
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   429
                            // reset context class loader - may throw
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   430
                            // SecurityException
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   431
                            Thread.currentThread().setContextClassLoader(loader);
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   432
                            return previous;
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   433
                        } catch (SecurityException x) {
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   434
                            logOnce("Permission to set ContextClassLoader missing. " +
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   435
                                    "Notifications will not be dispatched. " +
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   436
                                    "Please check your Java policy configuration: " +
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   437
                                    x, x);
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   438
                            throw x;
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   439
                        }
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   440
                    }
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   441
                }, ctxt);
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   442
        }
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   443
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        public void run() {
2615
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   445
            final ClassLoader previous;
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   446
            if (defaultClassLoader != null) {
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   447
                previous = setContextClassLoader(defaultClassLoader);
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   448
            } else {
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   449
                previous = null;
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   450
            }
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   451
            try {
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   452
                doRun();
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   453
            } finally {
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   454
                if (defaultClassLoader != null) {
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   455
                    setContextClassLoader(previous);
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   456
                }
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   457
            }
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   458
        }
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   459
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   460
        private void doRun() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            synchronized (ClientNotifForwarder.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                currentFetchThread = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
2615
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   464
                if (state == STARTING) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                    setState(STARTED);
2615
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   466
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            NotificationResult nr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            if (!shouldStop() && (nr = fetchNotifs()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                // nr == null means got exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                final TargetedNotification[] notifs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                    nr.getTargetedNotifications();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                final int len = notifs.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                final Map<Integer, ClientListenerInfo> listeners;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                final Integer myListenerID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                long missed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                synchronized(ClientNotifForwarder.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                    // check sequence number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                    if (clientSequenceNumber >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                        missed = nr.getEarliestSequenceNumber() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                            clientSequenceNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                    clientSequenceNumber = nr.getNextSequenceNumber();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                    listeners = new HashMap<Integer, ClientListenerInfo>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                    for (int i = 0 ; i < len ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                        final TargetedNotification tn = notifs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                        final Integer listenerID = tn.getListenerID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                        // check if an mbean unregistration notif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                        if (!listenerID.equals(mbeanRemovedNotifID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                            final ClientListenerInfo li = infoList.get(listenerID);
2615
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   501
                            if (li != null) {
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   502
                                listeners.put(listenerID, li);
be2f497eb537 6691246: Thread context class loader can be set using JMX remote ClientNotifForwarded
dfuchs
parents: 526
diff changeset
   503
                            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                        final Notification notif = tn.getNotification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                        final String unreg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                            MBeanServerNotification.UNREGISTRATION_NOTIFICATION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                        if (notif instanceof MBeanServerNotification &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                            notif.getType().equals(unreg)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                            MBeanServerNotification mbsn =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                                (MBeanServerNotification) notif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                            ObjectName name = mbsn.getMBeanName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                            removeNotificationListener(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                    myListenerID = mbeanRemovedNotifID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                if (missed > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                    final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                        "May have lost up to " + missed +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                        " notification" + (missed == 1 ? "" : "s");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                    lostNotifs(msg, missed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                    logger.trace("NotifFetcher.run", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                // forward
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                for (int i = 0 ; i < len ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                    final TargetedNotification tn = notifs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                    dispatchNotification(tn,myListenerID,listeners);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            synchronized (ClientNotifForwarder.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                currentFetchThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            if (nr == null || shouldStop()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                // tell that the thread is REALLY stopped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                setState(STOPPED);
1243
f5d91fb6df32 6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents: 1004
diff changeset
   544
f5d91fb6df32 6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents: 1004
diff changeset
   545
                try {
f5d91fb6df32 6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents: 1004
diff changeset
   546
                      removeListenerForMBeanRemovedNotif(mbeanRemovedNotifID);
f5d91fb6df32 6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents: 1004
diff changeset
   547
                } catch (Exception e) {
f5d91fb6df32 6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents: 1004
diff changeset
   548
                    if (logger.traceOn()) {
f5d91fb6df32 6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents: 1004
diff changeset
   549
                        logger.trace("NotifFetcher-run",
f5d91fb6df32 6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents: 1004
diff changeset
   550
                                "removeListenerForMBeanRemovedNotif", e);
f5d91fb6df32 6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents: 1004
diff changeset
   551
                    }
f5d91fb6df32 6697180: JMX query results in java.io.IOException: Illegal state - also a deadlock can also be seen
sjiang
parents: 1004
diff changeset
   552
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                executor.execute(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        void dispatchNotification(TargetedNotification tn,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                                  Integer myListenerID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                                  Map<Integer, ClientListenerInfo> listeners) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            final Notification notif = tn.getNotification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            final Integer listenerID = tn.getListenerID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            if (listenerID.equals(myListenerID)) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            final ClientListenerInfo li = listeners.get(listenerID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            if (li == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                logger.trace("NotifFetcher.dispatch",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                             "Listener ID not in map");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            NotificationListener l = li.getListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            Object h = li.getHandback();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                l.handleNotification(notif, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                    "Failed to forward a notification " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                    "to a listener";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                logger.trace("NotifFetcher-run", msg, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        private NotificationResult fetchNotifs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                NotificationResult nr = ClientNotifForwarder.this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                    fetchNotifs(clientSequenceNumber,maxNotifications,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                                timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                if (logger.traceOn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                    logger.trace("NotifFetcher-run",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                                 "Got notifications from the server: "+nr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                return nr;
14912
e6fc057a8011 6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents: 5506
diff changeset
   598
            } catch (ClassNotFoundException | NotSerializableException | UnmarshalException e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                logger.trace("NotifFetcher.fetchNotifs", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                return fetchOneNotif();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                if (!shouldStop()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                    logger.error("NotifFetcher-run",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                                 "Failed to fetch notification, " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                                 "stopping thread. Error is: " + ioe, ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                    logger.debug("NotifFetcher-run",ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                // no more fetching
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        /* Fetch one notification when we suspect that it might be a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
           notification that we can't deserialize (because of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
           missing class).  First we ask for 0 notifications with 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
           timeout.  This allows us to skip sequence numbers for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
           notifications that don't match our filters.  Then we ask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
           for one notification.  If that produces a
14912
e6fc057a8011 6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents: 5506
diff changeset
   620
           ClassNotFoundException, NotSerializableException or
e6fc057a8011 6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents: 5506
diff changeset
   621
           UnmarshalException, we increase our sequence number and ask again.
e6fc057a8011 6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents: 5506
diff changeset
   622
           Eventually we will either get a successful notification, or a
e6fc057a8011 6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents: 5506
diff changeset
   623
           return with 0 notifications.  In either case we can return a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
           NotificationResult.  This algorithm works (albeit less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
           well) even if the server implementation doesn't optimize a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
           request for 0 notifications to skip sequence numbers for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
           notifications that don't match our filters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
14912
e6fc057a8011 6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents: 5506
diff changeset
   629
           If we had at least one
e6fc057a8011 6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents: 5506
diff changeset
   630
           ClassNotFoundException/NotSerializableException/UnmarshalException,
e6fc057a8011 6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents: 5506
diff changeset
   631
           then we must emit a JMXConnectionNotification.LOST_NOTIFS.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        private NotificationResult fetchOneNotif() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            ClientNotifForwarder cnf = ClientNotifForwarder.this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            long startSequenceNumber = clientSequenceNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            int notFoundCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            NotificationResult result = null;
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 715
diff changeset
   641
            long firstEarliest = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            while (result == null && !shouldStop()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                NotificationResult nr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                    // 0 notifs to update startSequenceNumber
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                    nr = cnf.fetchNotifs(startSequenceNumber, 0, 0L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                    logger.warning("NotifFetcher.fetchOneNotif",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                                   "Impossible exception: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                    logger.debug("NotifFetcher.fetchOneNotif",e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                    if (!shouldStop())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                        logger.trace("NotifFetcher.fetchOneNotif", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                if (shouldStop())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                startSequenceNumber = nr.getNextSequenceNumber();
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 715
diff changeset
   664
                if (firstEarliest < 0)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 715
diff changeset
   665
                    firstEarliest = nr.getEarliestSequenceNumber();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                    // 1 notif to skip possible missing class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                    result = cnf.fetchNotifs(startSequenceNumber, 1, 0L);
14912
e6fc057a8011 6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents: 5506
diff changeset
   670
                } catch (ClassNotFoundException | NotSerializableException | UnmarshalException e) {
e6fc057a8011 6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents: 5506
diff changeset
   671
                    logger.warning("NotifFetcher.fetchOneNotif",
e6fc057a8011 6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents: 5506
diff changeset
   672
                                   "Failed to deserialize a notification: "+e.toString());
e6fc057a8011 6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents: 5506
diff changeset
   673
                    if (logger.traceOn()) {
e6fc057a8011 6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents: 5506
diff changeset
   674
                        logger.trace("NotifFetcher.fetchOneNotif",
e6fc057a8011 6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents: 5506
diff changeset
   675
                                     "Failed to deserialize a notification.", e);
e6fc057a8011 6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents: 5506
diff changeset
   676
                    }
e6fc057a8011 6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents: 5506
diff changeset
   677
e6fc057a8011 6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents: 5506
diff changeset
   678
                    notFoundCount++;
e6fc057a8011 6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents: 5506
diff changeset
   679
                    startSequenceNumber++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                } catch (Exception e) {
14912
e6fc057a8011 6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents: 5506
diff changeset
   681
                    if (!shouldStop())
e6fc057a8011 6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents: 5506
diff changeset
   682
                        logger.trace("NotifFetcher.fetchOneNotif", e);
e6fc057a8011 6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents: 5506
diff changeset
   683
                    return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            if (notFoundCount > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                    "Dropped " + notFoundCount + " notification" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                    (notFoundCount == 1 ? "" : "s") +
14912
e6fc057a8011 6937053: RMI unmarshalling errors in ClientNotifForwarder cause silent failure
dsamersoff
parents: 5506
diff changeset
   691
                    " because classes were missing locally or incompatible";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                lostNotifs(msg, notFoundCount);
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 715
diff changeset
   693
                // Even if result.getEarliestSequenceNumber() is now greater than
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 715
diff changeset
   694
                // it was initially, meaning some notifs have been dropped
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 715
diff changeset
   695
                // from the buffer, we don't want the caller to see that
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 715
diff changeset
   696
                // because it is then likely to renotify about the lost notifs.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 715
diff changeset
   697
                // So we put back the first value of earliestSequenceNumber
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 715
diff changeset
   698
                // that we saw.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 715
diff changeset
   699
                if (result != null) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 715
diff changeset
   700
                    result = new NotificationResult(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 715
diff changeset
   701
                            firstEarliest, result.getNextSequenceNumber(),
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 715
diff changeset
   702
                            result.getTargetedNotifications());
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 715
diff changeset
   703
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        private boolean shouldStop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            synchronized (ClientNotifForwarder.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                if (state != STARTED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                } else if (infoList.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                    // no more listener, stop fetching
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                    setState(STOPPING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
// -------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
// private methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
// -------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    private synchronized void setState(int newState) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        if (state == TERMINATED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        state = newState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        this.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * Called to decide whether need to start a thread for fetching notifs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * <P>The parameter reconnected will decide whether to initilize the clientSequenceNumber,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * initilaizing the clientSequenceNumber means to ignore all notifications arrived before.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * If it is reconnected, we will not initialize in order to get all notifications arrived
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * during the reconnection. It may cause the newly registered listeners to receive some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * notifications arrived before its registray.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    private synchronized void init(boolean reconnected) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        switch (state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        case STARTED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        case STARTING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        case TERMINATED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            throw new IOException("The ClientNotifForwarder has been terminated.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        case STOPPING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            if (beingReconnected == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                // wait for another thread to do, which is doing reconnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            while (state == STOPPING) { // make sure only one fetching thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                    wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                } catch (InterruptedException ire) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                    IOException ioe = new IOException(ire.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                    EnvHelp.initCause(ioe, ire);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                    throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            // re-call this method to check the state again,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            // the state can be other value like TERMINATED.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            init(reconnected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        case STOPPED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            if (beingReconnected == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                // wait for another thread to do, which is doing reconnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            if (logger.traceOn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                logger.trace("init", "Initializing...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            // init the clientSequenceNumber if not reconnected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            if (!reconnected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                    NotificationResult nr = fetchNotifs(-1, 0, 0);
23706
5ea287e13cdb 8038940: c.s.j.r.i.ClientNotifForwarder$LinearExecutor
sjiang
parents: 23010
diff changeset
   790
5ea287e13cdb 8038940: c.s.j.r.i.ClientNotifForwarder$LinearExecutor
sjiang
parents: 23010
diff changeset
   791
                    if (state != STOPPED) { // JDK-8038940
5ea287e13cdb 8038940: c.s.j.r.i.ClientNotifForwarder$LinearExecutor
sjiang
parents: 23010
diff changeset
   792
                                            // reconnection must happen during
5ea287e13cdb 8038940: c.s.j.r.i.ClientNotifForwarder$LinearExecutor
sjiang
parents: 23010
diff changeset
   793
                                            // fetchNotifs(-1, 0, 0), and a new
5ea287e13cdb 8038940: c.s.j.r.i.ClientNotifForwarder$LinearExecutor
sjiang
parents: 23010
diff changeset
   794
                                            // thread takes over the fetching job
5ea287e13cdb 8038940: c.s.j.r.i.ClientNotifForwarder$LinearExecutor
sjiang
parents: 23010
diff changeset
   795
                        return;
5ea287e13cdb 8038940: c.s.j.r.i.ClientNotifForwarder$LinearExecutor
sjiang
parents: 23010
diff changeset
   796
                    }
5ea287e13cdb 8038940: c.s.j.r.i.ClientNotifForwarder$LinearExecutor
sjiang
parents: 23010
diff changeset
   797
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                    clientSequenceNumber = nr.getNextSequenceNumber();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                    // can't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                    logger.warning("init", "Impossible exception: "+ e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                    logger.debug("init",e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            // for cleaning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                mbeanRemovedNotifID = addListenerForMBeanRemovedNotif();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                    "Failed to register a listener to the mbean " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                    "server: the client will not do clean when an MBean " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                    "is unregistered";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                if (logger.traceOn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                    logger.trace("init", msg, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            setState(STARTING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            // start fetching
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            executor.execute(new NotifFetcher());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            // should not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            throw new IOException("Unknown state.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * Import: should not remove a listener during reconnection, the reconnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * needs to change the listener list and that will possibly make removal fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    private synchronized void beforeRemove() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        while (beingReconnected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            if (state == TERMINATED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                throw new IOException("Terminated.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            } catch (InterruptedException ire) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                IOException ioe = new IOException(ire.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                EnvHelp.initCause(ioe, ire);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        if (state == TERMINATED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            throw new IOException("Terminated.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
// -------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
// private variables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
// -------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    private final ClassLoader defaultClassLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    private final Executor executor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    private final Map<Integer, ClientListenerInfo> infoList =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            new HashMap<Integer, ClientListenerInfo>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    // notif stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    private long clientSequenceNumber = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    private final int maxNotifications;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    private final long timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    private Integer mbeanRemovedNotifID = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    private Thread currentFetchThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
    // state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * This state means that a thread is being created for fetching and forwarding notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    private static final int STARTING = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * This state tells that a thread has been started for fetching and forwarding notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    private static final int STARTED = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * This state means that the fetching thread is informed to stop.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    private static final int STOPPING = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * This state means that the fetching thread is already stopped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    private static final int STOPPED = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * This state means that this object is terminated and no more thread will be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * for fetching notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    private static final int TERMINATED = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
    private int state = STOPPED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     * This variable is used to tell whether a connector (RMIConnector or ClientIntermediary)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * is doing reconnection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * This variable will be set to true by the method <code>preReconnection</code>, and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * to false by <code>postReconnection</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * When beingReconnected == true, no thread will be created for fetching notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
    private boolean beingReconnected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    private static final ClassLogger logger =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        new ClassLogger("javax.management.remote.misc",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                        "ClientNotifForwarder");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
}