1 31 package org.objectweb.proactive.core.body.request; 32 33 import java.io.ByteArrayInputStream ; 34 import java.io.IOException ; 35 import java.io.StreamCorruptedException ; 36 import java.security.cert.X509Certificate ; 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 { 57 public static Logger logger = Logger.getLogger(RequestImpl.class.getName()); 58 protected MethodCall methodCall; 59 60 63 protected int sendCounter; 64 65 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 codebase; 74 75 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 public void send(UniversalBody destinationBody) throws java.io.IOException , RenegotiateSessionException { 92 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 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 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 { 133 if (!hasBeenForwarded()) { 134 return; 135 } 136 137 if (sender != null) { 141 sender.updateLocation(bodyReceiver.getID(), 142 bodyReceiver.getRemoteAdapter()); 143 } 144 } 145 146 protected Object 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 e) { 157 Throwable t = e.getTargetException(); 158 159 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 result) { 170 ProActiveSecurityManager psm = null; 171 try { 172 psm = ProActive.getBodyOnThis().getProActiveSecurityManager(); 173 } catch (java.io.IOException e) { 174 e.printStackTrace(); 175 } catch (SecurityNotAvailableException e) { 176 } 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 , 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 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 } 211 212 destinationBody.receiveRequest(this); 213 214 if (logger.isDebugEnabled()) { 215 logger.debug(" sending request finished"); 216 } 217 } 218 219 public boolean isCiphered() { 221 return ciphered; 222 } 223 224 public boolean decrypt(ProActiveSecurityManager psm) throws RenegotiateSessionException{ 225 if (ciphered) { 228 try { 229 byte[] decryptedMethodCall = psm.decrypt(sessionID, methodCallCiphered); 230 231 ByteArrayInputStream bin = new ByteArrayInputStream (decryptedMethodCall); 233 MarshalInputStream in = new MarshalInputStream(bin); 234 235 methodCall = (MethodCall) in.readObject(); 237 in.close(); 238 ciphered = false; 239 240 return true; 242 } catch (ClassNotFoundException e) { 243 int index = e.toString().indexOf(':'); 244 String className = e.toString().substring(index).trim(); 245 className = className.substring(2); 246 247 this.decrypt(psm); 252 253 257 } catch (StreamCorruptedException e) { 258 e.printStackTrace(); 259 } catch (IOException e) { 260 e.printStackTrace(); 261 } 262 } 264 265 return false; 266 } 267 268 271 public long getSessionId() { 272 return sessionID; 273 } 274 275 private void writeObject(java.io.ObjectOutputStream out) 279 throws java.io.IOException { 280 out.defaultWriteObject(); 281 if (!isOneWay) { 282 out.writeObject(sender.getRemoteAdapter()); 283 } 284 } 285 286 private void readObject(java.io.ObjectInputStream in) 287 throws java.io.IOException , ClassNotFoundException { 288 in.defaultReadObject(); 289 if (!isOneWay) { 290 sender = (UniversalBody) in.readObject(); } 292 } 293 } 294 | Popular Tags |