1 package org.djutils.rmi;
2
3 import java.net.InetAddress;
4 import java.rmi.AccessException;
5 import java.rmi.AlreadyBoundException;
6 import java.rmi.ConnectException;
7 import java.rmi.NoSuchObjectException;
8 import java.rmi.NotBoundException;
9 import java.rmi.Remote;
10 import java.rmi.RemoteException;
11 import java.rmi.registry.LocateRegistry;
12 import java.rmi.registry.Registry;
13 import java.rmi.server.UnicastRemoteObject;
14
15 import org.djutils.exceptions.Throw;
16 import org.djutils.logger.CategoryLogger;
17
18
19
20
21
22
23
24
25
26 public final class RMIUtils
27 {
28
29
30
31 private RMIUtils()
32 {
33
34 }
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public static Registry getRegistry(final String host, final int port) throws RemoteException
49 {
50 Throw.whenNull(host, "host cannot be null");
51 Throw.when(port <= 0 || port > 65535, IllegalArgumentException.class, "port <= 0 or port > 65535");
52 try
53 {
54 Registry registry = LocateRegistry.getRegistry(host, port);
55 boolean validRegistry = registry != null;
56 if (validRegistry)
57 {
58 try
59 {
60
61
62
63 registry.list();
64 }
65 catch (ConnectException | NoSuchObjectException connectException)
66 {
67 validRegistry = false;
68 }
69 }
70
71
72 if (!validRegistry)
73 {
74 if (!(host.equals("localhost") || host.equals("127.0.0.1")
75 || host.equals(InetAddress.getLocalHost().getHostName())
76 || host.equals(InetAddress.getLocalHost().getHostAddress())))
77 {
78 throw new AccessException("Cannot create registry on remote host: " + host);
79 }
80 registry = LocateRegistry.createRegistry(port);
81 }
82 return registry;
83 }
84 catch (Exception exception)
85 {
86 CategoryLogger.always().error(exception, "RMI exception when locating or creating RMI registry");
87 throw new RemoteException("RMI exception when locating or creating RMI registry", exception);
88 }
89 }
90
91
92
93
94
95
96
97
98
99
100
101
102 public static void bind(final Registry registry, final String bindingKey, final Remote object)
103 throws RemoteException, AlreadyBoundException
104 {
105 Throw.whenNull(registry, "registry cannot be null");
106 Throw.whenNull(bindingKey, "bindingKey cannot be null");
107 Throw.when(bindingKey.length() == 0, IllegalArgumentException.class, "bindingKey cannot be the empty String");
108 Throw.whenNull(object, "null object cannot be bound");
109 try
110 {
111 registry.bind(bindingKey, object);
112 }
113 catch (RemoteException | AlreadyBoundException exception)
114 {
115 CategoryLogger.always().error(exception, "RMI exception when binding object to the registry");
116 throw exception;
117 }
118 }
119
120
121
122
123
124
125
126
127
128
129
130 public static void unbind(final Registry registry, final String bindingKey) throws RemoteException, NotBoundException
131 {
132 Throw.whenNull(registry, "registry cannot be null");
133 Throw.whenNull(bindingKey, "bindingKey cannot be null");
134 Throw.when(bindingKey.length() == 0, IllegalArgumentException.class, "bindingKey cannot be the empty String");
135 try
136 {
137 registry.unbind(bindingKey);
138 }
139 catch (RemoteException | NotBoundException exception)
140 {
141 CategoryLogger.always().error(exception, "RMI exception when unbinding object from the registry");
142 throw exception;
143 }
144 }
145
146
147
148
149
150
151
152
153
154
155
156 public static void rebind(final Registry registry, final String bindingKey, final Remote newObject) throws RemoteException
157 {
158 Throw.whenNull(registry, "registry cannot be null");
159 Throw.whenNull(bindingKey, "bindingKey cannot be null");
160 Throw.when(bindingKey.length() == 0, IllegalArgumentException.class, "bindingKey cannot be the empty String");
161 Throw.whenNull(newObject, "null object cannot be bound");
162 try
163 {
164 registry.rebind(bindingKey, newObject);
165 }
166 catch (RemoteException exception)
167 {
168 CategoryLogger.always().error(exception, "RMI exception when rebinding object to the registry");
169 throw exception;
170 }
171 }
172
173
174
175
176
177
178
179
180
181
182
183
184 public static Remote lookup(final Registry registry, final String bindingKey) throws RemoteException, NotBoundException
185 {
186 Throw.whenNull(registry, "registry cannot be null");
187 Throw.whenNull(bindingKey, "bindingKey cannot be null");
188 Throw.when(bindingKey.length() == 0, IllegalArgumentException.class, "bindingKey cannot be the empty String");
189 try
190 {
191 return registry.lookup(bindingKey);
192 }
193 catch (RemoteException | NotBoundException exception)
194 {
195 CategoryLogger.always().error(exception, "RMI exception when looking up key {} in the RMI registry", bindingKey);
196 throw exception;
197 }
198 }
199
200
201
202
203
204
205
206 public static void closeRegistry(final Registry registry) throws RemoteException
207 {
208 Throw.whenNull(registry, "registry cannot be null");
209 for (String key : registry.list())
210 {
211 try
212 {
213 unbind(registry, key);
214 }
215 catch (RemoteException | NotBoundException nbe)
216 {
217 CategoryLogger.always().error(nbe, "RMI exception when unbinding key {} from the RMI registry", key);
218 }
219 }
220 UnicastRemoteObject.unexportObject(registry, true);
221 }
222 }