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.adl.implementations;
00032 
00033 import org.objectweb.deployment.scheduling.component.api.FactoryProviderTask;
00034 import org.objectweb.deployment.scheduling.component.lib.AbstractInstanceProviderTask;
00035 
00036 import org.objectweb.fractal.adl.ADLException;
00037 import org.objectweb.fractal.adl.Definition;
00038 import org.objectweb.fractal.adl.Node;
00039 import org.objectweb.fractal.adl.TaskMap;
00040 import org.objectweb.fractal.adl.components.Component;
00041 import org.objectweb.fractal.adl.components.ComponentContainer;
00042 import org.objectweb.fractal.adl.implementations.ControllerContainer;
00043 import org.objectweb.fractal.adl.implementations.Implementation;
00044 import org.objectweb.fractal.adl.implementations.ImplementationCompiler;
00045 import org.objectweb.fractal.adl.implementations.ImplementationContainer;
00046 import org.objectweb.fractal.adl.nodes.VirtualNodeContainer;
00047 
00048 import org.objectweb.proactive.core.ProActiveRuntimeException;
00049 import org.objectweb.proactive.core.component.Constants;
00050 import org.objectweb.proactive.core.component.ContentDescription;
00051 import org.objectweb.proactive.core.component.ControllerDescription;
00052 import org.objectweb.proactive.core.component.adl.nodes.VirtualNode;
00053 import org.objectweb.proactive.core.component.type.Composite;
00054 
00055 import java.util.List;
00056 import java.util.Map;
00057 
00058 
00062 public class ProActiveImplementationCompiler extends ImplementationCompiler {
00063     private static int counter = 0;
00064 
00065     public void compile(final List path, final ComponentContainer container,
00066         final TaskMap tasks, final Map context) throws ADLException {
00067         counter++;
00068 
00069         String implementation = null;
00070 
00071         if (container instanceof ImplementationContainer) {
00072             ImplementationContainer ic = (ImplementationContainer) container;
00073             Implementation i = ic.getImplementation();
00074 
00075             if (i != null) {
00076                 implementation = i.getClassName();
00077             }
00078         }
00079 
00080         String controller = null;
00081 
00082         if (container instanceof ControllerContainer) {
00083             ControllerContainer cc = (ControllerContainer) container;
00084 
00085             if (cc.getController() != null) {
00086                 controller = cc.getController().getDescriptor();
00087             }
00088         }
00089 
00090         String name = null;
00091 
00092         if (container instanceof Definition) {
00093             name = ((Definition) container).getName();
00094         } else if (container instanceof Component) {
00095             name = ((Component) container).getName();
00096         }
00097 
00098         String definition = null;
00099 
00100         if (container instanceof Definition) {
00101             definition = name;
00102         } else {
00103             definition = (String) ((Node) container).astGetDecoration(
00104                     "definition");
00105         }
00106 
00107         
00108         VirtualNode n = null;
00109 
00110         if (container instanceof VirtualNodeContainer) {
00111             try {
00112                 n = (VirtualNode) ((VirtualNodeContainer) container).getVirtualNode();
00113             } catch (ClassCastException e) {
00114                 throw new ProActiveRuntimeException(
00115                     "DOCTYPE definition should be the following when using ProActive : \n" +
00116                     "<!DOCTYPE definition PUBLIC \"-//objectweb.org//DTD Fractal ADL 2.0//EN\" \"classpath://org/objectweb/proactive/core/component/adl/xml/proactive.dtd\">");
00117             }
00118 
00119             if (n == null) {
00120                 
00121                 
00122                 for (int i = path.size() - 1; i >= 0; --i) {
00123                     if (path.get(i) instanceof VirtualNodeContainer) {
00124                         try {
00125                             n = (VirtualNode) ((VirtualNodeContainer) path.get(i)).getVirtualNode();
00126 
00127                             if (n != null) {
00128                                 break;
00129                             }
00130                         } catch (ClassCastException e) {
00131                             throw new ProActiveRuntimeException(
00132                                 "DOCTYPE definition should be the following when using ProActive : \n" +
00133                                 "<!DOCTYPE definition PUBLIC \"-//objectweb.org//DTD Fractal ADL 2.0//EN\" \"classpath://org/objectweb/proactive/core/component/adl/xml/proactive.dtd\">");
00134                         }
00135                     }
00136                 }
00137             }
00138         }
00139 
00140         AbstractInstanceProviderTask createTask;
00141 
00142         ContentDescription contentDesc = null;
00143         ControllerDescription controllerDesc = null;
00144 
00145         if (implementation == null) {
00146             
00147             if ("composite".equals(controller) || (controller == null)) {
00148                 controllerDesc = new ControllerDescription(name,
00149                         Constants.COMPOSITE);
00150                 contentDesc = new ContentDescription(Composite.class.getName());
00151             } else {
00152                 controllerDesc = new ControllerDescription(name,
00153                         Constants.COMPOSITE,
00154                         getClass().getResource(controller).getPath());
00155             }
00156         } else {
00157             
00158             
00159             
00160             
00161             contentDesc = new ContentDescription(implementation);
00162 
00163             if ("primitive".equals(controller) || (controller == null)) {
00164                 controllerDesc = new ControllerDescription(name,
00165                         Constants.PRIMITIVE);
00166             } else {
00167                 
00168                 controllerDesc = new ControllerDescription(name,
00169                         Constants.PRIMITIVE,
00170                         getClass().getResource(controller).getPath());
00171             }
00172 
00173             
00174         }
00175 
00176         createTask = new CreateTask((ProActiveImplementationBuilder) builder,
00177                 container, name, definition, controllerDesc, contentDesc, n,
00178                 context);
00179 
00180         FactoryProviderTask typeTask = (FactoryProviderTask) tasks.getTask("type",
00181                 container);
00182         createTask.setFactoryProviderTask(typeTask);
00183 
00184         tasks.addTask("create", container, createTask);
00185     }
00186 
00187     
00188     static class CreateTask extends AbstractInstanceProviderTask {
00189         ProActiveImplementationBuilder builder;
00190         String name;
00191         String definition;
00192         ControllerDescription controllerDesc;
00193         ContentDescription contentDesc;
00194         VirtualNode vn;
00195         Map context;
00196         ComponentContainer container;
00197 
00198         public CreateTask(final ProActiveImplementationBuilder builder,
00199             final ComponentContainer container, final String name,
00200             final String definition,
00201             final ControllerDescription controllerDesc,
00202             final ContentDescription contentDesc, final VirtualNode vn,
00203             final Map context) {
00204             this.builder = builder;
00205             this.container = container;
00206             this.name = name;
00207             this.definition = definition;
00208             this.controllerDesc = controllerDesc;
00209             this.contentDesc = contentDesc;
00210             this.vn = vn;
00211             this.context = context;
00212         }
00213 
00214         public void execute(final Object context) throws Exception {
00215             if (getInstance() != null) {
00216                 return;
00217             }
00218 
00219             Object type = getFactoryProviderTask().getFactory();
00220             Object result = builder.createComponent(type, name, definition,
00221                     controllerDesc, contentDesc, vn, (Map) context);
00222             setInstance(result);
00223         }
00224 
00225         public String toString() {
00226             return "T" + System.identityHashCode(this) + "[CreateTask(" + name +
00227             "," + controllerDesc + "," + contentDesc + ")]";
00228         }
00229     }
00230 }