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 RmiRegistry
27 {
28
29
30
31 private RmiRegistry()
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 public static void bind(final Registry registry, final String bindingKey, final Remote object)
102 throws RemoteException, AlreadyBoundException
103 {
104 Throw.whenNull(registry, "registry cannot be null");
105 Throw.whenNull(bindingKey, "bindingKey cannot be null");
106 Throw.when(bindingKey.length() == 0, IllegalArgumentException.class, "bindingKey cannot be the empty String");
107 Throw.whenNull(object, "null object cannot be bound");
108 try
109 {
110 registry.bind(bindingKey, object);
111 }
112 catch (RemoteException | AlreadyBoundException exception)
113 {
114 CategoryLogger.always().error(exception, "RMI exception when binding object to the registry");
115 throw exception;
116 }
117 }
118
119
120
121
122
123
124
125
126
127
128 public static void unbind(final Registry registry, final String bindingKey) throws RemoteException, NotBoundException
129 {
130 Throw.whenNull(registry, "registry cannot be null");
131 Throw.whenNull(bindingKey, "bindingKey cannot be null");
132 Throw.when(bindingKey.length() == 0, IllegalArgumentException.class, "bindingKey cannot be the empty String");
133 try
134 {
135 registry.unbind(bindingKey);
136 }
137 catch (RemoteException | NotBoundException exception)
138 {
139 CategoryLogger.always().error(exception, "RMI exception when unbinding object from the registry");
140 throw exception;
141 }
142 }
143
144
145
146
147
148
149
150
151
152
153 public static void rebind(final Registry registry, final String bindingKey, final Remote newObject) throws RemoteException
154 {
155 Throw.whenNull(registry, "registry cannot be null");
156 Throw.whenNull(bindingKey, "bindingKey cannot be null");
157 Throw.when(bindingKey.length() == 0, IllegalArgumentException.class, "bindingKey cannot be the empty String");
158 Throw.whenNull(newObject, "null object cannot be bound");
159 try
160 {
161 registry.rebind(bindingKey, newObject);
162 }
163 catch (RemoteException exception)
164 {
165 CategoryLogger.always().error(exception, "RMI exception when rebinding object to the registry");
166 throw exception;
167 }
168 }
169
170
171
172
173
174
175
176
177
178
179
180 public static Remote lookup(final Registry registry, final String bindingKey) throws RemoteException, NotBoundException
181 {
182 Throw.whenNull(registry, "registry cannot be null");
183 Throw.whenNull(bindingKey, "bindingKey cannot be null");
184 Throw.when(bindingKey.length() == 0, IllegalArgumentException.class, "bindingKey cannot be the empty String");
185 try
186 {
187 return registry.lookup(bindingKey);
188 }
189 catch (RemoteException | NotBoundException exception)
190 {
191 CategoryLogger.always().error(exception, "RMI exception when looking up key {} in the RMI registry", bindingKey);
192 throw exception;
193 }
194 }
195
196
197
198
199
200
201
202 public static void closeRegistry(final Registry registry) throws RemoteException
203 {
204 Throw.whenNull(registry, "registry cannot be null");
205 for (String key : registry.list())
206 {
207 try
208 {
209 unbind(registry, key);
210 }
211 catch (RemoteException | NotBoundException nbe)
212 {
213 CategoryLogger.always().error(nbe, "RMI exception when unbinding key {} from the RMI registry", key);
214 }
215 }
216 UnicastRemoteObject.unexportObject(registry, true);
217 }
218 }