KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > adl > implementations > ImplementationCompiler


1 /***
2  * Fractal ADL Parser
3  * Copyright (C) 2002-2004 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: Eric.Bruneton@rd.francetelecom.com
20  *
21  * Author: Eric Bruneton
22  */

23
24 package org.objectweb.fractal.adl.implementations;
25
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.NoSuchElementException JavaDoc;
29
30 import org.objectweb.deployment.scheduling.component.api.FactoryProviderTask;
31 import org.objectweb.deployment.scheduling.component.lib.AbstractInstanceProviderTask;
32 import org.objectweb.fractal.adl.ADLException;
33 import org.objectweb.fractal.adl.Definition;
34 import org.objectweb.fractal.adl.Node;
35 import org.objectweb.fractal.adl.TaskMap;
36 import org.objectweb.fractal.adl.attributes.AttributesContainer;
37 import org.objectweb.fractal.adl.components.Component;
38 import org.objectweb.fractal.adl.components.ComponentContainer;
39 import org.objectweb.fractal.adl.components.PrimitiveCompiler;
40 import org.objectweb.fractal.api.control.BindingController;
41
42 /**
43  * A {@link PrimitiveCompiler} to compile {@link Implementation} nodes in definitions.
44  */

45
46 public class ImplementationCompiler implements BindingController, PrimitiveCompiler {
47   
48   /**
49    * Name of the mandatory interface bound to the {@link ImplementationBuilder}
50    * used by this compiler.
51    */

52   
53   public final static String JavaDoc BUILDER_BINDING = "builder";
54   
55   /**
56    * The {@link ImplementationBuilder} used by this compiler.
57    */

58   
59   public ImplementationBuilder builder;
60   
61   // --------------------------------------------------------------------------
62
// Implementation of the BindingController interface
63
// --------------------------------------------------------------------------
64

65   public String JavaDoc[] listFc() {
66     return new String JavaDoc[] { BUILDER_BINDING };
67   }
68
69   public Object JavaDoc lookupFc (final String JavaDoc itf) {
70     if (itf.equals(BUILDER_BINDING)) {
71       return builder;
72     }
73     return null;
74   }
75
76   public void bindFc (final String JavaDoc itf, final Object JavaDoc value) {
77     if (itf.equals(BUILDER_BINDING)) {
78       builder = (ImplementationBuilder)value;
79     }
80   }
81
82   public void unbindFc (final String JavaDoc itf) {
83     if (itf.equals(BUILDER_BINDING)) {
84       builder = null;
85     }
86   }
87   
88   // --------------------------------------------------------------------------
89
// Implementation of the Compiler interface
90
// --------------------------------------------------------------------------
91

92   public void compile (
93     final List JavaDoc path,
94     final ComponentContainer container,
95     final TaskMap tasks,
96     final Map JavaDoc context) throws ADLException
97   {
98     boolean template = context != null && "true".equals(context.get("template"));
99     
100     String JavaDoc implementation = null;
101     if (container instanceof ImplementationContainer) {
102       ImplementationContainer ic = (ImplementationContainer)container;
103       Implementation i = ic.getImplementation();
104       if (i != null) {
105         implementation = i.getClassName();
106       }
107     }
108
109     String JavaDoc controller = null;
110     if (container instanceof ControllerContainer) {
111       ControllerContainer cc = (ControllerContainer)container;
112       if (cc.getController() != null) {
113         controller = cc.getController().getDescriptor();
114       }
115     }
116     
117     String JavaDoc templateController = null;
118     if (container instanceof TemplateControllerContainer) {
119       TemplateControllerContainer tcc = (TemplateControllerContainer)container;
120       if (tcc.getTemplateController() != null) {
121         templateController = tcc.getTemplateController().getDescriptor();
122         template = true;
123       }
124     }
125     
126     String JavaDoc name = null;
127     if (container instanceof Definition) {
128       name = ((Definition)container).getName();
129     } else if (container instanceof Component) {
130       name = ((Component)container).getName();
131     }
132     
133     String JavaDoc definition = null;
134     if (container instanceof Definition) {
135       definition = name;
136     } else {
137       definition = (String JavaDoc)((Node)container).astGetDecoration("definition");
138     }
139     
140     boolean attrs = false;
141     if (container instanceof AttributesContainer) {
142       attrs = ((AttributesContainer)container).getAttributes() != null;
143     }
144     Component[] comps = ((ComponentContainer)container).getComponents();
145     
146     try {
147       // the task may already exist, in case of a shared component
148
tasks.getTask("create", container);
149     } catch (NoSuchElementException JavaDoc e) {
150       AbstractInstanceProviderTask createTask;
151       if (comps.length > 0 || implementation == null) {
152         if (implementation != null) {
153           throw new ADLException("Implementation must be empty", (Node)container);
154         }
155         if (controller == null) {
156           controller = "composite";
157         }
158         if (template) {
159           if (templateController == null) {
160             if (attrs) {
161               templateController = "parametricCompositeTemplate";
162             } else {
163               templateController = "compositeTemplate";
164             }
165           }
166           createTask = newCreateTask(path, container, name, definition, templateController, new Object JavaDoc[] { controller, null }, context);
167         } else {
168           createTask = newCreateTask(path, container, name, definition, controller, null, context);
169         }
170       } else {
171         if (controller == null) {
172           controller = "primitive";
173         }
174         if (template) {
175           if (templateController == null) {
176             if (attrs) {
177               templateController = "parametricPrimitiveTemplate";
178             } else {
179               templateController = "primitiveTemplate";
180             }
181           }
182           createTask = newCreateTask(path, container, name, definition, templateController, new Object JavaDoc[] { controller, implementation }, context);
183         } else {
184           createTask = newCreateTask(path, container, name, definition, controller, implementation, context);
185         }
186       }
187       
188       FactoryProviderTask typeTask =
189         (FactoryProviderTask)tasks.getTask("type", container);
190       createTask.setFactoryProviderTask(typeTask);
191       
192       tasks.addTask("create", container, createTask);
193     }
194   }
195   
196   public AbstractInstanceProviderTask newCreateTask (
197     final List JavaDoc path,
198     final ComponentContainer container,
199     final String JavaDoc name,
200     final String JavaDoc definition,
201     final Object JavaDoc controller,
202     final Object JavaDoc implementation,
203     final Map JavaDoc context)
204   {
205     return new CreateTask(builder, name, definition, controller, implementation);
206   }
207   
208   // --------------------------------------------------------------------------
209
// Inner classes
210
// --------------------------------------------------------------------------
211

212   static class CreateTask extends AbstractInstanceProviderTask {
213
214     ImplementationBuilder builder;
215     
216     String JavaDoc name;
217     
218     String JavaDoc definition;
219     
220     Object JavaDoc controllerDesc;
221     
222     Object JavaDoc contentDesc;
223     
224     public CreateTask (
225       final ImplementationBuilder builder,
226       final String JavaDoc name,
227       final String JavaDoc definition,
228       final Object JavaDoc controllerDesc,
229       final Object JavaDoc contentDesc)
230     {
231       this.builder = builder;
232       this.name = name;
233       this.definition = definition;
234       this.controllerDesc = controllerDesc;
235       this.contentDesc = contentDesc;
236     }
237     
238     public void execute (final Object JavaDoc context) throws Exception JavaDoc {
239       if (getInstance() != null) {
240         return;
241       }
242       Object JavaDoc type = getFactoryProviderTask().getFactory();
243       Object JavaDoc result = builder.createComponent(
244           type, name, definition, controllerDesc, contentDesc, context);
245       setInstance(result);
246     }
247     
248     public String JavaDoc toString () {
249       return "T" + System.identityHashCode(this) +
250           "[CreateTask(" + name + "," + controllerDesc + "," + contentDesc + ")]";
251     }
252   }
253 }
254
Popular Tags