View Javadoc

1   /*
2    * @(#) ContextUtil.java Oct 26, 2003
3    * 
4    * Copyright (c) 2003 Delft University of Technology Jaffalaan 5, 2628 BX Delft,
5    * the Netherlands All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the General Public License
9    */
10  package nl.tudelft.simulation.naming.context;
11  
12  import javax.naming.Binding;
13  import javax.naming.Context;
14  import javax.naming.InitialContext;
15  import javax.naming.NamingEnumeration;
16  import javax.naming.NamingException;
17  
18  import nl.tudelft.simulation.logger.Logger;
19  
20  /***
21   * ContextUtility class
22   * <p>
23   * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
24   * University of Technology </a>, the Netherlands. <br>
25   * See for project information <a href="http://www.simulation.tudelft.nl">
26   * www.simulation.tudelft.nl </a> <br>
27   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
28   * License (GPL) </a>, no warranty <br>
29   * 
30   * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
31   *         Jacobs </a>
32   * @version 1.2 2004-03-24
33   * @since 1.0
34   */
35  public class ContextUtil
36  {
37  	/***
38  	 * constructs a new ContextUtil
39  	 */
40  	protected ContextUtil()
41  	{
42  		super();
43  	}
44  
45  	/***
46  	 * resolves the name of an object under which it is accessible in the
47  	 * context
48  	 * 
49  	 * @param object the object
50  	 * @return String
51  	 * @throws NamingException whenever the object cannot be found
52  	 */
53  	public static String resolveKey(final Object object) throws NamingException
54  	{
55  		String result = ContextUtil
56  				.resolveKey(object, new InitialContext(), "");
57  		if (result == null)
58  		{
59  			throw new NamingException("could not resolve " + object.toString());
60  		}
61  		return result;
62  	}
63  
64  	/***
65  	 * resolves the key under which an object is stored in the context.
66  	 * 
67  	 * @param object the object which key to resolve.
68  	 * @param context the context.
69  	 * @param name the name of the parent.
70  	 * @return the key
71  	 * @throws NamingException on lookup failure
72  	 */
73  	private static String resolveKey(final Object object,
74  			final Context context, final String name) throws NamingException
75  	{
76  		NamingEnumeration list = context.listBindings(name);
77  		while (list.hasMore())
78  		{
79  			Binding binding = (Binding) list.next();
80  			if (binding.getObject() instanceof Context)
81  			{
82  				String result = ContextUtil.resolveKey(object,
83  						(Context) binding.getObject(), binding.getName());
84  				if (result != null)
85  				{
86  					return result;
87  				}
88  			} else if (binding.getObject().equals(object))
89  			{
90  				String key = context.getNameInNamespace() + "/"
91  						+ binding.getName();
92  				return key;
93  			}
94  		}
95  		return null;
96  	}
97  
98  	/***
99  	 * unbinds an object from the context
100 	 * 
101 	 * @param object the object to be removed.
102 	 */
103 	public static void unbindFromContext(Object object)
104 	{
105 		try
106 		{
107 			InitialContext context = new InitialContext();
108 			String key = ContextUtil.resolveKey(object, context, "/");
109 			context.unbind(key);
110 		} catch (NamingException namingException)
111 		{
112 			Logger.warning(ContextUtil.class, "unbindFromContext",
113 					namingException);
114 		}
115 	}
116 }