View Javadoc

1   /*
2    * @(#)JVMContextFactory.java Feb 1, 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;
11  
12  import java.net.InetAddress;
13  import java.net.URL;
14  import java.rmi.ConnectException;
15  import java.rmi.NotBoundException;
16  import java.rmi.registry.LocateRegistry;
17  import java.rmi.registry.Registry;
18  import java.util.Hashtable;
19  import java.util.Iterator;
20  
21  import javax.naming.Context;
22  import javax.naming.event.EventContext;
23  import javax.naming.spi.InitialContextFactory;
24  
25  import nl.tudelft.simulation.logger.Logger;
26  
27  /***
28   * A factory for RemoteContextClient instances, automatically invoked by JNDI
29   * when the correct jndi.properties file has been used.
30   * <p>
31   * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
32   * University of Technology </a>, the Netherlands. <br>
33   * See for project information <a href="http://www.simulation.tudelft.nl">
34   * www.simulation.tudelft.nl </a> <br>
35   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
36   * License (GPL) </a>, no warranty <br>
37   * 
38   * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
39   *         Jacobs </a>
40   * @version 1.1 2004-03-24
41   * @since 1.0
42   */
43  public class RemoteContextFactory implements InitialContextFactory
44  {
45  	/*** context refers to the static RemoteContextClient */
46  	private static RemoteContextClient context = null;
47  
48  	/***
49  	 * @see javax.naming.spi.InitialContextFactory #getInitialContext(Hashtable)
50  	 */
51  	public synchronized Context getInitialContext(final Hashtable environment)
52  	{
53  		//If the context is already looked up, let's return immediately
54  		if (RemoteContextFactory.context != null)
55  		{
56  			return RemoteContextFactory.context;
57  		}
58  
59  		//Let's look for our remote partner
60  		try
61  		{
62  			URL url = new URL(environment.get(Context.PROVIDER_URL).toString());
63  			Registry registry = LocateRegistry.getRegistry(url.getHost(), url
64  					.getPort());
65  
66  			//If there is no registry, registry!=null, so we have to test the
67  			// registry
68  			//to make sure whether there is one or not. We test by requesting a
69  			// list. This code
70  			//might be improved.
71  			try
72  			{
73  				registry.list();
74  			} catch (ConnectException connectException)
75  			{
76  				//Since we cannot find the registry, we must perhaps create
77  				// one..
78  				//This is only allowed if the host is our localhost. We cannot
79  				// create a registry on a remote host.
80  				if (!(url.getHost().equals("localhost")
81  						|| url.getHost().equals("127.0.0.1")
82  						|| url.getHost().equals(
83  								InetAddress.getLocalHost().getHostName()) || url
84  						.getHost().equals(
85  								InetAddress.getLocalHost().getHostAddress())))
86  				{
87  					throw new IllegalArgumentException(
88  							"cannot create registry on remote host");
89  				}
90  				registry = LocateRegistry.createRegistry(url.getPort());
91  			}
92  			//We now have a registry. Let's resolve the context object
93  
94  			RemoteContextInterface remoteContext = null;
95  			try
96  			{
97  				remoteContext = (RemoteContextInterface) registry.lookup(url
98  						.getFile());
99  			} catch (NotBoundException notBoundException)
100 			{
101 				//Since we cannot find the context, we must create one..
102 				//This is done based on the java.naming.wrapped properties in
103 				// jndi.properties
104 				Hashtable wrappedEnvironment = new Hashtable();
105 				for (Iterator iterator = environment.keySet().iterator(); iterator
106 						.hasNext();)
107 				{
108 					String key = iterator.next().toString();
109 					if (key.startsWith(RemoteContextInterface.WRAPPED_PREFIX))
110 					{
111 						wrappedEnvironment.put(key.replaceFirst(
112 								RemoteContextInterface.WRAPPED_PREFIX,
113 								"java.naming"), environment.get(key));
114 					}
115 				}
116 				if (wrappedEnvironment.isEmpty())
117 				{
118 					//If we do not throw this exception and accept an empty
119 					// environment, we'll get in an infinte loop
120 					throw new IllegalArgumentException(
121 							"no wrapped initial context defined");
122 				}
123 				EventContext wrappedContext = new InitialEventContext(
124 						wrappedEnvironment);
125 				remoteContext = new RemoteContext(wrappedContext);
126 				registry.bind(url.getFile(), remoteContext);
127 			}
128 			RemoteContextFactory.context = new RemoteContextClient(
129 					remoteContext);
130 			return RemoteContextFactory.context;
131 		} catch (Exception exception)
132 		{
133 			Logger.warning(this, "getInitialContext", exception);
134 			return null;
135 		}
136 	}
137 }