KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > julia > control > content > BindingContentMixin


1 /***
2  * Julia: France Telecom's implementation of the Fractal API
3  * Copyright (C) 2001-2002 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.julia.control.content;
25
26 import org.objectweb.fractal.api.Component;
27 import org.objectweb.fractal.api.Interface;
28 import org.objectweb.fractal.api.NoSuchInterfaceException;
29 import org.objectweb.fractal.api.control.BindingController;
30 import org.objectweb.fractal.api.control.ContentController;
31 import org.objectweb.fractal.api.control.IllegalBindingException;
32 import org.objectweb.fractal.api.control.IllegalContentException;
33 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
34 import org.objectweb.fractal.api.control.SuperController;
35 import org.objectweb.fractal.api.type.InterfaceType;
36
37 import org.objectweb.fractal.julia.control.binding.ChainedIllegalBindingException;
38 import org.objectweb.fractal.julia.control.binding.Util;
39
40 import java.util.List JavaDoc;
41
42 /**
43  * Provides binding related checks to a {@link ContentController}.
44  * <br>
45  * <br>
46  * <b>Requirements</b>
47  * <ul>
48  * <li>the types of the sub components of this component must be instances of
49  * {@link org.objectweb.fractal.api.type.ComponentType}.</li>
50  * <li>the sub components of this component must support interface
51  * introspection, i.e. their interfaces must implement {@link Interface}.</li>
52  * <li>TODO requirements pour parents, pour composants clients et servers des sous composants</li>
53  * </ul>
54  */

