1 31 package org.objectweb.proactive.core.runtime; 32 33 import org.objectweb.proactive.Body; 35 import org.objectweb.proactive.core.UniqueID; 36 import org.objectweb.proactive.core.body.LocalBodyStore; 37 import org.objectweb.proactive.core.body.UniversalBody; 38 import org.objectweb.proactive.core.descriptor.data.ProActiveDescriptor; 39 import org.objectweb.proactive.core.descriptor.data.VirtualNode; 40 import org.objectweb.proactive.core.descriptor.data.VirtualNodeImpl; 41 import org.objectweb.proactive.core.event.RuntimeRegistrationEvent; 42 import org.objectweb.proactive.core.event.RuntimeRegistrationEventProducerImpl; 43 import org.objectweb.proactive.core.mop.ConstructorCall; 44 import org.objectweb.proactive.core.mop.ConstructorCallExecutionFailedException; 45 import org.objectweb.proactive.core.node.NodeException; 46 import org.objectweb.proactive.core.process.UniversalProcess; 47 import org.objectweb.proactive.core.util.UrlBuilder; 48 import org.objectweb.proactive.ext.security.Entity; 49 import org.objectweb.proactive.ext.security.EntityCertificate; 50 import org.objectweb.proactive.ext.security.EntityVirtualNode; 51 import org.objectweb.proactive.ext.security.PolicyServer; 52 import org.objectweb.proactive.ext.security.ProActiveSecurityManager; 53 import org.objectweb.proactive.ext.security.SecurityNotAvailableException; 54 55 import java.io.File ; 56 import java.io.IOException ; 57 58 import java.security.PrivateKey ; 59 import java.security.Provider ; 60 import java.security.Security ; 61 import java.security.cert.X509Certificate ; 62 63 import java.util.ArrayList ; 64 import java.util.Enumeration ; 65 import java.util.Hashtable ; 66 import java.util.Random ; 67 68 69 79 public class ProActiveRuntimeImpl extends RuntimeRegistrationEventProducerImpl 80 implements ProActiveRuntime { 81 private static ProActiveRuntime proActiveRuntime = new ProActiveRuntimeImpl(); 86 private static Random prng = null; 88 private static synchronized int getNextInt() { 89 if (prng == null) { 90 prng = new Random (); 91 } 92 return prng.nextInt(); 93 } 94 95 private java.util.Hashtable policyServerMap; 97 98 private X509Certificate creatorCertificate; 100 private X509Certificate certificate; 101 private PrivateKey privateKey; 102 103 private ProActiveSecurityManager psm; 106 private String defaultNodeVirtualNode = null; 107 108 private VMInformation vmInformation; 112 113 private java.util.Hashtable nodeMap; 115 116 private Hashtable nodeJobIdMap; 118 119 private java.util.Hashtable virtualNodesMap; 121 122 private java.util.Hashtable descriptorMap; 124 125 private java.util.Hashtable proActiveRuntimeMap; 127 128 private ProActiveRuntimeImpl() { 133 try { 134 this.nodeMap = new java.util.Hashtable (); 135 136 this.vmInformation = new VMInformationImpl(); 137 this.proActiveRuntimeMap = new java.util.Hashtable (); 138 this.virtualNodesMap = new java.util.Hashtable (); 139 this.descriptorMap = new java.util.Hashtable (); 140 this.policyServerMap = new java.util.Hashtable (); 141 this.nodeJobIdMap = new java.util.Hashtable (); 142 String file = System.getProperties().getProperty("proactive.runtime.security"); 143 144 if ((file != null) && new File (file).exists()) { 145 logger.info("Runtime : loading policy file" + file); 147 Provider myProvider = new org.bouncycastle.jce.provider.BouncyCastleProvider(); 148 Security.addProvider(myProvider); 149 psm = new ProActiveSecurityManager(file); 151 } else { 152 logger.info("Runtime : loading policy file is null"); 154 } 158 159 } catch (java.net.UnknownHostException e) { 161 logger.fatal(" !!! Cannot do a reverse lookup on that host"); 163 e.printStackTrace(); 165 System.exit(1); 166 } catch (IOException e) { 167 e.printStackTrace(); 168 } 169 } 170 171 public static ProActiveRuntime getProActiveRuntime() { 175 return proActiveRuntime; 176 } 177 178 183 public void registerLocalVirtualNode(VirtualNode vn, String vnName) { 184 virtualNodesMap.put(vnName, vn); 186 } 187 188 public void registerDescriptor(String url, ProActiveDescriptor pad) { 189 descriptorMap.put(url, pad); 190 } 191 192 public ProActiveDescriptor getDescriptor(String url) { 193 return (ProActiveDescriptor) descriptorMap.get(url); 194 } 195 196 public void removeDescriptor(String url) { 197 descriptorMap.remove(url); 198 } 199 200 204 207 public String createLocalNode(String nodeName, 208 boolean replacePreviousBinding, PolicyServer ps, String vnName, 209 String jobId) throws NodeException { 210 if (replacePreviousBinding) { 213 if (nodeMap.get(nodeName) != null) { 214 nodeMap.remove(nodeName); 215 nodeJobIdMap.remove(nodeName); 216 } 217 } 218 if (!replacePreviousBinding && (nodeMap.get(nodeName) != null)) { 219 throw new NodeException("Node " + nodeName + 220 " already created on this ProActiveRuntime. To overwrite this node, use true for replacePreviousBinding"); 221 } 222 223 nodeMap.put(nodeName, new java.util.ArrayList ()); 224 nodeJobIdMap.put(nodeName, jobId); 225 226 if ((vnName != null) && (vnName.equals("currentJVM"))) { 227 vnName = defaultNodeVirtualNode; 229 logger.debug( 230 "++++++++++++++++++++++++++++++++++++++++++++CurrentJVM Tag ! Local Node : " + 231 nodeName + " VN name : " + vnName + " policyserver " + ps); 232 } else { 233 logger.debug( 234 "----------------------------------------Local Node : " + 235 nodeName + " VN name : " + vnName + " policyserver " + ps); 236 } 237 238 if (ps != null) { 239 logger.debug("generating node certificate"); 240 ps.generateNodeCertificate(vnName + " " + nodeName, vmInformation); 241 policyServerMap.put(nodeName, ps); 242 } 243 244 if (vnName != null) { 245 virtualNodesMap.put(nodeName, vnName); 246 } 247 248 return nodeName; 249 } 250 251 254 public void killAllNodes() { 255 virtualNodesMap.clear(); 256 policyServerMap.clear(); 257 nodeMap.clear(); 258 } 259 260 263 public void killNode(String nodeName) { 264 virtualNodesMap.remove(nodeName); 265 policyServerMap.remove(nodeName); 266 nodeMap.remove(nodeName); 267 } 268 269 272 public void createVM(UniversalProcess remoteProcess) 273 throws java.io.IOException { 274 remoteProcess.startProcess(); 275 } 276 277 280 public String [] getLocalNodeNames() { 281 int i = 0; 282 String [] nodeNames; 283 synchronized (nodeMap) { 284 nodeNames = new String [nodeMap.size()]; 285 for (java.util.Enumeration e = nodeMap.keys(); e.hasMoreElements();) { 286 nodeNames[i] = (String ) e.nextElement(); 287 i++; 288 } 289 } 290 return nodeNames; 291 } 292 293 296 public VMInformation getVMInformation() { 297 return vmInformation; 298 } 299 300 307 public void register(ProActiveRuntime proActiveRuntimeDist, 308 String proActiveRuntimeName, String creatorID, String creationProtocol, 309 String vmName) { 310 proActiveRuntimeMap.put(proActiveRuntimeName, proActiveRuntimeDist); 314 notifyListeners(this, RuntimeRegistrationEvent.RUNTIME_REGISTERED, 315 proActiveRuntimeName, creatorID, creationProtocol, vmName); 316 } 317 318 321 public ProActiveRuntime[] getProActiveRuntimes() { 322 int i = 0; 323 ProActiveRuntime[] runtimeArray; 324 synchronized (proActiveRuntimeMap) { 325 runtimeArray = new ProActiveRuntime[proActiveRuntimeMap.size()]; 326 for (java.util.Enumeration e = proActiveRuntimeMap.elements(); 327 e.hasMoreElements();) { 328 runtimeArray[i] = (ProActiveRuntime) e.nextElement(); 329 i++; 330 } 331 } 332 return runtimeArray; 333 } 334 335 338 public ProActiveRuntime getProActiveRuntime(String proActiveRuntimeName) { 339 return (ProActiveRuntime) proActiveRuntimeMap.get(proActiveRuntimeName); 340 } 341 342 345 public void killRT(boolean softly) { 346 System.exit(0); 347 } 348 349 352 public String getURL() { 353 return "//" + vmInformation.getInetAddress().getCanonicalHostName() + 354 "/" + vmInformation.getName(); 355 } 356 357 public ArrayList getActiveObjects(String nodeName) { 358 ArrayList bodyArray = (ArrayList ) ((ArrayList ) nodeMap.get(nodeName)).clone(); 361 362 ArrayList localBodies = new ArrayList (); 364 LocalBodyStore localBodystore = LocalBodyStore.getInstance(); 365 for (int i = 0; i < bodyArray.size(); i++) { 366 UniqueID bodyID = (UniqueID) bodyArray.get(i); 367 368 Body body = localBodystore.getLocalBody(bodyID); 370 if (body == null) { 371 runtimeLogger.warn("body null"); 372 unregisterBody(nodeName, bodyID); 375 } else { 376 ArrayList bodyAndObjectClass = new ArrayList (2); 379 380 bodyAndObjectClass.add(0, body.getRemoteAdapter()); 382 bodyAndObjectClass.add(1, 384 body.getReifiedObject().getClass().getName()); 385 localBodies.add(bodyAndObjectClass); 386 } 387 } 388 return localBodies; 389 } 390 391 public VirtualNode getVirtualNode(String virtualNodeName) { 392 return (VirtualNode) virtualNodesMap.get(virtualNodeName); 394 } 395 396 public void registerVirtualNode(String virtualNodeName, 397 boolean replacePreviousBinding) { 398 } 401 402 public void unregisterVirtualNode(String virtualNodeName) { 403 removeRuntimeRegistrationEventListener((VirtualNodeImpl) virtualNodesMap.get( 404 virtualNodeName)); 405 virtualNodesMap.remove(virtualNodeName); 406 } 407 408 public void unregisterAllVirtualNodes() { 409 virtualNodesMap.clear(); 410 } 411 412 415 public String getJobID(String nodeUrl) { 416 String name = UrlBuilder.getNameFromUrl(nodeUrl); 417 return (String ) nodeJobIdMap.get(name); 418 } 419 420 public ArrayList getActiveObjects(String nodeName, String objectName) { 421 ArrayList bodyArray = (ArrayList ) ((ArrayList ) nodeMap.get(nodeName)).clone(); 424 425 ArrayList localBodies = new ArrayList (); 427 LocalBodyStore localBodystore = LocalBodyStore.getInstance(); 428 for (int i = 0; i < bodyArray.size(); i++) { 429 UniqueID bodyID = (UniqueID) bodyArray.get(i); 430 431 Body body = localBodystore.getLocalBody(bodyID); 433 if (body == null) { 434 runtimeLogger.warn("body null"); 435 unregisterBody(nodeName, bodyID); 438 } else { 439 String objectClass = body.getReifiedObject().getClass().getName(); 440 441 if (objectClass.equals((String ) objectName)) { 444 localBodies.add(body.getRemoteAdapter()); 445 } 446 } 447 } 448 return localBodies; 449 } 450 451 454 public UniversalBody createBody(String nodeName, 455 ConstructorCall bodyConstructorCall, boolean isLocal) 456 throws ConstructorCallExecutionFailedException, 457 java.lang.reflect.InvocationTargetException { 458 Body localBody = (Body) bodyConstructorCall.execute(); 461 462 try { 464 localBody.getProActiveSecurityManager().setPolicyServer((PolicyServer) policyServerMap.get( 465 nodeName)); 466 } catch (IOException e) { 467 e.printStackTrace(); 468 } catch (SecurityNotAvailableException e) { 469 } 472 473 registerBody(nodeName, localBody); 474 475 if (isLocal) { 476 return (UniversalBody) localBody; 481 } else { 482 return localBody.getRemoteAdapter(); 486 } 487 } 488 489 492 public UniversalBody receiveBody(String nodeName, Body body) { 493 UniversalBody boa = body.getRemoteAdapter(); 494 try { 495 boa.getProActiveSecurityManager().setPolicyServer((PolicyServer) policyServerMap.get( 496 nodeName)); 497 } catch (IOException e) { 498 e.printStackTrace(); 499 } catch (SecurityNotAvailableException e) { 500 } 502 503 registerBody(nodeName, body); 504 return boa; 505 } 506 507 535 542 private void registerBody(String nodeName, Body body) { 543 UniqueID bodyID = body.getID(); 544 ArrayList bodyList = (ArrayList ) nodeMap.get(nodeName); 545 synchronized (bodyList) { 546 if (!bodyList.contains(bodyID)) { 547 bodyList.add(bodyID); 549 } 550 } 551 552 } 558 559 565 private void unregisterBody(String nodeName, UniqueID bodyID) { 566 ArrayList bodyList = (ArrayList ) nodeMap.get(nodeName); 569 synchronized (bodyList) { 570 bodyList.remove(bodyID); 571 } 573 } 574 575 protected static class VMInformationImpl implements VMInformation, 593 java.io.Serializable { 594 private java.net.InetAddress hostInetAddress; 595 596 private java.rmi.dgc.VMID uniqueVMID; 598 private String name; 599 private String processCreatorId; 600 private String jobId; 601 602 public VMInformationImpl() throws java.net.UnknownHostException { 603 this.uniqueVMID = UniqueID.getCurrentVMID(); 604 hostInetAddress = java.net.InetAddress.getLocalHost(); 605 String hostName = hostInetAddress.getCanonicalHostName(); 606 this.processCreatorId = "jvm"; 607 608 String random = Integer.toString(ProActiveRuntimeImpl.getNextInt()); 612 this.name = "PA_RT" + random + "_" + hostName; 613 if (System.getProperty("proactive.jobid") != null) { 614 this.jobId = System.getProperty("proactive.jobid"); 615 } else { 616 this.jobId = "JOB-" + random; 618 } 619 } 620 621 public java.rmi.dgc.VMID getVMID() { 628 return uniqueVMID; 629 } 630 631 public String getName() { 632 return name; 633 } 634 635 public java.net.InetAddress getInetAddress() { 636 return hostInetAddress; 637 } 638 639 public String getCreationProtocolID() { 640 return this.processCreatorId; 641 } 642 643 public void setCreationProtocolID(String protocolId) { 644 this.processCreatorId = protocolId; 645 } 646 647 650 public String getJobID() { 651 return this.jobId; 652 } 653 } 654 655 657 660 public X509Certificate getCreatorCertificate() { 661 return creatorCertificate; 662 } 663 664 667 public PolicyServer getPolicyServer() { 668 if (psm != null) { 670 return psm.getPolicyServer(); 671 } 672 return null; 673 } 674 675 public String getVNName(String nodeName) { 676 return (String ) virtualNodesMap.get(nodeName); 677 } 678 679 682 public void setProActiveSecurityManager(ProActiveSecurityManager server) { 683 if (psm != null) { 684 return; 685 } 686 psm = server; 687 } 688 689 692 public void setDefaultNodeVirtualNodeName(String s) { 693 System.out.println( 694 " ************************ **********************************setting current node as currentJVM tag " + 695 s); 696 defaultNodeVirtualNode = s; 697 } 698 699 public void listVirtualNodes() { 700 701 712 System.out.println("Local node 1"); 713 714 for (Enumeration e = nodeMap.keys(); e.hasMoreElements();) { 715 String key = (String ) e.nextElement(); 717 System.out.println("local element " + key); 718 } 719 720 System.out.println("Local node "); 721 for (Enumeration e = virtualNodesMap.elements(); e.hasMoreElements();) { 722 String key = (String ) e.nextElement(); 723 724 System.out.println(" node " + key + " belongs to "); 726 } 727 } 728 729 732 public PolicyServer getNodePolicyServer(String nodeName) { 733 return (PolicyServer) policyServerMap.get(nodeName); 734 } 735 736 739 public void enableSecurityIfNeeded() { 740 741 750 } 751 752 755 public X509Certificate getNodeCertificate(String nodeName) { 756 PolicyServer ps = null; 757 758 ps = (PolicyServer) policyServerMap.get(nodeName); 759 if (ps != null) { 760 return ps.getCertificate(); 761 } 762 return null; 763 } 764 765 768 public ArrayList getEntities(String nodeName) { 769 PolicyServer ps = null; 770 Entity nodeEntity = null; 771 String nodeVirtualName = (String ) virtualNodesMap.get(nodeName); 772 ps = (PolicyServer) policyServerMap.get(nodeName); 773 774 if (ps != null) { 775 nodeEntity = new EntityVirtualNode(nodeVirtualName, 776 ps.getApplicationCertificate(), ps.getCertificate()); 777 } 778 779 ArrayList entities = null; 780 781 if (entities == null) { 783 entities = new ArrayList (); 784 } 785 786 entities.add(nodeEntity); 787 return entities; 788 } 789 790 793 public ArrayList getEntities(UniversalBody uBody) { 794 try { 795 return uBody.getEntities(); 796 } catch (SecurityNotAvailableException e) { 797 return null; 798 } catch (IOException e) { 799 e.printStackTrace(); 800 } 801 return null; 802 } 803 804 807 public ArrayList getEntities() { 808 PolicyServer policyServer = psm.getPolicyServer(); 809 Entity e = new EntityCertificate(policyServer.getApplicationCertificate(), 810 policyServer.getCertificate()); 811 ArrayList array = new ArrayList (); 812 array.add(e); 813 814 return array; 815 } 816 817 820 public String getJobID() { 821 return vmInformation.getJobID(); 822 } 823 } 824 | Popular Tags |