org/objectweb/proactive/ProActive.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive;
00032 
00033 import java.io.IOException;
00034 import java.lang.reflect.Array;
00035 import java.net.UnknownHostException;
00036 import java.rmi.AlreadyBoundException;
00037 import java.util.Collection;
00038 import java.util.Vector;
00039 import java.util.concurrent.ExecutorService;
00040 import java.util.concurrent.Executors;
00041 import java.util.concurrent.TimeUnit;
00042 
00043 import org.apache.log4j.Logger;
00044 import org.objectweb.fractal.api.Component;
00045 import org.objectweb.fractal.api.NoSuchInterfaceException;
00046 import org.objectweb.fractal.api.factory.GenericFactory;
00047 import org.objectweb.fractal.api.factory.InstantiationException;
00048 import org.objectweb.fractal.util.Fractal;
00049 import org.objectweb.proactive.core.Constants;
00050 import org.objectweb.proactive.core.ProActiveException;
00051 import org.objectweb.proactive.core.ProActiveRuntimeException;
00052 import org.objectweb.proactive.core.body.BodyAdapter;
00053 import org.objectweb.proactive.core.body.LocalBodyStore;
00054 import org.objectweb.proactive.core.body.MetaObjectFactory;
00055 import org.objectweb.proactive.core.body.ProActiveMetaObjectFactory;
00056 import org.objectweb.proactive.core.body.UniversalBody;
00057 import org.objectweb.proactive.core.body.future.Future;
00058 import org.objectweb.proactive.core.body.future.FuturePool;
00059 import org.objectweb.proactive.core.body.http.HttpBodyAdapter;
00060 import org.objectweb.proactive.core.body.ibis.IbisBodyAdapter;
00061 import org.objectweb.proactive.core.body.migration.Migratable;
00062 import org.objectweb.proactive.core.body.migration.MigrationException;
00063 import org.objectweb.proactive.core.body.proxy.AbstractProxy;
00064 import org.objectweb.proactive.core.body.proxy.BodyProxy;
00065 import org.objectweb.proactive.core.body.request.BodyRequest;
00066 import org.objectweb.proactive.core.body.rmi.RmiBodyAdapter;
00067 import org.objectweb.proactive.core.body.rmi.SshRmiBodyAdapter;
00068 import org.objectweb.proactive.core.component.ComponentParameters;
00069 import org.objectweb.proactive.core.component.ContentDescription;
00070 import org.objectweb.proactive.core.component.ControllerDescription;
00071 import org.objectweb.proactive.core.component.factory.ProActiveGenericFactory;
00072 import org.objectweb.proactive.core.config.ProActiveConfiguration;
00073 import org.objectweb.proactive.core.descriptor.data.ProActiveDescriptor;
00074 import org.objectweb.proactive.core.descriptor.data.VirtualNode;
00075 import org.objectweb.proactive.core.descriptor.data.VirtualNodeImpl;
00076 import org.objectweb.proactive.core.descriptor.xml.ProActiveDescriptorHandler;
00077 import org.objectweb.proactive.core.event.NodeCreationEventProducerImpl;
00078 import org.objectweb.proactive.core.exceptions.manager.ExceptionHandler;
00079 import org.objectweb.proactive.core.exceptions.manager.NFEListener;
00080 import org.objectweb.proactive.core.exceptions.manager.NFEManager;
00081 import org.objectweb.proactive.core.group.Group;
00082 import org.objectweb.proactive.core.group.ProActiveGroup;
00083 import org.objectweb.proactive.core.group.ProxyForGroup;
00084 import org.objectweb.proactive.core.mop.ClassNotReifiableException;
00085 import org.objectweb.proactive.core.mop.ConstructionOfProxyObjectFailedException;
00086 import org.objectweb.proactive.core.mop.MOP;
00087 import org.objectweb.proactive.core.mop.MOPException;
00088 import org.objectweb.proactive.core.mop.Proxy;
00089 import org.objectweb.proactive.core.mop.StubObject;
00090 import org.objectweb.proactive.core.node.Node;
00091 import org.objectweb.proactive.core.node.NodeException;
00092 import org.objectweb.proactive.core.node.NodeFactory;
00093 import org.objectweb.proactive.core.runtime.ProActiveRuntime;
00094 import org.objectweb.proactive.core.runtime.ProActiveRuntimeImpl;
00095 import org.objectweb.proactive.core.runtime.RuntimeFactory;
00096 import org.objectweb.proactive.core.util.NodeCreationListenerForAoCreation;
00097 import org.objectweb.proactive.core.util.NonFunctionalServices;
00098 import org.objectweb.proactive.core.util.ProcessForAoCreation;
00099 import org.objectweb.proactive.core.util.UrlBuilder;
00100 import org.objectweb.proactive.core.util.log.Loggers;
00101 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00102 import org.objectweb.proactive.core.util.profiling.PAProfilerEngine;
00103 import org.objectweb.proactive.core.util.profiling.Profiling;
00104 import org.objectweb.proactive.core.util.timer.CompositeAverageMicroTimer;
00105 import org.objectweb.proactive.core.xml.VariableContract;
00106 import org.objectweb.proactive.ext.security.ProActiveSecurityManager;
00107 import org.objectweb.proactive.ext.webservices.soap.ProActiveDeployer;
00108 
00109 import ibis.rmi.RemoteException;
00110 
00111 
00201 public class ProActive {
00202     protected final static Logger logger = ProActiveLogger.getLogger(Loggers.CORE);
00203     public final static Logger loggerGroup = ProActiveLogger.getLogger(Loggers.GROUPS);
00204 
00206     private static CompositeAverageMicroTimer timer;
00207 
00208     static {
00209         ProActiveConfiguration.load();
00210         
00211         @SuppressWarnings("unused") // Execute RuntimeFactory's static blocks  
00212         Class c = org.objectweb.proactive.core.runtime.RuntimeFactory.class;
00213     }
00214 
00215     //
00216     // -- CONSTRUCTORS -----------------------------------------------
00217     //
00218     private ProActive() {
00219     }
00220 
00221     //
00222     // -- PUBLIC METHODS -----------------------------------------------
00223     //
00224 
00235     public static void newMain(String classname, String[] mainParameters,
00236         Node node)
00237         throws ClassNotFoundException, NoSuchMethodException, ProActiveException {
00238         ProActiveRuntime part = node.getProActiveRuntime();
00239         part.launchMain(classname, mainParameters);
00240     }
00241 
00250     public static void newRemote(String classname, Node node)
00251         throws ClassNotFoundException, ProActiveException {
00252         ProActiveRuntime part = node.getProActiveRuntime();
00253         part.newRemote(classname);
00254     }
00255 
00264     public static Object newActive(String classname,
00265         Object[] constructorParameters)
00266         throws ActiveObjectCreationException, NodeException {
00267         return newActive(classname, null, constructorParameters, (Node) null,
00268             null, null);
00269     }
00270 
00281     public static Object newActive(String classname,
00282         Object[] constructorParameters, String nodeURL)
00283         throws ActiveObjectCreationException, NodeException {
00284         if (nodeURL == null) {
00285             return newActive(classname, null, constructorParameters,
00286                 (Node) null, null, null);
00287         } else {
00288             return newActive(classname, null, constructorParameters,
00289                 NodeFactory.getNode(nodeURL), null, null);
00290         }
00291     }
00292 
00303     public static Object newActive(String classname,
00304         Object[] constructorParameters, Node node)
00305         throws ActiveObjectCreationException, NodeException {
00306         return newActive(classname, null, constructorParameters, node, null,
00307             null);
00308     }
00309 
00327     public static Object[] newActiveInParallel(String className,
00328         Object[][] constructorParameters, Node[] nodes)
00329         throws ClassNotFoundException {
00330         return newActiveInParallel(className, null, constructorParameters, nodes);
00331     }
00332 
00352     public static Object[] newActiveInParallel(String className,
00353         Object[] constructorParameters, VirtualNode virtualNode)
00354         throws NodeException, ClassNotFoundException {
00355         return newActiveInParallel(className, null, constructorParameters,
00356             virtualNode);
00357     }
00358 
00372     public static Object newActiveAsGroup(String classname,
00373         Object[] constructorParameters, VirtualNode virtualnode)
00374         throws ActiveObjectCreationException, NodeException {
00375         return ProActive.newActiveAsGroup(classname, null,
00376             constructorParameters, virtualnode, null, null);
00377     }
00378 
00397     public static Object newActiveAsGroup(String className,
00398         Object[] constructorParameters, VirtualNode virtualNode,
00399         Active activity, MetaObjectFactory factory)
00400         throws ActiveObjectCreationException, NodeException {
00401         return newActiveAsGroup(className, null, constructorParameters,
00402             virtualNode, activity, factory);
00403     }
00404 
00427     public static Component newActiveComponent(String className,
00428         Object[] constructorParameters, Node node, Active activity,
00429         MetaObjectFactory factory, ComponentParameters componentParameters)
00430         throws ActiveObjectCreationException, NodeException {
00431         return newActiveComponent(className, null, constructorParameters, node,
00432             activity, factory, componentParameters);
00433     }
00434 
00460     public static Component newActiveComponent(String className,
00461         Object[] constructorParameters, VirtualNode vn,
00462         ComponentParameters componentParameters)
00463         throws ActiveObjectCreationException, NodeException {
00464         return newActiveComponent(className, null, constructorParameters, vn,
00465             componentParameters);
00466     }
00467 
00476     public static Object turnActive(Object target)
00477         throws ActiveObjectCreationException, NodeException {
00478         return turnActive(target, (Class[]) null, (Node) null);
00479     }
00480 
00492     public static Object turnActive(Object target, String nodeURL)
00493         throws ActiveObjectCreationException, NodeException {
00494         if (nodeURL == null) {
00495             return turnActive(target, null, target.getClass().getName(), null,
00496                 null, null);
00497         } else {
00498             return turnActive(target, null, target.getClass().getName(),
00499                 NodeFactory.getNode(nodeURL), null, null);
00500         }
00501     }
00502 
00514     public static Object turnActive(Object target, Node node)
00515         throws ActiveObjectCreationException, NodeException {
00516         return turnActive(target, null, target.getClass().getName(), node,
00517             null, null);
00518     }
00519 
00535     public static Object turnActive(Object target, Node node, Active activity,
00536         MetaObjectFactory factory)
00537         throws ActiveObjectCreationException, NodeException {
00538         return turnActive(target, null, target.getClass().getName(), node,
00539             activity, factory);
00540     }
00541 
00555     public static Object turnActive(Object target, String nameOfTargetType,
00556         Node node) throws ActiveObjectCreationException, NodeException {
00557         return turnActive(target, null, nameOfTargetType, node, null, null);
00558     }
00559 
00581     public static Object turnActive(Object target, String nameOfTargetType,
00582         Node node, Active activity, MetaObjectFactory factory)
00583         throws ActiveObjectCreationException, NodeException {
00584         return turnActive(target, null, nameOfTargetType, node, activity,
00585             factory);
00586     }
00587 
00601     public static Object turnActiveAsGroup(Object target,
00602         String nameOfTargetType, VirtualNode virtualnode)
00603         throws ActiveObjectCreationException, NodeException {
00604         return turnActiveAsGroup(target, null, nameOfTargetType, virtualnode);
00605     }
00606 
00609 
00619     public static Object newActive(String classname, Class[] genericParameters,
00620         Object[] constructorParameters)
00621         throws ActiveObjectCreationException, NodeException {
00622         // avoid ambiguity for method parameters types
00623         Node nullNode = null;
00624         return newActive(classname, genericParameters, constructorParameters,
00625             nullNode, null, null);
00626     }
00627 
00639     public static Object newActive(String classname, Class[] genericParameters,
00640         Object[] constructorParameters, String nodeURL)
00641         throws ActiveObjectCreationException, NodeException {
00642         if (nodeURL == null) {
00643             // avoid ambiguity for method parameters types
00644             Node nullNode = null;
00645             return newActive(classname, genericParameters,
00646                 constructorParameters, nullNode, null, null);
00647         } else {
00648             return newActive(classname, genericParameters,
00649                 constructorParameters, NodeFactory.getNode(nodeURL), null, null);
00650         }
00651     }
00652 
00664     public static Object newActive(String classname, Class[] genericParameters,
00665         Object[] constructorParameters, Node node)
00666         throws ActiveObjectCreationException, NodeException {
00667         return newActive(classname, genericParameters, constructorParameters,
00668             node, null, null);
00669     }
00670 
00695     public static Object newActive(String classname, Class[] genericParameters,
00696         Object[] constructorParameters, Node node, Active activity,
00697         MetaObjectFactory factory)
00698         throws ActiveObjectCreationException, NodeException {
00699         //using default proactive node
00700         if (node == null) {
00701             node = NodeFactory.getDefaultNode();
00702         }
00703 
00704         if (factory == null) {
00705             factory = ProActiveMetaObjectFactory.newInstance();
00706         }
00707 
00708         if (Profiling.SECURITY) {
00709             if (timer == null) {
00710                 timer = new CompositeAverageMicroTimer("newActiveSecurityTimer");
00711                 PAProfilerEngine.registerTimer(timer);
00712             }
00713             timer.setTimer("constructing certificate");
00714             timer.start();
00715         }
00716 
00717         MetaObjectFactory clonedFactory = factory;
00718 
00719         ProActiveSecurityManager factorySM = factory.getProActiveSecurityManager();
00720         if (factorySM != null) {
00721             try {
00722                 clonedFactory = (MetaObjectFactory) factory.clone();
00723             } catch (CloneNotSupportedException e) {
00724                 e.printStackTrace();
00725             }
00726 
00727             ProActiveSecurityManager psm = clonedFactory.getProActiveSecurityManager();
00728             psm = psm.generateSiblingCertificate(classname);
00729             clonedFactory.setProActiveSecurityManager(psm);
00730         }
00731         if (Profiling.SECURITY) {
00732             timer.stop();
00733         }
00734 
00735         try {
00736             //          create stub object
00737             Object stub = createStubObject(classname, genericParameters,
00738                     constructorParameters, node, activity, clonedFactory);
00739 
00740             return stub;
00741         } catch (MOPException e) {
00742             Throwable t = e;
00743 
00744             if (e.getTargetException() != null) {
00745                 t = e.getTargetException();
00746             }
00747 
00748             throw new ActiveObjectCreationException(t);
00749         }
00750     }
00751 
00770     public static Object[] newActiveInParallel(String className,
00771         Class[] genericParameters, Object[][] constructorParameters,
00772         Node[] nodes) throws ClassNotFoundException {
00773         if (constructorParameters.length != nodes.length) {
00774             throw new ProActiveRuntimeException(
00775                 "The total of constructors must" +
00776                 " be equal to the total of nodes");
00777         }
00778 
00779         ExecutorService threadPool = Executors.newCachedThreadPool();
00780 
00781         Vector result = new Vector();
00782 
00783         // TODO execute tasks
00784         // The Virtual Node is already activate
00785         for (int i = 0; i < constructorParameters.length; i++) {
00786             threadPool.execute(new ProcessForAoCreation(result, className,
00787                     genericParameters, constructorParameters[i],
00788                     nodes[i % nodes.length]));
00789         }
00790 
00791         threadPool.shutdown();
00792         try {
00793             threadPool.awaitTermination(new Integer(System.getProperty(
00794                         "components.creation.timeout")), TimeUnit.SECONDS);
00795         } catch (InterruptedException e1) {
00796             // TODO Auto-generated catch block
00797             e1.printStackTrace();
00798         }
00799 
00800         Class classForResult = Class.forName(className);
00801         return result.toArray((Object[]) Array.newInstance(classForResult,
00802                 result.size()));
00803     }
00804 
00821     public static Object[] newActiveInParallel(String className,
00822         Class[] genericParameters, Object[] constructorParameters,
00823         VirtualNode virtualNode) throws NodeException, ClassNotFoundException {
00824         // Creation of the thread pool
00825         ExecutorService threadPool = Executors.newCachedThreadPool();
00826 
00827         Vector result = new Vector();
00828         if (virtualNode.isActivated()) {
00829             // The Virtual Node is already activate
00830             Node[] nodes = virtualNode.getNodes();
00831             for (int i = 0; i < nodes.length; i++) {
00832                 threadPool.execute(new ProcessForAoCreation(result, className,
00833                         genericParameters, constructorParameters, nodes[i]));
00834             }
00835         } else {
00836             // Use the node creation event mechanism
00837             ((NodeCreationEventProducerImpl) virtualNode).addNodeCreationEventListener(new NodeCreationListenerForAoCreation(
00838                     result, className, genericParameters,
00839                     constructorParameters, threadPool));
00840             virtualNode.activate();
00841             ((VirtualNodeImpl) virtualNode).waitForAllNodesCreation();
00842         }
00843         threadPool.shutdown();
00844         try {
00845             threadPool.awaitTermination(new Integer(System.getProperty(
00846                         "components.creation.timeout")), TimeUnit.SECONDS);
00847         } catch (InterruptedException e1) {
00848             // TODO Auto-generated catch block
00849             e1.printStackTrace();
00850         }
00851 
00852         Class classForResult = Class.forName(className);
00853         return result.toArray((Object[]) Array.newInstance(classForResult,
00854                 result.size()));
00855     }
00856 
00871     public static Object newActiveAsGroup(String classname,
00872         Class[] genericParameters, Object[] constructorParameters,
00873         VirtualNode virtualnode)
00874         throws ActiveObjectCreationException, NodeException {
00875         return ProActive.newActiveAsGroup(classname, genericParameters,
00876             constructorParameters, virtualnode, null, null);
00877     }
00878 
00898     public static Object newActiveAsGroup(String classname,
00899         Class[] genericParameters, Object[] constructorParameters,
00900         VirtualNode virtualnode, Active activity, MetaObjectFactory factory)
00901         throws ActiveObjectCreationException, NodeException {
00902         if (virtualnode != null) {
00903             if (!virtualnode.isActivated()) {
00904                 virtualnode.activate();
00905             }
00906             Node[] nodeTab = virtualnode.getNodes();
00907             Group aoGroup = null;
00908             try {
00909                 aoGroup = ProActiveGroup.getGroup(ProActiveGroup.newGroup(
00910                             classname, genericParameters));
00911             } catch (ClassNotFoundException e) {
00912                 throw new ActiveObjectCreationException(
00913                     "Cannot create group of active objects" + e);
00914             } catch (ClassNotReifiableException e) {
00915                 throw new ActiveObjectCreationException(
00916                     "Cannot create group of active objects" + e);
00917             }
00918             for (int i = 0; i < nodeTab.length; i++) {
00919                 Object tmp = newActive(classname, null, constructorParameters,
00920                         (Node) nodeTab[i], activity, factory);
00921                 aoGroup.add(tmp);
00922             }
00923 
00924             return aoGroup.getGroupByType();
00925         } else {
00926             throw new NodeException(
00927                 "VirtualNode is null, unable to activate the object");
00928         }
00929     }
00930 
00953     public static Object turnActive(Object target, String nameOfTargetType,
00954         Class[] genericParameters, Node node, Active activity,
00955         MetaObjectFactory factory)
00956         throws ActiveObjectCreationException, NodeException {
00957         if (node == null) {
00958             //using default proactive node
00959             node = NodeFactory.getDefaultNode();
00960         }
00961 
00962         if (factory == null) {
00963             factory = ProActiveMetaObjectFactory.newInstance();
00964         }
00965 
00966         ProActiveSecurityManager factorySM = factory.getProActiveSecurityManager();
00967 
00968         MetaObjectFactory clonedFactory = factory;
00969 
00970         if (factorySM != null) {
00971             try {
00972                 clonedFactory = (MetaObjectFactory) factory.clone();
00973             } catch (CloneNotSupportedException e) {
00974                 e.printStackTrace();
00975             }
00976 
00977             clonedFactory.setProActiveSecurityManager(factory.getProActiveSecurityManager()
00978                                                              .generateSiblingCertificate(nameOfTargetType));
00979 
00980             ProActiveLogger.getLogger(Loggers.SECURITY)
00981                            .debug("new active object with security manager");
00982         }
00983 
00984         try {
00985             return createStubObject(target, nameOfTargetType,
00986                 genericParameters, node, activity, clonedFactory);
00987         } catch (MOPException e) {
00988             Throwable t = e;
00989 
00990             if (e.getTargetException() != null) {
00991                 t = e.getTargetException();
00992             }
00993 
00994             throw new ActiveObjectCreationException(t);
00995         }
00996     }
00997 
01021     public static Component newActiveComponent(String classname,
01022         Class[] genericParameters, Object[] constructorParameters, Node node,
01023         Active activity, MetaObjectFactory factory,
01024         ComponentParameters componentParameters)
01025         throws ActiveObjectCreationException, NodeException {
01026         try {
01027             Component boot = Fractal.getBootstrapComponent();
01028             GenericFactory cf = Fractal.getGenericFactory(boot);
01029             return cf.newFcInstance(componentParameters.getComponentType(),
01030                 new ControllerDescription(componentParameters.getName(),
01031                     componentParameters.getHierarchicalType()),
01032                 new ContentDescription(classname, constructorParameters,
01033                     activity, factory));
01034         } catch (NoSuchInterfaceException e) {
01035             throw new ActiveObjectCreationException(e);
01036         } catch (InstantiationException e) {
01037             if (e.getCause() instanceof NodeException) {
01038                 throw new NodeException(e);
01039             } else {
01040                 throw new ActiveObjectCreationException(e);
01041             }
01042         }
01043     }
01044 
01071     public static Component newActiveComponent(String className,
01072         Class[] genericParameters, Object[] constructorParameters,
01073         VirtualNode vn, ComponentParameters componentParameters)
01074         throws ActiveObjectCreationException, NodeException {
01075         try {
01076             Component boot = Fractal.getBootstrapComponent();
01077             ProActiveGenericFactory cf = (ProActiveGenericFactory) Fractal.getGenericFactory(boot);
01078             return cf.newFcInstance(componentParameters.getComponentType(),
01079                 new ControllerDescription(componentParameters.getName(),
01080                     componentParameters.getHierarchicalType()),
01081                 new ContentDescription(className, constructorParameters));
01082         } catch (NoSuchInterfaceException e) {
01083             throw new ActiveObjectCreationException(e);
01084         } catch (InstantiationException e) {
01085             if (e.getCause() instanceof NodeException) {
01086                 throw new NodeException(e);
01087             } else {
01088                 throw new ActiveObjectCreationException(e);
01089             }
01090         }
01091     }
01092 
01102     public static Object turnActive(Object target, Class[] genericParameters)
01103         throws ActiveObjectCreationException, NodeException {
01104         return turnActive(target, genericParameters, (Node) null,
01105             (Active) null, (MetaObjectFactory) null);
01106     }
01107 
01120     public static Object turnActive(Object target, Class[] genericParameters,
01121         String nodeURL) throws ActiveObjectCreationException, NodeException {
01122         if (nodeURL == null) {
01123             return turnActive(target, genericParameters,
01124                 target.getClass().getName(), null, null, null);
01125         } else {
01126             return turnActive(target, genericParameters,
01127                 target.getClass().getName(), NodeFactory.getNode(nodeURL),
01128                 null, null);
01129         }
01130     }
01131 
01144     public static Object turnActive(Object target, Class[] genericParameters,
01145         Node node) throws ActiveObjectCreationException, NodeException {
01146         return turnActive(target, genericParameters,
01147             target.getClass().getName(), node, null, null);
01148     }
01149 
01166     public static Object turnActive(Object target, Class[] genericParameters,
01167         Node node, Active activity, MetaObjectFactory factory)
01168         throws ActiveObjectCreationException, NodeException {
01169         return turnActive(target, genericParameters,
01170             target.getClass().getName(), node, activity, factory);
01171     }
01172 
01187     public static Object turnActive(Object target, Class[] genericParameters,
01188         String nameOfTargetType, Node node)
01189         throws ActiveObjectCreationException, NodeException {
01190         return turnActive(target, genericParameters, nameOfTargetType, node,
01191             null, null);
01192     }
01193 
01216     public static Object turnActive(Object target, Class[] genericParameters,
01217         String nameOfTargetType, Node node, Active activity,
01218         MetaObjectFactory factory)
01219         throws ActiveObjectCreationException, NodeException {
01220         if (node == null) {
01221             //using default proactive node
01222             node = NodeFactory.getDefaultNode();
01223         }
01224 
01225         if (factory == null) {
01226             factory = ProActiveMetaObjectFactory.newInstance();
01227         }
01228 
01229         ProActiveSecurityManager factorySM = factory.getProActiveSecurityManager();
01230 
01231         MetaObjectFactory clonedFactory = factory;
01232 
01233         if (factorySM != null) {
01234             try {
01235                 clonedFactory = (MetaObjectFactory) factory.clone();
01236             } catch (CloneNotSupportedException e) {
01237                 e.printStackTrace();
01238             }
01239 
01240             clonedFactory.setProActiveSecurityManager(factory.getProActiveSecurityManager()
01241                                                              .generateSiblingCertificate(nameOfTargetType));
01242 
01243             ProActiveLogger.getLogger(Loggers.SECURITY)
01244                            .debug("new active object with security manager");
01245         }
01246 
01247         try {
01248             return createStubObject(target, nameOfTargetType,
01249                 genericParameters, node, activity, clonedFactory);
01250         } catch (MOPException e) {
01251             Throwable t = e;
01252 
01253             if (e.getTargetException() != null) {
01254                 t = e.getTargetException();
01255             }
01256 
01257             throw new ActiveObjectCreationException(t);
01258         }
01259     }
01260 
01275     public static Object turnActiveAsGroup(Object target,
01276         Class[] genericParameters, String nameOfTargetType,
01277         VirtualNode virtualnode)
01278         throws ActiveObjectCreationException, NodeException {
01279         if (virtualnode != null) {
01280             Node[] nodeTab = virtualnode.getNodes();
01281             Group aoGroup = null;
01282             try {
01283                 aoGroup = ProActiveGroup.getGroup(ProActiveGroup.newGroup(
01284                             target.getClass().getName(), genericParameters));
01285             } catch (ClassNotFoundException e) {
01286                 throw new ActiveObjectCreationException(
01287                     "Cannot create group of active objects" + e);
01288             } catch (ClassNotReifiableException e) {
01289                 throw new ActiveObjectCreationException(
01290                     "Cannot create group of active objects" + e);
01291             }
01292 
01293             for (int i = 0; i < nodeTab.length; i++) {
01294                 Object tmp = turnActive(target, genericParameters,
01295                         nameOfTargetType, (Node) nodeTab[i], null, null);
01296                 aoGroup.add(tmp);
01297             }
01298 
01299             return aoGroup;
01300         } else {
01301             throw new NodeException(
01302                 "VirtualNode is null, unable to active the object");
01303         }
01304     }
01305 
01319     public static void register(Object obj, String url)
01320         throws java.io.IOException {
01321         BodyAdapter body = getRemoteBody(obj);
01322         body.register(url);
01323         if (logger.isInfoEnabled()) {
01324             logger.info("Success at binding url " + url);
01325         }
01326     }
01327 
01333     public static void unregister(String url) throws java.io.IOException {
01334         String protocol = UrlBuilder.getProtocol(url);
01335 
01336         // First step towards Body factory, will be introduced after the release
01337         if (protocol.equals("rmi:")) {
01338             new RmiBodyAdapter().unregister(url);
01339         } else if (protocol.equals("rmissh:")) {
01340             new SshRmiBodyAdapter().unregister(url);
01341         } else if (protocol.equals("http:")) {
01342             new HttpBodyAdapter().unregister(url);
01343         } else if (protocol.equals("ibis:")) {
01344             new IbisBodyAdapter().unregister(url);
01345         } else {
01346             throw new IOException("Protocol " + protocol + " not defined");
01347         }
01348         if (logger.isDebugEnabled()) {
01349             logger.debug("Success at unbinding url " + url);
01350         }
01351     }
01352 
01369     public static Object lookupActive(String classname, String url)
01370         throws ActiveObjectCreationException, java.io.IOException {
01371         //      try {
01372         //              // this ensures the class server is initialized,
01373         //              // which ensures that the java.rmi.server.codebase property is initialized,
01374         //              // which ensures parameters are annotated with the correct codebase in RMI communications
01375         //                      RuntimeFactory.getDefaultRuntime();
01376         //              } catch (ProActiveException e1) {
01377         //                      throw new ActiveObjectCreationException("Exception occured when trying to get default runtime",e1);
01378         //              }
01379         UniversalBody b = null;
01380 
01381         String protocol = UrlBuilder.getProtocol(url);
01382 
01383         // First step towards Body factory, will be introduced after the release
01384         if (protocol.equals("rmi:")) {
01385             b = new RmiBodyAdapter().lookup(url);
01386         } else if (protocol.equals("rmissh:")) {
01387             b = new SshRmiBodyAdapter().lookup(url);
01388         } else if (protocol.equals("http:")) {
01389             b = new HttpBodyAdapter().lookup(url);
01390         } else if (protocol.equals("ibis:")) {
01391             b = new IbisBodyAdapter().lookup(url);
01392         } else {
01393             throw new IOException("Protocol " + protocol + " not defined");
01394         }
01395 
01396         try {
01397             return createStubObject(classname, b);
01398         } catch (MOPException e) {
01399             Throwable t = e;
01400 
01401             if (e.getTargetException() != null) {
01402                 t = e.getTargetException();
01403             }
01404 
01405             throw new ActiveObjectCreationException("Exception occured when trying to create stub-proxy",
01406                 t);
01407         }
01408     }
01409 
01419     public static String[] listActive(String url) throws java.io.IOException {
01420         String[] activeNames = null;
01421 
01422         String protocol = UrlBuilder.getProtocol(url);
01423 
01424         // First step towards Body factory, will be introduced after the release
01425         if (protocol.equals("rmi:")) {
01426             activeNames = new RmiBodyAdapter().list(url);
01427         } else if (protocol.equals("rmissh:")) {
01428             activeNames = new SshRmiBodyAdapter().list(url);
01429         } else if (protocol.equals("http:")) {
01430             activeNames = new HttpBodyAdapter().list(url);
01431         } else if (protocol.equals("ibis:")) {
01432             activeNames = new IbisBodyAdapter().list(url);
01433         } else {
01434             throw new IOException("Protocol " + protocol + " not defined");
01435         }
01436 
01437         return activeNames;
01438     }
01439 
01445     public static String getActiveObjectNodeUrl(Object activeObject) {
01446         UniversalBody body = getRemoteBody(activeObject);
01447         return body.getNodeURL();
01448     }
01449 
01455     public static boolean isException(Object future) {
01456         // If the object is not reified, it cannot be a future
01457         if ((MOP.isReifiedObject(future)) == false) {
01458             return false;
01459         } else {
01460             org.objectweb.proactive.core.mop.Proxy theProxy = ((StubObject) future).getProxy();
01461 
01462             // If it is reified but its proxy is not of type future it's not an exception
01463             if (!(theProxy instanceof Future)) {
01464                 return false;
01465             } else {
01466                 return ((Future) theProxy).getRaisedException() != null;
01467             }
01468         }
01469     }
01470 
01479     public static void waitFor(Object future) {
01480         // If the object is not reified, it cannot be a future
01481         if ((MOP.isReifiedObject(future)) == false) {
01482             return;
01483         } else {
01484             org.objectweb.proactive.core.mop.Proxy theProxy = ((StubObject) future).getProxy();
01485 
01486             // If it is reified but its proxy is not of type future, we cannot wait
01487             if (!(theProxy instanceof Future)) {
01488                 return;
01489             } else {
01490                 ((Future) theProxy).waitFor();
01491             }
01492         }
01493     }
01494 
01505     public static void waitFor(Object future, long timeout)
01506         throws ProActiveException {
01507         // If the object is not reified, it cannot be a future
01508         if ((MOP.isReifiedObject(future)) == false) {
01509             return;
01510         } else {
01511             org.objectweb.proactive.core.mop.Proxy theProxy = ((StubObject) future).getProxy();
01512 
01513             // If it is reified but its proxy is not of type future, we cannot wait
01514             if (!(theProxy instanceof Future)) {
01515                 return;
01516             } else {
01517                 ((Future) theProxy).waitFor(timeout);
01518             }
01519         }
01520     }
01521 
01529     public static ProActiveDescriptor getProactiveDescriptor()
01530         throws ProActiveException, IOException {
01531         String padURL = System.getProperty("proactive.pad");
01532 
01533         //System.out.println("pad propertie : " + padURL) ;
01534         if (padURL == null) {
01535             //System.out.println("pad null");
01536             return null;
01537         } else {
01538             return getProActiveDescriptor(padURL, new VariableContract(), true);
01539         }
01540     }
01541 
01552     public static ProActiveDescriptor getProactiveDescriptor(
01553         String xmlDescriptorUrl) throws ProActiveException {
01554         return getProActiveDescriptor(xmlDescriptorUrl, new VariableContract(),
01555             false);
01556     }
01557 
01568     public static ProActiveDescriptor getProactiveDescriptor(
01569         String xmlDescriptorUrl, VariableContract variableContract)
01570         throws ProActiveException {
01571         if (variableContract == null) {
01572             throw new NullPointerException(
01573                 "Argument variableContract can not be null");
01574         }
01575 
01576         return getProActiveDescriptor(xmlDescriptorUrl, variableContract, false);
01577     }
01578 
01579     private static ProActiveDescriptor getProActiveDescriptor(
01580         String xmlDescriptorUrl, VariableContract variableContract,
01581         boolean hierarchicalSearch) throws ProActiveException {
01582         //Get lock on XMLProperties global static variable
01583         org.objectweb.proactive.core.xml.VariableContract.lock.aquire();
01584         org.objectweb.proactive.core.xml.VariableContract.xmlproperties = variableContract;
01585 
01586         //Get the pad
01587         ProActiveDescriptor pad;
01588         try {
01589             pad = internalGetProActiveDescriptor(xmlDescriptorUrl,
01590                     variableContract, hierarchicalSearch);
01591         } catch (ProActiveException e) {
01592             org.objectweb.proactive.core.xml.VariableContract.lock.release();
01593             throw e;
01594         }
01595 
01596         //No further modifications can be donde on the xmlproperties, thus we close the contract
01597         variableContract.close();
01598 
01599         //Check the contract (proposed optimization: Do this when parsing </variable> tag instead of here!)
01600         if (!variableContract.checkContract()) {
01601             logger.error(variableContract.toString());
01602             org.objectweb.proactive.core.xml.VariableContract.lock.release();
01603             throw new ProActiveException("Variable Contract has not been met!");
01604         }
01605 
01606         //Release lock on static global variable XMLProperties
01607         VariableContract.xmlproperties = new VariableContract();
01608         org.objectweb.proactive.core.xml.VariableContract.lock.release();
01609 
01610         return pad;
01611         //return getProactiveDescriptor(xmlDescriptorUrl, false);
01612     }
01613 
01622     private static ProActiveDescriptor internalGetProActiveDescriptor(
01623         String xmlDescriptorUrl, VariableContract variableContract,
01624         boolean hierarchicalSearch) throws ProActiveException {
01625         RuntimeFactory.getDefaultRuntime();
01626         if (!xmlDescriptorUrl.startsWith("file:")) {
01627             xmlDescriptorUrl = "file:" + xmlDescriptorUrl;
01628         }
01629         ProActiveRuntimeImpl part = (ProActiveRuntimeImpl) ProActiveRuntimeImpl.getProActiveRuntime();
01630         ProActiveDescriptor pad;
01631         try {
01632             if (!hierarchicalSearch) {
01633                 //if not hierarchical search, we assume that the descriptor might has been
01634                 //register with the default jobID
01635                 pad = part.getDescriptor(xmlDescriptorUrl +
01636                         ProActive.getJobId(), hierarchicalSearch);
01637             } else {
01638                 pad = part.getDescriptor(xmlDescriptorUrl, hierarchicalSearch);
01639             }
01640         } catch (Exception e) {
01641             throw new ProActiveException(e);
01642         }
01643 
01644         // if pad found, returns it
01645         if (pad != null) {
01646             return pad;
01647         }
01648 
01649         // else parses it
01650         try {
01651             if (logger.isInfoEnabled()) {
01652                 logger.info("************* Reading deployment descriptor: " +
01653                     xmlDescriptorUrl + " ********************");
01654             }
01655             ProActiveDescriptorHandler proActiveDescriptorHandler = ProActiveDescriptorHandler.createProActiveDescriptor(xmlDescriptorUrl,
01656                     variableContract);
01657             pad = (ProActiveDescriptor) proActiveDescriptorHandler.getResultObject();
01658             part.registerDescriptor(pad.getUrl(), pad);
01659             return pad;
01660         } catch (org.xml.sax.SAXException e) {
01661             //e.printStackTrace(); hides errors when testing parameters in xml descriptors
01662             logger.fatal(
01663                 "A problem occured when getting the proActiveDescriptor at location \""+xmlDescriptorUrl+"\".");
01664             throw new ProActiveException("A problem occured when getting the proActiveDescriptor at location \""+xmlDescriptorUrl+"\"."+e);
01665         } catch (java.io.IOException e) {
01666             //e.printStackTrace(); hides errors when testing parameters in xml descriptors
01667             logger.fatal(
01668                 "A problem occured when getting the proActiveDescriptor at location \""+xmlDescriptorUrl+"\".");
01669             throw new ProActiveException(e);
01670         }
01671     }
01672 
01683     public static void registerVirtualNode(VirtualNode virtualNode,
01684         String registrationProtocol, boolean replacePreviousBinding)
01685         throws ProActiveException, AlreadyBoundException {
01686         if (!(virtualNode instanceof VirtualNodeImpl)) {
01687             throw new ProActiveException(
01688                 "Cannot register such virtualNode since it results from a lookup!");
01689         }
01690         if (registrationProtocol == null) {
01691             registrationProtocol = System.getProperty(
01692                     "proactive.communication.protocol");
01693         }
01694         String virtualnodeName = virtualNode.getName();
01695         ProActiveRuntime part = RuntimeFactory.getProtocolSpecificRuntime(registrationProtocol);
01696         VirtualNode vn = part.getVirtualNode(virtualnodeName);
01697         if (vn == null) {
01698             throw new ProActiveException("VirtualNode " + virtualnodeName +
01699                 " has not been yet activated or does not exist! Try to activate it first !");
01700         }
01701         part.registerVirtualNode(UrlBuilder.appendVnSuffix(virtualnodeName),
01702             replacePreviousBinding);
01703     }
01704 
01714     public static VirtualNode lookupVirtualNode(String url)
01715         throws ProActiveException {
01716         ProActiveRuntime remoteProActiveRuntime = null;
01717         try {
01718             remoteProActiveRuntime = RuntimeFactory.getRuntime(UrlBuilder.buildVirtualNodeUrl(
01719                         url), UrlBuilder.getProtocol(url));
01720         } catch (UnknownHostException ex) {
01721             throw new ProActiveException(ex);
01722         }
01723         return remoteProActiveRuntime.getVirtualNode(UrlBuilder.getNameFromUrl(
01724                 url));
01725     }
01726 
01733     public static void unregisterVirtualNode(VirtualNode virtualNode)
01734         throws ProActiveException {
01735         //VirtualNode vn = ((VirtualNodeStrategy)virtualNode).getVirtualNode();
01736         if (!(virtualNode instanceof VirtualNodeImpl)) {
01737             throw new ProActiveException(
01738                 "Cannot unregister such virtualNode since it results from a lookup!");
01739         }
01740         String virtualNodeName = virtualNode.getName();
01741         ProActiveRuntime part = RuntimeFactory.getProtocolSpecificRuntime(((VirtualNodeImpl) virtualNode).getRegistrationProtocol());
01742         part.unregisterVirtualNode(UrlBuilder.appendVnSuffix(
01743                 virtualNode.getName()));
01744         if (logger.isInfoEnabled()) {
01745             logger.info("Success at unbinding " + virtualNodeName);
01746         }
01747     }
01748 
01763     public static Body getBodyOnThis() {
01764         return LocalBodyStore.getInstance().getCurrentThreadBody();
01765     }
01766 
01773     public static StubObject getStubOnThis() {
01774         Body body = getBodyOnThis();
01775 
01776         if (logger.isDebugEnabled()) {
01777             //logger.debug("ProActive: getStubOnThis() returns " + body);
01778         }
01779         if (body == null) {
01780             return null;
01781         }
01782 
01783         return getStubForBody(body);
01784     }
01785 
01796     public static void migrateTo(Object activeObject) throws MigrationException {
01797         migrateTo(getNodeFromURL(getNodeURLFromActiveObject(activeObject)));
01798     }
01799 
01810     public static void migrateTo(String nodeURL) throws MigrationException {
01811         if (logger.isDebugEnabled()) {
01812             logger.debug("migrateTo " + nodeURL);
01813         }
01814         ProActive.migrateTo(getNodeFromURL(nodeURL));
01815     }
01816 
01826     public static void migrateTo(Node node) throws MigrationException {
01827         if (logger.isDebugEnabled()) {
01828             logger.debug("migrateTo " + node);
01829         }
01830         Body bodyToMigrate = getBodyOnThis();
01831         if (!(bodyToMigrate instanceof Migratable)) {
01832             throw new MigrationException(
01833                 "This body cannot migrate. It doesn't implement Migratable interface");
01834         }
01835 
01836         ((Migratable) bodyToMigrate).migrateTo(node);
01837     }
01838 
01849     public static void migrateTo(Body bodyToMigrate, Object activeObject,
01850         boolean isNFRequest) throws MigrationException {
01851         ProActive.migrateTo(bodyToMigrate,
01852             getNodeFromURL(getNodeURLFromActiveObject(activeObject)),
01853             isNFRequest);
01854     }
01855 
01866     public static void migrateTo(Body bodyToMigrate, String nodeURL,
01867         boolean isNFRequest) throws MigrationException {
01868         ProActive.migrateTo(bodyToMigrate, getNodeFromURL(nodeURL), isNFRequest);
01869     }
01870 
01881     public static void migrateTo(Body bodyToMigrate, Node node,
01882         boolean isNFRequest) throws MigrationException {
01883         //In the context of ProActive, migration of an active object is considered as a non functional request. 
01884         //That's why "true" is set by default for the "isNFRequest" parameter. 
01885         ProActive.migrateTo(bodyToMigrate, node, true,
01886             org.objectweb.proactive.core.body.request.Request.NFREQUEST_IMMEDIATE_PRIORITY);
01887     }
01888 
01900     public static void migrateTo(Body bodyToMigrate, Node node,
01901         boolean isNFRequest, int priority) throws MigrationException {
01902         if (!(bodyToMigrate instanceof Migratable)) {
01903             throw new MigrationException(
01904                 "This body cannot migrate. It doesn't implement Migratable interface");
01905         }
01906 
01907         Object[] arguments = { node };
01908 
01909         try {
01910             BodyRequest request = new BodyRequest(bodyToMigrate, "migrateTo",
01911                     new Class[] { Node.class }, arguments, isNFRequest, priority);
01912             request.send(bodyToMigrate);
01913         } catch (NoSuchMethodException e) {
01914             throw new MigrationException("Cannot find method migrateTo this body. Non sense since the body is instance of Migratable",
01915                 e);
01916         } catch (java.io.IOException e) {
01917             throw new MigrationException("Cannot send the request to migrate", e);
01918         }
01919     }
01920 
01927     public static int waitForAny(java.util.Vector futures) {
01928         try {
01929             return waitForAny(futures, 0);
01930         } catch (ProActiveException e) {
01931             //Exception above should never be thrown since timeout=0 means no timeout
01932             e.printStackTrace();
01933             return -1;
01934         }
01935     }
01936 
01946     public static int waitForAny(java.util.Vector futures, long timeout)
01947         throws ProActiveException {
01948         FuturePool fp = getBodyOnThis().getFuturePool();
01949 
01950         synchronized (fp) {
01951             while (true) {
01952                 java.util.Iterator it = futures.iterator();
01953                 int index = 0;
01954 
01955                 while (it.hasNext()) {
01956                     Object current = it.next();
01957 
01958                     if (!isAwaited(current)) {
01959                         return index;
01960                     }
01961 
01962                     index++;
01963                 }
01964                 fp.waitForReply(timeout);
01965             }
01966         }
01967     }
01968 
01974     public static void waitForAll(java.util.Vector futures) {
01975         try {
01976             ProActive.waitForAll(futures, 0);
01977         } catch (ProActiveException e) {
01978             //Exception above should never be thrown since timeout=0 means no timeout
01979             e.printStackTrace();
01980         }
01981     }
01982 
01991     public static void waitForAll(java.util.Vector futures, long timeout)
01992         throws ProActiveException {
01993         FuturePool fp = getBodyOnThis().getFuturePool();
01994 
01995         synchronized (fp) {
01996             boolean oneIsMissing = true;
01997 
01998             while (oneIsMissing) {
01999                 oneIsMissing = false;
02000 
02001                 java.util.Iterator it = futures.iterator();
02002 
02003                 while (it.hasNext()) {
02004                     Object current = it.next();
02005 
02006                     if (isAwaited(current)) {
02007                         oneIsMissing = true;
02008                     }
02009                 }
02010 
02011                 if (oneIsMissing) {
02012                     fp.waitForReply(timeout);
02013                 }
02014             }
02015         }
02016     }
02017 
02023     public static void waitForTheNth(java.util.Vector futures, int n) {
02024         FuturePool fp = getBodyOnThis().getFuturePool();
02025 
02026         synchronized (fp) {
02027             Object current = futures.get(n);
02028 
02029             if (isAwaited(current)) {
02030                 waitFor(current);
02031             }
02032         }
02033     }
02034 
02043     public static void waitForTheNth(java.util.Vector futures, int n,
02044         long timeout) throws ProActiveException {
02045         FuturePool fp = getBodyOnThis().getFuturePool();
02046 
02047         synchronized (fp) {
02048             Object current = futures.get(n);
02049 
02050             if (isAwaited(current)) {
02051                 waitFor(current, timeout);
02052             }
02053         }
02054     }
02055 
02063     public static boolean allAwaited(java.util.Vector futures) {
02064         FuturePool fp = getBodyOnThis().getFuturePool();
02065 
02066         synchronized (fp) {
02067             java.util.Iterator it = futures.iterator();
02068 
02069             while (it.hasNext()) {
02070                 Object current = it.next();
02071 
02072                 if (!isAwaited(current)) {
02073                     return false;
02074                 }
02075             }
02076             return true;
02077         }
02078     }
02079 
02085     public static boolean isAwaited(Object future) {
02086         // If the object is not reified, it cannot be a future
02087         if ((MOP.isReifiedObject(future)) == false) {
02088             return false;
02089         } else {
02090             org.objectweb.proactive.core.mop.Proxy theProxy = ((StubObject) future).getProxy();
02091 
02092             // If it is reified but its proxy is not of type future, we cannot wait
02093             if (!(theProxy instanceof Future)) {
02094                 return false;
02095             } else {
02096                 if (((Future) theProxy).isAwaited()) {
02097                     return true;
02098                 } else {
02099                     return isAwaited(((Future) theProxy).getResult());
02100                 }
02101             }
02102         }
02103     }
02104 
02112     public static Object getFutureValue(Object future) {
02113         // If the object is not reified, it cannot be a future
02114         if ((MOP.isReifiedObject(future)) == false) {
02115             return future;
02116         } else {
02117             org.objectweb.proactive.core.mop.Proxy theProxy = ((StubObject) future).getProxy();
02118 
02119             // If it is reified but its proxy is not of type future, we cannot wait
02120             if (!(theProxy instanceof Future)) {
02121                 return future;
02122             } else {
02123                 Object o = ((Future) theProxy).getResult();
02124 
02125                 return getFutureValue(o);
02126             }
02127         }
02128     }
02129 
02133     public static void enableAC(Object obj) throws java.io.IOException {
02134         UniversalBody body = getRemoteBody(obj);
02135         body.enableAC();
02136     }
02137 
02141     public static void disableAC(Object obj) throws java.io.IOException {
02142         UniversalBody body = getRemoteBody(obj);
02143         body.disableAC();
02144     }
02145 
02153     public static void terminateActiveObject(Object ao, boolean immediate) {
02154         Proxy proxy = ((StubObject) ao).getProxy();
02155         try {
02156             if (immediate) {
02157                 NonFunctionalServices.terminateAOImmediately(proxy);
02158             } else {
02159                 NonFunctionalServices.terminateAO(proxy);
02160             }
02161         } catch (Throwable e) {
02162             e.printStackTrace();
02163         }
02164     }
02165 
02177     public static void setImmediateService(Object obj, String methodName)
02178         throws java.io.IOException {
02179         UniversalBody body = getRemoteBody(obj);
02180         body.setImmediateService(methodName);
02181     }
02182 
02195     public static void setImmediateService(Object obj, String methodName,
02196         Class[] parametersTypes) throws IOException {
02197         UniversalBody body = getRemoteBody(obj);
02198         body.setImmediateService(methodName, parametersTypes);
02199     }
02200 
02212     public static void removeImmediateService(Object obj, String methodName,
02213         Class[] parametersTypes) throws IOException {
02214         UniversalBody body = getRemoteBody(obj);
02215         body.removeImmediateService(methodName, parametersTypes);
02216     }
02217 
02222     private static BodyAdapter getRemoteBody(Object obj) {
02223         // Check if obj is really a reified object
02224         if (!(MOP.isReifiedObject(obj))) {
02225             throw new ProActiveRuntimeException("The given object " + obj +
02226                 " is not a reified object");
02227         }
02228 
02229         // Find the appropriate remoteBody
02230         org.objectweb.proactive.core.mop.Proxy myProxy = ((StubObject) obj).getProxy();
02231 
02232         if (myProxy == null) {
02233             throw new ProActiveRuntimeException(
02234                 "Cannot find a Proxy on the stub object: " + obj);
02235         }
02236 
02237         BodyProxy myBodyProxy = (BodyProxy) myProxy;
02238         BodyAdapter body = myBodyProxy.getBody().getRemoteAdapter();
02239         return body;
02240     }
02241 
02245     public static String getJobId() {
02246         return ProActive.getBodyOnThis().getJobID();
02247     }
02248 
02256     public static void exposeAsWebService(Object o, String url, String urn,
02257         String[] methods) {
02258         ProActiveDeployer.deploy(urn, url, o, methods);
02259     }
02260 
02266     public static void unExposeAsWebService(String urn, String url) {
02267         ProActiveDeployer.undeploy(urn, url);
02268     }
02269 
02278     public static void exposeComponentAsWebService(Component component,
02279         String url, String componentName) {
02280         ProActiveDeployer.deployComponent(componentName, url, component);
02281     }
02282 
02289     public static void unExposeComponentAsWebService(String componentName,
02290         String url, Component component) {
02291         ProActiveDeployer.undeployComponent(componentName, url, component);
02292     }
02293 
02294     //
02295     // -- PRIVATE METHODS -----------------------------------------------
02296     //
02297     private static String getNodeURLFromActiveObject(Object o)
02298         throws MigrationException {
02299         //first we check if the parameter is an active object,
02300         if (!org.objectweb.proactive.core.mop.MOP.isReifiedObject(o)) {
02301             throw new MigrationException(
02302                 "The parameter is not an active object");
02303         }
02304 
02305         //now we get a reference on the remoteBody of this guy
02306         BodyProxy destProxy = (BodyProxy) ((org.objectweb.proactive.core.mop.StubObject) o).getProxy();
02307 
02308         return destProxy.getBody().getNodeURL();
02309     }
02310 
02311     private static Node getNodeFromURL(String url) throws MigrationException {
02312         try {
02313             return NodeFactory.getNode(url);
02314         } catch (NodeException e) {
02315             throw new MigrationException("The node of given URL " + url +
02316                 " cannot be localized", e);
02317         }
02318     }
02319 
02320     // -------------------------------------------------------------------------------------------
02321     // 
02322     // STUB CREATION
02323     // 
02324     // -------------------------------------------------------------------------------------------
02325     private static StubObject getStubForBody(Body body) {
02326         try {
02327             return createStubObject(body.getReifiedObject(),
02328                 new Object[] { body },
02329                 body.getReifiedObject().getClass().getName(), null);
02330         } catch (MOPException e) {
02331             throw new ProActiveRuntimeException(
02332                 "Cannot create Stub for this Body e=" + e);
02333         }
02334     }
02335 
02336     public static Object createStubObject(String className, UniversalBody body)
02337         throws MOPException {
02338         return createStubObject(className, null, null, new Object[] { body });
02339     }
02340 
02341     private static Object createStubObject(String className,
02342         Class[] genericParameters, Object[] constructorParameters, Node node,
02343         Active activity, MetaObjectFactory factory) throws MOPException {
02344         return createStubObject(className, genericParameters,
02345             constructorParameters,
02346             new Object[] { node, activity, factory, ProActive.getJobId() });
02347     }
02348 
02349     private static Object createStubObject(String className,
02350         Class[] genericParameters, Object[] constructorParameters,
02351         Object[] proxyParameters) throws MOPException {
02352         try {
02353             return MOP.newInstance(className, genericParameters,
02354                 constructorParameters, Constants.DEFAULT_BODY_PROXY_CLASS_NAME,
02355                 proxyParameters);
02356         } catch (ClassNotFoundException e) {
02357             throw new ConstructionOfProxyObjectFailedException(
02358                 "Class can't be found e=" + e);
02359         }
02360     }
02361 
02362     private static Object createStubObject(Object target,
02363         String nameOfTargetType, Class[] genericParameters, Node node,
02364         Active activity, MetaObjectFactory factory) throws MOPException {
02365         return createStubObject(target,
02366             new Object[] { node, activity, factory, ProActive.getJobId() },
02367             nameOfTargetType, genericParameters);
02368     }
02369 
02370     private static StubObject createStubObject(Object object,
02371         Object[] proxyParameters, String nameOfTargetType,
02372         Class[] genericParameters) throws MOPException {
02373         try {
02374             return (StubObject) MOP.turnReified(nameOfTargetType,
02375                 Constants.DEFAULT_BODY_PROXY_CLASS_NAME, proxyParameters,
02376                 object, genericParameters);
02377         } catch (ClassNotFoundException e) {
02378             throw new ConstructionOfProxyObjectFailedException(
02379                 "Class can't be found e=" + e);
02380         }
02381     }
02382 
02383     /*** <Exceptions> See ExceptionHandler.java for the documentation ***/
02389     public static void tryWithCatch(Class c) {
02390         tryWithCatch(new Class[] { c });
02391     }
02392 
02398     public static void tryWithCatch(Class[] c) {
02399         ExceptionHandler.tryWithCatch(c);
02400     }
02401 
02405     public static void endTryWithCatch() {
02406         ExceptionHandler.endTryWithCatch();
02407     }
02408 
02413     public static void removeTryWithCatch() {
02414         ExceptionHandler.removeTryWithCatch();
02415     }
02416 
02421     public static void throwArrivedException() {
02422         ExceptionHandler.throwArrivedException();
02423     }
02424 
02429     public static void waitForPotentialException() {
02430         ExceptionHandler.waitForPotentialException();
02431     }
02432 
02438     public static void addNFEListenerOnJVM(NFEListener listener) {
02439         NFEManager.addNFEListener(listener);
02440     }
02441 
02447     public static void removeNFEListenerOnJVM(NFEListener listener) {
02448         NFEManager.removeNFEListener(listener);
02449     }
02450 
02457     public static void addNFEListenerOnAO(Object ao, NFEListener listener) {
02458 
02459         /* Security hazard: arbitrary code execution by the ao... */
02460         BodyAdapter body = getRemoteBody(ao);
02461         body.addNFEListener(listener);
02462     }
02463 
02470     public static void removeNFEListenerOnAO(Object ao, NFEListener listener) {
02471         BodyAdapter body = getRemoteBody(ao);
02472         body.removeNFEListener(listener);
02473     }
02474 
02481     public static void addNFEListenerOnProxy(Object ao, NFEListener listener) {
02482         try {
02483             ((AbstractProxy) ao).addNFEListener(listener);
02484         } catch (ClassCastException cce) {
02485             throw new IllegalArgumentException(
02486                 "The object must be a proxy to an active object");
02487         }
02488     }
02489 
02496     public static void removeNFEListenerOnProxy(Object ao, NFEListener listener) {
02497         try {
02498             ((AbstractProxy) ao).removeNFEListener(listener);
02499         } catch (ClassCastException cce) {
02500             throw new IllegalArgumentException(
02501                 "The object must be a proxy to an active object");
02502         }
02503     }
02504 
02505     private static ProxyForGroup getGroupProxy(Object group) {
02506         ProxyForGroup pfg;
02507 
02508         try {
02509             pfg = (ProxyForGroup) ProActiveGroup.getGroup(group);
02510         } catch (ClassCastException cce) {
02511             pfg = null;
02512         }
02513 
02514         if (pfg == null) {
02515             throw new IllegalArgumentException("The argument must be a group");
02516         }
02517 
02518         return pfg;
02519     }
02520 
02527     public static void addNFEListenerOnGroup(Object group, NFEListener listener) {
02528         getGroupProxy(group).addNFEListener(listener);
02529     }
02530 
02537     public static void removeNFEListenerOnGroup(Object group,
02538         NFEListener listener) {
02539         getGroupProxy(group).removeNFEListener(listener);
02540     }
02541 
02549     public static Collection getAllExceptions() {
02550         return ExceptionHandler.getAllExceptions();
02551     }
02552 
02557     public static Node getNode() throws NodeException {
02558         BodyProxy destProxy = (BodyProxy) ((StubObject) getStubOnThis()).getProxy();
02559 
02560         return NodeFactory.getNode(destProxy.getBody().getNodeURL());
02561     }
02562 
02567     public static void exitSuccess() {
02568         System.exit(0);
02569     }
02570 
02575     public static void exitFailure() {
02576         System.exit(1);
02577     }
02578 
02584     public void enableExitOnEmpty() {
02585         LocalBodyStore.getInstance().enableExitOnEmpty();
02586     }
02587 
02592     public static String getProActiveVersion() {
02593         return "3.2";
02594     }
02595 }

Generated on Mon Jan 22 15:16:11 2007 for ProActive by  doxygen 1.5.1