* Copyright (c) 2002-2021 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
* for project information https://simulation.tudelft.nl. The DSOL
* project is distributed under a three-clause BSD-style license, which can be found at
*
* https://simulation.tudelft.nl/dsol/3.0/license.html.
*
* @author Peter Jacobs
* @author Alexander Verbraeck
*/
public class RemoteContext extends RMIObject implements RemoteContextInterface
{
/** The default serial version UID for serializable classes. */
private static final long serialVersionUID = 1L;
/** The underlying event context. */
protected ContextInterface embeddedContext = null;
/** The (remote) event producer for this context. */
protected RemoteChangeEventProducer remoteEventProducer;
/**
* Constructs a new RemoteContext. Register the new context in the RMI registry. When the RMI registry does not exist yet,
* it will be created, but only on the local host. Remote creation of a registry on another computer is not possible.
* Any attempt to do so will cause an AccessException to be fired.
* @param host String; the host where the RMI registry resides or will be created. Creation is only possible on localhost.
* @param port int; the port where the RMI registry can be found or will be created
* @param bindingKey the key under which this context will be bound in the RMI registry
* @param embeddedContext ContextInterface; the underlying context
* @param eventProducerBindingKey String; the key under which the event producer will be bound
* @throws RemoteException when there is a problem with the RMI registry
* @throws AlreadyBoundException when there is already another object bound to the bindingKey
* @throws NullPointerException when host, path, or bindingKey is null
* @throws IllegalArgumentException when port < 0 or port > 65535
* @throws AccessException when there is an attempt to create a registry on a remote host
*/
public RemoteContext(final String host, final int port, final String bindingKey, final ContextInterface embeddedContext,
final String eventProducerBindingKey) throws RemoteException, AlreadyBoundException
{
super(host, port, bindingKey);
Throw.whenNull(embeddedContext, "embedded context cannot be null");
this.embeddedContext = embeddedContext;
this.remoteEventProducer = new RemoteChangeEventProducer(host, port, eventProducerBindingKey);
}
/**
* Constructs a new RemoteContext. Register the new context in the RMI registry. When the host has not been specified in the
* 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.
* When the RMI registry does not exist yet, it will be created, but only on the local host. Remote creation of a
* registry on another computer is not possible. Any attempt to do so will cause an AccessException to be fired.
* @param registryURL URL; the URL of the registry, e.g., "http://localhost:1099" or "http://130.161.185.14:28452"
* @param embeddedContext ContextInterface; the underlying context
* @param eventProducerBindingKey String; the key under which the event producer will be bound
* @throws RemoteException when there is a problem with the RMI registry
* @throws AlreadyBoundException when there is already another object bound to the bindingKey
* @throws NullPointerException when registryURL, bindingKey, or embeddedContext is null
* @throws AccessException when there is an attempt to create a registry on a remote host
*/
public RemoteContext(final URL registryURL, final ContextInterface embeddedContext, final String eventProducerBindingKey)
throws RemoteException, AlreadyBoundException
{
super(registryURL, registryURL.getPath());
Throw.whenNull(embeddedContext, "embedded context cannot be null");
this.embeddedContext = embeddedContext;
String host = registryURL.getHost() == null ? "127.0.0.1" : registryURL.getHost();
int port = registryURL.getPort() == -1 ? 1099 : registryURL.getPort();
this.remoteEventProducer = new RemoteChangeEventProducer(host, port, eventProducerBindingKey);
}
/** {@inheritDoc} */
@Override
public Serializable getSourceId() throws RemoteException
{
return getAbsolutePath();
}
/** {@inheritDoc} */
@Override
public String getAtomicName() throws RemoteException
{
return this.embeddedContext.getAtomicName();
}
/** {@inheritDoc} */
@Override
public ContextInterface getParent() throws RemoteException
{
return this.embeddedContext.getParent();
}
/** {@inheritDoc} */
@Override
public ContextInterface getRootContext() throws RemoteException
{
return this.embeddedContext.getRootContext();
}
/** {@inheritDoc} */
@Override
public String getAbsolutePath() throws RemoteException
{
return this.embeddedContext.getAbsolutePath();
}
/** {@inheritDoc} */
@Override
public Object get(final String name) throws NamingException, RemoteException
{
return this.embeddedContext.get(name);
}
/** {@inheritDoc} */
@Override
public Object getObject(final String key) throws NamingException, RemoteException
{
return this.embeddedContext.getObject(key);
}
/** {@inheritDoc} */
@Override
public boolean exists(final String name) throws NamingException, RemoteException
{
return this.embeddedContext.exists(name);
}
/** {@inheritDoc} */
@Override
public boolean hasKey(final String key) throws NamingException, RemoteException
{
return this.embeddedContext.hasKey(key);
}
/** {@inheritDoc} */
@Override
public boolean hasObject(final Object object) throws RemoteException
{
return this.embeddedContext.hasObject(object);
}
/** {@inheritDoc} */
@Override
public boolean isEmpty() throws RemoteException
{
return this.embeddedContext.isEmpty();
}
/** {@inheritDoc} */
@Override
public void bind(final String name, final Object object) throws NamingException, RemoteException
{
this.embeddedContext.bind(name, object);
}
/** {@inheritDoc} */
@Override
public void bindObject(final String key, final Object object) throws NamingException, RemoteException
{
this.embeddedContext.bindObject(key, object);
}
/** {@inheritDoc} */
@Override
public void bindObject(final Object object) throws NamingException, RemoteException
{
this.embeddedContext.bindObject(object);
}
/** {@inheritDoc} */
@Override
public void unbind(final String name) throws NamingException, RemoteException
{
this.embeddedContext.unbind(name);
}
/** {@inheritDoc} */
@Override
public void unbindObject(final String key) throws NamingException, RemoteException
{
this.embeddedContext.unbindObject(key);
}
/** {@inheritDoc} */
@Override
public void rebind(final String name, final Object object) throws NamingException, RemoteException
{
this.embeddedContext.rebind(name, object);
}
/** {@inheritDoc} */
@Override
public void rebindObject(final String key, final Object object) throws NamingException, RemoteException
{
this.embeddedContext.rebindObject(key, object);
}
/** {@inheritDoc} */
@Override
public void rename(final String oldName, final String newName) throws NamingException, RemoteException
{
this.embeddedContext.rename(oldName, newName);
}
/** {@inheritDoc} */
@Override
public ContextInterface createSubcontext(final String name) throws NamingException, RemoteException
{
return this.embeddedContext.createSubcontext(name);
}
/** {@inheritDoc} */
@Override
public void destroySubcontext(final String name) throws NamingException, RemoteException
{
this.embeddedContext.destroySubcontext(name);
}
/** {@inheritDoc} */
@Override
public Set keySet() throws RemoteException
{
return new LinkedHashSet(this.embeddedContext.keySet());
}
/** {@inheritDoc} */
@Override
public Collection