KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.IOException JavaDoc;
34 import java.security.PublicKey JavaDoc;
35 import java.security.cert.X509Certificate JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.Hashtable JavaDoc;
38
39 import org.apache.log4j.Logger;
40 import org.objectweb.proactive.Body;
41 import org.objectweb.proactive.core.UniqueID;
42 import org.objectweb.proactive.core.body.future.Future;
43 import org.objectweb.proactive.core.body.future.FuturePool;
44 import org.objectweb.proactive.core.body.future.FutureProxy;
45 import org.objectweb.proactive.core.body.reply.Reply;
46 import org.objectweb.proactive.core.body.request.BlockingRequestQueue;
47 import org.objectweb.proactive.core.body.request.Request;
48 import org.objectweb.proactive.core.group.ProActiveGroupManager;
49 import org.objectweb.proactive.core.mop.MethodCall;
50 import org.objectweb.proactive.core.util.ThreadStore;
51 import org.objectweb.proactive.ext.security.Communication;
52 import org.objectweb.proactive.ext.security.CommunicationForbiddenException;
53 import org.objectweb.proactive.ext.security.DefaultProActiveSecurityManager;
54 import org.objectweb.proactive.ext.security.InternalBodySecurity;
55 import org.objectweb.proactive.ext.security.Policy;
56 import org.objectweb.proactive.ext.security.ProActiveSecurity;
57 import org.objectweb.proactive.ext.security.ProActiveSecurityManager;
58 import org.objectweb.proactive.ext.security.RenegotiateSessionException;
59 import org.objectweb.proactive.ext.security.SecurityContext;
60 import org.objectweb.proactive.ext.security.SecurityNotAvailableException;
61 import org.objectweb.proactive.ext.security.crypto.AuthenticationException;
62 import org.objectweb.proactive.ext.security.crypto.ConfidentialityTicket;
63 import org.objectweb.proactive.ext.security.crypto.KeyExchangeException;
64
65
66 /**
67  * <i><font size="-1" color="#FF0000">**For internal use only** </font></i><br>
68  * <p>
69  * This class gives a common implementation of the Body interface. It provides all
70  * the non specific behavior allowing sub-class to write the detail implementation.
71  * </p><p>
72  * Each body is identify by an unique identifier.
73  * </p><p>
74  * All active bodies that get created in one JVM register themselves into a table that allows
75  * to tack them done. The registering and deregistering is done by the AbstractBody and
76  * the table is managed here as well using some static methods.
77  * </p><p>
78  * In order to let somebody customize the body of an active object without subclassing it,
79  * AbstractBody delegates lot of tasks to satellite objects that implements a given
80  * interface. Abstract protected methods instantiate those objects allowing subclasses
81  * to create them as they want (using customizable factories or instance).
82  * </p>
83  *
84  * @author ProActive Team
85  * @version 1.0, 2001/10/23
86  * @since ProActive 0.9
87  * @see Body
88  * @see UniqueID
89  *
90  */

