1 31 package org.objectweb.proactive.core.body; 32 33 import java.io.IOException ; 34 import java.security.PublicKey ; 35 import java.security.cert.X509Certificate ; 36 import java.util.ArrayList ; 37 import java.util.Hashtable ; 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 91 public abstract class AbstractBody extends AbstractUniversalBody implements Body, java.io.Serializable { 92 93 97 private static final String TERMINATED_BODY_EXCEPTION_MESSAGE = "The body has been Terminated"; 98 99 100 104 protected ThreadStore threadStore; 105 106 protected LocalBodyStrategy localBodyStrategy; 108 109 protected ProActiveSecurityManager psm; 111 protected boolean isSecurityOn = false; 112 protected transient InternalBodySecurity internalBodySecurity; 113 protected Hashtable openedSessions; 114 protected static Logger logger = Logger.getLogger(AbstractBody.class.getName()); 115 116 protected ProActiveGroupManager pgm; 118 119 123 124 private transient boolean isActive; 125 126 128 private transient boolean isDead; 129 130 131 135 139 public AbstractBody() {} 140 141 148 public AbstractBody(Object reifiedObject, String nodeURL, MetaObjectFactory factory) { 149 super(nodeURL, factory.newRemoteBodyFactory()); 150 this.threadStore = factory.newThreadStoreFactory().newThreadStore(); 151 152 this.pgm = factory.newProActiveGroupManagerFactory().newProActiveGroupManager(); 154 155 this.psm = factory.getProActiveSecurityManager(); 157 if (psm != null) { 158 isSecurityOn = (psm != null); 160 logger.debug("Security is on " + isSecurityOn); 161 psm.setBody(this); 162 internalBodySecurity = new InternalBodySecurity(null); 163 } 164 } 165 166 167 171 175 public String toString() { 176 return "Body for "+localBodyStrategy.getName()+" node="+nodeURL+" id=" + bodyID; 177 } 178 179 180 184 public void receiveRequest(Request request) throws java.io.IOException , RenegotiateSessionException { 185 try { 187 this.enterInThreadStore(); 188 if (this.isDead) { 189 throw new java.io.IOException (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 } 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 { 212 try { 214 enterInThreadStore(); 215 if (isDead) { 216 throw new java.io.IOException (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 e) { 225 e.printStackTrace(); 226 } 227 } 228 this.registerIncomingFutures(); 229 internalReceiveReply(reply); 230 } finally { 231 exitFromThreadStore(); 232 } 233 } 234 235 241 private void registerIncomingFutures (){ 242 java.util.ArrayList incomingFutures = FuturePool.getIncomingFutures(); 244 245 if (incomingFutures!=null) { 246 if (getFuturePool()!=null) { 248 java.util.Iterator it = incomingFutures.iterator(); 250 while (it.hasNext()){ 251 Future current = (Future)(it.next()); 252 getFuturePool().receiveFuture(current); 253 } 254 FuturePool.removeIncomingFutures(); 255 } else { 256 java.util.Iterator 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 , RenegotiateSessionException, 279 SecurityNotAvailableException { 280 try { 281 enterInThreadStore(); 282 if (!internalBodySecurity.isLocalBody() && 283 (openedSessions != null)) { 284 Long sessionID; 286 287 if (sID != 0) { 289 sessionID = new Long (sID); 290 if (openedSessions.containsKey(sessionID)) { 291 openedSessions.remove(sessionID); 292 internalBodySecurity.terminateSession(sID); 293 throw new RenegotiateSessionException(internalBodySecurity.getDistantBody()); 295 } 296 } 297 } 298 } finally { 299 exitFromThreadStore(); 300 } 301 } 302 303 public String getVNName() 304 throws java.io.IOException , 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 , 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 , 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 getCertificate() 357 throws java.io.IOException , SecurityNotAvailableException { 358 try { 359 enterInThreadStore(); 360 if (isSecurityOn) { 361 if (internalBodySecurity.isLocalBody()) { 362 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 , SecurityNotAvailableException { 379 try { 380 enterInThreadStore(); 381 if (isSecurityOn) { 382 if (internalBodySecurity.isLocalBody()) { 383 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 certificate) 401 throws java.io.IOException , SecurityNotAvailableException { 402 try { 403 enterInThreadStore(); 404 if (isSecurityOn) { 405 Policy pol; 406 407 if (internalBodySecurity.isLocalBody()) { 408 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 , RenegotiateSessionException, 428 SecurityNotAvailableException { 429 try { 430 enterInThreadStore(); 431 if (isSecurityOn) { 432 long sessionID; 433 434 if (internalBodySecurity.isLocalBody()) { 435 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 , KeyExchangeException, 457 SecurityNotAvailableException { 458 try { 459 enterInThreadStore(); 460 ConfidentialityTicket tick; 461 462 if (internalBodySecurity.isLocalBody()) { 463 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 getPublicKey() 483 throws java.io.IOException , SecurityNotAvailableException { 484 try { 485 enterInThreadStore(); 486 if (isSecurityOn) { 487 PublicKey pk; 488 489 if (internalBodySecurity.isLocalBody()) { 490 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 , SecurityNotAvailableException, Exception { 511 try { 512 enterInThreadStore(); 513 if (isSecurityOn) { 514 byte[] plop; 515 516 if (internalBodySecurity.isLocalBody()) { 517 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 , SecurityNotAvailableException, Exception { 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 , Exception , 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 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 type, String from, String to) 595 throws SecurityNotAvailableException, java.io.IOException { 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 , 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 , 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 this.psm = new DefaultProActiveSecurityManager("vide "); 655 isSecurityOn = true; 656 psm.setBody(this); 657 internalBodySecurity = new InternalBodySecurity(null); 658 } catch (Exception e) { 659 System.out.println( 660 "Error when contructing a DefaultProActiveManager"); 661 e.printStackTrace(); 662 } 663 } 664 665 public ArrayList getEntities() throws SecurityNotAvailableException, IOException { 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 687 public void terminate() { 688 if (isDead) return; 689 isDead = true; 690 activityStopped(); 691 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 Body body = LocalBodyStore.getInstance().getLocalBody(bodyID); 725 if (body != null) { 726 location.updateBody(bodyID, body); 728 return body; 729 } else { 730 return location.getBody(bodyID); 732 } 733 } 734 735 739 public FuturePool getFuturePool() { 740 return localBodyStrategy.getFuturePool(); 741 } 742 743 public BlockingRequestQueue getRequestQueue() { 744 return localBodyStrategy.getRequestQueue(); 745 } 746 747 public Object getReifiedObject() { 748 return localBodyStrategy.getReifiedObject(); 749 } 750 751 public String getName() { 752 return localBodyStrategy.getName(); 753 } 754 755 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 , RenegotiateSessionException{ 763 long sessionID = 0; 764 765 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 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 logger.debug("communication without security"); 786 } 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 } catch (AuthenticationException e) { 799 e.printStackTrace(); 800 } 801 } 802 803 807 813 protected abstract void internalReceiveRequest(Request request) throws java.io.IOException , RenegotiateSessionException; 814 815 820 protected abstract void internalReceiveReply(Reply reply) throws java.io.IOException ; 821 822 823 protected void setLocalBodyImpl(LocalBodyStrategy localBody) { 824 localBodyStrategy = localBody; 825 } 826 827 828 831 protected void activityStopped() { 832 if (! isActive) return; 833 isActive = false; 834 LocalBodyStore.getInstance().unregisterBody(this); 836 } 837 838 842 845 protected void activityStarted() { 846 if (isActive) return; 847 isActive = true; 848 LocalBodyStore.getInstance().setCurrentThreadBody(this); 850 LocalBodyStore.getInstance().registerBody(this); 852 } 853 854 858 public void setSPMDGroup(Object o) { 859 this.pgm.setSPMDGroup(o); 860 } 861 862 866 public Object getSPMDGroup() { 867 return this.pgm.getSPMDGroup(); 868 } 869 870 public void test() { 871 this.pgm.test(); 872 } 873 874 875 private void writeObject(java.io.ObjectOutputStream out) 879 throws java.io.IOException { 880 out.defaultWriteObject(); 881 } 882 883 private void readObject(java.io.ObjectInputStream in) 884 throws java.io.IOException , ClassNotFoundException { 885 in.defaultReadObject(); 886 logger = Logger.getLogger("AbstractBody"); 887 } 888 889 } 890 | Popular Tags |