KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > runtime > rmi > RemoteProActiveRuntimeImpl


1 package org.objectweb.proactive.core.runtime.rmi;
2
3 import org.objectweb.proactive.Body;
4 import org.objectweb.proactive.core.body.UniversalBody;
5 import org.objectweb.proactive.core.descriptor.data.VirtualNode;
6 import org.objectweb.proactive.core.mop.ConstructorCall;
7 import org.objectweb.proactive.core.mop.ConstructorCallExecutionFailedException;
8 import org.objectweb.proactive.core.node.NodeException;
9 import org.objectweb.proactive.core.process.UniversalProcess;
10 import org.objectweb.proactive.core.rmi.RegistryHelper;
11 import org.objectweb.proactive.core.runtime.ProActiveRuntime;
12 import org.objectweb.proactive.core.runtime.ProActiveRuntimeImpl;
13 import org.objectweb.proactive.core.runtime.VMInformation;
14 import org.objectweb.proactive.core.util.UrlBuilder;
15 import org.objectweb.proactive.ext.security.PolicyServer;
16 import org.objectweb.proactive.ext.security.ProActiveSecurityManager;
17
18 import java.io.IOException JavaDoc;
19
20 import java.lang.reflect.InvocationTargetException JavaDoc;
21
22 import java.net.UnknownHostException JavaDoc;
23
24 import java.rmi.AccessException JavaDoc;
25 import java.rmi.ConnectException JavaDoc;
26 import java.rmi.RemoteException JavaDoc;
27 import java.rmi.server.UnicastRemoteObject JavaDoc;
28
29 import java.security.cert.X509Certificate JavaDoc;
30
31 import java.util.ArrayList JavaDoc;
32
33
34 /**
35  * An adapter for a ProActiveRuntime to be able to receive remote calls. This helps isolate RMI-specific
36  * code into a small set of specific classes, thus enabling reuse if we one day decide to switch
37  * to anothe remote objects library.
38  * @see <a HREF="http://www.javaworld.com/javaworld/jw-05-1999/jw-05-networked_p.html">Adapter Pattern</a>
39  */