91 public abstract class AbstractBody extends AbstractUniversalBody implements Body, java.io.Serializable JavaDoc {
92
93   //
94
// -- STATIC MEMBERS -----------------------------------------------
95
//
96

97   private static final String JavaDoc TERMINATED_BODY_EXCEPTION_MESSAGE = "The body has been Terminated";
98
99
100   //
101
// -- PROTECTED MEMBERS -----------------------------------------------
102
//
103

104   protected ThreadStore threadStore;
105   
106   // the current implementation of the local view of this body
107
protected LocalBodyStrategy localBodyStrategy;
108
109   // SECURITY
110
protected ProActiveSecurityManager psm;
111   protected boolean isSecurityOn = false;
112   protected transient InternalBodySecurity internalBodySecurity;
113   protected Hashtable JavaDoc openedSessions;
114   protected static Logger logger = Logger.getLogger(AbstractBody.class.getName());
115
116   // GROUP
117
protected ProActiveGroupManager pgm;
118
119   //
120
// -- PRIVATE MEMBERS -----------------------------------------------
121
//
122

123   /** whether the body has an activity done with a active thread */
124   private transient boolean isActive;
125
126   /** whether the body has been killed. A killed body has no more activity although
127    stopping the activity thread is not immediate */

128   private transient boolean isDead;
129
130
131   //
132
// -- CONSTRUCTORS -----------------------------------------------
133
//
134

135   /**
136    * Creates a new AbstractBody.
137    * Used for serialization.
138    */

139   public AbstractBody() {}
140
141   /**
142    * Creates a new AbstractBody for an active object attached to a given node.
143    * @param reifiedObject the active object that body is for
144    * @param nodeURL the URL of the node that body is attached to
145    * @param factory the factory able to construct new factories for each type of meta objects
146    * needed by this body
147    */

148   public AbstractBody(Object JavaDoc reifiedObject, String JavaDoc nodeURL, MetaObjectFactory factory) {
149     super(nodeURL, factory.newRemoteBodyFactory());
150     this.threadStore = factory.newThreadStoreFactory().newThreadStore();
151     
152     // GROUP
153
this.pgm = factory.newProActiveGroupManagerFactory().newProActiveGroupManager();
154
155     // SECURITY
156
this.psm = factory.getProActiveSecurityManager();
157        if (psm != null) {
158            // startDefaultProActiveSecurityManager();
159
isSecurityOn = (psm != null);
160            logger.debug("Security is on " + isSecurityOn);
161            psm.setBody(this);
162            internalBodySecurity = new InternalBodySecurity(null);
163        }
164   }
165
166
167   //
168
// -- PUBLIC METHODS -----------------------------------------------
169
//
170

171   /**
172    * Returns a string representation of this object.
173    * @return a string representation of this object
174    */

175   public String JavaDoc toString() {
176      return "Body for "+localBodyStrategy.getName()+" node="+nodeURL+" id=" + bodyID;
177   }
178
179
180   //
181
// -- implements UniversalBody -----------------------------------------------
182
//
183

184   public void receiveRequest(Request request) throws java.io.IOException JavaDoc, RenegotiateSessionException {
185     //System.out.println(" --> receiveRequest m="+request.getMethodName());
186
try {
187         this.enterInThreadStore();
188         if (this.isDead) {
189             throw new java.io.IOException JavaDoc(TERMINATED_BODY_EXCEPTION_MESSAGE);
190         }
191         if (this.isSecurityOn) {
192             try {
193                 this.renegociateSessionIfNeeded(request.getSessionId());
194                 if ((this.internalBodySecurity.isLocalBody()) &&
195                         request.isCiphered()) {
196                     request.decrypt(psm);
197                 }
198             } catch (SecurityNotAvailableException e) {
199                 // do nothing
200
}
201         }
202         this.registerIncomingFutures();
203         this.internalReceiveRequest(request);
204     } finally {
205         this.exitFromThreadStore();
206     }
207   }
208
209
210
211   public void receiveReply(Reply reply) throws java.io.IOException JavaDoc {
212     //System.out.println(" --> receiveReply m="+reply.getMethodName());
213
try {
214         enterInThreadStore();
215         if (isDead) {
216             throw new java.io.IOException JavaDoc(TERMINATED_BODY_EXCEPTION_MESSAGE);
217         }
218         if (isSecurityOn) {
219             try {
220                 if ((internalBodySecurity.isLocalBody()) &&
221                         reply.isCiphered()) {
222                     reply.decrypt(psm);
223                 }
224             } catch (Exception JavaDoc e) {
225                 e.printStackTrace();
226             }
227         }
228         this.registerIncomingFutures();
229         internalReceiveReply(reply);
230     } finally {
231         exitFromThreadStore();
232     }
233   }
234   
235   /**
236    * This method effectively register futures (ie in the futurePool) that arrive in this active
237    * object (by parameter or by result). Incoming futures have been registered in the static table
238    * FuturePool.incomingFutures during their deserialization. This effective registration must be perform
239    * AFTER entering in the ThreadStore.
240    */

241   private void registerIncomingFutures (){
242     // get list of futures that should be deserialized and registred "behind the ThreadStore"
243
java.util.ArrayList JavaDoc incomingFutures = FuturePool.getIncomingFutures();
244     
245     if (incomingFutures!=null) {
246         // if futurePool is not null, we are in an Active Body
247
if (getFuturePool()!=null) {
248             // some futures have to be registred in the local futurePool
249
java.util.Iterator JavaDoc it = incomingFutures.iterator();
250             while (it.hasNext()){
251                 Future current = (Future)(it.next());
252                 getFuturePool().receiveFuture(current);
253             }
254             FuturePool.removeIncomingFutures();
255          } else {
256             // we are in a forwarder
257
// some futures have to set their continuation tag
258
java.util.Iterator JavaDoc it = incomingFutures.iterator();
259             while (it.hasNext()){
260                 FutureProxy current = (FutureProxy)(it.next());
261                 current.setContinuationTag();
262             }
263             FuturePool.removeIncomingFutures();
264          }
265     }
266   }
267   
268   
269   public void enableAC(){
270     localBodyStrategy.getFuturePool().enableAC();
271   }
272   
273   public void disableAC(){
274     localBodyStrategy.getFuturePool().disableAC();
275   }
276
277   public void renegociateSessionIfNeeded(long sID)
278       throws IOException JavaDoc, RenegotiateSessionException,
279           SecurityNotAvailableException {
280       try {
281           enterInThreadStore();
282           if (!internalBodySecurity.isLocalBody() &&
283                   (openedSessions != null)) {
284               // inside a forwarder
285
Long JavaDoc sessionID;
286
287               //long sID = request.getSessionId();
288
if (sID != 0) {
289                   sessionID = new Long JavaDoc(sID);
290                   if (openedSessions.containsKey(sessionID)) {
291                       openedSessions.remove(sessionID);
292                       internalBodySecurity.terminateSession(sID);
293                       //System.out.println("Object has migrated : Renegotiate Session");
294
throw new RenegotiateSessionException(internalBodySecurity.getDistantBody());
295                   }
296               }
297           }
298       } finally {
299           exitFromThreadStore();
300       }
301   }
302
303   public String JavaDoc getVNName()
304       throws java.io.IOException JavaDoc, SecurityNotAvailableException {
305       try {
306           enterInThreadStore();
307           if (isSecurityOn) {
308               if (internalBodySecurity.isLocalBody()) {
309                   return psm.getVNName();
310               } else {
311                   return internalBodySecurity.getVNName();
312               }
313           }
314       } finally {
315           exitFromThreadStore();
316       }
317       return null;
318   }
319
320   public void initiateSession(int type, UniversalBody body)
321       throws java.io.IOException JavaDoc, CommunicationForbiddenException,
322           org.objectweb.proactive.ext.security.crypto.AuthenticationException,
323           RenegotiateSessionException, SecurityNotAvailableException {
324       try {
325           enterInThreadStore();
326           if (isSecurityOn) {
327               if (internalBodySecurity.isLocalBody()) {
328                   psm.initiateSession(type,body);
329               } else {
330                   internalBodySecurity.initiateSession(type,body);
331               }
332           }
333       } finally {
334           exitFromThreadStore();
335       }
336       throw new SecurityNotAvailableException();
337   }
338
339   public void terminateSession(long sessionID)
340       throws java.io.IOException JavaDoc, SecurityNotAvailableException {
341       try {
342           enterInThreadStore();
343           if (isSecurityOn) {
344               if (internalBodySecurity.isLocalBody()) {
345                   psm.terminateSession(sessionID);
346               } else {
347                   internalBodySecurity.terminateSession(sessionID);
348               }
349           }
350           throw new SecurityNotAvailableException();
351       } finally {
352           exitFromThreadStore();
353       }
354   }
355
356   public X509Certificate JavaDoc getCertificate()
357       throws java.io.IOException JavaDoc, SecurityNotAvailableException {
358       try {
359           enterInThreadStore();
360           if (isSecurityOn) {
361               if (internalBodySecurity.isLocalBody()) {
362                   // System.out.println(" getCertificate on demande un security manager a " + ProActive.getBodyOnThis());
363
// if (psm == null) {
364
// startDefaultProActiveSecurityManager();
365
//}
366
return psm.getCertificate();
367               } else {
368                   return internalBodySecurity.getCertificate();
369               }
370           }
371           throw new SecurityNotAvailableException();
372       } finally {
373           exitFromThreadStore();
374       }
375   }
376
377   public ProActiveSecurityManager getProActiveSecurityManager()
378       throws java.io.IOException JavaDoc, SecurityNotAvailableException {
379       try {
380           enterInThreadStore();
381           if (isSecurityOn) {
382               if (internalBodySecurity.isLocalBody()) {
383                   // System.out.println("getProActiveSecurityManager on demande un security manager a " + ProActive.getBodyOnThis());
384
// if (psm == null) {
385
// startDefaultProActiveSecurityManager();
386
// }
387
return psm;
388               } else {
389                   ProActiveSecurityManager plop = internalBodySecurity.getProActiveSecurityManager();
390
391                   return plop;
392               }
393           }
394           throw new SecurityNotAvailableException();
395       } finally {
396           exitFromThreadStore();
397       }
398   }
399
400   public Policy getPolicyFrom(X509Certificate JavaDoc certificate)
401       throws java.io.IOException JavaDoc, SecurityNotAvailableException {
402       try {
403           enterInThreadStore();
404           if (isSecurityOn) {
405               Policy pol;
406
407               if (internalBodySecurity.isLocalBody()) {
408                   // if (psm == null) {
409
// startDefaultProActiveSecurityManager();
410
// }
411
pol = psm.getPolicyTo(certificate);
412
413                   return pol;
414               } else {
415                   pol = internalBodySecurity.getPolicyFrom(certificate);
416
417                   return pol;
418               }
419           }
420           throw new SecurityNotAvailableException();
421       } finally {
422           exitFromThreadStore();
423       }
424   }
425
426   public long startNewSession(Communication policy)
427       throws java.io.IOException JavaDoc, RenegotiateSessionException,
428           SecurityNotAvailableException {
429       try {
430           enterInThreadStore();
431           if (isSecurityOn) {
432               long sessionID;
433
434               if (internalBodySecurity.isLocalBody()) {
435                   //System.out.println("startNewSession on demande un security manager a " + ProActive.getBodyOnThis());
436
//if (psm == null) {
437
//startDefaultProActiveSecurityManager();
438
//}
439
sessionID = psm.startNewSession(policy);
440
441                   return sessionID;
442               } else {
443                   sessionID = internalBodySecurity.startNewSession(policy);
444
445                   return sessionID;
446               }
447           }
448           throw new SecurityNotAvailableException();
449       } finally {
450           exitFromThreadStore();
451       }
452   }
453
454   public ConfidentialityTicket negociateKeyReceiverSide(
455       ConfidentialityTicket confidentialityTicket, long sessionID)
456       throws java.io.IOException JavaDoc, KeyExchangeException,
457           SecurityNotAvailableException {
458       try {
459           enterInThreadStore();
460           ConfidentialityTicket tick;
461
462           if (internalBodySecurity.isLocalBody()) {
463               //System.out.println("negociateKeyReceiverSide on demande un security manager a " + ProActive.getBodyOnThis());
464
//if (psm == null) {
465
// startDefaultProActiveSecurityManager();
466
// }
467
tick = psm.keyNegociationReceiverSide(confidentialityTicket,
468                       sessionID);
469
470               return tick;
471           } else {
472               tick = internalBodySecurity.negociateKeyReceiverSide(confidentialityTicket,
473                       sessionID);
474
475               return tick;
476           }
477       } finally {
478           exitFromThreadStore();
479       }
480   }
481
482   public PublicKey JavaDoc getPublicKey()
483       throws java.io.IOException JavaDoc, SecurityNotAvailableException {
484       try {
485           enterInThreadStore();
486           if (isSecurityOn) {
487               PublicKey JavaDoc pk;
488
489               if (internalBodySecurity.isLocalBody()) {
490                   // System.out.println("getPublicKey on demande un security manager a " + ProActive.getBodyOnThis());
491
//if (psm == null) {
492
// startDefaultProActiveSecurityManager();
493
// }
494
pk = psm.getPublicKey();
495
496                   return pk;
497               } else {
498                   pk = internalBodySecurity.getPublicKey();
499
500                   return pk;
501               }
502           }
503           throw new SecurityNotAvailableException();
504       } finally {
505           exitFromThreadStore();
506       }
507   }
508
509   public byte[] randomValue(long sessionID, byte[] cl_rand)
510       throws java.io.IOException JavaDoc, SecurityNotAvailableException, Exception JavaDoc {
511       try {
512           enterInThreadStore();
513           if (isSecurityOn) {
514               byte[] plop;
515
516               if (internalBodySecurity.isLocalBody()) {
517                   // System.out.println("randomValue on demande un security manager a " + ProActive.getBodyOnThis());
518
plop = psm.randomValue(sessionID, cl_rand);
519
520                   return plop;
521               } else {
522                   plop = internalBodySecurity.randomValue(sessionID, cl_rand);
523
524                   return plop;
525               }
526           }
527           throw new SecurityNotAvailableException();
528       } finally {
529           exitFromThreadStore();
530       }
531   }
532
533   public byte[][] publicKeyExchange(long sessionID,
534       UniversalBody distantBody, byte[] my_pub, byte[] my_cert,
535       byte[] sig_code)
536       throws java.io.IOException JavaDoc, SecurityNotAvailableException, Exception JavaDoc {
537       try {
538           enterInThreadStore();
539           if (isSecurityOn) {
540               renegociateSessionIfNeeded(sessionID);
541
542               byte[][] pke;
543
544               if (internalBodySecurity.isLocalBody()) {
545                   pke = psm.publicKeyExchange(sessionID, distantBody, my_pub,
546                           my_cert, sig_code);
547
548                   return pke;
549               } else {
550                   pke = internalBodySecurity.publicKeyExchange(sessionID,
551                           distantBody, my_pub, my_cert, sig_code);
552
553                   return pke;
554               }
555           }
556           throw new SecurityNotAvailableException();
557       } finally {
558           exitFromThreadStore();
559       }
560   }
561
562   public byte[][] secretKeyExchange(long sessionID, byte[] tmp, byte[] tmp1,
563       byte[] tmp2, byte[] tmp3, byte[] tmp4)
564       throws java.io.IOException JavaDoc, Exception JavaDoc, SecurityNotAvailableException {
565       try {
566           enterInThreadStore();
567           if (!isSecurityOn) {
568               throw new SecurityNotAvailableException();
569           }
570
571           renegociateSessionIfNeeded(sessionID);
572
573           byte[][] ske;
574
575           renegociateSessionIfNeeded(sessionID);
576
577           if (internalBodySecurity.isLocalBody()) {
578               // System.out.println("secretKeyExchange demande un security manager a " + ProActive.getBodyOnThis());
579
ske = psm.secretKeyExchange(sessionID, tmp, tmp1, tmp2, tmp3,
580                       tmp4);
581
582               return ske;
583           } else {
584               ske = internalBodySecurity.secretKeyExchange(sessionID, tmp,
585                       tmp1, tmp2, tmp3, tmp4);
586
587               return ske;
588           }
589       } finally {
590           threadStore.exit();
591       }
592   }
593
594   public Communication getPolicyTo(String JavaDoc type, String JavaDoc from, String JavaDoc to)
595       throws SecurityNotAvailableException, java.io.IOException JavaDoc {
596       try {
597           enterInThreadStore();
598           if (!isSecurityOn) {
599               throw new SecurityNotAvailableException();
600           }
601
602           if (internalBodySecurity.isLocalBody()) {
603               return psm.getPolicyTo(type, from, to);
604           } else {
605               return internalBodySecurity.getPolicyTo(type, from, to);
606           }
607       } finally {
608           exitFromThreadStore();
609       }
610   }
611
612   public SecurityContext getPolicy(SecurityContext securityContext)
613       throws java.io.IOException JavaDoc, SecurityNotAvailableException {
614       try {
615           enterInThreadStore();
616           if (!isSecurityOn) {
617               throw new SecurityNotAvailableException();
618           }
619
620           if (internalBodySecurity.isLocalBody()) {
621               return psm.getPolicy(securityContext);
622           } else {
623               return internalBodySecurity.getPolicy(securityContext);
624           }
625       } finally {
626           exitFromThreadStore();
627       }
628   }
629
630   public byte[] getCertificateEncoded()
631       throws IOException JavaDoc, SecurityNotAvailableException {
632       try {
633           enterInThreadStore();
634           if (psm == null) {
635               startDefaultProActiveSecurityManager();
636           }
637           if (!isSecurityOn) {
638               throw new SecurityNotAvailableException();
639           }
640
641           if (internalBodySecurity.isLocalBody()) {
642               return psm.getCertificateEncoded();
643           } else {
644               return internalBodySecurity.getCertificatEncoded();
645           }
646       } finally {
647           exitFromThreadStore();
648       }
649   }
650
651   protected void startDefaultProActiveSecurityManager() {
652       try {
653           // logger.info("starting a new psm ");
654
this.psm = new DefaultProActiveSecurityManager("vide ");
655           isSecurityOn = true;
656           psm.setBody(this);
657           internalBodySecurity = new InternalBodySecurity(null);
658       } catch (Exception JavaDoc e) {
659           System.out.println(
660               "Error when contructing a DefaultProActiveManager");
661           e.printStackTrace();
662       }
663   }
664
665   public ArrayList JavaDoc getEntities() throws SecurityNotAvailableException, IOException JavaDoc {
666       try {
667             enterInThreadStore();
668             if (!isSecurityOn) {
669                 throw new SecurityNotAvailableException();
670             }
671
672             if (internalBodySecurity.isLocalBody()) {
673                 return psm.getEntities();
674             } else {
675                 return internalBodySecurity.getEntities();
676             }
677         } finally {
678             exitFromThreadStore();
679         }
680     }
681
682
683   //
684
// -- implements Body -----------------------------------------------
685
//
686

687   public void terminate() {
688     if (isDead) return;
689     isDead = true;
690     activityStopped();
691     // unblock is thread was block
692
acceptCommunication();
693   }
694   
695
696   public void blockCommunication() {
697      threadStore.close();
698   }
699
700   public void acceptCommunication() {
701      threadStore.open();
702   }
703   
704   public void enterInThreadStore() {
705         threadStore.enter();
706   }
707   
708   public void exitFromThreadStore() {
709     threadStore.exit();
710   }
711  
712
713   public boolean isAlive() {
714     return !isDead;
715   }
716
717   public boolean isActive() {
718     return isActive;
719   }
720
721
722   public UniversalBody checkNewLocation(UniqueID bodyID) {
723     //we look in the location table of the current JVM
724
Body body = LocalBodyStore.getInstance().getLocalBody(bodyID);
725     if (body != null) {
726       // we update our table to say that this body is local
727
location.updateBody(bodyID, body);
728       return body;
729     } else {
730       //it was not found in this vm let's try the location table
731
return location.getBody(bodyID);
732     }
733   }
734
735   //
736
// -- implements LocalBody -----------------------------------------------
737
//
738

739   public FuturePool getFuturePool() {
740     return localBodyStrategy.getFuturePool();
741   }
742
743   public BlockingRequestQueue getRequestQueue() {
744     return localBodyStrategy.getRequestQueue();
745   }
746
747   public Object JavaDoc getReifiedObject() {
748     return localBodyStrategy.getReifiedObject();
749   }
750
751   public String JavaDoc getName() {
752     return localBodyStrategy.getName();
753   }
754
755         /** Serves the request. The request should be removed from the request queue
756          * before serving, which is correctly done by all methods of the Service class.
757          * However, this condition is not ensured for custom calls on serve. */

758   public void serve(Request request) {
759     localBodyStrategy.serve(request);
760   }
761
762   public void sendRequest(MethodCall methodCall, Future future, UniversalBody destinationBody) throws java.io.IOException JavaDoc , RenegotiateSessionException{
763     long sessionID = 0;
764
765     // logger.debug("send Request Body" + destinationBody);
766
//logger.debug(" bla" + destinationBody.getRemoteAdapter());
767
try {
768         try {
769             if (!isSecurityOn) {
770                 logger.debug("security is off");
771                 throw new SecurityNotAvailableException();
772             }
773             if (internalBodySecurity.isLocalBody()) {
774                 System.out.println("send Request AbstractBody");
775                 byte[] certE = destinationBody.getRemoteAdapter()
776                                               .getCertificateEncoded();
777                 X509Certificate JavaDoc cert = ProActiveSecurity.decodeCertificate(certE);
778                 if ((sessionID = psm.getSessionIDTo(cert)) == 0) {
779                     psm.initiateSession(SecurityContext.COMMUNICATION_SEND_REQUEST_TO, destinationBody.getRemoteAdapter());
780                     sessionID = psm.getSessionIDTo(cert);
781                 }
782             }
783         } catch (SecurityNotAvailableException e) {
784             // do nothing
785
logger.debug("communication without security");
786             //e.printStackTrace();
787
}
788         localBodyStrategy.sendRequest(methodCall, future, destinationBody);
789     } catch (RenegotiateSessionException e) {
790         if (e.getUniversalBody() != null) {
791             updateLocation(destinationBody.getID(), e.getUniversalBody());
792         }
793         psm.terminateSession(sessionID);
794         sendRequest(methodCall, future, e.getUniversalBody());
795     } catch (CommunicationForbiddenException e) {
796         logger.warn(e);
797         //e.printStackTrace();
798
} catch (AuthenticationException e) {
799         e.printStackTrace();
800     }
801   }
802
803   //
804
// -- PROTECTED METHODS -----------------------------------------------
805
//
806

807   /**
808    * Receives a request for later processing. The call to this method is non blocking
809    * unless the body cannot temporary receive the request.
810    * @param request the request to process
811    * @exception java.io.IOException if the request cannot be accepted
812    */

813   protected abstract void internalReceiveRequest(Request request) throws java.io.IOException JavaDoc, RenegotiateSessionException;
814
815   /**
816    * Receives a reply in response to a former request.
817    * @param reply the reply received
818    * @exception java.io.IOException if the reply cannot be accepted
819    */

820   protected abstract void internalReceiveReply(Reply reply) throws java.io.IOException JavaDoc;
821
822
823   protected void setLocalBodyImpl(LocalBodyStrategy localBody) {
824     localBodyStrategy = localBody;
825   }
826   
827   
828   /**
829    * Signals that the activity of this body, managed by the active thread has just stopped.
830    */

831   protected void activityStopped() {
832     if (! isActive) return;
833     isActive = false;
834     //We are no longer an active body
835
LocalBodyStore.getInstance().unregisterBody(this);
836   }
837
838 //protected void activityStopped2(){
839
// LocalBodyStore.getInstance().unregisterBody(this);
840
//}
841

842   /**
843    * Signals that the activity of this body, managed by the active thread has just started.
844    */

845   protected void activityStarted() {
846     if (isActive) return;
847     isActive = true;
848     // we associated this body to the thread running it
849
LocalBodyStore.getInstance().setCurrentThreadBody(this);
850     // we register in this JVM
851
LocalBodyStore.getInstance().registerBody(this);
852   }
853
854   /**
855    * Set the SPMD group for the active object
856    * @param o - the new SPMD group
857    */

858   public void setSPMDGroup(Object JavaDoc o) {
859       this.pgm.setSPMDGroup(o);
860   }
861     
862   /**
863    * Returns the SPMD group of the active object
864    * @return the SPMD group of the active object
865    */

866   public Object JavaDoc getSPMDGroup() {
867       return this.pgm.getSPMDGroup();
868   }
869
870     public void test() {
871         this.pgm.test();
872     }
873
874
875   //
876
// -- SERIALIZATION METHODS -----------------------------------------------
877
//
878
private void writeObject(java.io.ObjectOutputStream JavaDoc out)
879       throws java.io.IOException JavaDoc {
880       out.defaultWriteObject();
881   }
882
883   private void readObject(java.io.ObjectInputStream JavaDoc in)
884       throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc {
885       in.defaultReadObject();
886       logger = Logger.getLogger("AbstractBody");
887   }
888
889 }
890
Popular Tags