1 package nl.tudelft.simulation.naming.context;
2
3 import java.net.InetAddress;
4 import java.net.URL;
5 import java.rmi.ConnectException;
6 import java.rmi.NotBoundException;
7 import java.rmi.registry.LocateRegistry;
8 import java.rmi.registry.Registry;
9 import java.util.Hashtable;
10 import java.util.Iterator;
11
12 import javax.naming.Context;
13
14 import org.djutils.logger.CategoryLogger;
15
16 import nl.tudelft.simulation.naming.context.event.InitialEventContext;
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 public class RemoteContextFactory implements ContextFactory
32 {
33
34 private static RemoteContext context = null;
35
36
37 @Override
38 public synchronized ContextInterface getInitialContext(final Hashtable<?, ?> environment, final String atomicName)
39 {
40
41 if (RemoteContextFactory.context != null)
42 {
43 return RemoteContextFactory.context;
44 }
45
46
47 try
48 {
49 URL url = new URL(environment.get(Context.PROVIDER_URL).toString());
50 Registry registry = LocateRegistry.getRegistry(url.getHost(), url.getPort());
51
52
53
54 try
55 {
56 registry.list();
57 }
58 catch (ConnectException connectException)
59 {
60
61
62 if (!(url.getHost().equals("localhost") || url.getHost().equals("127.0.0.1")
63 || url.getHost().equals(InetAddress.getLocalHost().getHostName())
64 || url.getHost().equals(InetAddress.getLocalHost().getHostAddress())))
65 {
66 throw new IllegalArgumentException("cannot create registry on remote host");
67 }
68 registry = LocateRegistry.createRegistry(url.getPort());
69 }
70
71
72 RemoteContext remoteContext = null;
73 try
74 {
75 remoteContext = (RemoteContext) registry.lookup(url.getFile());
76 }
77 catch (NotBoundException notBoundException)
78 {
79
80
81 Hashtable<Object, Object> wrappedEnvironment = new Hashtable<Object, Object>();
82 for (Iterator<?> iterator = environment.keySet().iterator(); iterator.hasNext();)
83 {
84 String key = iterator.next().toString();
85 if (key.equals(InitialEventContext.WRAPPED_CONTEXT_FACTORY))
86 {
87 wrappedEnvironment.put(InitialEventContext.INITIAL_CONTEXT_FACTORY, environment.get(key));
88 }
89 }
90 if (wrappedEnvironment.isEmpty())
91 {
92
93
94 throw new IllegalArgumentException("no wrapped initial context factory defined");
95 }
96 ContextInterface wrappedContext = InitialEventContext.instantiate(wrappedEnvironment, atomicName);
97 remoteContext = new RemoteContext(url, wrappedContext, url.getFile() + "_producer");
98
99 }
100 RemoteContextFactory.context = remoteContext;
101 return RemoteContextFactory.context;
102 }
103 catch (Exception exception)
104 {
105 CategoryLogger.always().error(exception, "getInitialContext");
106 return null;
107 }
108 }
109 }