RmiObject.java

  1. package org.djutils.rmi;

  2. import java.net.URL;
  3. import java.rmi.AccessException;
  4. import java.rmi.AlreadyBoundException;
  5. import java.rmi.RemoteException;
  6. import java.rmi.registry.Registry;
  7. import java.rmi.server.UnicastRemoteObject;

  8. import org.djutils.exceptions.Throw;

  9. /**
  10.  * The RMIObject is an object that registers iteself in the RMI registry using a key by which it can be found. The class creates
  11.  * the RMI registry when it does not exist yet.
  12.  * <p>
  13.  * Copyright (c) 2020-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  14.  * BSD-style license. See <a href="https://djutils.org/docs/license.html">DJUTILS License</a>.
  15.  * </p>
  16.  * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a>
  17.  */
  18. public class RmiObject extends UnicastRemoteObject
  19. {
  20.     /** */
  21.     private static final long serialVersionUID = 20200111L;

  22.     /** pointer to the registry in which the object has been registered to look up other objects. */
  23.     private Registry registry;

  24.     /**
  25.      * Register this object in the RMI registry. When the host has not been specified in the URL, 127.0.0.1 will be used. When
  26.      * the port has not been specified in the URL, the default RMI port 1099 will be used. When the RMI registry does not exist
  27.      * yet, it will be created, but <b>only</b> on the local host. Remote creation of a registry on another computer is not
  28.      * possible. Any attempt to do so will cause an AccessException to be fired.
  29.      * @param registryURL URL; the URL of the registry, e.g., "http://localhost:1099" or "http://130.161.185.14:28452"
  30.      * @param bindingKey String; the key under which this object will be bound in the RMI registry
  31.      * @throws RemoteException when there is a problem with the RMI registry
  32.      * @throws AlreadyBoundException when there is already another object bound to the bindingKey
  33.      * @throws NullPointerException when registryURL or bindingKey is null
  34.      * @throws AccessException when there is an attempt to create a registry on a remote host
  35.      */
  36.     public RmiObject(final URL registryURL, final String bindingKey) throws RemoteException, AlreadyBoundException
  37.     {
  38.         Throw.whenNull(registryURL, "registryURL cannot be null");
  39.         Throw.whenNull(bindingKey, "bindingKey cannot be null");
  40.         String host = registryURL.getHost() == null ? "127.0.0.1" : registryURL.getHost();
  41.         int port = registryURL.getPort() == -1 ? 1099 : registryURL.getPort();
  42.         register(host, port, bindingKey);
  43.     }

  44.     /**
  45.      * Register this object in the RMI registry. When the RMI registry does not exist yet, it will be created, but <b>only</b>
  46.      * on the local host. Remote creation of a registry on another computer is not possible. Any attempt to do so will cause an
  47.      * AccessException to be fired.
  48.      * @param host String; the host where the RMI registry resides or will be created. Creation is only possible on localhost.
  49.      * @param port int; the port where the RMI registry can be found or will be created
  50.      * @param bindingKey String; the key under which this object will be bound in the RMI registry
  51.      * @throws RemoteException when there is a problem with the RMI registry
  52.      * @throws AlreadyBoundException when there is already another object bound to the bindingKey
  53.      * @throws NullPointerException when host, path, or bindingKey is null
  54.      * @throws IllegalArgumentException when port &lt; 0 or port &gt; 65535
  55.      * @throws AccessException when there is an attempt to create a registry on a remote host
  56.      */
  57.     public RmiObject(final String host, final int port, final String bindingKey) throws RemoteException, AlreadyBoundException
  58.     {
  59.         register(host, port, bindingKey);
  60.     }

  61.     /**
  62.      * Register this object in the RMI registry. When the RMI registry does not exist yet, it will be created, but <b>only</b>
  63.      * on the local host. Remote creation of a registry on another computer is not possible. Any attempt to do so will cause an
  64.      * AccessException to be fired.
  65.      * @param host String; the host where the RMI registry resides or will be created. Creation is only possible on localhost.
  66.      * @param port int; the port where the RMI registry can be found or will be created
  67.      * @param bindingKey String; the key under which this object will be bound in the RMI registry
  68.      * @throws RemoteException when there is a problem with the RMI registry
  69.      * @throws AlreadyBoundException when there is already another object bound to the bindingKey
  70.      * @throws AccessException when there is an attempt to create a registry on a remote host
  71.      */
  72.     protected void register(final String host, final int port, final String bindingKey)
  73.             throws RemoteException, AlreadyBoundException
  74.     {
  75.         Throw.whenNull(bindingKey, "bindingKey cannot be null");
  76.         this.registry = RmiRegistry.getRegistry(host, port);
  77.         RmiRegistry.bind(this.registry, bindingKey, this);
  78.     }

  79.     /**
  80.      * Returns the registry in which this object has been bound, e.g., to look up other objects in the registry.
  81.      * @return Registry; the registry in which this object has been bound
  82.      * @throws RemoteException on network error
  83.      */
  84.     public Registry getRegistry() throws RemoteException
  85.     {
  86.         return this.registry;
  87.     }

  88. }