1 31 package org.objectweb.proactive; 32 33 import org.apache.log4j.Logger; 34 35 import org.objectweb.fractal.api.Component; 36 import org.objectweb.fractal.api.NoSuchInterfaceException; 37 import org.objectweb.fractal.api.factory.GenericFactory; 38 import org.objectweb.fractal.api.factory.InstantiationException; 39 import org.objectweb.fractal.util.Fractal; 40 41 import org.objectweb.proactive.core.Constants; 42 import org.objectweb.proactive.core.ProActiveException; 43 import org.objectweb.proactive.core.ProActiveRuntimeException; 44 import org.objectweb.proactive.core.body.LocalBodyStore; 45 import org.objectweb.proactive.core.body.MetaObjectFactory; 46 import org.objectweb.proactive.core.body.ProActiveMetaObjectFactory; 47 import org.objectweb.proactive.core.body.UniversalBody; 48 import org.objectweb.proactive.core.body.future.Future; 49 import org.objectweb.proactive.core.body.future.FuturePool; 50 import org.objectweb.proactive.core.body.ibis.IbisRemoteBodyAdapter; 51 import org.objectweb.proactive.core.body.migration.Migratable; 52 import org.objectweb.proactive.core.body.migration.MigrationException; 53 import org.objectweb.proactive.core.body.proxy.BodyProxy; 54 import org.objectweb.proactive.core.body.request.BodyRequest; 55 import org.objectweb.proactive.core.body.rmi.RemoteBodyAdapter; 56 import org.objectweb.proactive.core.component.ComponentParameters; 57 import org.objectweb.proactive.core.component.ContentDescription; 58 import org.objectweb.proactive.core.component.ControllerDescription; 59 import org.objectweb.proactive.core.config.ProActiveConfiguration; 60 import org.objectweb.proactive.core.descriptor.data.ProActiveDescriptor; 61 import org.objectweb.proactive.core.descriptor.data.VirtualNode; 62 import org.objectweb.proactive.core.descriptor.data.VirtualNodeImpl; 63 import org.objectweb.proactive.core.descriptor.xml.ProActiveDescriptorHandler; 64 import org.objectweb.proactive.core.exceptions.NonFunctionalException; 65 import org.objectweb.proactive.core.exceptions.communication.ProActiveCommunicationException; 66 import org.objectweb.proactive.core.exceptions.creation.ProActiveCreationException; 67 import org.objectweb.proactive.core.exceptions.group.ProActiveGroupException; 68 import org.objectweb.proactive.core.exceptions.handler.HandlerCommunicationException; 69 import org.objectweb.proactive.core.exceptions.handler.HandlerCreationException; 70 import org.objectweb.proactive.core.exceptions.handler.HandlerGroupException; 71 import org.objectweb.proactive.core.exceptions.handler.HandlerMigrationException; 72 import org.objectweb.proactive.core.exceptions.handler.HandlerNonFunctionalException; 73 import org.objectweb.proactive.core.exceptions.handler.HandlerSecurityException; 74 import org.objectweb.proactive.core.exceptions.handler.HandlerServiceException; 75 import org.objectweb.proactive.core.exceptions.handler.IHandler; 76 import org.objectweb.proactive.core.exceptions.migration.ProActiveMigrationException; 77 import org.objectweb.proactive.core.exceptions.security.ProActiveSecurityException; 78 import org.objectweb.proactive.core.exceptions.service.ProActiveServiceException; 79 import org.objectweb.proactive.core.group.Group; 80 import org.objectweb.proactive.core.group.ProActiveGroup; 81 import org.objectweb.proactive.core.mop.ClassNotReifiableException; 82 import org.objectweb.proactive.core.mop.ConstructionOfProxyObjectFailedException; 83 import org.objectweb.proactive.core.mop.MOP; 84 import org.objectweb.proactive.core.mop.MOPException; 85 import org.objectweb.proactive.core.mop.StubObject; 86 import org.objectweb.proactive.core.node.Node; 87 import org.objectweb.proactive.core.node.NodeException; 88 import org.objectweb.proactive.core.node.NodeFactory; 89 import org.objectweb.proactive.core.runtime.ProActiveRuntime; 90 import org.objectweb.proactive.core.runtime.ProActiveRuntimeImpl; 91 import org.objectweb.proactive.core.runtime.RuntimeFactory; 92 import org.objectweb.proactive.core.util.UrlBuilder; 93 94 import java.net.UnknownHostException ; 95 96 import java.util.HashMap ; 97 98 99 public class ProActive { 100 protected static Logger logger = Logger.getLogger(ProActive.class.getName()); 101 102 106 109 static public HashMap defaultLevel = null; 110 111 114 static public HashMap VMLevel = null; 115 116 119 static public HashMap codeLevel = null; 120 121 static { 122 ProActiveConfiguration.load(); 123 Class c = org.objectweb.proactive.core.runtime.RuntimeFactory.class; 124 125 defaultLevel = new HashMap (); 127 VMLevel = new HashMap (); 128 codeLevel = new HashMap (); 129 130 if (logger.isDebugEnabled()) { 132 logger.debug("Initialization of default level handlers"); 133 } 134 setExceptionHandler(IHandler.ID_defaultLevel, null, 135 HandlerNonFunctionalException.class, NonFunctionalException.class); 136 setExceptionHandler(IHandler.ID_defaultLevel, null, 137 HandlerCommunicationException.class, 138 ProActiveCommunicationException.class); 139 setExceptionHandler(IHandler.ID_defaultLevel, null, 140 HandlerCreationException.class, ProActiveCreationException.class); 141 setExceptionHandler(IHandler.ID_defaultLevel, null, 142 HandlerGroupException.class, ProActiveGroupException.class); 143 setExceptionHandler(IHandler.ID_defaultLevel, null, 144 HandlerMigrationException.class, ProActiveMigrationException.class); 145 setExceptionHandler(IHandler.ID_defaultLevel, null, 146 HandlerSecurityException.class, ProActiveSecurityException.class); 147 setExceptionHandler(IHandler.ID_defaultLevel, null, 148 HandlerServiceException.class, ProActiveServiceException.class); 149 150 } 152 153 private ProActive() { 157 } 158 159 163 171 public static Object newActive(String classname, 172 Object [] constructorParameters) 173 throws ActiveObjectCreationException, NodeException { 174 Node nullNode = null; 176 return newActive(classname, constructorParameters, nullNode, null, null); 177 } 178 179 189 public static Object newActive(String classname, 190 Object [] constructorParameters, String nodeURL) 191 throws ActiveObjectCreationException, NodeException { 192 if (nodeURL == null) { 193 Node nullNode = null; 195 return newActive(classname, constructorParameters, nullNode, null, 196 null); 197 } else { 198 return newActive(classname, constructorParameters, 199 NodeFactory.getNode(nodeURL), null, null); 200 } 201 } 202 203 213 public static Object newActive(String classname, 214 Object [] constructorParameters, Node node) 215 throws ActiveObjectCreationException, NodeException { 216 return newActive(classname, constructorParameters, node, null, null); 217 } 218 219 242 public static Object newActive(String classname, 243 Object [] constructorParameters, Node node, Active activity, 244 MetaObjectFactory factory) 245 throws ActiveObjectCreationException, NodeException { 246 if (node == null) { 248 node = NodeFactory.getDefaultNode(); 249 } 250 251 if (factory == null) { 252 factory = ProActiveMetaObjectFactory.newInstance(); 253 } 254 255 try { 256 return createStubObject(classname, constructorParameters, node, 257 activity, factory); 258 } catch (MOPException e) { 259 Throwable t = e; 260 261 if (e.getTargetException() != null) { 262 t = e.getTargetException(); 263 } 264 265 throw new ActiveObjectCreationException(t); 266 } 267 } 268 269 279 public static Object newActive(String classname, 280 Object [] constructorParameters, VirtualNode virtualnode) 281 throws ActiveObjectCreationException, NodeException { 282 return ProActive.newActive(classname, constructorParameters, 283 virtualnode, null, null); 284 } 285 286 301 public static Object newActive(String classname, 302 303 Object [] constructorParameters, VirtualNode virtualnode, 304 Active activity, MetaObjectFactory factory) 305 throws ActiveObjectCreationException, NodeException { 306 if (virtualnode != null) { 307 if (!virtualnode.isActivated()) { 308 virtualnode.activate(); 309 } 310 Node[] nodeTab = virtualnode.getNodes(); 311 Group aoGroup = null; 312 try { 313 aoGroup = ProActiveGroup.getGroup(ProActiveGroup.newGroup( 314 classname)); 315 } catch (ClassNotFoundException e) { 316 throw new ActiveObjectCreationException( 317 "Cannot create group of active objects" + e); 318 } catch (ClassNotReifiableException e) { 319 throw new ActiveObjectCreationException( 320 "Cannot create group of active objects" + e); 321 } 322 for (int i = 0; i < nodeTab.length; i++) { 323 Object tmp = newActive(classname, constructorParameters, 324 (Node) nodeTab[i], activity, factory); 325 aoGroup.add(tmp); 326 } 327 328 return aoGroup.getGroupByType(); 329 } else { 330 throw new NodeException( 331 "VirtualNode is null, unable to activate the object"); 332 } 333 } 334 335 357 public static Component newActiveComponent(String classname, 358 Object [] constructorParameters, Node node, Active activity, 359 MetaObjectFactory factory, ComponentParameters componentParameters) 360 throws ActiveObjectCreationException, NodeException { 361 try { 363 Component boot = Fractal.getBootstrapComponent(); 364 GenericFactory cf = Fractal.getGenericFactory(boot); 365 return cf.newFcInstance(componentParameters.getComponentType(), 366 new ControllerDescription(componentParameters.getName(), 367 componentParameters.getHierarchicalType()), 368 new ContentDescription(classname, constructorParameters, node, 369 activity, factory)); 370 } catch (NoSuchInterfaceException e) { 371 throw new ActiveObjectCreationException(e); 372 } catch (InstantiationException e) { 373 if (e.getCause() instanceof NodeException) { 374 throw new NodeException(e); 375 } else { 376 throw new ActiveObjectCreationException(e); 377 } 378 } 379 } 380 381 409 public static Component newActiveComponent(String className, 410 Object [] constructorParameters, VirtualNode vn, 411 ComponentParameters componentParameters) 412 throws ActiveObjectCreationException, NodeException { 413 try { 415 Component boot = Fractal.getBootstrapComponent(); 416 GenericFactory cf = Fractal.getGenericFactory(boot); 417 return cf.newFcInstance(componentParameters.getComponentType(), 418 new ControllerDescription(componentParameters.getName(), 419 componentParameters.getHierarchicalType()), 420 new ContentDescription(className, constructorParameters, vn)); 421 } catch (NoSuchInterfaceException e) { 422 throw new ActiveObjectCreationException(e); 423 } catch (InstantiationException e) { 424 if (e.getCause() instanceof NodeException) { 425 throw new NodeException(e); 426 } else { 427 throw new ActiveObjectCreationException(e); 428 } 429 } 430 } 431 432 440 public static Object turnActive(Object target) 441 throws ActiveObjectCreationException, NodeException { 442 return turnActive(target, (Node) null); 443 } 444 445 456 public static Object turnActive(Object target, String nodeURL) 457 throws ActiveObjectCreationException, NodeException { 458 if (nodeURL == null) { 459 return turnActive(target, target.getClass().getName(), null, null, 460 null); 461 } else { 462 return turnActive(target, target.getClass().getName(), 463 NodeFactory.getNode(nodeURL), null, null); 464 } 465 } 466 467 478 public static Object turnActive(Object target, Node node) 479 throws ActiveObjectCreationException, NodeException { 480 return turnActive(target, target.getClass().getName(), node, null, null); 481 } 482 483 498 public static Object turnActive(Object target, Node node, Active activity, 499 MetaObjectFactory factory) 500 throws ActiveObjectCreationException, NodeException { 501 return turnActive(target, target.getClass().getName(), node, activity, 502 factory); 503 } 504 505 518 public static Object turnActive(Object target, String nameOfTargetType, 519 Node node) throws ActiveObjectCreationException, NodeException { 520 return turnActive(target, nameOfTargetType, node, null, null); 521 } 522 523 544 public static Object turnActive(Object target, String nameOfTargetType, 545 Node node, Active activity, MetaObjectFactory factory) 546 throws ActiveObjectCreationException, NodeException { 547 if (node == null) { 548 node = NodeFactory.getDefaultNode(); 550 } 551 552 if (factory == null) { 553 factory = ProActiveMetaObjectFactory.newInstance(); 554 } 555 556 try { 557 return createStubObject(target, nameOfTargetType, node, activity, 558 factory); 559 } catch (MOPException e) { 560 Throwable t = e; 561 562 if (e.getTargetException() != null) { 563 t = e.getTargetException(); 564 } 565 566 throw new ActiveObjectCreationException(t); 567 } 568 } 569 570 583 public static Object turnActive(Object target, String nameOfTargetType, 584 VirtualNode virtualnode) 585 throws ActiveObjectCreationException, NodeException { 586 if (virtualnode != null) { 587 Node[] nodeTab = virtualnode.getNodes(); 588 Group aoGroup = null; 589 try { 590 aoGroup = ProActiveGroup.getGroup(ProActiveGroup.newGroup( 591 target.getClass().getName())); 592 } catch (ClassNotFoundException e) { 593 throw new ActiveObjectCreationException( 594 "Cannot create group of active objects" + e); 595 } catch (ClassNotReifiableException e) { 596 throw new ActiveObjectCreationException( 597 "Cannot create group of active objects" + e); 598 } 599 600 for (int i = 0; i < nodeTab.length; i++) { 601 Object tmp = turnActive(target, nameOfTargetType, 602 (Node) nodeTab[i], null, null); 603 aoGroup.add(tmp); 604 } 605 606 return aoGroup; 607 } else { 608 throw new NodeException( 609 "VirtualNode is null, unable to active the object"); 610 } 611 } 612 613 621 public static void register(Object obj, String url) 622 throws java.io.IOException { 623 if (!(MOP.isReifiedObject(obj))) { 625 throw new java.io.IOException ("The given object " + obj + 626 " is not a reified object"); 627 } 628 629 org.objectweb.proactive.core.mop.Proxy myProxy = ((StubObject) obj).getProxy(); 631 632 if (myProxy == null) { 633 throw new java.io.IOException ( 634 "Cannot find a Proxy on the stub object: " + obj); 635 } 636 637 BodyProxy myBodyProxy = (BodyProxy) myProxy; 638 UniversalBody body = myBodyProxy.getBody().getRemoteAdapter(); 639 640 if (body instanceof RemoteBodyAdapter) { 641 RemoteBodyAdapter.register((RemoteBodyAdapter) body, url); 642 if (logger.isInfoEnabled()) { 643 logger.info("Success at binding url " + url); 644 } 645 } else { 646 if (body instanceof IbisRemoteBodyAdapter) { 647 IbisRemoteBodyAdapter.register((IbisRemoteBodyAdapter) body, url); 648 if (logger.isInfoEnabled()) { 649 logger.info("Success at binding url " + url); 650 } 651 } else { 652 throw new java.io.IOException ( 653 "Cannot reconize the type of this UniversalBody: " + 654 body.getClass().getName()); 655 } 656 } 657 } 658 659 664 public static void unregister(String url) throws java.io.IOException { 665 RemoteBodyAdapter.unregister(url); 666 if (logger.isDebugEnabled()) { 667 logger.debug("Success at unbinding url " + url); 668 } 669 } 670 671 684 public static Object lookupActive(String classname, String url) 685 throws ActiveObjectCreationException, java.io.IOException { 686 UniversalBody b = null; 687 if ("ibis".equals(System.getProperty("proactive.communication.protocol"))) { 688 b = IbisRemoteBodyAdapter.lookup(url); 689 } else { 690 b = RemoteBodyAdapter.lookup(url); 691 } 692 693 try { 694 return createStubObject(classname, b); 695 } catch (MOPException e) { 696 Throwable t = e; 697 698 if (e.getTargetException() != null) { 699 t = e.getTargetException(); 700 } 701 702 throw new ActiveObjectCreationException("Exception occured when trying to create stub-proxy", 703 t); 704 } 705 } 706 707 714 public static void waitFor(Object future) { 715 if ((MOP.isReifiedObject(future)) == false) { 717 return; 718 } else { 719 org.objectweb.proactive.core.mop.Proxy theProxy = ((StubObject) future).getProxy(); 720 721 if (!(theProxy instanceof Future)) { 723 return; 724 } else { 725 ((Future) theProxy).waitFor(); 726 } 727 } 728 } 729 730 740 public static ProActiveDescriptor getProactiveDescriptor( 741 String xmlDescriptorUrl) throws ProActiveException { 742 RuntimeFactory.getDefaultRuntime(); 743 if (!xmlDescriptorUrl.startsWith("file:")) { 744 xmlDescriptorUrl = "file:" + xmlDescriptorUrl; 745 } 746 ProActiveRuntimeImpl part = (ProActiveRuntimeImpl) ProActiveRuntimeImpl.getProActiveRuntime(); 747 if (part.getDescriptor(xmlDescriptorUrl) != null) { 748 return part.getDescriptor(xmlDescriptorUrl); 749 } 750 751 try { 752 if (logger.isInfoEnabled()) { 753 logger.info("************* Reading deployment descriptor: " + 754 xmlDescriptorUrl + " ********************"); 755 } 756 ProActiveDescriptorHandler proActiveDescriptorHandler = ProActiveDescriptorHandler.createProActiveDescriptor(xmlDescriptorUrl); 757 ProActiveDescriptor pad = (ProActiveDescriptor) proActiveDescriptorHandler.getResultObject(); 758 part.registerDescriptor(xmlDescriptorUrl, pad); 759 return pad; 760 } catch (org.xml.sax.SAXException e) { 761 e.printStackTrace(); 762 logger.fatal( 763 "a problem occurs when getting the proActiveDescriptor"); 764 throw new ProActiveException(e); 765 } catch (java.io.IOException e) { 766 e.printStackTrace(); 767 logger.fatal( 768 "a problem occurs during the ProActiveDescriptor object creation"); 769 throw new ProActiveException(e); 770 } 771 } 772 773 781 public static void registerVirtualNode(VirtualNode virtualNode, 782 String registrationProtocol, boolean replacePreviousBinding) 783 throws ProActiveException { 784 if (!(virtualNode instanceof VirtualNodeImpl)) { 785 throw new ProActiveException( 786 "Cannot register such virtualNode since it results from a lookup!"); 787 } 788 String virtualnodeName = virtualNode.getName(); 789 ProActiveRuntime part = RuntimeFactory.getProtocolSpecificRuntime(registrationProtocol); 790 VirtualNode vn = part.getVirtualNode(virtualnodeName); 791 if (vn == null) { 792 throw new ProActiveException("VirtualNode " + virtualnodeName + 793 " does not exist !"); 794 } 795 part.registerVirtualNode(UrlBuilder.appendVnSuffix(virtualnodeName), 796 replacePreviousBinding); 797 } 798 799 806 public static VirtualNode lookupVirtualNode(String url, String protocol) 807 throws ProActiveException { 808 ProActiveRuntime remoteProActiveRuntime = null; 809 try { 810 remoteProActiveRuntime = RuntimeFactory.getRuntime(UrlBuilder.buildVirtualNodeUrl( 811 url), protocol); 812 } catch (UnknownHostException ex) { 813 throw new ProActiveException(ex); 814 } 815 return remoteProActiveRuntime.getVirtualNode(UrlBuilder.getNameFromUrl( 816 url)); 817 } 818 819 825 public static void unregisterVirtualNode(VirtualNode virtualNode) 826 throws ProActiveException { 827 if (!(virtualNode instanceof VirtualNodeImpl)) { 829 throw new ProActiveException( 830 "Cannot unregister such virtualNode since it results from a lookup!"); 831 } 832 String virtualNodeName = virtualNode.getName(); 833 ProActiveRuntime part = RuntimeFactory.getProtocolSpecificRuntime(((VirtualNodeImpl) virtualNode).getRegistrationProtocol()); 834 part.unregisterVirtualNode(UrlBuilder.appendVnSuffix( 835 virtualNode.getName())); 836 if (logger.isInfoEnabled()) { 837 logger.info("Success at unbinding " + virtualNodeName); 838 } 839 } 840 841 855 public static Body getBodyOnThis() { 856 return LocalBodyStore.getInstance().getCurrentThreadBody(); 857 } 858 859 865 public static StubObject getStubOnThis() { 866 Body body = getBodyOnThis(); 867 868 if (logger.isDebugEnabled()) { 869 } 871 if (body == null) { 872 return null; 873 } 874 875 return getStubForBody(body); 876 } 877 878 888 public static void migrateTo(Object activeObject) throws MigrationException { 889 migrateTo(getNodeFromURL(getNodeURLFromActiveObject(activeObject))); 890 } 891 892 902 public static void migrateTo(String nodeURL) throws MigrationException { 903 if (logger.isDebugEnabled()) { 904 logger.debug("migrateTo " + nodeURL); 905 } 906 ProActive.migrateTo(getNodeFromURL(nodeURL)); 907 } 908 909 918 public static void migrateTo(Node node) throws MigrationException { 919 if (logger.isDebugEnabled()) { 920 logger.debug("migrateTo " + node); 921 } 922 Body bodyToMigrate = getBodyOnThis(); 923 if (!(bodyToMigrate instanceof Migratable)) { 924 throw new MigrationException( 925 "This body cannot migrate. It doesn't implement Migratable interface"); 926 } 927 928 ((Migratable) bodyToMigrate).migrateTo(node); 929 } 930 931 946 public static void migrateTo(Body bodyToMigrate, Object activeObject, 947 boolean priority) throws MigrationException { 948 migrateTo(bodyToMigrate, 949 getNodeFromURL(getNodeURLFromActiveObject(activeObject)), priority); 950 } 951 952 967 public static void migrateTo(Body bodyToMigrate, String nodeURL, 968 boolean priority) throws MigrationException { 969 ProActive.migrateTo(bodyToMigrate, getNodeFromURL(nodeURL), priority); 970 } 971 972 987 public static void migrateTo(Body bodyToMigrate, Node node, boolean priority) 988 throws MigrationException { 989 if (!(bodyToMigrate instanceof Migratable)) { 990 throw new MigrationException( 991 "This body cannot migrate. It doesn't implement Migratable interface"); 992 } 993 994 Object [] arguments = { node }; 995 996 try { 997 BodyRequest request = new BodyRequest(bodyToMigrate, "migrateTo", 998 new Class [] { Node.class }, arguments, priority); 999 request.send(bodyToMigrate); 1000 } catch (NoSuchMethodException e) { 1001 throw new MigrationException("Cannot find method migrateTo this body. Non sense since the body is instance of Migratable", 1002 e); 1003 } catch (java.io.IOException e) { 1004 throw new MigrationException("Cannot send the request to migrate", e); 1005 } 1006 } 1007 1008 1014 public static int waitForAny(java.util.Vector futures) { 1015 FuturePool fp = getBodyOnThis().getFuturePool(); 1016 1017 synchronized (fp) { 1018 while (true) { 1019 java.util.Iterator it = futures.iterator(); 1020 int index = 0; 1021 1022 while (it.hasNext()) { 1023 Object current = it.next(); 1024 1025 if (!(isAwaited(current))) { 1026 return index; 1027 } 1028 1029 index++; 1030 } 1031 fp.waitForReply(); 1032 } 1033 } 1034 } 1035 1036 1041 public static void waitForAll(java.util.Vector futures) { 1042 FuturePool fp = getBodyOnThis().getFuturePool(); 1043 1044 synchronized (fp) { 1045 boolean oneIsMissing = true; 1046 1047 while (oneIsMissing) { 1048 oneIsMissing = false; 1049 1050 java.util.Iterator it = futures.iterator(); 1051 1052 while (it.hasNext()) { 1053 Object current = it.next(); 1054 1055 if (isAwaited(current)) { 1056 oneIsMissing = true; 1057 } 1058 } 1059 1060 if (oneIsMissing) { 1061 fp.waitForReply(); 1062 } 1063 } 1064 } 1065 } 1066 1067 1072 public static void waitForTheNth(java.util.Vector futures, int n) { 1073 FuturePool fp = getBodyOnThis().getFuturePool(); 1074 1075 synchronized (fp) { 1076 Object current = futures.get(n); 1077 1078 if (isAwaited(current)) { 1079 waitFor(current); 1080 } 1081 } 1082 } 1083 1084 1089 public static boolean isAwaited(Object future) { 1090 if ((MOP.isReifiedObject(future)) == false) { 1092 return false; 1093 } else { 1094 org.objectweb.proactive.core.mop.Proxy theProxy = ((StubObject) future).getProxy(); 1095 1096 if (!(theProxy instanceof Future)) { 1098 return false; 1099 } else { 1100 if (((Future) theProxy).isAwaited()) { 1101 return true; 1102 } else { 1103 return isAwaited(((Future) theProxy).getResult()); 1104 } 1105 } 1106 } 1107 } 1108 1109 1116 public static Object getFutureValue(Object future) { 1117 if ((MOP.isReifiedObject(future)) == false) { 1119 return future; 1120 } else { 1121 org.objectweb.proactive.core.mop.Proxy theProxy = ((StubObject) future).getProxy(); 1122 1123 if (!(theProxy instanceof Future)) { 1125 return future; 1126 } else { 1127 Object o = ((Future) theProxy).getResult(); 1128 1129 return getFutureValue(o); 1130 } 1131 } 1132 } 1133 1134 1137 public static void enableAC(Object obj) throws java.io.IOException { 1138 if (!(MOP.isReifiedObject(obj))) { 1140 throw new ProActiveRuntimeException("The given object " + obj + 1141 " is not a reified object"); 1142 } 1143 1144 org.objectweb.proactive.core.mop.Proxy myProxy = ((StubObject) obj).getProxy(); 1146 1147 if (myProxy == null) { 1148 throw new ProActiveRuntimeException( 1149 "Cannot find a Proxy on the stub object: " + obj); 1150 } 1151 1152 BodyProxy myBodyProxy = (BodyProxy) myProxy; 1153 UniversalBody body = myBodyProxy.getBody().getRemoteAdapter(); 1154 body.enableAC(); 1155 } 1156 1157 1160 public static void disableAC(Object obj) throws java.io.IOException { 1161 if (!(MOP.isReifiedObject(obj))) { 1163 throw new ProActiveRuntimeException("The given object " + obj + 1164 " is not a reified object"); 1165 } 1166 1167 org.objectweb.proactive.core.mop.Proxy myProxy = ((StubObject) obj).getProxy(); 1169 1170 if (myProxy == null) { 1171 throw new ProActiveRuntimeException( 1172 "Cannot find a Proxy on the stub object: " + obj); 1173 } 1174 1175 BodyProxy myBodyProxy = (BodyProxy) myProxy; 1176 UniversalBody body = myBodyProxy.getBody().getRemoteAdapter(); 1177 body.disableAC(); 1178 } 1179 1180 1186 public static void setImmediateService(Object obj, String methodName) 1187 throws java.io.IOException { 1188 if (!(MOP.isReifiedObject(obj))) { 1190 throw new ProActiveRuntimeException("The given object " + obj + 1191 " is not a reified object"); 1192 } 1193 1194 org.objectweb.proactive.core.mop.Proxy myProxy = ((StubObject) obj).getProxy(); 1196 1197 if (myProxy == null) { 1198 throw new ProActiveRuntimeException( 1199 "Cannot find a Proxy on the stub object: " + obj); 1200 } 1201 1202 BodyProxy myBodyProxy = (BodyProxy) myProxy; 1203 UniversalBody body = myBodyProxy.getBody().getRemoteAdapter(); 1204 body.setImmediateService(methodName); 1205 } 1206 1207 1210 public static String getJobId() { 1211 return ProActive.getBodyOnThis().getJobID(); 1212 } 1213 1214 1221 1222 public static IHandler searchExceptionHandler(NonFunctionalException ex) { 1223 IHandler handler = null; 1225 1226 if ((handler = searchExceptionHandler(ex, codeLevel, 1228 IHandler.ID_codeLevel)) != null) { 1229 return handler; 1230 } 1231 1232 if ((handler = searchExceptionHandler(ex, VMLevel, IHandler.ID_VMLevel)) != null) { 1236 return handler; 1237 } 1238 1239 return searchExceptionHandler(ex, defaultLevel, IHandler.ID_defaultLevel); 1241 } 1242 1243 1251 public static IHandler searchExceptionHandler(NonFunctionalException ex, 1252 HashMap level, int levelID) { 1253 if (level.isEmpty()) { 1255 return null; 1256 } 1257 1258 Class exClass = ex.getClass(); 1260 IHandler handler = null; 1261 1262 while ((handler == null) && 1265 (exClass.getName().compareTo(NonFunctionalException.class.getName()) != 0)) { 1266 if (logger.isDebugEnabled()) { 1268 } 1270 1271 if (level.containsKey(exClass)) { 1273 try { 1274 handler = (IHandler) Class.forName(((Class ) level.get( 1275 exClass)).getName()).newInstance(); 1276 } catch (Exception e) { 1277 if (e instanceof ClassNotFoundException ) { 1278 if (logger.isDebugEnabled()) { 1279 } 1281 break; 1282 } else { 1283 e.printStackTrace(); 1284 } 1285 } 1286 } else { 1287 exClass = exClass.getSuperclass(); 1288 } 1289 } 1290 1291 return handler; 1293 } 1294 1295 1303 public static void setExceptionHandler(int ID_level, Object obj, 1304 Class hName, Class exName) { 1305 HashMap level = null; 1309 switch (ID_level) { 1310 case (IHandler.ID_defaultLevel): 1311 level = defaultLevel; 1312 break; 1313 case (IHandler.ID_VMLevel): 1314 level = VMLevel; 1315 break; 1316 case (IHandler.ID_activeObjectLevel): break; 1318 case (IHandler.ID_proxyLevel): break; 1320 case (IHandler.ID_futureLevel): break; 1322 case (IHandler.ID_codeLevel): break; 1324 } 1325 1326 if (level == defaultLevel) { 1328 if (level.get(exName) == null) { 1329 level.put(exName, hName); 1330 if (logger.isDebugEnabled()) { 1331 } 1333 } else { 1334 if (logger.isDebugEnabled()) { 1335 } 1337 } 1338 } else { 1339 level.put(exName, hName); 1340 if (logger.isDebugEnabled()) { 1341 } 1343 } 1344 } 1345 1346 1352 public static IHandler unsetExceptionHandler(int ID_level, Object obj, 1353 Class exName) { 1354 IHandler handler = null; 1356 1357 HashMap level = null; 1359 switch (ID_level) { 1360 case (IHandler.ID_defaultLevel): 1361 level = defaultLevel; 1362 break; 1363 case (IHandler.ID_VMLevel): 1364 level = VMLevel; 1365 break; 1366 case (IHandler.ID_activeObjectLevel): break; 1368 case (IHandler.ID_proxyLevel): break; 1370 case (IHandler.ID_futureLevel): break; 1372 case (IHandler.ID_codeLevel): break; 1374 } 1375 1376 if (level != defaultLevel) { 1378 try { 1379 String hName = (String ) ((Class ) level.remove(exName)).getName(); 1380 if (hName != null) { 1381 handler = (IHandler) Class.forName(hName).newInstance(); 1382 if (logger.isDebugEnabled()) { 1383 } 1385 } else { 1386 if (logger.isDebugEnabled()) { 1387 } 1389 } 1390 } catch (Exception e) { 1391 e.printStackTrace(); 1392 } 1393 } else { 1394 if (logger.isDebugEnabled()) { 1395 } 1397 } 1398 1399 return handler; 1401 } 1402 1403 private static String getNodeURLFromActiveObject(Object o) 1407 throws MigrationException { 1408 if (!org.objectweb.proactive.core.mop.MOP.isReifiedObject(o)) { 1410 throw new MigrationException( 1411 "The parameter is not an active object"); 1412 } 1413 1414 BodyProxy destProxy = (BodyProxy) ((org.objectweb.proactive.core.mop.StubObject) o).getProxy(); 1416 1417 return destProxy.getBody().getNodeURL(); 1418 } 1419 1420 private static Node getNodeFromURL(String url) throws MigrationException { 1421 try { 1422 return NodeFactory.getNode(url); 1423 } catch (NodeException e) { 1424 throw new MigrationException("The node of given URL " + url + 1425 " cannot be localized", e); 1426 } 1427 } 1428 1429 private static StubObject getStubForBody(Body body) { 1435 try { 1436 return createStubObject(body.getReifiedObject(), 1437 new Object [] { body }, 1438 body.getReifiedObject().getClass().getName()); 1439 } catch (MOPException e) { 1440 throw new ProActiveRuntimeException( 1441 "Cannot create Stub for this Body e=" + e); 1442 } 1443 } 1444 1445 private static Object createStubObject(String className, UniversalBody body) 1446 throws MOPException { 1447 return createStubObject(className, null, new Object [] { body }); 1448 } 1449 1450 private static Object createStubObject(String className, 1451 Object [] constructorParameters, Node node, Active activity, 1452 MetaObjectFactory factory) throws MOPException { 1453 return createStubObject(className, constructorParameters, 1454 new Object [] { node, activity, factory, ProActive.getJobId() }); 1455 } 1456 1457 private static Object createStubObject(String className, 1458 Object [] constructorParameters, Object [] proxyParameters) 1459 throws MOPException { 1460 try { 1461 return MOP.newInstance(className, constructorParameters, 1462 Constants.DEFAULT_BODY_PROXY_CLASS_NAME, proxyParameters); 1463 } catch (ClassNotFoundException e) { 1464 throw new ConstructionOfProxyObjectFailedException( 1465 "Class can't be found e=" + e); 1466 } 1467 } 1468 1469 private static Object createStubObject(Object target, 1470 String nameOfTargetType, Node node, Active activity, 1471 MetaObjectFactory factory) throws MOPException { 1472 return createStubObject(target, 1473 new Object [] { node, activity, factory, ProActive.getJobId() }, 1474 nameOfTargetType); 1475 } 1476 1477 private static StubObject createStubObject(Object object, 1478 Object [] proxyParameters, String nameOfTargetType) 1479 throws MOPException { 1480 try { 1481 return (StubObject) MOP.turnReified(nameOfTargetType, 1482 Constants.DEFAULT_BODY_PROXY_CLASS_NAME, proxyParameters, object); 1483 } catch (ClassNotFoundException e) { 1484 throw new ConstructionOfProxyObjectFailedException( 1485 "Class can't be found e=" + e); 1486 } 1487 } 1488} 1489 | Popular Tags |