KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > adl > bindings > BindingCompiler


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.bindings;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.NoSuchElementException JavaDoc;
30
31 import org.objectweb.deployment.scheduling.core.api.Task;
32 import org.objectweb.deployment.scheduling.component.api.InstanceProviderTask;
33 import org.objectweb.deployment.scheduling.component.lib.AbstractRequireInstanceProviderTask;
34 import org.objectweb.fractal.adl.ADLException;
35 import org.objectweb.fractal.adl.TaskMap;
36 import org.objectweb.fractal.adl.components.Component;
37 import org.objectweb.fractal.adl.components.ComponentContainer;
38 import org.objectweb.fractal.adl.components.ComponentPair;
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 Binding} nodes in definitions.
44  */

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

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

58   
59   public BindingBuilder 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 = (BindingBuilder)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     Map JavaDoc subComponents = new HashMap JavaDoc();
99     subComponents.put("this", container);
100     Component[] comps = container.getComponents();
101     for (int i = 0; i < comps.length; i++) {
102       subComponents.put(comps[i].getName(), comps[i]);
103     }
104     
105     if (container instanceof BindingContainer) {
106       Binding[] bindings = ((BindingContainer)container).getBindings();
107       for (int i = 0; i < bindings.length; i++) {
108         Binding binding = bindings[i];
109         
110         String JavaDoc value = binding.getFrom();
111         int index = value.indexOf('.');
112         Object JavaDoc clientComp = subComponents.get(value.substring(0, index));
113         String JavaDoc clientItf = value.substring(index + 1);
114           
115         value = binding.getTo();
116         index = value.indexOf('.');
117         Object JavaDoc serverComp = subComponents.get(value.substring(0, index));
118         String JavaDoc serverItf = value.substring(index + 1);
119             
120         InstanceProviderTask createClientTask =
121           (InstanceProviderTask)tasks.getTask("create", clientComp);
122         InstanceProviderTask createServerTask =
123           (InstanceProviderTask)tasks.getTask("create", serverComp);
124         
125         int type = BindingBuilder.NORMAL_BINDING;
126         if (binding.getFrom().startsWith("this.")) {
127           type = BindingBuilder.EXPORT_BINDING;
128         }
129         if (binding.getTo().startsWith("this.")) {
130           type = BindingBuilder.IMPORT_BINDING;
131         }
132         
133         try {
134           // the task may already exist, in case of a shared component
135
tasks.getTask("bind" + clientItf, clientComp);
136         } catch (NoSuchElementException JavaDoc e) {
137           BindTask bindTask = new BindTask(builder, type, clientItf, serverItf);
138           bindTask.setInstanceProviderTask(createClientTask);
139           bindTask.setServerInstanceProviderTask(createServerTask);
140           
141           tasks.addTask("bind" + clientItf, clientComp, bindTask);
142           
143           if (clientComp != container) {
144             Task addTask = tasks.getTask("add", new ComponentPair(
145               container, (Component)clientComp));
146             bindTask.addPreviousTask(addTask);
147           }
148           if (serverComp != container) {
149             Task addTask = tasks.getTask("add", new ComponentPair(
150               container, (Component)serverComp));
151             bindTask.addPreviousTask(addTask);
152           }
153           
154           Task startTask = tasks.getTask("start", clientComp);
155           startTask.addPreviousTask(bindTask);
156         }
157       }
158     }
159   }
160
161   // --------------------------------------------------------------------------
162
// Inner classes
163
// --------------------------------------------------------------------------
164

165   static class BindTask extends AbstractRequireInstanceProviderTask {
166
167     private InstanceProviderTask serverInstanceProviderTask;
168
169     private BindingBuilder builder;
170     
171     private int type;
172     
173     private String JavaDoc clientItf;
174     
175     private String JavaDoc serverItf;
176         
177     public BindTask (
178       final BindingBuilder builder,
179       final int type,
180       final String JavaDoc clientItf,
181       final String JavaDoc serverItf)
182     {
183       this.builder = builder;
184       this.type = type;
185       this.clientItf = clientItf;
186       this.serverItf = serverItf;
187     }
188
189     public InstanceProviderTask getServerInstanceProviderTask () {
190       return serverInstanceProviderTask;
191     }
192     
193     public void setServerInstanceProviderTask (final InstanceProviderTask task) {
194       if (serverInstanceProviderTask != null) {
195         removePreviousTask(serverInstanceProviderTask);
196       }
197       serverInstanceProviderTask = task;
198       if (serverInstanceProviderTask != null) {
199         addPreviousTask(serverInstanceProviderTask);
200       }
201     }
202     
203     public void execute (final Object JavaDoc context) throws Exception JavaDoc {
204       Object JavaDoc client = getInstanceProviderTask().getInstance();
205       Object JavaDoc server = getServerInstanceProviderTask().getInstance();
206       builder.bindComponent(type, client, clientItf, server, serverItf, context);
207     }
208
209     public Object JavaDoc getResult () {
210       return null;
211     }
212
213     public void setResult (final Object JavaDoc result) {
214     }
215
216     public String JavaDoc toString () {
217       return "T" + System.identityHashCode(this) +
218           "[BindTask(" + clientItf + "," + serverItf + ")]";
219     }
220   }
221 }
222
Popular Tags