55
56 public abstract class BindingContentMixin implements ContentController {
57
58   // -------------------------------------------------------------------------
59
// Private constructor
60
// -------------------------------------------------------------------------
61

62   private BindingContentMixin () {
63   }
64
65   // -------------------------------------------------------------------------
66
// Fields and methods added and overriden by the mixin class
67
// -------------------------------------------------------------------------
68

69   /**
70    * Checks that this operation will not create non local bindings, and then
71    * calls the overriden method.
72    *
73    * @param subComponent the component to be removed from this component.
74    * @throws IllegalContentException if the given component cannot be removed
75    * from this component.
76    * @throws IllegalLifeCycleException if this component has a {@link
77    * LifeCycleController} interface, but it is not in an appropriate state
78    * to perform this operation.
79    */

80
81   public void removeFcSubComponent (final Component subComponent)
82     throws IllegalContentException, IllegalLifeCycleException
83   {
84     try {
85       checkFcRemoveSubComponent(subComponent);
86     } catch (IllegalBindingException e) {
87       throw new ChainedIllegalContentException(
88       e,
89       _this_weaveableOptC,
90       subComponent,
91       "Would create non local bindings");
92     }
93     _super_removeFcSubComponent(subComponent);
94   }
95
96   /**
97    * Checks that the removal of the given sub component will not create non
98    * local bindings.
99    *
100    * @param subComponent a sub component that will be removed from this
101    * component.
102    * @throws IllegalBindingException if the removal of the given sub component
103    * would create non local bindings.
104    */

105
106   public void checkFcRemoveSubComponent (final Component subComponent)
107     throws IllegalBindingException
108   {
109     Component parent;
110     try {
111       parent = (Component)_this_weaveableOptC.getFcInterface("component");
112       if (parent == null) {
113         return;
114       }
115     } catch (NoSuchInterfaceException e) {
116       return;
117     }
118     BindingController bc;
119     try {
120       bc = (BindingController)subComponent.getFcInterface("binding-controller");
121     } catch (NoSuchInterfaceException e) {
122       bc = null;
123     }
124     Object JavaDoc[] itfs = subComponent.getFcInterfaces();
125     for (int i = 0; i < itfs.length; ++i) {
126       Interface itf;
127       InterfaceType itfType;
128       try {
129         itf = (Interface)itfs[i];
130         itfType = (InterfaceType)itf.getFcItfType();
131       } catch (ClassCastException JavaDoc e) {
132         continue;
133       }
134       if (itfType.isFcClientItf()) {
135         if (bc != null) {
136           Interface sItf;
137           try {
138             sItf = (Interface)bc.lookupFc(itf.getFcItfName());
139           } catch (NoSuchInterfaceException e) {
140             continue;
141           } catch (ClassCastException JavaDoc e) {
142             continue;
143           }
144           if (sItf != null) {
145             checkFcLocalBinding(itf, parent, sItf, null);
146           }
147         }
148       } else {
149         Object JavaDoc[] potentialClients;
150         try {
151           potentialClients = Util.getFcPotentialClientsOf(itf).toArray();
152         } catch (Exception JavaDoc e) {
153           continue;
154         }
155         for (int j = 0; j < potentialClients.length; ++j) {
156           Component c = (Component)potentialClients[j];
157           List JavaDoc clientItfs;
158           try {
159             clientItfs = Util.getFcClientItfsBoundTo(c, itf);
160           } catch (Exception JavaDoc e) {
161             continue;
162           }
163           if (clientItfs.size() > 0) {
164             checkFcLocalBinding((Interface)clientItfs.get(0), null, itf, parent);
165           }
166         }
167       }
168     }
169   }
170
171   /**
172    * Checks that a given binding is a local binding.
173    *
174    * @param cItf a client interface.
175    * @param cId the parent from which the client component has been removed, or
176    * <tt>null</tt> if the client component has not been removed from a
177    * parent component.
178    * @param sItf the server interface to which the client interface is bound.
179    * @param sId the parent from which the server component has been removed, or
180    * <tt>null</tt> if the server component has not been removed from a
181    * parent component.
182    * @throws IllegalBindingException if the given binding is not a local
183    * binding.
184    */

185
186   private void checkFcLocalBinding (
187     final Interface cItf,
188     final Component cId,
189     final Interface sItf,
190     final Component sId) throws IllegalBindingException
191   {
192     Component client = cItf.getFcItfOwner();
193     Component server = sItf.getFcItfOwner();
194     if (client.equals(server)) {
195       return;
196     }
197     SuperController cSc = null;
198     SuperController sSc = null;
199     try {
200       cSc = (SuperController)client.getFcInterface("super-controller");
201     } catch (NoSuchInterfaceException ignored) {
202     }
203     try {
204       sSc = (SuperController)server.getFcInterface("super-controller");
205     } catch (NoSuchInterfaceException ignored) {
206     }
207     if (cItf.isFcInternalItf()) {
208       // check client component is a parent of server component
209
if (cSc != null) {
210         Component[] sP = sSc.getFcSuperComponents();
211         for (int i = 0; i < sP.length; ++i) {
212           Component p = sP[i];
213           if (sId == null || !p.equals(sId)) {
214             if (p.equals(client)) {
215               return;
216             }
217           }
218         }
219       } else {
220         return;
221       }
222     } else {
223       if (sItf.isFcInternalItf()) {
224         // check server component is a parent of client component
225
if (cSc != null) {
226           Component[] cP = cSc.getFcSuperComponents();
227           for (int i = 0; i < cP.length; ++i) {
228             Component p = cP[i];
229             if (cId == null || !p.equals(cId)) {
230               if (p.equals(server)) {
231                 return;
232               }
233             }
234           }
235         } else {
236           return;
237         }
238       } else {
239         // check client and server components have a common parent
240
if (cSc != null && sSc != null) {
241           Component[] cP = cSc.getFcSuperComponents();
242           Component[] sP = sSc.getFcSuperComponents();
243           for (int i = 0; i < cP.length; ++i) {
244             Component p = cP[i];
245             if (cId == null || !p.equals(cId)) {
246               for (int j = 0; j < sP.length; ++j) {
247                 Component q = sP[j];
248                 if (sId == null || !q.equals(sId)) {
249                   if (p.equals(q)) {
250                     return;
251                   }
252                 }
253               }
254             }
255           }
256         } else {
257           return;
258         }
259       }
260     }
261     throw new ChainedIllegalBindingException(
262       null,
263       _this_weaveableOptC,
264       sId,
265       cItf.getFcItfName(),
266       sItf.getFcItfName(),
267       "Not a local binding");
268   }
269
270   // -------------------------------------------------------------------------
271
// Fields and methods required by the mixin class in the base class
272
// -------------------------------------------------------------------------
273

274   /**
275    * The <tt>weaveableOptC</tt> field required by this mixin. This field is
276    * supposed to reference the {@link Component} interface of the component to
277    * which this controller object belongs.
278    */

279
280   public Component _this_weaveableOptC;
281
282   /**
283    * The {@link ContentController#removeFcSubComponent removeFcSubComponent}
284    * method overriden by this mixin.
285    *
286    * @param subComponent the component to be removed from this component.
287    * @throws IllegalContentException if the given component cannot be removed
288    * from this component.
289    * @throws IllegalLifeCycleException if this component has a {@link
290    * LifeCycleController} interface, but it is not in an appropriate state
291    * to perform this operation.
292    */

293
294   public abstract void _super_removeFcSubComponent (Component subComponent)
295     throws IllegalContentException, IllegalLifeCycleException;
296 }
297
Popular Tags