KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > julia > control > binding > ContentBindingMixin


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.binding;
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.IllegalLifeCycleException;
33 import org.objectweb.fractal.api.control.SuperController;
34 import org.objectweb.fractal.api.type.InterfaceType;
35
36 /**
37  * Provides component hierarchy related checks to a {@link BindingController}.
38  * <br>
39  * <br>
40  * <b>Requirements</b>
41  * <ul>
42  * <li>the component to which this controller object belongs must provide the
43  * {@link Component} and {@link SuperController} interfaces.</li>
44  * <li>the type of the component to which this controller object belongs must be
45  * an instance of {@link org.objectweb.fractal.api.type.ComponentType}.</li>
46  * <li>in order to be able to check bindings, this mixin needs the components to
47  * which this component is bound to provide interface introspection, and to
48  * provide the {@link SuperController} interface: if this component is bound to
49  * a component that does not satisfy these requirements, the binding will not be
50  * checked (and no error will be thrown).</li>
51  * </ul>
52  */

53
54 public abstract class ContentBindingMixin implements BindingController {
55
56   // -------------------------------------------------------------------------
57
// Private constructor
58
// -------------------------------------------------------------------------
59

60   private ContentBindingMixin () {
61   }
62
63   // -------------------------------------------------------------------------
64
// Fields and methods added and overriden by the mixin class
65
// -------------------------------------------------------------------------
66

67   /**
68    * Calls the {@link #checkFcLocalBinding checkFcLocalBinding} method and then
69    * calls the overriden method.
70    *
71    * @param clientItfType the type of the <tt>clientItfName</tt> interface.
72    * @param clientItfName the name of a client interface of the component to
73    * which this interface belongs.
74    * @param serverItf a server interface.
75    * @throws NoSuchInterfaceException if there is no such client interface.
76    * @throws IllegalBindingException if the binding cannot be created.
77    * @throws IllegalLifeCycleException if this component has a {@link
78    * org.objectweb.fractal.api.control.LifeCycleController} interface, but
79    * it is not in an appropriate state to perform this operation.
80    */

81
82   public void bindFc (
83     final InterfaceType clientItfType,
84     final String JavaDoc clientItfName,
85     final Object JavaDoc serverItf)
86     throws
87     NoSuchInterfaceException,
88     IllegalBindingException,
89     IllegalLifeCycleException
90   {
91     checkFcLocalBinding(clientItfType, clientItfName, serverItf);
92     _super_bindFc(clientItfType, clientItfName, serverItf);
93   }
94
95   /**
96    * Checks that the given binding is a local binding.
97    *
98    * @param clientItfType the type of the <tt>clientItfName</tt> interface.
99    * @param clientItfName the name of a client interface.
100    * @param serverItf a server interface.
101    * @throws IllegalBindingException if the given binding is not a local
102    * binding.
103    */

104
105   public void checkFcLocalBinding (
106     final InterfaceType clientItfType,
107     final String JavaDoc clientItfName,
108     final Object JavaDoc serverItf) throws IllegalBindingException
109   {
110     Interface sItf;
111     Component sComp;
112     try {
113       sItf = (Interface)serverItf;
114       sComp = sItf.getFcItfOwner();
115     } catch (ClassCastException JavaDoc e) {
116       // if the server interface does not provide interface introspection
117
// functions, the checks below cannot be performed
118
return;
119     }
120
121     Component[] cParents = _this_weaveableSC.getFcSuperComponents();
122
123     String JavaDoc msg;
124     if (!clientItfType.isFcClientItf()) {
125       // internal client interface
126
ContentController cc;
127       try {
128         cc = (ContentController)_this_weaveableC.
129           getFcInterface("content-controller");
130       } catch (NoSuchInterfaceException e) {
131         return;
132       }
133       // yes, check export binding:
134
// server component must be a sub component of client component...
135
Component[] cSubComps = cc.getFcSubComponents();
136       for (int i = 0; i < cSubComps.length; ++i) {
137         if (cSubComps[i].equals(sComp)) {
138           return;
139         }
140       }
141       // ...or equal to client component
142
Component thisComp;
143       try {
144         thisComp = (Component)_this_weaveableC.getFcInterface("component");
145       } catch (NoSuchInterfaceException e) {
146         throw new ChainedIllegalBindingException(
147           e, _this_weaveableC,
148         sItf.getFcItfOwner(),
149         clientItfName,
150         sItf.getFcItfName(),
151         "Cannot get the Component interface of the client component");
152       }
153       if (sComp.equals(thisComp) && sItf.isFcInternalItf()) {
154         return;
155       }
156       msg = "Invalid export binding";
157     } else if (sItf.isFcInternalItf()) {
158       // checks that the server component is a parent of the client component
159
for (int i = 0; i < cParents.length; ++i) {
160         if (sComp.equals(cParents[i])) {
161           return;
162         }
163       }
164       msg = "Invalid import binding";
165     } else {
166       SuperController sCompSC;
167       try {
168         sCompSC = (SuperController)sComp.getFcInterface("super-controller");
169       } catch (NoSuchInterfaceException e) {
170         // if the server component does not have a SuperController interface,
171
// the checks below cannot be performed
172
return;
173       }
174       Component[] sParents = sCompSC.getFcSuperComponents();
175       // checks that the client and server components have a common parent
176
for (int i = 0; i < cParents.length; ++i) {
177         for (int j = 0; j < sParents.length; ++j) {
178           if (cParents[i].equals(sParents[j])) {
179             return;
180           }
181         }
182       }
183       msg = "Not a local binding";
184     }
185     throw new ChainedIllegalBindingException(
186       null,
187       _this_weaveableC,
188       sItf.getFcItfOwner(),
189       clientItfName,
190       sItf.getFcItfName(),
191       msg);
192   }
193
194   // -------------------------------------------------------------------------
195
// Fields and methods required by the mixin class in the base class
196
// -------------------------------------------------------------------------
197

198   /**
199    * The <tt>weaveableC</tt> field required by this mixin. This field is
200    * supposed to reference the {@link Component} interface of the component to
201    * which this controller object belongs.
202    */

203
204   public Component _this_weaveableC;
205
206   /**
207    * The <tt>weaveableSC</tt> field required by this mixin. This field is
208    * supposed to reference the {@link SuperController} interface of the
209    * component to which this controller object belongs.
210    */

211
212   public SuperController _this_weaveableSC;
213
214   /**
215    * The {@link TypeBindingMixin#bindFc(InterfaceType,String,Object) bindFc}
216    * method overriden by this mixin.
217    *
218    * @param clientItfType the type of the <tt>clientItfName</tt> interface.
219    * @param clientItfName the name of a client interface of the component to
220    * which this interface belongs.
221    * @param serverItf a server interface.
222    * @throws NoSuchInterfaceException if there is no such client interface.
223    * @throws IllegalBindingException if the binding cannot be created.
224    * @throws IllegalLifeCycleException if this component has a {@link
225    * org.objectweb.fractal.api.control.LifeCycleController} interface, but
226    * it is not in an appropriate state to perform this operation.
227    */

228
229   public abstract void _super_bindFc (
230     InterfaceType clientItfType,
231     String JavaDoc clientItfName,
232     Object JavaDoc serverItf)
233     throws
234     NoSuchInterfaceException,
235     IllegalBindingException,
236     IllegalLifeCycleException;
237 }
238
Popular Tags