1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.objectweb.fractal.cecilia.adl.types;
25
26 import static org.objectweb.fractal.cecilia.adl.types.TypeDecorationUtil.setTypeDecoration;
27
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31
32 import org.objectweb.fractal.adl.ADLException;
33 import org.objectweb.fractal.adl.ComponentVisitor;
34 import org.objectweb.fractal.adl.ContextLocal;
35 import org.objectweb.fractal.adl.Node;
36 import org.objectweb.fractal.adl.components.ComponentContainer;
37 import org.objectweb.fractal.adl.interfaces.Interface;
38 import org.objectweb.fractal.adl.interfaces.InterfaceContainer;
39 import org.objectweb.fractal.api.Component;
40 import org.objectweb.fractal.api.NoSuchInterfaceException;
41 import org.objectweb.fractal.api.control.BindingController;
42 import org.objectweb.fractal.api.control.IllegalBindingException;
43 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
44 import org.objectweb.fractal.task.core.TaskException;
45
46
47
48
49
50
51
52
53
54 public class PrimitiveTypeDefinitionFilter
55 implements
56 ComponentVisitor,
57 BindingController {
58
59
60
61
62
63
64
65
66 protected final ContextLocal<Map<ComponentTypeDesc, ComponentTypeDesc>> componentTypes = new ContextLocal<Map<ComponentTypeDesc, ComponentTypeDesc>>();
67
68
69
70
71
72
73 public static final String DEFINITION_VISITOR_ITF_NAME = "definition-visitor";
74
75
76
77
78
79 public ComponentVisitor definitionVisitorItf;
80
81
82
83
84
85 public Component visit(final List<Node> path,
86 final ComponentContainer container, final Map<Object, Object> context)
87 throws ADLException, TaskException {
88 if (needTypeDefinition(path, container, context)) {
89 return definitionVisitorItf.visit(path, container, context);
90 } else {
91 return null;
92 }
93 }
94
95
96
97
98
99 protected boolean needTypeDefinition(final List<Node> path,
100 final ComponentContainer container, final Map<Object, Object> context)
101 throws ADLException, TaskException {
102
103
104 Map<ComponentTypeDesc, ComponentTypeDesc> types = componentTypes
105 .get(context);
106 if (types == null) {
107 types = new HashMap<ComponentTypeDesc, ComponentTypeDesc>();
108 componentTypes.set(context, types);
109 }
110
111
112 final ComponentTypeDesc componentDefDesc = new ComponentTypeDesc(container);
113
114
115
116 final ComponentTypeDesc typeComp = types.get(componentDefDesc);
117 if (typeComp == null) {
118
119
120 types.put(componentDefDesc, componentDefDesc);
121
122
123 setTypeDecoration(container, container);
124
125
126 return true;
127 } else {
128
129
130
131 setTypeDecoration(container, typeComp.container);
132
133
134
135
136
137
138 if (container instanceof InterfaceContainer) {
139 final Map<String, Interface> itfs = new HashMap<String, Interface>();
140
141
142 final InterfaceContainer interfaceContainer = (InterfaceContainer) container;
143 for (final Interface itf : interfaceContainer.getInterfaces()) {
144 itfs.put(itf.getName(), itf);
145 interfaceContainer.removeInterface(itf);
146 }
147
148 for (final Interface itf : ((InterfaceContainer) typeComp.container)
149 .getInterfaces()) {
150 interfaceContainer.addInterface(itfs.get(itf.getName()));
151 }
152 }
153
154
155 return false;
156 }
157 }
158
159
160
161
162
163 public String[] listFc() {
164 return new String[]{DEFINITION_VISITOR_ITF_NAME};
165 }
166
167 public void bindFc(final String clientItfName, final Object serverItf)
168 throws NoSuchInterfaceException, IllegalBindingException,
169 IllegalLifeCycleException {
170
171 if (clientItfName == null) {
172 throw new IllegalArgumentException("Interface name can't be null");
173 }
174
175 if (DEFINITION_VISITOR_ITF_NAME.equals(clientItfName))
176 definitionVisitorItf = (ComponentVisitor) serverItf;
177 else
178 throw new NoSuchInterfaceException("There is no interface named '"
179 + clientItfName + "'");
180 }
181
182 public Object lookupFc(final String clientItfName)
183 throws NoSuchInterfaceException {
184
185 if (clientItfName == null) {
186 throw new IllegalArgumentException("Interface name can't be null");
187 }
188
189 if (DEFINITION_VISITOR_ITF_NAME.equals(clientItfName))
190 return definitionVisitorItf;
191 else
192 throw new NoSuchInterfaceException("There is no interface named '"
193 + clientItfName + "'");
194 }
195
196 public void unbindFc(final String clientItfName)
197 throws NoSuchInterfaceException, IllegalBindingException,
198 IllegalLifeCycleException {
199
200 if (clientItfName == null) {
201 throw new IllegalArgumentException("Interface name can't be null");
202 }
203
204 if (DEFINITION_VISITOR_ITF_NAME.equals(clientItfName))
205 definitionVisitorItf = null;
206 else
207 throw new NoSuchInterfaceException("There is no interface named '"
208 + clientItfName + "'");
209 }
210 }