KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > body > jini > JiniBodyAdapter


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31 package org.objectweb.proactive.core.body.jini;
32
33 import java.io.IOException JavaDoc;
34 import java.rmi.RemoteException JavaDoc;
35 import java.security.PublicKey JavaDoc;
36 import java.security.cert.X509Certificate JavaDoc;
37 import java.util.ArrayList JavaDoc;
38
39 import org.objectweb.proactive.core.ProActiveException;
40 import org.objectweb.proactive.core.UniqueID;
41 import org.objectweb.proactive.core.body.UniversalBody;
42 import org.objectweb.proactive.core.body.reply.Reply;
43 import org.objectweb.proactive.core.body.request.Request;
44 import org.objectweb.proactive.ext.security.Communication;
45 import org.objectweb.proactive.ext.security.CommunicationForbiddenException;
46 import org.objectweb.proactive.ext.security.Policy;
47 import org.objectweb.proactive.ext.security.ProActiveSecurityManager;
48 import org.objectweb.proactive.ext.security.RenegotiateSessionException;
49 import org.objectweb.proactive.ext.security.SecurityContext;
50 import org.objectweb.proactive.ext.security.SecurityNotAvailableException;
51 import org.objectweb.proactive.ext.security.crypto.AuthenticationException;
52 import org.objectweb.proactive.ext.security.crypto.ConfidentialityTicket;
53 import org.objectweb.proactive.ext.security.crypto.KeyExchangeException;
54
55
56 /**
57  * An adapter for a JiniBody to be able to receive remote calls. This helps isolate JINI-specific
58  * code into a small set of specific classes, thus enabling reuse if we one day decide to switch
59  * to another jini objects library.
60  */

