KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > body > request > RequestImpl


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.request;
32
33 import java.io.ByteArrayInputStream JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.StreamCorruptedException JavaDoc;
36 import java.security.cert.X509Certificate JavaDoc;
37
38 import org.apache.log4j.Logger;
39 import org.objectweb.proactive.Body;
40 import org.objectweb.proactive.ProActive;
41 import org.objectweb.proactive.core.body.UniversalBody;
42 import org.objectweb.proactive.core.body.message.MessageImpl;
43 import org.objectweb.proactive.core.body.reply.Reply;
44 import org.objectweb.proactive.core.body.reply.ReplyImpl;
45 import org.objectweb.proactive.core.mop.MethodCall;
46 import org.objectweb.proactive.core.mop.MethodCallExecutionFailedException;
47 import org.objectweb.proactive.ext.security.ProActiveSecurity;
48 import org.objectweb.proactive.ext.security.ProActiveSecurityManager;
49 import org.objectweb.proactive.ext.security.RenegotiateSessionException;
50 import org.objectweb.proactive.ext.security.SecurityNotAvailableException;
51
52 import sun.rmi.server.MarshalInputStream;
53
54
55 public class RequestImpl extends MessageImpl implements Request,
56     java.io.Serializable JavaDoc {
57     public static Logger logger = Logger.getLogger(RequestImpl.class.getName());
58     protected MethodCall methodCall;
59
60     /**
61      * Indicates if the method has been sent through a forwarder
62      */

63     protected int sendCounter;
64
65     /** transient because we deal with the serialization of this variable
66        in a custom manner. see writeObject method*/

67     protected transient UniversalBody sender;
68
69     private transient ProActiveSecurityManager psm;
70      private byte[][] methodCallCiphered;
71      private byte[] methodCallCipheredSignature;
72      public long sessionID;
73      protected String JavaDoc codebase;
74
75     //
76
// -- CONSTRUCTORS -----------------------------------------------
77
//
78
public RequestImpl(MethodCall methodCall, UniversalBody sender,
79         boolean isOneWay, long nextSequenceID) {
80         super(sender.getID(), nextSequenceID, isOneWay, methodCall.getName());
81         this.methodCall = methodCall;
82         this.sender = sender;
83     }
84
85     //
86
// -- PUBLIC METHODS -----------------------------------------------
87
//
88
//
89
// -- Implements Request -----------------------------------------------
90
//
91
public void send(UniversalBody destinationBody) throws java.io.IOException JavaDoc, RenegotiateSessionException {
92         //System.out.println("RequestSender: sendRequest " + methodName + " to destination");
93
sendCounter++;
94         sendRequest(destinationBody);
95     }
96
97     public UniversalBody getSender() {
98         return sender;
99     }
100
101     public Reply serve(Body targetBody) throws ServeException {
102         if (logger.isDebugEnabled()) {
103             logger.debug("serving " + this.getMethodName());
104         }
105         Object JavaDoc result = serveInternal(targetBody);
106         if (logger.isDebugEnabled()) {
107             if (result != null) {
108                 logger.debug("result " + result.getClass().getName());
109             } else {
110                 logger.debug("result null");
111             }
112         }
113         if (isOneWay || (sender == null)) {
114             return null;
115         }
116         return createReply(targetBody, result);
117     }
118
119     public boolean hasBeenForwarded() {
120         return sendCounter > 1;
121     }
122
123     public Object JavaDoc getParameter(int index) {
124         return methodCall.getParameter(index);
125     }
126
127     public MethodCall getMethodCall() {
128         return methodCall;
129     }
130
131     public void notifyReception(UniversalBody bodyReceiver)
132         throws java.io.IOException JavaDoc {
133         if (!hasBeenForwarded()) {
134             return;
135         }
136
137         // System.out.println("the request has been forwarded " + forwardCounter + " times");
138
//we know c.res is a remoteBody since the call has been forwarded
139
//if it is null, this is a one way call
140
if (sender != null) {
141             sender.updateLocation(bodyReceiver.getID(),
142                 bodyReceiver.getRemoteAdapter());
143         }
144     }
145
146     //
147
// -- PROTECTED METHODS -----------------------------------------------
148
//
149
protected Object JavaDoc serveInternal(Body targetBody) throws ServeException {
150         try {
151             return methodCall.execute(targetBody.getReifiedObject());
152         } catch (MethodCallExecutionFailedException e) {
153             e.printStackTrace();
154             throw new ServeException("serve method " +
155                 methodCall.getReifiedMethod().toString() + " failed", e);
156         } catch (java.lang.reflect.InvocationTargetException JavaDoc e) {
157             Throwable JavaDoc t = e.getTargetException();
158
159             // t.printStackTrace();
160
if (isOneWay) {
161                 throw new ServeException("serve method " +
162                     methodCall.getReifiedMethod().toString() + " failed", t);
163             } else {
164                 return t;
165             }
166         }
167     }
168
169     protected Reply createReply(Body targetBody, Object JavaDoc result) {
170         ProActiveSecurityManager psm = null;
171              try {
172                  psm = ProActive.getBodyOnThis().getProActiveSecurityManager();
173              } catch (java.io.IOException JavaDoc e) {
174                  e.printStackTrace();
175              } catch (SecurityNotAvailableException e) {
176                  // do nothing
177
}
178
179         return new ReplyImpl(targetBody.getID(), sequenceNumber, methodName,
180             result,psm);
181     }
182
183     protected void sendRequest(UniversalBody destinationBody)
184         throws java.io.IOException JavaDoc, RenegotiateSessionException {
185         if (logger.isDebugEnabled()) {
186             logger.debug(" sending request " + methodCall.getName());
187         }
188         try {
189                   if (!ciphered && !hasBeenForwarded()) {
190                       sessionID = 0;
191
192                       if (sender == null) {
193                           logger.warn("sender is null but why");
194                       }
195                 
196                       this.psm = sender.getProActiveSecurityManager();
197
198                       byte[] certE = destinationBody.getCertificateEncoded();
199                       X509Certificate JavaDoc cert = ProActiveSecurity.decodeCertificate(certE);
200                       sessionID = psm.getSessionIDTo(cert);
201
202                       if (sessionID != 0) {
203                           methodCallCiphered = psm.encrypt(sessionID, methodCall);
204                           ciphered = true;
205                           methodCall = null;
206                       }
207                   }
208               } catch (SecurityNotAvailableException e) {
209                   // do nothing
210
}
211
212         destinationBody.receiveRequest(this);
213         
214         if (logger.isDebugEnabled()) {
215             logger.debug(" sending request finished");
216         }
217     }
218
219     // security issue
220
public boolean isCiphered() {
221            return ciphered;
222        }
223
224        public boolean decrypt(ProActiveSecurityManager psm) throws RenegotiateSessionException{
225            // String localCodeBase = null;
226
// if (ciphered) {
227
if (ciphered) {
228                try {
229                    byte[] decryptedMethodCall = psm.decrypt(sessionID, methodCallCiphered);
230
231                    // System.out.println("ReceiveRequest :method call apres decryption : " + ProActiveSecurityManager.displayByte(decryptedMethodCall));
232
ByteArrayInputStream JavaDoc bin = new ByteArrayInputStream JavaDoc(decryptedMethodCall);
233                    MarshalInputStream in = new MarshalInputStream(bin);
234
235                    // ObjectInputStream in = new ObjectInputStream(bin);
236
methodCall = (MethodCall) in.readObject();
237                    in.close();
238                    ciphered = false;
239
240                    // logger.info("After decoding method call seq id " +sequenceNumber + ":" + ciphered + ":" + sessionID + " "+ methodCall + ":" +methodCallCiphered);
241
return true;
242                } catch (ClassNotFoundException JavaDoc e) {
243                    int index = e.toString().indexOf(':');
244                    String JavaDoc className = e.toString().substring(index).trim();
245                    className = className.substring(2);
246
247                    // // try {
248
// MOPClassLoader currentClassLoader = org.objectweb.proactive.core.mop.MOPClassLoader.createMOPClassLoader();
249
// this.getClass().getClassLoader().loadClass(className);
250
// currentClassLoader.loadClass(className);
251
this.decrypt(psm);
252
253                    // } catch (ClassNotFoundException ex) {
254
// e.printStackTrace();
255
// }
256

257                } catch (StreamCorruptedException JavaDoc e) {
258                    e.printStackTrace();
259                } catch (IOException JavaDoc e) {
260                    e.printStackTrace();
261                }
262                // System.setProperty("java.rmi.server.codebase",localCodeBase);
263
}
264
265            return false;
266        }
267
268        /* (non-Javadoc)
269           * @see org.objectweb.proactive.core.body.request.Request#getSessionId()
270           */

271          public long getSessionId() {
272              return sessionID;
273          }
274          
275     //
276
// -- PRIVATE METHODS FOR SERIALIZATION -----------------------------------------------
277
//
278
private void writeObject(java.io.ObjectOutputStream JavaDoc out)
279         throws java.io.IOException JavaDoc {
280         out.defaultWriteObject();
281         if (!isOneWay) {
282             out.writeObject(sender.getRemoteAdapter());
283         }
284     }
285
286     private void readObject(java.io.ObjectInputStream JavaDoc in)
287         throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc {
288         in.defaultReadObject();
289         if (!isOneWay) {
290             sender = (UniversalBody) in.readObject(); // it is actually a UniversalBody
291
}
292     }
293 }
294
Popular Tags