KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > body > reply > ReplyImpl


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.reply;
32
33 import java.io.ByteArrayInputStream JavaDoc;
34 import java.io.ObjectInputStream JavaDoc;
35
36 import org.objectweb.proactive.core.UniqueID;
37 import org.objectweb.proactive.core.body.LocalBodyStore;
38 import org.objectweb.proactive.core.body.UniversalBody;
39 import org.objectweb.proactive.core.body.message.MessageImpl;
40 import org.objectweb.proactive.core.mop.Utils;
41 import org.objectweb.proactive.ext.security.ProActiveSecurityManager;
42 import org.objectweb.proactive.ext.security.RenegotiateSessionException;
43 import org.objectweb.proactive.ext.security.SecurityContext;
44 import org.objectweb.proactive.ext.security.SecurityNotAvailableException;
45
46
47 public class ReplyImpl extends MessageImpl implements Reply, java.io.Serializable JavaDoc {
48
49   /**
50    * The hypothetic result
51    */

52   protected Object JavaDoc result;
53   
54   //security features
55

56    /**
57     * the encrypted result
58     */

59    protected byte[][] encryptedResult;
60
61
62    /*
63     * the session ID used to find the key and decrypt the reply
64     */

65    protected long sessionID;
66    protected transient ProActiveSecurityManager psm = null;
67   
68   public ReplyImpl(UniqueID senderID, long sequenceNumber, String JavaDoc methodName, Object JavaDoc result, ProActiveSecurityManager psm) {
69     super(senderID, sequenceNumber, true, methodName);
70     this.result = result;
71     this.psm = psm;
72   }
73
74   public Object JavaDoc getResult() {
75     return result;
76   }
77     
78   public void send(UniversalBody destinationBody) throws java.io.IOException JavaDoc {
79     // if destination body is on the same VM that the sender, we must perform
80
// a deep copy of result in order to preserve ProActive model.
81
UniqueID destinationID = destinationBody.getID();
82     boolean isLocal = ((LocalBodyStore.getInstance().getLocalBody(destinationID) != null)
83                         || (LocalBodyStore.getInstance().getLocalHalfBody(destinationID) != null));
84     
85     if (isLocal) {
86         result = Utils.makeDeepCopy(result);
87     }
88     // security issue
89
// System.out.println("ReplyImpl send : Current Thread " + Thread.currentThread().getName() + " result : " + result + "!");
90
if (!ciphered) {
91            long sessionID = 0;
92
93            try {
94                sessionID = psm.getSessionIDTo(destinationBody.getCertificate());
95
96                if (sessionID == 0) {
97                    psm.initiateSession(SecurityContext.COMMUNICATION_SEND_REPLY_TO, destinationBody);
98                    sessionID = psm.getSessionIDTo(destinationBody.getCertificate());
99                }
100
101                if (sessionID != 0) {
102                    encryptedResult = psm.encrypt(sessionID, result);
103
104                    ciphered = true;
105                    this.sessionID = sessionID;
106                }
107
108                // result = null;
109
} catch (SecurityNotAvailableException e) {
110                // do nothing
111
} catch (Exception JavaDoc e) {
112                e.printStackTrace();
113            }
114        }
115
116        // end security
117
destinationBody.receiveReply(this);
118   }
119   
120   // security issue
121
public boolean isCiphered() {
122       return ciphered;
123   }
124
125   public boolean decrypt(ProActiveSecurityManager psm) throws RenegotiateSessionException {
126       if ((sessionID != 0) && ciphered) {
127           byte[] decryptedMethodCall = psm.decrypt(sessionID, encryptedResult);
128           try {
129               ByteArrayInputStream JavaDoc bin = new ByteArrayInputStream JavaDoc(decryptedMethodCall);
130               ObjectInputStream JavaDoc in = new ObjectInputStream JavaDoc(bin);
131               result = (Object JavaDoc) in.readObject();
132               in.close();
133               return true;
134           } catch (Exception JavaDoc e) {
135               e.printStackTrace();
136           }
137       }
138
139       return false;
140   }
141
142 /* (non-Javadoc)
143  * @see org.objectweb.proactive.core.body.reply.Reply#getSessionId()
144  */

145 public long getSessionId() {
146     return sessionID;
147 }
148 }
149
Popular Tags