00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030  
00031 package org.objectweb.proactive.core.component;
00032 
00033 import java.io.IOException;
00034 import java.io.Serializable;
00035 import java.util.ArrayList;
00036 import java.util.Arrays;
00037 import java.util.Hashtable;
00038 import java.util.List;
00039 import java.util.Map;
00040 import java.util.Vector;
00041 import java.util.concurrent.ExecutorService;
00042 import java.util.concurrent.Executors;
00043 import java.util.concurrent.TimeUnit;
00044 
00045 import javax.naming.NamingException;
00046 
00047 import org.apache.log4j.Logger;
00048 import org.objectweb.fractal.api.Component;
00049 import org.objectweb.fractal.api.NoSuchInterfaceException;
00050 import org.objectweb.fractal.api.Type;
00051 import org.objectweb.fractal.api.factory.Factory;
00052 import org.objectweb.fractal.api.factory.GenericFactory;
00053 import org.objectweb.fractal.api.factory.InstantiationException;
00054 import org.objectweb.fractal.api.type.ComponentType;
00055 import org.objectweb.fractal.api.type.InterfaceType;
00056 import org.objectweb.fractal.api.type.TypeFactory;
00057 import org.objectweb.proactive.ActiveObjectCreationException;
00058 import org.objectweb.proactive.ProActive;
00059 import org.objectweb.proactive.core.ProActiveRuntimeException;
00060 import org.objectweb.proactive.core.body.ProActiveMetaObjectFactory;
00061 import org.objectweb.proactive.core.body.UniversalBody;
00062 import org.objectweb.proactive.core.body.http.HttpBodyAdapter;
00063 import org.objectweb.proactive.core.body.ibis.IbisBodyAdapter;
00064 import org.objectweb.proactive.core.body.rmi.RmiBodyAdapter;
00065 import org.objectweb.proactive.core.body.rmi.SshRmiBodyAdapter;
00066 import org.objectweb.proactive.core.component.body.ComponentBody;
00067 import org.objectweb.proactive.core.component.controller.ComponentParametersController;
00068 import org.objectweb.proactive.core.component.controller.GathercastController;
00069 import org.objectweb.proactive.core.component.controller.MigrationController;
00070 import org.objectweb.proactive.core.component.controller.MulticastController;
00071 import org.objectweb.proactive.core.component.controller.ProActiveBindingController;
00072 import org.objectweb.proactive.core.component.controller.ProActiveContentController;
00073 import org.objectweb.proactive.core.component.exceptions.InstantiationExceptionListException;
00074 import org.objectweb.proactive.core.component.factory.ProActiveGenericFactory;
00075 import org.objectweb.proactive.core.component.identity.ProActiveComponent;
00076 import org.objectweb.proactive.core.component.representative.ProActiveComponentRepresentative;
00077 import org.objectweb.proactive.core.component.representative.ProActiveComponentRepresentativeFactory;
00078 import org.objectweb.proactive.core.component.type.Composite;
00079 import org.objectweb.proactive.core.component.type.ProActiveInterfaceType;
00080 import org.objectweb.proactive.core.component.type.ProActiveTypeFactory;
00081 import org.objectweb.proactive.core.component.type.ProActiveTypeFactoryImpl;
00082 import org.objectweb.proactive.core.descriptor.data.VirtualNode;
00083 import org.objectweb.proactive.core.group.ProActiveComponentGroup;
00084 import org.objectweb.proactive.core.group.ProActiveGroup;
00085 import org.objectweb.proactive.core.mop.StubObject;
00086 import org.objectweb.proactive.core.node.Node;
00087 import org.objectweb.proactive.core.node.NodeException;
00088 import org.objectweb.proactive.core.util.UrlBuilder;
00089 import org.objectweb.proactive.core.util.log.Loggers;
00090 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00091 
00092 
00104 public class Fractive implements ProActiveGenericFactory, Component, Factory {
00105     private static Fractive instance = null;
00106     private TypeFactory typeFactory = (TypeFactory) ProActiveTypeFactoryImpl.instance();
00107     private Type type = null;
00108     private static Logger logger = ProActiveLogger.getLogger(Loggers.COMPONENTS);
00109 
00114     public Fractive() {
00115     }
00116 
00122     private static Fractive instance() {
00123         if (instance == null) {
00124             instance = new Fractive();
00125         }
00126         return instance;
00127     }
00128 
00138     public static ComponentParametersController getComponentParametersController(
00139         final Component component) throws NoSuchInterfaceException {
00140         return (ComponentParametersController) component.getFcInterface(Constants.COMPONENT_PARAMETERS_CONTROLLER);
00141     }
00142     
00143     public static ProActiveBindingController getBindingController(
00144             final ProActiveComponent component) throws NoSuchInterfaceException {
00145             return (ProActiveBindingController) component.getFcInterface(Constants.BINDING_CONTROLLER);
00146         }
00147 
00157     public static MulticastController getMulticastController(
00158         final Component component) throws NoSuchInterfaceException {
00159         return (MulticastController) component.getFcInterface(Constants.MULTICAST_CONTROLLER);
00160     }
00161     
00171     public static ProActiveContentController getProActiveContentController(
00172         final Component component) throws NoSuchInterfaceException {
00173         return (ProActiveContentController) component.getFcInterface(Constants.CONTENT_CONTROLLER);
00174     }
00175     
00185     public static GathercastController getGathercastController(
00186         final Component component) throws NoSuchInterfaceException {
00187         return (GathercastController) component.getFcInterface(Constants.GATHERCAST_CONTROLLER);
00188     }
00189 
00199     public static MigrationController getMigrationController(
00200         final Component component) throws NoSuchInterfaceException {
00201         return (MigrationController) component.getFcInterface(Constants.MIGRATION_CONTROLLER);
00202     }
00203 
00214     public static ProActiveInterface createCollectiveClientInterface(
00215         String itfName, String itfSignature, Component owner)
00216         throws ProActiveRuntimeException {
00217         try {
00218             ProActiveInterfaceType itf_type = (ProActiveInterfaceType) ProActiveTypeFactoryImpl.instance()
00219                                                                                                .createFcItfType(itfName,
00220                     itfSignature, TypeFactory.CLIENT, TypeFactory.MANDATORY,
00221                     TypeFactory.COLLECTION);
00222             ProActiveInterface itf_ref_group = ProActiveComponentGroup.newComponentInterfaceGroup(itf_type,
00223                     owner);
00224             return itf_ref_group;
00225         } catch (Exception e) {
00226             throw new ProActiveRuntimeException("Impossible to create a collective client interface ",
00227                 e);
00228         }
00229     }
00230 
00241     public static ProActiveInterface createMulticastClientInterface(
00242         String itfName, String itfSignature, Component owner)
00243         throws ProActiveRuntimeException {
00244         try {
00245             ProActiveInterfaceType itf_type = (ProActiveInterfaceType) ProActiveTypeFactoryImpl.instance()
00246                                                                                                .createFcItfType(itfName,
00247                     itfSignature, TypeFactory.CLIENT, TypeFactory.MANDATORY,
00248                     ProActiveTypeFactory.MULTICAST_CARDINALITY);
00249 
00250             ProActiveInterface itf_ref_group = ProActiveComponentGroup.newComponentInterfaceGroup(itf_type,
00251                     owner);
00252             return itf_ref_group;
00253         } catch (Exception e) {
00254             throw new ProActiveRuntimeException("Impossible to create a collective client interface ",
00255                 e);
00256         }
00257     }
00258 
00269     
00270     
00271     
00272     
00273     
00274 
00284     public static ProActiveInterface createMulticastClientInterface(
00285         String itfName, String itfSignature) throws ProActiveRuntimeException {
00286         return Fractive.createCollectiveClientInterface(itfName, itfSignature,
00287             null);
00288     }
00289 
00290     
00291 
00292 
00293 
00294     public Component newFcInstance(Type type,
00295         ControllerDescription controllerDesc, ContentDescription contentDesc)
00296         throws InstantiationException {
00297         return newFcInstance(type, controllerDesc, contentDesc, (Node) null);
00298     }
00299 
00300     
00301 
00302 
00303 
00304     public Component newFcInstance(Type type, Object controllerDesc,
00305         Object contentDesc) throws InstantiationException {
00306         try {
00307             return newFcInstance(type, (ControllerDescription) controllerDesc,
00308                 (ContentDescription) contentDesc);
00309         } catch (ClassCastException e) {
00310             if ((type == null) && (controllerDesc == null) &&
00311                     (contentDesc instanceof Map)) {
00312                 
00313                 
00314                 return this;
00315             }
00316             if ((controllerDesc instanceof ControllerDescription) &&
00317                     ((contentDesc instanceof String) || (contentDesc == null))) {
00318                 
00319                 
00320                 return newFcInstance(type, controllerDesc,
00321                     (contentDesc == null) ? null
00322                                           : new ContentDescription(
00323                         (String) contentDesc));
00324             }
00325 
00326             
00327             if ("composite".equals(controllerDesc) && (contentDesc == null)) {
00328                 return newFcInstance(type,
00329                     new ControllerDescription(null, Constants.COMPOSITE), null);
00330             }
00331             if ("primitive".equals(controllerDesc) &&
00332                     (contentDesc instanceof String)) {
00333                 return newFcInstance(type,
00334                     new ControllerDescription(null, Constants.PRIMITIVE),
00335                     new ContentDescription((String) contentDesc));
00336             }
00337 
00338             
00339             throw new InstantiationException(
00340                 "With this implementation, parameters must be of respective types : " +
00341                 Type.class.getName() + ',' +
00342                 ControllerDescription.class.getName() + ',' +
00343                 ContentDescription.class.getName());
00344         }
00345     }
00346 
00347     
00348 
00349 
00350 
00351     public Component newFcInstance(Type type,
00352         ControllerDescription controllerDesc, ContentDescription contentDesc,
00353         Node node) throws InstantiationException {
00354         if (contentDesc == null) {
00355             
00356             
00357             if (Constants.COMPOSITE.equals(controllerDesc.getHierarchicalType())) {
00358                 contentDesc = new ContentDescription(Composite.class.getName());
00359             } else {
00360                 throw new InstantiationException(
00361                     "Content can be null only if the hierarchical type of the component is composite");
00362             }
00363         }
00364         try {
00365             
00366             
00367             ComponentParameters componentParameters = new ComponentParameters((ComponentType) type,
00368                     controllerDesc);
00369             if (contentDesc.getFactory() == null) {
00370                 
00371                 Map<String, Object> factory_params = new Hashtable<String, Object>(1);
00372 
00373                 factory_params.put(ProActiveMetaObjectFactory.COMPONENT_PARAMETERS_KEY,
00374                     componentParameters);
00375                 if (controllerDesc.isSynchronous() &&
00376                         (Constants.COMPOSITE.equals(
00377                             controllerDesc.getHierarchicalType()))) {
00378                     factory_params.put(ProActiveMetaObjectFactory.SYNCHRONOUS_COMPOSITE_COMPONENT_KEY,
00379                             Constants.SYNCHRONOUS);
00380                 }
00381                 contentDesc.setFactory(new ProActiveMetaObjectFactory(
00382                         factory_params));
00383                 
00384                 
00385             }
00386 
00387             
00388             Object ao = null ;
00389 
00390                 
00391             
00392             
00393             ao = ProActive.newActive(contentDesc.getClassName(),
00394                     null, contentDesc.getConstructorParameters(),
00395                     node, contentDesc.getActivity(), contentDesc.getFactory());
00396 
00397             
00398             org.objectweb.proactive.core.mop.Proxy myProxy = ((StubObject) ao).getProxy();
00399             if (myProxy == null) {
00400                 throw new ProActiveRuntimeException(
00401                     "Cannot find a Proxy on the stub object: " + ao);
00402             }
00403             ProActiveComponentRepresentative representative = ProActiveComponentRepresentativeFactory.instance()
00404                                                                                                      .createComponentRepresentative((ComponentType) type,
00405                     componentParameters.getHierarchicalType(), myProxy,
00406                     componentParameters.getControllerDescription()
00407                                        .getControllersConfigFileLocation());
00408             representative.setStubOnBaseObject((StubObject) ao);
00409             return representative;
00410         } catch (ActiveObjectCreationException e) {
00411             e.printStackTrace();
00412             throw new InstantiationException(e.getMessage());
00413         } catch (NodeException e) {
00414             throw new InstantiationException(e.getMessage());
00415         }
00416     }
00417 
00418     
00419 
00420 
00421 
00422     public Component newFcInstance(Type type,
00423         ControllerDescription controllerDesc, ContentDescription contentDesc,
00424         VirtualNode virtualNode) throws InstantiationException {
00425         if (virtualNode == null) {
00426             return newFcInstance(type, controllerDesc, contentDesc, (Node) null);
00427         }
00428         try {
00429             virtualNode.activate();
00430             if (virtualNode.getNodes().length == 0) {
00431                 throw new InstantiationException(
00432                     "Cannot create component on virtual node as no node is associated with this virtual node");
00433             }
00434             return newFcInstance(type, controllerDesc, contentDesc,
00435                 virtualNode.getNode());
00436         } catch (NodeException e) {
00437             throw new InstantiationException(
00438                 "could not instantiate components due to a deployment problem : " +
00439                 e.getMessage());
00440         }
00441     }
00442 
00443     
00444 
00445 
00446 
00447 
00448 
00449     public List<Component> newFcInstanceAsList(Type type,
00450         ControllerDescription controllerDesc, ContentDescription contentDesc,
00451         VirtualNode virtualNode) throws InstantiationException {
00452         if (virtualNode == null) {
00453             return newFcInstanceAsList(type, controllerDesc, contentDesc,
00454                 (Node[]) null);
00455         }
00456         try {
00457             virtualNode.activate();
00458             return newFcInstanceAsList(type, controllerDesc, contentDesc,
00459                 virtualNode.getNodes());
00460         } catch (NodeException e) {
00461             throw new InstantiationException(
00462                 "could not instantiate components due to a deployment problem : " +
00463                 e.getMessage());
00464         }
00465     }
00466 
00467     
00468 
00469 
00470 
00471 
00472 
00473     public List<Component> newFcInstanceAsList(Type type,
00474         ControllerDescription controllerDesc, ContentDescription contentDesc,
00475         Node[] nodes) throws InstantiationException {
00476         ContentDescription[] contentDescArray = new ContentDescription[nodes.length];
00477         Arrays.fill(contentDescArray, contentDesc);
00478         return newFcInstanceAsList(type, controllerDesc, contentDescArray, nodes);
00479     }
00480 
00481     
00482 
00483 
00484 
00485     public List<Component> newFcInstanceAsList(Type type,
00486         ControllerDescription controllerDesc, ContentDescription[] contentDesc,
00487         Node[] nodes) throws InstantiationException {
00488         try {
00489             Component components = ProActiveComponentGroup.newComponentRepresentativeGroup((ComponentType) type,
00490                     controllerDesc);
00491             List<Component> componentsList = ProActiveGroup.getGroup(components);
00492             if (Constants.PRIMITIVE.equals(controllerDesc.getHierarchicalType())) {
00493                 if (contentDesc.length > 1) { 
00494                                               
00495                                               
00496                                               
00497                                               
00498                                               
00499                                               
00500                                               
00501                     String original_component_name = controllerDesc.getName();
00502 
00503                     
00504                     ExecutorService threadPool = Executors.newCachedThreadPool();
00505 
00506                     List<InstantiationException> exceptions = new Vector<InstantiationException>();
00507 
00508                     Component c = new MockComponent();
00509 
00510                     for (int i = 0; i < nodes.length; i++) {
00511                         componentsList.add(c);
00512                     }
00513 
00514                     for (int i = 0; i < nodes.length; i++) {
00515                         ComponentBuilderTask task = new ComponentBuilderTask(exceptions,
00516                                 componentsList, i, type, controllerDesc,
00517                                 original_component_name, contentDesc, nodes);
00518                         threadPool.execute(task);
00519                     }
00520                     threadPool.shutdown();
00521                     try {
00522                         threadPool.awaitTermination(new Integer(
00523                                 System.getProperty(
00524                                     "components.creation.timeout")),
00525                             TimeUnit.SECONDS);
00526                     } catch (InterruptedException e1) {
00527                         
00528                         e1.printStackTrace();
00529                     }
00530                     if (!exceptions.isEmpty()) {
00531                         InstantiationException e = new InstantiationException(
00532                                 "Creation of some of the components failed");
00533                         e.initCause(new InstantiationExceptionListException(
00534                                 exceptions));
00535                         throw e;
00536                     }
00537                 } else {
00538                     
00539                     
00540                     componentsList.add(newFcInstance(type, controllerDesc,
00541                             contentDesc[0], nodes[0]));
00542                 }
00543             }
00544             return componentsList;
00545         } catch (ClassNotFoundException e) {
00546             throw new InstantiationException(e.getMessage());
00547         }
00548     }
00549 
00550     
00551 
00552 
00553 
00554     public List<Component> newFcInstanceAsList(Type type,
00555         ControllerDescription controllerDesc, ContentDescription[] contentDesc,
00556         VirtualNode virtualNode) throws InstantiationException {
00557         if (virtualNode == null) {
00558             return newFcInstanceAsList(type, controllerDesc, contentDesc,
00559                 (Node[]) null);
00560         }
00561         try {
00562             virtualNode.activate();
00563 
00564             return newFcInstanceAsList(type, controllerDesc, contentDesc,
00565                 virtualNode.getNodes());
00566         } catch (NodeException e) {
00567             throw new InstantiationException(
00568                 "could not instantiate components due to a deployment problem : " +
00569                 e.getMessage());
00570         }
00571     }
00572 
00573     
00574 
00575 
00576 
00577     public Object getFcInterface(String itfName)
00578         throws NoSuchInterfaceException {
00579         if ("generic-factory".equals(itfName)) {
00580             return this;
00581         } else if ("type-factory".equals(itfName)) {
00582             return typeFactory;
00583         } else {
00584             throw new NoSuchInterfaceException(itfName);
00585         }
00586     }
00587 
00588     
00589 
00590 
00591 
00592     public Object[] getFcInterfaces() {
00593         return null;
00594     }
00595 
00596     
00597 
00598 
00599 
00600     public Type getFcType() {
00601         if (type == null) {
00602             try {
00603                 return type = typeFactory.createFcType(new InterfaceType[] {
00604                             typeFactory.createFcItfType("generic-factory",
00605                                 GenericFactory.class.getName(), false, false,
00606                                 false),
00607                             typeFactory.createFcItfType("type-factory",
00608                                 TypeFactory.class.getName(), false, false, false)
00609                         });
00610             } catch (InstantiationException e) {
00611                 ProActiveLogger.getLogger(Loggers.COMPONENTS)
00612                                .error(e.getMessage());
00613                 return null;
00614             }
00615         } else {
00616             return type;
00617         }
00618     }
00619 
00620     
00621 
00622 
00623 
00624     public Object getFcContentDesc() {
00625         return null;
00626     }
00627 
00628     
00629 
00630 
00631 
00632     public Object getFcControllerDesc() {
00633         return null;
00634     }
00635 
00636     
00637 
00638 
00639 
00640     public Type getFcInstanceType() {
00641         return null;
00642     }
00643 
00644     
00645 
00646 
00647 
00648     public Component newFcInstance() throws InstantiationException {
00649         return this;
00650     }
00651 
00659     public static InterfaceType[] getClientInterfaceTypes(
00660         ComponentType componentType) {
00661         ArrayList<InterfaceType> client_interfaces = new ArrayList<InterfaceType>();
00662         InterfaceType[] interfaceTypes = componentType.getFcInterfaceTypes();
00663         for (int i = 0; i < interfaceTypes.length; i++) {
00664             if (interfaceTypes[i].isFcClientItf()) {
00665                 client_interfaces.add(interfaceTypes[i]);
00666             }
00667         }
00668         return client_interfaces.toArray(new InterfaceType[client_interfaces.size()]);
00669     }
00670 
00679     public static Component getComponentRepresentativeOnThis() {
00680         ComponentBody componentBody;
00681         try {
00682             componentBody = (ComponentBody) ProActive.getBodyOnThis();
00683         } catch (ClassCastException e) {
00684             logger.error(
00685                 "Cannot get a component representative from the current object, because this object is not a component");
00686             return null;
00687         }
00688         ProActiveComponent currentComponent = componentBody.getProActiveComponentImpl();
00689         return currentComponent.getRepresentativeOnThis();
00690     }
00691 
00703     public static void register(Component ref, String url)
00704         throws IOException {
00705         if (!(ref instanceof ProActiveComponentRepresentative)) {
00706             throw new IllegalArgumentException(
00707                 "This method can only register ProActive components");
00708         }
00709         ProActive.register(ref, url);
00710     }
00711 
00722     public static ProActiveComponentRepresentative lookup(String url)
00723         throws IOException, NamingException {
00724         UniversalBody b = null;
00725 
00726         String protocol = UrlBuilder.getProtocol(url);
00727 
00728         
00729         if (protocol.equals("rmi:")) {
00730             b = new RmiBodyAdapter().lookup(url);
00731         } else if (protocol.equals("rmissh:")) {
00732             b = new SshRmiBodyAdapter().lookup(url);
00733         } else if (protocol.equals("http:")) {
00734             b = new HttpBodyAdapter().lookup(url);
00735         } else if (protocol.equals("ibis:")) {
00736             b = new IbisBodyAdapter().lookup(url);
00737         } else {
00738             throw new IOException("Protocol " + protocol + " not defined");
00739         }
00740 
00741         try {
00742             StubObject stub = (StubObject) ProActive.createStubObject(ProActiveComponentRepresentative.class.getName(),
00743                     b);
00744             return ProActiveComponentRepresentativeFactory.instance()
00745                                                           .createComponentRepresentative(stub.getProxy());
00746         } catch (Throwable t) {
00747             t.printStackTrace();
00748             logger.error("Could not perform lookup for component at URL: " +
00749                 url +
00750                 ", because construction of component representative failed." +
00751                 t.toString());
00752             throw new NamingException(
00753                 "Could not perform lookup for component at URL: " +
00754                 url+
00755                 ", because construction of component representative failed.");
00756         }
00757     }
00758 
00768     public static ProActiveGenericFactory getGenericFactory(
00769         final Component component) throws NoSuchInterfaceException {
00770         return (ProActiveGenericFactory) component.getFcInterface(
00771             "generic-factory");
00772     }
00773 
00774     private static class ComponentBuilderTask implements Runnable {
00775         List<InstantiationException> exceptions;
00776         List<Component> targetList;
00777         int indexInList;
00778         Type type;
00779         ControllerDescription controllerDesc;
00780         ContentDescription[] contentDesc;
00781         String originalName;
00782         Node[] nodes;
00783 
00784         public ComponentBuilderTask(List<InstantiationException> exceptions,
00785             List<Component> targetList, int indexInList, Type type,
00786             ControllerDescription controllerDesc, String originalName,
00787             ContentDescription[] contentDesc, Node[] nodes) {
00788             this.exceptions = exceptions;
00789             this.targetList = targetList;
00790             this.indexInList = indexInList;
00791             this.type = type;
00792             this.controllerDesc = controllerDesc;
00793             this.contentDesc = contentDesc;
00794             this.originalName = originalName;
00795             this.nodes = nodes;
00796         }
00797 
00798         public void run() {
00799             controllerDesc.setName(originalName + Constants.CYCLIC_NODE_SUFFIX +
00800                 indexInList);
00801             Component instance;
00802             try {
00803                 instance = Fractive.instance()
00804                                    .newFcInstance(type, controllerDesc,
00805                         contentDesc[indexInList],
00806                         nodes[indexInList % nodes.length]);
00807                 
00808                 targetList.set(indexInList, instance);
00809             } catch (InstantiationException e) {
00810                 e.printStackTrace();
00811                 
00812                 exceptions.add(e);
00813             }
00814         }
00815     }
00816 
00817     
00818     private static class MockComponent implements Component, Serializable {
00819         public Object getFcInterface(String interfaceName)
00820             throws NoSuchInterfaceException {
00821             
00822             return null;
00823         }
00824 
00825         public Object[] getFcInterfaces() {
00826             
00827             return null;
00828         }
00829 
00830         public Type getFcType() {
00831             
00832             return null;
00833         }
00834     }
00835 }