|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| package jtr.remote.utils; |
|
8 |
| |
|
9 |
| import java.net.MalformedURLException; |
|
10 |
| import java.rmi.AlreadyBoundException; |
|
11 |
| import java.rmi.Naming; |
|
12 |
| import java.rmi.NotBoundException; |
|
13 |
| import java.rmi.RMISecurityManager; |
|
14 |
| import java.rmi.Remote; |
|
15 |
| import java.rmi.RemoteException; |
|
16 |
| import java.rmi.registry.LocateRegistry; |
|
17 |
| import java.rmi.registry.Registry; |
|
18 |
| import java.util.concurrent.atomic.AtomicBoolean; |
|
19 |
| import jtr.enterprise.LocatorException; |
|
20 |
| import jtr.remote.test.NodeInfo; |
|
21 |
| |
|
22 |
| import static jtr.test.SystemProperties.*; |
|
23 |
| import org.apache.log4j.Logger; |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| public class RmiUtil { |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
3
| public static final Registry launchRegistry(String host, int port) {
|
|
47 |
3
| Registry registry = null;
|
|
48 |
| |
|
49 |
3
| setSecurityManager();
|
|
50 |
| |
|
51 |
3
| try {
|
|
52 |
3
| logger.debug("Creating registry on localhost, on port "+port+"...");
|
|
53 |
3
| registry = LocateRegistry.createRegistry(port);
|
|
54 |
2
| logger.debug("... done");
|
|
55 |
| } catch(RemoteException re1) { |
|
56 |
| |
|
57 |
| |
|
58 |
1
| logger.error(re1);
|
|
59 |
1
| logger.warn("Failed, maybe there is already a running instance, trying to locate it...");
|
|
60 |
1
| try {
|
|
61 |
1
| registry = LocateRegistry.getRegistry(port);
|
|
62 |
1
| logger.debug("... located on port "+port);
|
|
63 |
| } catch(RemoteException re2) { |
|
64 |
0
| try {
|
|
65 |
0
| logger.debug("Trying to locate it on host "+host+", port "+port+"...");
|
|
66 |
0
| registry = LocateRegistry.getRegistry(host,port);
|
|
67 |
0
| logger.debug("... done");
|
|
68 |
| } catch(RemoteException re3) { |
|
69 |
0
| String msg = "Unable to locate/create any registry with host "+host+" and port "+port;
|
|
70 |
0
| logger.fatal(msg,re3);
|
|
71 |
0
| throw new RmiUtilException(msg,re3);
|
|
72 |
| } |
|
73 |
| } |
|
74 |
| } |
|
75 |
| |
|
76 |
3
| return registry;
|
|
77 |
| } |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
3
| public static final Remote registerService(Registry r, Class<? extends Remote> service, final String serviceName) {
|
|
89 |
3
| Remote srvc = null;
|
|
90 |
3
| try {
|
|
91 |
3
| logger.debug("Trying to look up an already registered "+serviceName+"...");
|
|
92 |
3
| srvc = r.lookup(serviceName);
|
|
93 |
0
| logger.debug("... done");
|
|
94 |
| } catch (RemoteException ex) { |
|
95 |
0
| logger.fatal("Unable to contact the registry, aborting "+serviceName+" registration",ex);
|
|
96 |
| } catch (NotBoundException ex) { |
|
97 |
3
| logger.error(ex);
|
|
98 |
3
| logger.warn("Unable to find any "+serviceName+", trying to bind a new one...");
|
|
99 |
3
| try {
|
|
100 |
3
| srvc = service.newInstance();
|
|
101 |
3
| r.bind(serviceName,srvc);
|
|
102 |
3
| logger.info("Service "+serviceName+" successfully registered");
|
|
103 |
| } catch (RemoteException ex1) { |
|
104 |
0
| logger.fatal("Unable to bind the "+serviceName+". Aborting.",ex1);
|
|
105 |
| } catch (AlreadyBoundException ex1) { |
|
106 |
0
| logger.fatal("Unable to bind the "+serviceName+". Aboring.",ex1);
|
|
107 |
| } catch (InstantiationException ie) { |
|
108 |
0
| logger.fatal("Unable to bind the "+serviceName+". Aboring.",ie);
|
|
109 |
| } catch (IllegalAccessException ie) { |
|
110 |
0
| logger.fatal("Unable to bind the "+serviceName+". Aboring.",ie);
|
|
111 |
| } |
|
112 |
| } |
|
113 |
3
| return srvc;
|
|
114 |
| } |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
8
| public static Remote lookupServer(NodeInfo jtrNode) throws LocatorException {
|
|
123 |
8
| Remote srvc = null;
|
|
124 |
8
| String url = jtrNode.toURL();
|
|
125 |
8
| try {
|
|
126 |
8
| srvc = Naming.lookup(url);
|
|
127 |
| } catch (MalformedURLException ex) { |
|
128 |
0
| String msg = "Unable to look-up JTR-node "+jtrNode;
|
|
129 |
0
| logger.fatal(msg,ex);
|
|
130 |
0
| throw new LocatorException(msg,ex);
|
|
131 |
| } catch (RemoteException ex) { |
|
132 |
0
| String msg = "Unable to look-up JTR-node "+jtrNode;
|
|
133 |
0
| logger.fatal(msg,ex);
|
|
134 |
0
| throw new LocatorException(msg,ex);
|
|
135 |
| } catch (NotBoundException ex) { |
|
136 |
0
| String msg = "Unable to look-up JTR-node "+jtrNode;
|
|
137 |
0
| logger.fatal(msg,ex);
|
|
138 |
0
| throw new LocatorException(msg,ex);
|
|
139 |
| } |
|
140 |
8
| return srvc;
|
|
141 |
| } |
|
142 |
| |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
| |
|
147 |
| |
|
148 |
3
| public static final void logRegistryContent(Registry r) {
|
|
149 |
3
| String entries[];
|
|
150 |
3
| try {
|
|
151 |
3
| entries = r.list();
|
|
152 |
3
| for(int i=0; i<entries.length; i++) {
|
|
153 |
4
| System.out.println("Registry entry #"+i+": "+entries[i]);
|
|
154 |
| } |
|
155 |
| } catch (RemoteException ex) { |
|
156 |
0
| ex.printStackTrace();
|
|
157 |
| } |
|
158 |
| } |
|
159 |
| |
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
| |
|
164 |
| |
|
165 |
| |
|
166 |
| |
|
167 |
2
| public synchronized static final void setSystemProperties() {
|
|
168 |
2
| String remStubsHost = System.getProperty(REMOTE_STUBS_HOST);
|
|
169 |
2
| if(remStubsHost==null) {
|
|
170 |
2
| remStubsHost = System.getProperty(RMI_REGISTRY_HOST, DEF_RMI_REGISTRY_HOST);
|
|
171 |
2
| System.setProperty(REMOTE_STUBS_HOST, remStubsHost);
|
|
172 |
| } |
|
173 |
| } |
|
174 |
| |
|
175 |
3
| private synchronized static void setSecurityManager() {
|
|
176 |
3
| if(!secured.get()) {
|
|
177 |
2
| System.setSecurityManager(new RMISecurityManager());
|
|
178 |
2
| secured.compareAndSet(false,true);
|
|
179 |
| } |
|
180 |
| } |
|
181 |
| |
|
182 |
| private static AtomicBoolean secured = new AtomicBoolean(false); |
|
183 |
| private static Logger logger = Logger.getLogger(RmiUtil.class); |
|
184 |
| } |