KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > adl > components > PrimitiveComponentCompiler


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.components;
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.InstanceProviderTask;
31 import org.objectweb.deployment.scheduling.component.lib.AbstractRequireInstanceProviderTask;
32 import org.objectweb.deployment.scheduling.component.lib.AbstractInitializationTask;
33 import org.objectweb.fractal.adl.ADLException;
34 import org.objectweb.fractal.adl.TaskMap;
35 import org.objectweb.fractal.api.control.BindingController;
36
37 /**
38  * A {@link PrimitiveCompiler} to compile {@link Component} nodes in definitions.
39  */

40
41 public class PrimitiveComponentCompiler implements BindingController, PrimitiveCompiler {
42   
43   /**
44    * Name of the mandatory interface bound to the {@link ComponentBuilder} used
45    * by this compiler.
46    */

47   
48   public final static String JavaDoc BUILDER_BINDING = "builder";
49   
50   /**
51    * The {@link ComponentBuilder} used by this compiler.
52    */

53   
54   public ComponentBuilder builder;
55   
56   // --------------------------------------------------------------------------
57
// Implementation of the BindingController interface
58
// --------------------------------------------------------------------------
59

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

87   public void compile (
88     final List JavaDoc path,
89     final ComponentContainer container,
90     final TaskMap tasks,
91     final Map JavaDoc context) throws ADLException
92   {
93     InstanceProviderTask createTask =
94       (InstanceProviderTask)tasks.getTask("create", container);
95
96     StartTask startTask = new StartTask(builder);
97     startTask.setInstanceProviderTask(createTask);
98     tasks.addTask("start", container, startTask);
99     
100     Component[] comps = ((ComponentContainer)container).getComponents();
101     for (int i = 0; i < comps.length; i++) {
102       InstanceProviderTask createSubComponentTask =
103         (InstanceProviderTask)tasks.getTask("create", comps[i]);
104
105       ComponentPair pair = new ComponentPair(container, comps[i]);
106       try {
107         // the task may already exist, in case of a shared component
108
tasks.getTask("add", pair);
109       } catch (NoSuchElementException JavaDoc e) {
110         AddTask addTask = new AddTask(builder, comps[i].getName());
111         addTask.setInstanceProviderTask(createTask);
112         addTask.setSubInstanceProviderTask(createSubComponentTask);
113         tasks.addTask("add", pair, addTask);
114       }
115       
116       // to start super component only after all sub components are added:
117
// startTask.getPreviousTasks().addTask(addTask);
118
}
119   }
120   
121   // --------------------------------------------------------------------------
122
// Inner classes
123
// --------------------------------------------------------------------------
124

125   static class AddTask extends AbstractRequireInstanceProviderTask {
126
127     private ComponentBuilder builder;
128     
129     private InstanceProviderTask subInstanceProviderTask;
130     
131     private String JavaDoc name;
132     
133     public AddTask (final ComponentBuilder builder, final String JavaDoc name) {
134       this.builder = builder;
135       this.name = name;
136     }
137     
138     public InstanceProviderTask getSubInstanceProviderTask () {
139       return subInstanceProviderTask;
140     }
141     
142     public void setSubInstanceProviderTask (final InstanceProviderTask task) {
143       if (subInstanceProviderTask != null) {
144         removePreviousTask(subInstanceProviderTask);
145       }
146       subInstanceProviderTask = task;
147       if (subInstanceProviderTask != null) {
148         addPreviousTask(subInstanceProviderTask);
149       }
150     }
151     
152     public void execute (final Object JavaDoc context) throws Exception JavaDoc {
153       Object JavaDoc parent = getInstanceProviderTask().getInstance();
154       Object JavaDoc child = getSubInstanceProviderTask().getInstance();
155       builder.addComponent(parent, child, name, context);
156     }
157
158     public Object JavaDoc getResult () {
159       return null;
160     }
161
162     public void setResult (Object JavaDoc result) {
163     }
164     
165     public String JavaDoc toString () {
166       return "T" + System.identityHashCode(this) + "[AddTask(" + name + ")]";
167     }
168   }
169
170   static class StartTask extends AbstractInitializationTask {
171
172     private ComponentBuilder builder;
173     
174     public StartTask (final ComponentBuilder builder) {
175       this.builder = builder;
176     }
177     
178     public void execute (final Object JavaDoc context) throws Exception JavaDoc {
179       builder.startComponent(getInstanceProviderTask().getInstance(), context);
180     }
181
182     public String JavaDoc toString () {
183       return "T" + System.identityHashCode(this) + "[StartTask()]";
184     }
185   }
186 }
187
Popular Tags