61 public class JiniBodyAdapter implements UniversalBody, java.io.Serializable JavaDoc {
62
63     /**
64      * The encapsulated JiniBody
65      */

66     protected JiniBody proxiedJiniBody;
67
68     /**
69      * Cache the ID of the Body locally for speed
70      */

71     protected UniqueID bodyID;
72
73     //
74
// -- CONSTRUCTORS -----------------------------------------------
75
//
76
public JiniBodyAdapter() {
77     }
78
79     public JiniBodyAdapter(JiniBody jiniBody) throws ProActiveException {
80         this.proxiedJiniBody = jiniBody;
81         try {
82             this.bodyID = jiniBody.getID();
83         } catch (java.rmi.RemoteException JavaDoc e) {
84             throw new ProActiveException(e);
85         }
86     }
87
88     public JiniBodyAdapter(UniversalBody body) throws ProActiveException {
89         try {
90             this.proxiedJiniBody = new JiniBodyImpl(body);
91         } catch (java.rmi.RemoteException JavaDoc e) {
92             throw new ProActiveException(e);
93         }
94         this.bodyID = body.getID();
95     }
96
97     //
98
// -- PUBLIC METHODS -----------------------------------------------
99
//
100

101     /**
102      * Registers an active object into a RMI registry. In fact it is the
103      * jini version of the body of the active object that is registered into the
104      * RMI Registry under the given URL.
105      * @param obj the active object to register.
106      * @param url the url under which the jini body is registered.
107      * @exception java.io.IOException if the jini body cannot be registered
108      */

109     public static void register(JiniBodyAdapter bodyAdapter, String JavaDoc url)
110         throws java.io.IOException JavaDoc {
111         java.rmi.Naming.rebind(url, bodyAdapter.proxiedJiniBody);
112     }
113
114     /**
115      * Unregisters an active object previously registered into a RMI registry.
116      * @param url the url under which the active object is registered.
117      * @exception java.io.IOException if the jini object cannot be removed from the registry
118      */

119     public static void unregister(String JavaDoc url) throws java.io.IOException JavaDoc {
120         try {
121             java.rmi.Naming.unbind(url);
122         } catch (java.rmi.NotBoundException JavaDoc e) {
123             throw new java.io.IOException JavaDoc(
124                 "No object is bound to the given url : " + url);
125         }
126     }
127
128     /**
129      * Looks-up an active object previously registered in a RMI registry. In fact it is the
130      * jini version of the body of an active object that can be registered into the
131      * RMI Registry under a given URL.
132      * @param url the url the jini Body is registered to
133      * @return a UniversalBody
134      * @exception java.io.IOException if the jini body cannot be found under the given url
135      * or if the object found is not of type JiniBody
136      */

137     public static UniversalBody lookup(String JavaDoc url) throws java.io.IOException JavaDoc {
138         Object JavaDoc o = null;
139
140         // Try if URL is the address of a JiniBody
141
try {
142             o = java.rmi.Naming.lookup(url);
143         } catch (java.rmi.NotBoundException JavaDoc e) {
144             throw new java.io.IOException JavaDoc("The url " + url +
145                 " is not bound to any known object");
146         }
147         if (o instanceof JiniBody) {
148             try {
149                 return new JiniBodyAdapter((JiniBody) o);
150             } catch (ProActiveException e) {
151                 throw new java.io.IOException JavaDoc("Cannot build a Jini Adapter" +
152                     e.toString());
153             }
154         } else {
155             throw new java.io.IOException JavaDoc(
156                 "The given url does exist but doesn't point to a jini body url=" +
157                 url + " class found is " + o.getClass().getName());
158         }
159     }
160
161     public boolean equals(Object JavaDoc o) {
162         if (!(o instanceof JiniBodyAdapter)) {
163             return false;
164         }
165         JiniBodyAdapter rba = (JiniBodyAdapter) o;
166         return proxiedJiniBody.equals(rba.proxiedJiniBody);
167     }
168
169     public int hashCode() {
170         return proxiedJiniBody.hashCode();
171     }
172
173     //
174
// -- implements UniversalBody -----------------------------------------------
175
//
176
public void receiveRequest(Request r) throws java.io.IOException JavaDoc, RenegotiateSessionException {
177         proxiedJiniBody.receiveRequest(r);
178     }
179
180     public void receiveReply(Reply r) throws java.io.IOException JavaDoc {
181         proxiedJiniBody.receiveReply(r);
182     }
183
184     public String JavaDoc getNodeURL() {
185         try {
186             return proxiedJiniBody.getNodeURL();
187         } catch (java.rmi.RemoteException JavaDoc e) {
188             return "cannot contact the body to get the nodeURL";
189         }
190     }
191
192     public UniqueID getID() {
193         return bodyID;
194     }
195
196     public void updateLocation(UniqueID id, UniversalBody jiniBody)
197         throws java.io.IOException JavaDoc {
198         proxiedJiniBody.updateLocation(id, jiniBody);
199     }
200
201     public UniversalBody getRemoteAdapter() {
202         return this;
203     }
204
205     public void enableAC() throws java.io.IOException JavaDoc {
206         proxiedJiniBody.enableAC();
207     }
208
209     public void disableAC() throws java.io.IOException JavaDoc {
210         proxiedJiniBody.disableAC();
211     }
212
213     public void setImmediateService(String JavaDoc methodName)
214         throws java.io.IOException JavaDoc {
215         proxiedJiniBody.setImmediateService(methodName);
216     }
217
218     // SECURITY
219

220     public void initiateSession(int type,UniversalBody body)
221         throws RemoteException JavaDoc, IOException JavaDoc, CommunicationForbiddenException,
222             AuthenticationException, RenegotiateSessionException,
223             SecurityNotAvailableException {
224         proxiedJiniBody.initiateSession(type, body);
225     }
226
227     public void terminateSession(long sessionID)
228         throws java.io.IOException JavaDoc, SecurityNotAvailableException {
229         proxiedJiniBody.terminateSession(sessionID);
230     }
231
232     public X509Certificate JavaDoc getCertificate()
233         throws java.io.IOException JavaDoc, SecurityNotAvailableException {
234         return proxiedJiniBody.getCertificate();
235     }
236
237     public ProActiveSecurityManager getProActiveSecurityManager()
238         throws java.io.IOException JavaDoc, SecurityNotAvailableException {
239         return proxiedJiniBody.getProActiveSecurityManager();
240     }
241
242     public Policy getPolicyFrom(X509Certificate JavaDoc certificate)
243         throws java.io.IOException JavaDoc, SecurityNotAvailableException {
244         return proxiedJiniBody.getPolicyFrom(certificate);
245     }
246
247     public long startNewSession(Communication policy)
248         throws IOException JavaDoc, RenegotiateSessionException,
249             SecurityNotAvailableException {
250         return proxiedJiniBody.startNewSession(policy);
251     }
252
253     public ConfidentialityTicket negociateKeyReceiverSide(
254         ConfidentialityTicket confidentialityTicket, long sessionID)
255         throws java.io.IOException JavaDoc, KeyExchangeException,
256             SecurityNotAvailableException {
257         return proxiedJiniBody.negociateKeyReceiverSide(confidentialityTicket,
258             sessionID);
259     }
260
261     public PublicKey JavaDoc getPublicKey()
262         throws java.io.IOException JavaDoc, SecurityNotAvailableException {
263         return proxiedJiniBody.getPublicKey();
264     }
265
266     public byte[] randomValue(long sessionID, byte[] cl_rand)
267         throws Exception JavaDoc, SecurityNotAvailableException {
268         return proxiedJiniBody.randomValue(sessionID, cl_rand);
269     }
270
271     public byte[][] publicKeyExchange(long sessionID,
272         UniversalBody distantBody, byte[] my_pub, byte[] my_cert,
273         byte[] sig_code) throws Exception JavaDoc, SecurityNotAvailableException {
274         return proxiedJiniBody.publicKeyExchange(sessionID, distantBody,
275             my_pub, my_cert, sig_code);
276     }
277
278     public byte[][] secretKeyExchange(long sessionID, byte[] tmp, byte[] tmp1,
279         byte[] tmp2, byte[] tmp3, byte[] tmp4)
280         throws Exception JavaDoc, SecurityNotAvailableException {
281         return proxiedJiniBody.secretKeyExchange(sessionID, tmp, tmp1, tmp2,
282             tmp3, tmp4);
283     }
284
285     public Communication getPolicyTo(String JavaDoc type, String JavaDoc from, String JavaDoc to)
286         throws java.io.IOException JavaDoc, SecurityNotAvailableException {
287         return proxiedJiniBody.getPolicyTo(type, from, to);
288     }
289
290     /* (non-Javadoc)
291      * @see org.objectweb.proactive.core.body.UniversalBody#getVNName()
292      */

293     public String JavaDoc getVNName() throws IOException JavaDoc, SecurityNotAvailableException {
294         return proxiedJiniBody.getVNName();
295     }
296
297     /* (non-Javadoc)
298      * @see org.objectweb.proactive.core.body.UniversalBody#getCertificateEncoded()
299      */

300     public byte[] getCertificateEncoded()
301         throws IOException JavaDoc, SecurityNotAvailableException {
302         return proxiedJiniBody.getCertificateEncoded();
303     }
304
305     /* (non-Javadoc)
306      * @see org.objectweb.proactive.core.body.UniversalBody#getPolicy(org.objectweb.proactive.ext.security.SecurityContext)
307      */

308     public SecurityContext getPolicy(SecurityContext securityContext)
309         throws SecurityNotAvailableException, IOException JavaDoc {
310         return proxiedJiniBody.getPolicy(securityContext);
311     }
312
313     public ArrayList JavaDoc getEntities() throws SecurityNotAvailableException, IOException JavaDoc {
314             return proxiedJiniBody.getEntities();
315         }
316
317
318     //
319
// -- PRIVATE METHODS -----------------------------------------------
320
//
321
}
322
Popular Tags