jdk/src/share/classes/javax/management/event/RMIPushEventRelay.java
changeset 4156 acaa49a2768a
parent 4155 460e37d40f12
child 4159 9e3aae7675f1
equal deleted inserted replaced
4155:460e37d40f12 4156:acaa49a2768a
     1 /*
       
     2  * Copyright 2007-2008 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 package javax.management.event;
       
    27 
       
    28 import com.sun.jmx.remote.util.ClassLogger;
       
    29 import java.io.IOException;
       
    30 import java.rmi.NoSuchObjectException;
       
    31 import java.rmi.RemoteException;
       
    32 import java.rmi.server.UnicastRemoteObject;
       
    33 import java.rmi.server.RMIClientSocketFactory;
       
    34 import java.rmi.server.RMIServerSocketFactory;
       
    35 import javax.management.MBeanException;
       
    36 import javax.management.remote.NotificationResult;
       
    37 
       
    38 /**
       
    39  * This class is an implementation of the {@link EventRelay} interface, using
       
    40  * push mode. It exports an RMI object that {@link RMIPushEventForwarder} uses
       
    41  * to forward notifications.
       
    42  *
       
    43  * @since JMX 2.0
       
    44  */
       
    45 public class RMIPushEventRelay implements EventRelay {
       
    46     /**
       
    47      * Constructs a default {@code RMIPushEventRelay} object
       
    48      * and exports its {@linkplain RMIPushServer notification
       
    49      * receiver} on any free port. This constructor is equivalent
       
    50      * to {@link #RMIPushEventRelay(EventClientDelegateMBean,
       
    51      * int, RMIClientSocketFactory, RMIServerSocketFactory, int)
       
    52      * RMIPushEventRelay(delegate, 0, null, null, <em>&lt;default buffer
       
    53      * size&gt;</em>)}.
       
    54      *
       
    55      * @param delegate The {@link EventClientDelegateMBean} proxy to work with.
       
    56      * @throws IOException if failed to communicate with
       
    57      * {@link EventClientDelegateMBean}.
       
    58      * @throws MBeanException if the {@link EventClientDelegateMBean} failed
       
    59      * to create an {@code EventForwarder} for this object.
       
    60      */
       
    61     public RMIPushEventRelay(EventClientDelegateMBean delegate)
       
    62     throws IOException, MBeanException {
       
    63         this(delegate, 0, null,  null, 0);
       
    64     }
       
    65 
       
    66     /**
       
    67      * Constructs a {@code RMIPushEventRelay} object and exports its
       
    68      * {@linkplain RMIPushServer notification receiver} on a specified port.
       
    69      *
       
    70      * @param delegate The {@link EventClientDelegateMBean} proxy to work with.
       
    71      * @param port The port used to export an RMI object to receive notifications
       
    72      * from a server. If the port is zero, an anonymous port is used.
       
    73      * @param csf The client socket factory used to export the RMI object.
       
    74      * Can be null.
       
    75      * @param ssf The server socket factory used to export the RMI object.
       
    76      * Can be null.
       
    77      * @param bufferSize The number of notifications held on the server
       
    78      * while waiting for the previous transmission to complete.  A value of
       
    79      * zero means the default buffer size.
       
    80      *
       
    81      * @throws IOException if failed to communicate with
       
    82      * {@link EventClientDelegateMBean}.
       
    83      * @throws MBeanException if the {@link EventClientDelegateMBean} failed
       
    84      * to create an {@code EventForwarder} for this object.
       
    85      *
       
    86      * @see RMIPushEventForwarder#RMIPushEventForwarder(RMIPushServer, int)
       
    87      */
       
    88     public RMIPushEventRelay(EventClientDelegateMBean delegate,
       
    89             int port,
       
    90             RMIClientSocketFactory csf,
       
    91             RMIServerSocketFactory ssf,
       
    92             int bufferSize)
       
    93             throws IOException, MBeanException {
       
    94 
       
    95         UnicastRemoteObject.exportObject(exportedReceiver, port, csf, ssf);
       
    96 
       
    97         clientId = delegate.addClient(
       
    98                 RMIPushEventForwarder.class.getName(),
       
    99                 new Object[] {exportedReceiver, bufferSize},
       
   100                 new String[] {RMIPushServer.class.getName(),
       
   101                               int.class.getName()});
       
   102     }
       
   103 
       
   104     public String getClientId() {
       
   105         return clientId;
       
   106     }
       
   107 
       
   108     public void setEventReceiver(EventReceiver receiver) {
       
   109         if (logger.traceOn()) {
       
   110             logger.trace("setEventReceiver", ""+receiver);
       
   111         }
       
   112         synchronized(lock) {
       
   113             this.receiver = receiver;
       
   114         }
       
   115     }
       
   116 
       
   117     public void stop() {
       
   118         if (logger.traceOn()) {
       
   119             logger.trace("stop", "");
       
   120         }
       
   121         synchronized(lock) {
       
   122             if (stopped) {
       
   123                 return;
       
   124             } else {
       
   125                 stopped = true;
       
   126             }
       
   127 
       
   128             if (clientId == null) {
       
   129                 return;
       
   130             }
       
   131 
       
   132             try {
       
   133                 UnicastRemoteObject.unexportObject(exportedReceiver, true);
       
   134             } catch (NoSuchObjectException nsoe) {
       
   135                 logger.fine("RMIPushEventRelay.stop", "unexport", nsoe);
       
   136                 // OK: we wanted it unexported, and apparently it already is
       
   137             }
       
   138         }
       
   139     }
       
   140 
       
   141     private volatile String clientId;
       
   142     private volatile EventReceiver receiver;
       
   143 
       
   144     private RMIPushServer exportedReceiver = new RMIPushServer() {
       
   145         public void receive(NotificationResult nr) throws RemoteException {
       
   146             if (logger.traceOn()) {
       
   147                 logger.trace("EventPusherImpl-receive","");
       
   148             }
       
   149             receiver.receive(nr);
       
   150             // Any exception will be sent back to the client.
       
   151         }
       
   152     };
       
   153 
       
   154     private boolean stopped = false;
       
   155 
       
   156     private final int[] lock = new int[0];
       
   157 
       
   158     private static final ClassLogger logger =
       
   159             new ClassLogger("javax.management.event",
       
   160             "PushEventRelay");
       
   161 }