KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > adl > types > TypeCompiler


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.types;
25
26 import java.util.List JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.NoSuchElementException JavaDoc;
30
31 import org.objectweb.deployment.scheduling.component.lib.AbstractFactoryProviderTask;
32 import org.objectweb.fractal.adl.ADLException;
33 import org.objectweb.fractal.adl.Definition;
34 import org.objectweb.fractal.adl.TaskMap;
35 import org.objectweb.fractal.adl.attributes.Attributes;
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.adl.interfaces.Interface;
41 import org.objectweb.fractal.adl.interfaces.InterfaceContainer;
42 import org.objectweb.fractal.api.control.BindingController;
43
44 /**
45  * A {@link PrimitiveCompiler} to compile {@link TypeInterface} nodes in definitions.
46  */

47
48 public class TypeCompiler implements BindingController, PrimitiveCompiler {
49
50   /**
51    * Name of the mandatory interface bound to the {@link TypeBuilder} used
52    * by this compiler.
53    */

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

60   
61   public TypeBuilder builder;
62   
63   // --------------------------------------------------------------------------
64
// Implementation of the BindingController interface
65
// --------------------------------------------------------------------------
66

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

94   public void compile (
95     final List JavaDoc path,
96     final ComponentContainer container,
97     final TaskMap tasks,
98     final Map JavaDoc context) throws ADLException
99   {
100     if (container instanceof InterfaceContainer) {
101       try {
102         // the task may already exist, in case of a shared component
103
tasks.getTask("type", container);
104       } catch (NoSuchElementException JavaDoc e) {
105         CreateTypeTask createTypeTask =
106           new CreateTypeTask(builder, (InterfaceContainer)container);
107         tasks.addTask("type", container, createTypeTask);
108       }
109     }
110   }
111
112   // --------------------------------------------------------------------------
113
// Inner classes
114
// --------------------------------------------------------------------------
115

116   static class CreateTypeTask extends AbstractFactoryProviderTask {
117
118     private TypeBuilder builder;
119     
120     private InterfaceContainer container;
121     
122     public CreateTypeTask (
123       final TypeBuilder builder,
124       final InterfaceContainer container)
125     {
126       this.builder = builder;
127       this.container = container;
128     }
129     
130     public void execute (final Object JavaDoc context) throws Exception JavaDoc {
131       if (getFactory() != null) {
132         return;
133       }
134       List JavaDoc itfTypes = new ArrayList JavaDoc();
135       Interface[] itfs = container.getInterfaces();
136       for (int i = 0; i < itfs.length; i++) {
137         if (itfs[i] instanceof TypeInterface) {
138           TypeInterface itf = (TypeInterface)itfs[i];
139           Object JavaDoc itfType = builder.createInterfaceType(
140             itf.getName(),
141             itf.getSignature(),
142             itf.getRole(),
143             itf.getContingency(),
144             itf.getCardinality(),
145             context);
146           itfTypes.add(itfType);
147         }
148       }
149       if (container instanceof AttributesContainer) { // TODO improve module separation
150
Attributes attr = ((AttributesContainer)container).getAttributes();
151         if (attr != null) {
152           Object JavaDoc itfType = builder.createInterfaceType(
153             "attribute-controller",
154             attr.getSignature(),
155             "server",
156             "mandatory",
157             "singleton",
158             context);
159           itfTypes.add(itfType);
160         }
161       }
162       String JavaDoc name = null;
163       if (container instanceof Definition) {
164         name = ((Definition)container).getName();
165       } else if (container instanceof Component) {
166         name = ((Component)container).getName();
167       }
168       setFactory(builder.createComponentType(name, itfTypes.toArray(), context));
169     }
170
171     public String JavaDoc toString () {
172       return "T" + System.identityHashCode(this) + "[CreateTypeTask()]";
173     }
174   }
175 }
176
Popular Tags