1
2
3
4
5
6
7
8
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
54 if (RemoteContextFactory.context != null)
55 {
56 return RemoteContextFactory.context;
57 }
58
59
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
67
68
69
70
71 try
72 {
73 registry.list();
74 } catch (ConnectException connectException)
75 {
76
77
78
79
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
93
94 RemoteContextInterface remoteContext = null;
95 try
96 {
97 remoteContext = (RemoteContextInterface) registry.lookup(url
98 .getFile());
99 } catch (NotBoundException notBoundException)
100 {
101
102
103
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
119
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 }