View Javadoc
1   package org.djutils.event.rmi;
2   
3   import java.net.URL;
4   import java.rmi.AccessException;
5   import java.rmi.AlreadyBoundException;
6   import java.rmi.Remote;
7   import java.rmi.RemoteException;
8   import java.rmi.registry.Registry;
9   
10  import org.djutils.event.EventListenerMap;
11  import org.djutils.event.EventProducer;
12  import org.djutils.rmi.RmiObject;
13  
14  /**
15   * The RmiEventProducer provides a remote implementation of the eventProducer using the RMI protocol.
16   * <p>
17   * Copyright (c) 2002-2024 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
18   * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
19   * distributed under a three-clause BSD-style license, which can be found at
20   * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>. This class was
21   * originally part of the DSOL project, see <a href="https://simulation.tudelft.nl/dsol/manual" target="_blank">
22   * https://simulation.tudelft.nl/dsol/manual</a>.
23   * </p>
24   * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
25   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
26   */
27  public class RmiEventProducer implements EventProducer, Remote
28  {
29      /** The default serial version UID for serializable classes. */
30      private static final long serialVersionUID = 20140830L;
31  
32      /** The embedded RmiObject class for the remote firing of events. */
33      private final RmiObject rmiObject;
34      
35      /** the subscriber list. */
36      private final EventListenerMap eventListenerMap;
37  
38      /**
39       * Create a remote event listener and register the listener in the RMI registry. When the RMI registry does not exist yet,
40       * it will be created, but <b>only</b> on the local host. Remote creation of a registry on another computer is not possible.
41       * Any attempt to do so will cause an AccessException to be fired.
42       * @param host String; the host where the RMI registry resides or will be created. Creation is only possible on localhost.
43       * @param port int; the port where the RMI registry can be found or will be created
44       * @param bindingKey String; the key under which this object will be bound in the RMI registry
45       * @throws RemoteException when there is a problem with the RMI registry
46       * @throws AlreadyBoundException when there is already another object bound to the bindingKey
47       * @throws NullPointerException when host, path, or bindingKey is null
48       * @throws IllegalArgumentException when port &lt; 0 or port &gt; 65535
49       * @throws AccessException when there is an attempt to create a registry on a remote host
50       */
51      public RmiEventProducer(final String host, final int port, final String bindingKey)
52              throws RemoteException, AlreadyBoundException
53      {
54          this.rmiObject = new RmiObject(host, port, bindingKey);
55          this.eventListenerMap = new EventListenerMap();
56      }
57  
58      /**
59       * Create a remote event listener and register the listener in the RMI registry. When the host has not been specified in the
60       * URL, 127.0.0.1 will be used. When the port has not been specified in the URL, the default RMI port 1099 will be used.
61       * When the RMI registry does not exist yet, it will be created, but <b>only</b> on the local host. Remote creation of a
62       * registry on another computer is not possible. Any attempt to do so will cause an AccessException to be fired.
63       * @param registryURL URL; the URL of the registry, e.g., "http://localhost:1099" or "http://130.161.185.14:28452"
64       * @param bindingKey String; the key under which this object will be bound in the RMI registry
65       * @throws RemoteException when there is a problem with the RMI registry
66       * @throws AlreadyBoundException when there is already another object bound to the bindingKey
67       * @throws NullPointerException when registryURL or bindingKey is null
68       * @throws AccessException when there is an attempt to create a registry on a remote host
69       */
70      public RmiEventProducer(final URL registryURL, final String bindingKey) throws RemoteException, AlreadyBoundException
71      {
72          this.rmiObject = new RmiObject(registryURL, bindingKey);
73          this.eventListenerMap = new EventListenerMap();
74      }
75  
76      /**
77       * Returns the registry in which this object has been bound, e.g., to look up other objects in the registry.
78       * @return Registry; the registry in which this object has been bound
79       * @throws RemoteException on network error
80       */
81      public Registry getRegistry() throws RemoteException
82      {
83          return this.rmiObject.getRegistry();
84      }
85  
86      @Override
87      public EventListenerMap getEventListenerMap() throws RemoteException
88      {
89          return this.eventListenerMap;
90      }
91  }