KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

230    public X509Certificate JavaDoc getCreatorCertificate()
231        throws RemoteException {
232        return proActiveRuntime.getCreatorCertificate();
233    }
234
235    /**
236     * @return policy server
237     */

238    public PolicyServer getPolicyServer() throws RemoteException {
239        return proActiveRuntime.getPolicyServer();
240    }
241
242    public String JavaDoc getVNName(String JavaDoc nodename) throws RemoteException {
243        return proActiveRuntime.getVNName(nodename);
244    }
245
246    public void setProActiveSecurityManager(ProActiveSecurityManager ps)
247        throws RemoteException {
248        proActiveRuntime.setProActiveSecurityManager(ps);
249    }
250
251    /* (non-Javadoc)
252     * @see org.objectweb.proactive.core.runtime.rmi.RemoteProActiveRuntime#setDefaultNodeVirtualNodeNAme(java.lang.String)
253     */

254    public void setDefaultNodeVirtualNodeName(String JavaDoc s)
255        throws RemoteException {
256        proActiveRuntime.setDefaultNodeVirtualNodeName(s);
257    }
258
259    /* (non-Javadoc)
260     * @see org.objectweb.proactive.core.runtime.rmi.RemoteProActiveRuntime#updateLocalNodeVirtualName()
261     */

262    public void updateLocalNodeVirtualName() throws RemoteException {
263     // proActiveRuntime.updateLocalNodeVirtualName();
264
}
265
266    /* (non-Javadoc)
267     * @see org.objectweb.proactive.core.runtime.ibis.RemoteProActiveRuntime#listVirtualNodes()
268     */

269    public void listVirtualNodes() throws RemoteException {
270     proActiveRuntime.listVirtualNodes();
271    }
272
273    /* (non-Javadoc)
274     * @see org.objectweb.proactive.core.runtime.ibis.RemoteProActiveRuntime#getNodePolicyServer(java.lang.String)
275     */

276    public PolicyServer getNodePolicyServer(String JavaDoc nodeName) throws RemoteException {
277        return null;
278    }
279
280    /* (non-Javadoc)
281     * @see org.objectweb.proactive.core.runtime.ibis.RemoteProActiveRuntime#enableSecurityIfNeeded()
282     */

283    public void enableSecurityIfNeeded() throws RemoteException {
284        // TODOremove this function
285

286    }
287
288    /* (non-Javadoc)
289     * @see org.objectweb.proactive.core.runtime.ibis.RemoteProActiveRuntime#getNodeCertificate(java.lang.String)
290     */

291    public X509Certificate JavaDoc getNodeCertificate(String JavaDoc nodeName) throws RemoteException{
292        return proActiveRuntime.getNodeCertificate(nodeName);
293    }
294     
295    /**
296     * @param nodeName
297     * @return returns all entities associated to the node
298     */

299    public ArrayList JavaDoc getEntities(String JavaDoc nodeName)throws RemoteException {
300        return proActiveRuntime.getEntities(nodeName);
301    }
302
303    /**
304     * @param nodeName
305     * @return returns all entities associated to the node
306     */

307    public ArrayList JavaDoc getEntities(UniversalBody uBody) throws RemoteException {
308        return proActiveRuntime.getEntities(uBody);
309    }
310  
311
312
313    /**
314     * @return returns all entities associated to this runtime
315     */

316    public ArrayList JavaDoc getEntities() throws RemoteException {
317        return proActiveRuntime.getEntities();
318    }
319    
320    /**
321         * @see org.objectweb.proactive.core.runtime.ibis.RemoteProActiveRuntime#getJobID(java.lang.String)
322         */

323        public String JavaDoc getJobID(String JavaDoc nodeUrl) throws RemoteException {
324         return proActiveRuntime.getJobID(nodeUrl);
325        }
326
327         
328
329
330     //
331
// ---PRIVATE METHODS--------------------------------------
332
//
333
private void register(String JavaDoc url, boolean replacePreviousBinding)
334         throws RemoteException {
335         try {
336             if (replacePreviousBinding) {
337                 Naming.rebind(UrlBuilder.removeProtocol(url, "ibis:"), this);
338             } else {
339                 Naming.bind(UrlBuilder.removeProtocol(url, "ibis:"), this);
340             }
341             if(url.indexOf("PA_RT")<0){
342                 logger.info(url + " successfully bound in registry at " + url);
343             }
344         } catch (AlreadyBoundException e) {
345             throw new RemoteException(url + " already bound in registry", e);
346         } catch (java.net.MalformedURLException JavaDoc e) {
347             throw new RemoteException("cannot bind in registry at " + url, e);
348         }
349     }
350
351     private void unregister(String JavaDoc url) throws RemoteException {
352         try {
353             Naming.unbind(UrlBuilder.removeProtocol(url, "ibis:"));
354             if(url.indexOf("PA_RT")<0){
355                 logger.info(url + " unbound in registry");
356             }
357         } catch (java.net.MalformedURLException JavaDoc e) {
358             throw new RemoteException("cannot unbind in registry at " + url, e);
359         } catch (NotBoundException e) {
360             throw new RemoteException(url + "is not bound in the registry", e);
361         }
362     }
363
364     private String JavaDoc buildRuntimeURL() {
365         int port = RemoteRuntimeFactory.getRegistryHelper()
366                                        .getRegistryPortNumber();
367         String JavaDoc host = getVMInformation().getInetAddress().getCanonicalHostName();
368         String JavaDoc name = getVMInformation().getName();
369
370         return UrlBuilder.buildUrl(host, name, "ibis:", port);
371     }
372
373     private String JavaDoc buildNodeURL(String JavaDoc url)
374         throws java.net.UnknownHostException JavaDoc {
375         int i = url.indexOf('/');
376
377         if (i == -1) {
378             //it is an url given by a descriptor
379
String JavaDoc host = getVMInformation().getInetAddress().getCanonicalHostName();
380             int port = RemoteRuntimeFactory.getRegistryHelper()
381                                            .getRegistryPortNumber();
382             return UrlBuilder.buildUrl(host, url, "ibis:", port);
383         } else {
384             return UrlBuilder.checkUrl(url);
385         }
386     }
387
388     
389 }
390
Popular Tags