| 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 |