KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > body > HalfBody


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;
32
33 import org.objectweb.proactive.core.ProActiveRuntimeException;
34 import org.objectweb.proactive.core.body.future.Future;
35 import org.objectweb.proactive.core.body.future.FuturePool;
36 import org.objectweb.proactive.core.body.reply.Reply;
37 import org.objectweb.proactive.core.body.reply.ReplyReceiver;
38 import org.objectweb.proactive.core.body.request.BlockingRequestQueue;
39 import org.objectweb.proactive.core.body.request.Request;
40 import org.objectweb.proactive.core.body.request.RequestFactory;
41 import org.objectweb.proactive.core.body.request.RequestImpl;
42 import org.objectweb.proactive.core.body.request.RequestQueue;
43 import org.objectweb.proactive.core.component.request.ComponentRequestImpl;
44 import org.objectweb.proactive.core.event.MessageEventListener;
45 import org.objectweb.proactive.core.mop.MethodCall;
46 import org.objectweb.proactive.core.runtime.ProActiveRuntimeImpl;
47 import org.objectweb.proactive.ext.security.CommunicationForbiddenException;
48 import org.objectweb.proactive.ext.security.InternalBodySecurity;
49 import org.objectweb.proactive.ext.security.ProActiveSecurity;
50 import org.objectweb.proactive.ext.security.RenegotiateSessionException;
51 import org.objectweb.proactive.ext.security.SecurityContext;
52 import org.objectweb.proactive.ext.security.SecurityNotAvailableException;
53 import org.objectweb.proactive.ext.security.crypto.AuthenticationException;
54
55 import java.security.cert.X509Certificate JavaDoc;
56
57
58 public class HalfBody extends AbstractBody {
59     //
60
// -- PRIVATE MEMBERS -----------------------------------------------
61
//
62
private static final String JavaDoc HALF_BODY_EXCEPTION_MESSAGE = "This method is not implemented in class HalfBody.";
63     private static final String JavaDoc NAME = "Other thread";
64
65     /** The component in charge of receiving reply */
66     private ReplyReceiver replyReceiver;
67
68     public synchronized static HalfBody getHalfBody(MetaObjectFactory factory) {
69         return new HalfBody(factory);
70     }
71
72     //
73
// -- CONSTRUCTORS -----------------------------------------------
74
//
75
private HalfBody(MetaObjectFactory factory) {
76         //SECURITY
77
super(new Object JavaDoc(), "LOCAL", factory);
78         //super(new Object(),
79
// NodeFactory.getDefaultNode().getNodeInformation().getURL(), factory);
80
// creating a default psm for HalfBody
81
// TODO get application certificate instead of generated one
82
//Object[] o = ProActiveSecurity.generateGenericCertificate();
83
//psm = new ProActiveSecurityManager((X509Certificate) o[0], (PrivateKey) o[1], null);
84
//psm = new ProActiveSecurityManager();
85
//isSecurityOn = true;
86
//psm.setBody(this);
87
internalBodySecurity = new InternalBodySecurity(null);
88
89         this.replyReceiver = factory.newReplyReceiverFactory().newReplyReceiver();
90         setLocalBodyImpl(new HalfLocalBodyStrategy(factory.newRequestFactory()));
91         this.localBodyStrategy.getFuturePool().setOwnerBody(this.getID());
92     }
93
94     //
95
// -- PUBLIC METHODS -----------------------------------------------
96
//
97
//
98
// -- implements MessageEventProducer -----------------------------------------------
99
//
100
public void addMessageEventListener(MessageEventListener listener) {
101     }
102
103     public void removeMessageEventListener(MessageEventListener listener) {
104     }
105
106     //
107
// -- PROTECTED METHODS -----------------------------------------------
108
//
109

110     /**
111      * Receives a request for later processing. The call to this method is non blocking
112      * unless the body cannot temporary receive the request.
113      * @param request the request to process
114      * @exception java.io.IOException if the request cannot be accepted
115      */

116     protected void internalReceiveRequest(Request request)
117         throws java.io.IOException JavaDoc {
118         throw new ProActiveRuntimeException(
119             "The method 'receiveRequest' is not implemented in class HalfBody.");
120     }
121
122     /**
123      * Receives a reply in response to a former request.
124      * @param reply the reply received
125      * @exception java.io.IOException if the reply cannot be accepted
126      */

127     protected void internalReceiveReply(Reply reply) throws java.io.IOException JavaDoc {
128         try {
129             if (reply.isCiphered()) {
130                 reply.decrypt(psm);
131             }
132         } catch (Exception JavaDoc e) {
133             e.printStackTrace();
134         }
135         replyReceiver.receiveReply(reply, this, getFuturePool());
136     }
137
138     public void setImmediateService(String JavaDoc methodName) {
139         throw new ProActiveRuntimeException(HALF_BODY_EXCEPTION_MESSAGE);
140     }
141
142      /**
143      * @see org.objectweb.proactive.Job#getJobID()
144      */

145     public String JavaDoc getJobID() {
146         return ProActiveRuntimeImpl.getProActiveRuntime().getJobID();
147     }
148
149     //
150
// -- inner classes -----------------------------------------------
151
//
152
private class HalfLocalBodyStrategy implements LocalBodyStrategy,
153         java.io.Serializable JavaDoc {
154
155         /** A pool future that contains the pending future objects */
156         protected FuturePool futures;
157         protected RequestFactory internalRequestFactory;
158         private long absoluteSequenceID;
159
160         //
161
// -- CONSTRUCTORS -----------------------------------------------
162
//
163
public HalfLocalBodyStrategy(RequestFactory requestFactory) {
164             this.futures = new FuturePool();
165             this.internalRequestFactory = requestFactory;
166         }
167
168         //
169
// -- PUBLIC METHODS -----------------------------------------------
170
//
171
//
172
// -- implements LocalBody -----------------------------------------------
173
//
174
public FuturePool getFuturePool() {
175             return futures;
176         }
177
178         public BlockingRequestQueue getRequestQueue() {
179             throw new ProActiveRuntimeException(HALF_BODY_EXCEPTION_MESSAGE);
180         }
181
182         public RequestQueue getHighPriorityRequestQueue() {
183             throw new ProActiveRuntimeException(HALF_BODY_EXCEPTION_MESSAGE);
184         }
185
186         public Object JavaDoc getReifiedObject() {
187             throw new ProActiveRuntimeException(HALF_BODY_EXCEPTION_MESSAGE);
188         }
189
190         public String JavaDoc getName() {
191             return NAME;
192         }
193
194         public void serve(Request request) {
195             throw new ProActiveRuntimeException(HALF_BODY_EXCEPTION_MESSAGE);
196         }
197
198         public void sendRequest(MethodCall methodCall, Future future,
199             UniversalBody destinationBody)
200             throws java.io.IOException JavaDoc, RenegotiateSessionException {
201             long sequenceID = getNextSequenceID();
202             Request request = internalRequestFactory.newRequest(methodCall,
203                     HalfBody.this, future == null, sequenceID);
204
205             // COMPONENTS : generate ComponentRequest for component messages
206
if (methodCall.getTag() != null) {
207                 if (methodCall.getTag().equals(MethodCall.COMPONENT_TAG)) {
208                     request = new ComponentRequestImpl((RequestImpl) request);
209                 }
210             }
211             if (future != null) {
212                 future.setID(sequenceID);
213                 futures.receiveFuture(future);
214             }
215
216             // SECURITY
217
long sessionID = 0;
218
219             // logger.debug("send Request Body" + destinationBody);
220
// logger.debug(" halfbla" + destinationBody.getRemoteAdapter());
221
try {
222                 try {
223                     if (!isSecurityOn) {
224                         logger.debug("security is off");
225                         throw new SecurityNotAvailableException();
226                     }
227                     if (internalBodySecurity.isLocalBody()) {
228                         byte[] certE = destinationBody.getRemoteAdapter()
229                                                       .getCertificateEncoded();
230                         X509Certificate JavaDoc cert = ProActiveSecurity.decodeCertificate(certE);
231                         if ((sessionID = psm.getSessionIDTo(cert)) == 0) {
232                             psm.initiateSession(SecurityContext.COMMUNICATION_SEND_REPLY_TO,
233                                 destinationBody.getRemoteAdapter());
234                             sessionID = psm.getSessionIDTo(cert);
235                         }
236                     }
237                 } catch (SecurityNotAvailableException e) {
238                     // do nothing
239
logger.debug("communication without security");
240                     //e.printStackTrace();
241
}
242                 request.send(destinationBody);
243             } catch (RenegotiateSessionException e) {
244                 //e.printStackTrace();
245
updateLocation(destinationBody.getID(), e.getUniversalBody());
246                 psm.terminateSession(sessionID);
247                 logger.debug("renegotiate session");
248                 sendRequest(methodCall, future, e.getUniversalBody());
249             } catch (CommunicationForbiddenException e) {
250                 logger.warn(e);
251                 //e.printStackTrace();
252
} catch (AuthenticationException e) {
253                 e.printStackTrace();
254             }
255         }
256
257         //
258
// -- PROTECTED METHODS -----------------------------------------------
259
//
260

261         /**
262          * Returns a unique identifier that can be used to tag a future, a request
263          * @return a unique identifier that can be used to tag a future, a request.
264          */

265         private synchronized long getNextSequenceID() {
266             return ++absoluteSequenceID;
267         }
268     }
269
270     // end inner class LocalHalfBody
271
}
272
Popular Tags