40 public class RemoteProActiveRuntimeImpl extends UnicastRemoteObject JavaDoc
41     implements RemoteProActiveRuntime {
42     protected transient ProActiveRuntimeImpl proActiveRuntime;
43     protected String JavaDoc proActiveRuntimeURL;
44
45     //stores nodes urls to be able to unregister nodes
46
protected ArrayList JavaDoc nodesArray;
47
48     //store vn urls to be able to unregister vns
49
protected ArrayList JavaDoc vnNodesArray;
50     protected boolean hasCreatedRegistry;
51
52     //
53
// -- CONSTRUCTORS -----------------------------------------------
54
//
55
public RemoteProActiveRuntimeImpl()
56         throws java.rmi.RemoteException JavaDoc, java.rmi.AlreadyBoundException JavaDoc {
57         //System.out.println("toto");
58
this.hasCreatedRegistry = RegistryHelper.getRegistryCreator();
59         this.proActiveRuntime = (ProActiveRuntimeImpl) ProActiveRuntimeImpl.getProActiveRuntime();
60         this.nodesArray = new java.util.ArrayList JavaDoc();
61         this.vnNodesArray = new java.util.ArrayList JavaDoc();
62         //this.urlBuilder = new UrlBuilder();
63
this.proActiveRuntimeURL = buildRuntimeURL();
64         // java.rmi.Naming.bind(proActiveRuntimeURL, this);
65
register(proActiveRuntimeURL, false);
66         //System.out.println ("ProActiveRuntime successfully bound in registry at "+proActiveRuntimeURL);
67
}
68
69     //
70
// -- PUBLIC METHODS -----------------------------------------------
71
//
72
public String JavaDoc createLocalNode(String JavaDoc nodeName,
73         boolean replacePreviousBinding, PolicyServer ps, String JavaDoc VNname, String JavaDoc jobId)
74         throws java.rmi.RemoteException JavaDoc, NodeException {
75         String JavaDoc nodeURL = null;
76
77         //Node node;
78
try {
79             //first we build a well-formed url
80
nodeURL = buildNodeURL(nodeName);
81             //then take the name of the node
82
String JavaDoc name = UrlBuilder.getNameFromUrl(nodeURL);
83
84             //register the url in rmi registry
85
register(nodeURL, replacePreviousBinding);
86
87             proActiveRuntime.createLocalNode(name, replacePreviousBinding, ps,
88                 VNname, jobId);
89         } catch (java.net.UnknownHostException JavaDoc e) {
90             throw new java.rmi.RemoteException JavaDoc("Host unknown in " + nodeURL, e);
91         }
92         nodesArray.add(nodeURL);
93         return nodeURL;
94     }
95
96     public void killAllNodes() throws java.rmi.RemoteException JavaDoc {
97         for (int i = 0; i < nodesArray.size(); i++) {
98             String JavaDoc url = (String JavaDoc) nodesArray.get(i);
99             killNode(url);
100         }
101     }
102
103     public void killNode(String JavaDoc nodeName) throws java.rmi.RemoteException JavaDoc {
104         String JavaDoc nodeUrl = null;
105         String JavaDoc name = null;
106         try {
107             nodeUrl = buildNodeURL(nodeName);
108             name = UrlBuilder.getNameFromUrl(nodeUrl);
109             unregister(nodeUrl);
110         } catch (UnknownHostException JavaDoc e) {
111             throw new java.rmi.RemoteException JavaDoc("Host unknown in " + nodeUrl, e);
112         }
113         proActiveRuntime.killNode(nodeName);
114     }
115
116     // public void createLocalVM(JVMProcess jvmProcess)
117
// throws IOException
118
// {
119
// proActiveRuntime.createLocalVM(jvmProcess);
120
// }
121
public void createVM(UniversalProcess remoteProcess)
122         throws IOException JavaDoc {
123         proActiveRuntime.createVM(remoteProcess);
124     }
125
126     // public Node[] getLocalNodes()
127
// {
128
// return proActiveRuntime.getLocalNodes();
129
// }
130
public String JavaDoc[] getLocalNodeNames() {
131         return proActiveRuntime.getLocalNodeNames();
132     }
133
134     // public String getLocalNode(String nodeName)
135
// {
136
// return proActiveRuntime.getLocalNode(nodeName);
137
// }
138
//
139
//
140
// public String getNode(String nodeName)
141
// {
142
// return proActiveRuntime.getNode(nodeName);
143
// }
144
// public String getDefaultNodeName(){
145
// return proActiveRuntime.getDefaultNodeName();
146
// }
147
public VMInformation getVMInformation() {
148         return proActiveRuntime.getVMInformation();
149     }
150
151     public void register(ProActiveRuntime proActiveRuntimeDist,
152         String JavaDoc proActiveRuntimeName, String JavaDoc creatorID, String JavaDoc creationProtocol,
153         String JavaDoc vmName) {
154         proActiveRuntime.register(proActiveRuntimeDist, proActiveRuntimeName,
155             creatorID, creationProtocol, vmName);
156     }
157
158     public ProActiveRuntime[] getProActiveRuntimes() {
159         return proActiveRuntime.getProActiveRuntimes();
160     }
161
162     public ProActiveRuntime getProActiveRuntime(String JavaDoc proActiveRuntimeName) {
163         return proActiveRuntime.getProActiveRuntime(proActiveRuntimeName);
164     }
165
166     public void killRT(boolean softly) throws java.rmi.RemoteException JavaDoc {
167         killAllNodes();
168         unregisterAllVirtualNodes();
169         unregister(proActiveRuntimeURL);
170         if (hasCreatedRegistry) {
171             if (softly) {
172                 if (RegistryHelper.getRegistry().list().length > 0) {
173                     new RMIKillerThread().start();
174                     return;
175                 }
176             }
177         }
178         proActiveRuntime.killRT(softly);
179     }
180
181     public String JavaDoc getURL() {
182         return proActiveRuntimeURL;
183     }
184
185     // public void setPortNumber(int p) {
186
// this.portNumber = p;
187
// }
188
//
189
// public int getPortNumber() {
190
// return this.portNumber;
191
// }
192
public ArrayList JavaDoc getActiveObjects(String JavaDoc nodeName) {
193         return proActiveRuntime.getActiveObjects(nodeName);
194     }
195
196     public ArrayList JavaDoc getActiveObjects(String JavaDoc nodeName, String JavaDoc objectName) {
197         return proActiveRuntime.getActiveObjects(nodeName, objectName);
198     }
199
200     public VirtualNode getVirtualNode(String JavaDoc virtualNodeName) {
201         return proActiveRuntime.getVirtualNode(virtualNodeName);
202     }
203
204     public void registerVirtualNode(String JavaDoc virtualNodeName,
205         boolean replacePreviousBinding) throws java.rmi.RemoteException JavaDoc {
206         String JavaDoc virtualNodeURL = null;
207
208         try {
209             //first we build a well-formed url
210
virtualNodeURL = buildNodeURL(virtualNodeName);
211             //register it with the url
212
register(virtualNodeURL, replacePreviousBinding);
213         } catch (java.net.UnknownHostException JavaDoc e) {
214             throw new java.rmi.RemoteException JavaDoc("Host unknown in " +
215                 virtualNodeURL, e);
216         }
217         vnNodesArray.add(virtualNodeURL);
218     }
219
220     public void unregisterVirtualNode(String JavaDoc virtualnodeName)
221         throws java.rmi.RemoteException JavaDoc {
222         String JavaDoc virtualNodeURL = null;
223         proActiveRuntime.unregisterVirtualNode(UrlBuilder.removeVnSuffix(
224                 virtualnodeName));
225         try {
226             //first we build a well-formed url
227
virtualNodeURL = buildNodeURL(virtualnodeName);
228             unregister(virtualNodeURL);
229         } catch (java.net.UnknownHostException JavaDoc e) {
230             throw new java.rmi.RemoteException JavaDoc("Host unknown in " +
231                 virtualNodeURL, e);
232         }
233         vnNodesArray.remove(virtualNodeURL);
234     }
235
236     public void unregisterAllVirtualNodes() throws java.rmi.RemoteException JavaDoc {
237         for (int i = 0; i < vnNodesArray.size(); i++) {
238             String JavaDoc url = (String JavaDoc) vnNodesArray.get(i);
239             unregisterVirtualNode(url);
240         }
241     }
242
243     public UniversalBody createBody(String JavaDoc nodeName,
244         ConstructorCall bodyConstructorCall, boolean isNodeLocal)
245         throws ConstructorCallExecutionFailedException,
246             InvocationTargetException JavaDoc {
247         return proActiveRuntime.createBody(nodeName, bodyConstructorCall,
248             isNodeLocal);
249     }
250
251     public UniversalBody receiveBody(String JavaDoc nodeName, Body body) {
252         return proActiveRuntime.receiveBody(nodeName, body);
253     }
254
255     // SECURITY
256

257     /* (non-Javadoc)
258      * @see org.objectweb.proactive.core.runtime.rmi.RemoteProActiveRuntime#getCreatorCertificate()
259      */

260     public X509Certificate JavaDoc getCreatorCertificate()
261         throws java.rmi.RemoteException JavaDoc {
262         return proActiveRuntime.getCreatorCertificate();
263     }
264
265     /**
266     * @return policy server
267     */

268     public PolicyServer getPolicyServer() throws java.rmi.RemoteException JavaDoc {
269         return proActiveRuntime.getPolicyServer();
270     }
271
272     public String JavaDoc getVNName(String JavaDoc nodename) throws java.rmi.RemoteException JavaDoc {
273         return proActiveRuntime.getVNName(nodename);
274     }
275
276     public void setProActiveSecurityManager(ProActiveSecurityManager ps)
277         throws java.rmi.RemoteException JavaDoc {
278         proActiveRuntime.setProActiveSecurityManager(ps);
279     }
280
281     /* (non-Javadoc)
282      * @see org.objectweb.proactive.core.runtime.rmi.RemoteProActiveRuntime#setDefaultNodeVirtualNodeNAme(java.lang.String)
283      */

284     public void setDefaultNodeVirtualNodeNAme(String JavaDoc s)
285         throws RemoteException JavaDoc {
286         proActiveRuntime.setDefaultNodeVirtualNodeName(s);
287     }
288
289     /* (non-Javadoc)
290      * @see org.objectweb.proactive.core.runtime.rmi.RemoteProActiveRuntime#updateLocalNodeVirtualName()
291      */

292     public void updateLocalNodeVirtualName() throws RemoteException JavaDoc {
293         proActiveRuntime.listVirtualNodes();
294     }
295
296     /* (non-Javadoc)
297      * @see org.objectweb.proactive.core.runtime.rmi.RemoteProActiveRuntime#getNodePolicyServer(java.lang.String)
298      */

299     public PolicyServer getNodePolicyServer(String JavaDoc nodeName)
300         throws RemoteException JavaDoc {
301         return proActiveRuntime.getNodePolicyServer(nodeName);
302     }
303
304     /* (non-Javadoc)
305      * @see org.objectweb.proactive.core.runtime.rmi.RemoteProActiveRuntime#enableSecurityIfNeeded()
306      */

307     public void enableSecurityIfNeeded() throws RemoteException JavaDoc {
308         proActiveRuntime.enableSecurityIfNeeded();
309     }
310
311     /* (non-Javadoc)
312      * @see org.objectweb.proactive.core.runtime.rmi.RemoteProActiveRuntime#getNodeCertificate(java.lang.String)
313      */

314     public X509Certificate JavaDoc getNodeCertificate(String JavaDoc nodeName)
315         throws RemoteException JavaDoc {
316         return proActiveRuntime.getNodeCertificate(nodeName);
317     }
318
319     /**
320      * @param nodeName
321      * @return returns all entities associated to the node
322      */

323     public ArrayList JavaDoc getEntities(String JavaDoc nodeName) throws RemoteException JavaDoc {
324         return proActiveRuntime.getEntities(nodeName);
325     }
326
327     /**
328      * @param nodeName
329      * @return returns all entities associated to the node
330      */

331     public ArrayList JavaDoc getEntities(UniversalBody uBody) throws RemoteException JavaDoc {
332         return proActiveRuntime.getEntities(uBody);
333     }
334
335     /**
336      * @return returns all entities associated to this runtime
337      */

338     public ArrayList JavaDoc getEntities() throws RemoteException JavaDoc {
339         return proActiveRuntime.getEntities();
340     }
341
342     /**
343      * @see org.objectweb.proactive.core.runtime.rmi.RemoteProActiveRuntime#getJobID(java.lang.String)
344      */

345     public String JavaDoc getJobID(String JavaDoc nodeUrl) throws RemoteException JavaDoc {
346         return proActiveRuntime.getJobID(nodeUrl);
347     }
348
349     //
350
// ---PRIVATE METHODS--------------------------------------
351
//
352
private void register(String JavaDoc url, boolean replacePreviousBinding)
353         throws java.rmi.RemoteException JavaDoc {
354         try {
355             if (replacePreviousBinding) {
356                 java.rmi.Naming.rebind(UrlBuilder.removeProtocol(url, "rmi:"),
357                     this);
358             } else {
359                 java.rmi.Naming.bind(UrlBuilder.removeProtocol(url, "rmi:"),
360                     this);
361             }
362             if (url.indexOf("PA_RT") < 0) {
363                 logger.info(url + " successfully bound in registry at " + url);
364             }
365         } catch (java.rmi.AlreadyBoundException JavaDoc e) {
366             throw new java.rmi.RemoteException JavaDoc(url +
367                 " already bound in registry", e);
368         } catch (java.net.MalformedURLException JavaDoc e) {
369             throw new java.rmi.RemoteException JavaDoc("cannot bind in registry at " +
370                 url, e);
371         }
372     }
373
374     private void unregister(String JavaDoc url) throws java.rmi.RemoteException JavaDoc {
375         try {
376             java.rmi.Naming.unbind(UrlBuilder.removeProtocol(url, "rmi:"));
377             if (url.indexOf("PA_RT") < 0) {
378                 logger.info(url + " unbound in registry");
379             }
380         } catch (ConnectException JavaDoc e) {
381             //if we get a connect exception, the rmi registry is unreachable. We cannot throw
382
//an exception otherwise the killRT method cannot reach the end!
383
if ((e.getCause().getClass().getName().equals("java.net.ConnectException") &&
384                     e.getCause().getMessage().equals("Connection refused"))) {
385                 if (url.indexOf("PA_RT") < 0) {
386                     logger.info("RMIRegistry unreachable on host " +
387                         getVMInformation().getInetAddress().getCanonicalHostName() +
388                         " to unregister " + url + ". Killed anyway !!!");
389                 }
390             }
391         } catch (java.net.MalformedURLException JavaDoc e) {
392             throw new java.rmi.RemoteException JavaDoc("cannot unbind in registry at " +
393                 url, e);
394         } catch (java.rmi.NotBoundException JavaDoc e) {
395             //No need to throw an exception if an object is already unregistered
396
logger.info(url + "is not bound in the registry", e);
397         }
398     }
399
400     private String JavaDoc buildRuntimeURL() {
401         int port = RemoteRuntimeFactory.getRegistryHelper()
402                                        .getRegistryPortNumber();
403         String JavaDoc host = getVMInformation().getInetAddress().getCanonicalHostName();
404         String JavaDoc name = getVMInformation().getName();
405         return UrlBuilder.buildUrl(host, name, "rmi:", port);
406     }
407
408     private String JavaDoc buildNodeURL(String JavaDoc url)
409         throws java.net.UnknownHostException JavaDoc {
410         int i = url.indexOf('/');
411         if (i == -1) {
412             //it is an url given by a descriptor
413
String JavaDoc host = getVMInformation().getInetAddress().getCanonicalHostName();
414
415             int port = RemoteRuntimeFactory.getRegistryHelper()
416                                            .getRegistryPortNumber();
417             return UrlBuilder.buildUrl(host, url, "rmi:", port);
418         } else {
419             return UrlBuilder.checkUrl(url);
420         }
421     }
422
423     //
424
// ----------------- INNER CLASSES --------------------------------
425
//
426
private class RMIKillerThread extends Thread JavaDoc {
427         public RMIKillerThread() {
428         }
429
430         public void run() {
431             try {
432                 while (RegistryHelper.getRegistry().list().length > 0) {
433                     // the thread sleeps for 10 minutes
434
Thread.sleep(600000);
435                 }
436
437                 proActiveRuntime.killRT(false);
438             } catch (InterruptedException JavaDoc e) {
439                 proActiveRuntime.killRT(false);
440                 e.printStackTrace();
441             } catch (AccessException JavaDoc e) {
442                 logger.error(e.getMessage());
443                 proActiveRuntime.killRT(false);
444             } catch (RemoteException JavaDoc e) {
445                 logger.error(e.getMessage());
446                 proActiveRuntime.killRT(false);
447             }
448         }
449     }
450 }
451
Popular Tags