KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > adl > attributes > AttributeCompiler


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

42
43 public class AttributeCompiler implements BindingController, PrimitiveCompiler {
44
45   /**
46    * Name of the mandatory interface bound to the {@link AttributeBuilder} used
47    * by this compiler.
48    */

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

55   
56   public AttributeBuilder builder;
57   
58   // --------------------------------------------------------------------------
59
// Implementation of the BindingController interface
60
// --------------------------------------------------------------------------
61

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

89   public void compile (
90     final List JavaDoc path,
91     final ComponentContainer container,
92     final TaskMap tasks,
93     final Map JavaDoc context) throws ADLException
94   {
95     if (container instanceof AttributesContainer) {
96       Attributes attributes = ((AttributesContainer)container).getAttributes();
97       if (attributes != null) {
98         InstanceProviderTask createTask =
99           (InstanceProviderTask)tasks.getTask("create", container);
100         
101         Task startTask = tasks.getTask("start", container);
102         
103         Attribute[] attrs = attributes.getAttributes();
104         for (int i = 0; i < attrs.length; ++i) {
105           try {
106             // the task may already exist, in case of a shared component
107
tasks.getTask("attr" + attrs[i].getName(), container);
108           } catch (NoSuchElementException JavaDoc e) {
109             AttributeTask t = new AttributeTask(
110                 builder,
111                 attributes.getSignature(),
112                 attrs[i].getName(),
113                 attrs[i].getValue());
114             t.setInstanceProviderTask(createTask);
115             
116             startTask.addPreviousTask(t);
117             
118             tasks.addTask("attr" + attrs[i].getName(), container, t);
119           }
120         }
121       }
122     }
123   }
124   
125   // --------------------------------------------------------------------------
126
// Inner classes
127
// --------------------------------------------------------------------------
128

129   static class AttributeTask extends AbstractAttributeSetterTask {
130
131     private AttributeBuilder builder;
132        
133     private String JavaDoc attributeController;
134     
135     private String JavaDoc name;
136     
137     public AttributeTask (
138       final AttributeBuilder builder,
139       final String JavaDoc attributeController,
140       final String JavaDoc name,
141       final String JavaDoc value)
142     {
143       this.builder = builder;
144       this.attributeController = attributeController;
145       this.name = name;
146       setValue(value);
147     }
148     
149     public void execute (final Object JavaDoc context) throws Exception JavaDoc {
150       Object JavaDoc component = getInstanceProviderTask().getInstance();
151       builder.setAttribute(
152         component,
153         attributeController,
154         name,
155         (String JavaDoc)getValue(),
156         context);
157     }
158     
159     public String JavaDoc toString () {
160       return "T" + System.identityHashCode(this) +
161              "[AttributeTask(" + name + "," + getValue() + ")]";
162     }
163   }
164 }
165
Popular Tags