package nl.tudelft.simulation.naming.context.util; import java.rmi.RemoteException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map.Entry; import javax.naming.NameNotFoundException; import javax.naming.NamingException; import nl.tudelft.simulation.naming.context.ContextInterface; /** * ContextUtil contains a few helper methods to deal with an InitialEventContext. *
* Copyright (c) 2020-2022 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 Alexander Verbraeck */ public class ContextUtil { /** * Lookup or create a sub-context in the parentContext with the name as its path. The path can be absolute or relative. The * terminating part of the name will be used as the key under which the created subcontext will be registered. * @param parentContext ContextInterface; the parent context * @param name String; the name to register the new subcontext * @return ContextInterface; the newly created subcontext * @throws NamingException when terminating key in the name is blank or contains "/" character(s) * @throws RemoteException on a network error when the Context is used over RMI */ public static ContextInterface lookupOrCreateSubContext(final ContextInterface parentContext, final String name) throws NamingException, RemoteException { try { if (parentContext.exists(name)) { Object result = parentContext.get(name); if (result instanceof ContextInterface) return (ContextInterface) result; throw new NamingException( "lookup for " + name + " in context " + parentContext + " returned object that is not a Context"); } } catch (NameNotFoundException nnfe) { // ignore -- create the context path } return parentContext.createSubcontext(name); } /** * Lookup a sub-context in the parentContext with the name as its path. The path can be absolute or relative. The * terminating part of the name will be used as the key under which the created subcontext will be registered. * @param parentContext ContextInterface; the parent context * @param name String; the name to register the new subcontext * @return ContextInterface; the newly created subcontext * @throws NamingException when terminating key in the name is blank or contains "/" character(s) * @throws RemoteException on a network error when the Context is used over RMI */ public static ContextInterface lookupSubContext(final ContextInterface parentContext, final String name) throws NamingException, RemoteException { if (parentContext.exists(name)) { Object result = parentContext.get(name); if (result instanceof ContextInterface) return (ContextInterface) result; throw new NamingException( "lookup for " + name + " in context " + parentContext + " returned object that is not a Context"); } throw new NamingException("Context " + name + " not found in parentContext " + parentContext); } /** * Destroy a sub-context in the parentContext with the name as its path. The path can be absolute or relative. The * terminating part of the name will be used as the key under for the subcontext to be removed. * @param parentContext ContextInterface; the parent context * @param name String; the name to use to find the subcontext to remove * @throws NamingException when terminating key in the name is blank or contains "/" character(s) * @throws RemoteException on a network error when the Context is used over RMI */ public static void destroySubContext(final ContextInterface parentContext, final String name) throws NamingException, RemoteException { parentContext.destroySubcontext(name); } /** * Resolve the key(s) for an object for a given context. This can be an expensive operation if the context is large. An * object can be registered zero or more times in the context, so a List of keys under which the object is registered will * be returned. The keys are relative to the startContext. The method starts with the given context. It is possible to look * up null objects in the Context. * @param startContext ContextInterface; the context to start the search * @param object Object; the object to look up in the tree under the startContext * @return List<String>; the list of keys that are bound to the object, or an empty list if no bindings for the object * were found * @throws NamingException when an error occurs during searching * @throws RemoteException on a network error when the Context is used over RMI */ public static List