KickJava   Java API By Example, From Geeks To Geeks.

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


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.IllegalBindingException;
31 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
32 import org.objectweb.fractal.api.control.ContentController;
33 import org.objectweb.fractal.api.type.ComponentType;
34 import org.objectweb.fractal.api.type.InterfaceType;
35
36 import org.objectweb.fractal.julia.ChainedNoSuchInterfaceException;
37
38 /**
39  * Provides basic type system related checks to a {@link BindingController}.
40  * <br>
41  * <br>
42  * <b>Requirements</b>
43  * <ul>
44  * <li>the component to which this controller object belongs must provide the
45  * {@link Component} interface.</li>
46  * <li>the type of the component to which this controller object belongs must be
47  * an instance of {@link ComponentType}.</li>
48  * </ul>
49  */

50
51 public abstract class TypeBindingMixin implements BindingController {
52
53   // -------------------------------------------------------------------------
54
// Private constructor
55
// -------------------------------------------------------------------------
56

57   private TypeBindingMixin () {
58   }
59
60   // -------------------------------------------------------------------------
61
// Fields and methods added and overriden by the mixin class
62
// -------------------------------------------------------------------------
63

64   /**
65    * Checks the interface name with the component's type and then calls the
66    * {@link #lookupFc(InterfaceType,String) lookupFc} method.
67    *
68    * @param clientItfName the name of a client interface of the component to
69    * which this interface belongs.
70    * @return the server interface to which the given interface is bound, or <tt>
71    * null</tt> if it is not bound.
72    * @throws NoSuchInterfaceException if the component to which this interface
73    * belongs does not have a client interface whose name is equal to the
74    * given name.
75    */

76
77   public Object JavaDoc lookupFc (final String JavaDoc clientItfName)
78     throws NoSuchInterfaceException
79   {
80     ComponentType compType = (ComponentType)_this_weaveableC.getFcType();
81     InterfaceType clientItfType;
82     try {
83       clientItfType = compType.getFcInterfaceType(clientItfName);
84     } catch (NoSuchInterfaceException e) {
85       throw new ChainedNoSuchInterfaceException(
86         null, _this_weaveableC, clientItfName);
87     }
88     checkFcClientInterface(clientItfType);
89     return lookupFc(clientItfType, clientItfName);
90   }
91
92   /**
93    * Checks the interface name with the component's type and then calls the
94    * {@link #bindFc(InterfaceType,String,Object) bindFc} method. If the server
95    * interface implements {@link Interface}, and if its type is an instance of
96    * {@link InterfaceType}, this method also checks the compatibility between
97    * the client and server interface types.
98    *
99    * @param clientItfName the name of a client interface of the component to
100    * which this interface belongs.
101    * @param serverItf a server interface.
102    * @throws NoSuchInterfaceException if there is no such client interface.
103    * @throws IllegalBindingException if the binding cannot be created.
104    * @throws IllegalLifeCycleException if this component has a {@link
105    * LifeCycleController} interface, but it is not in an appropriate state
106    * to perform this operation.
107    */

108
109   public void bindFc (final String JavaDoc clientItfName, final Object JavaDoc serverItf) throws
110     NoSuchInterfaceException,
111     IllegalBindingException,
112     IllegalLifeCycleException
113   {
114     ComponentType compType = (ComponentType)_this_weaveableC.getFcType();
115     InterfaceType cItfType;
116     try {
117       cItfType = compType.getFcInterfaceType(clientItfName);
118     } catch (NoSuchInterfaceException e) {
119       throw new ChainedNoSuchInterfaceException(
120         null, _this_weaveableC, clientItfName);
121     }
122     checkFcClientInterface(cItfType);
123
124     ContentController cc;
125     try {
126       cc = (ContentController)_this_weaveableC.
127         getFcInterface("content-controller");
128     } catch (NoSuchInterfaceException e) {
129       cc = null;
130     }
131
132     // for lazy creation of collection interfaces
133
if (cItfType.isFcClientItf()) {
134       _this_weaveableC.getFcInterface(clientItfName);
135     } else {
136       cc.getFcInternalInterface(clientItfName);
137     }
138
139     Interface sItf;
140     InterfaceType sItfType;
141     try {
142       sItf = (Interface)serverItf;
143       sItfType = (InterfaceType)sItf.getFcItfType();
144     } catch (ClassCastException JavaDoc e) {
145       // if the server interface does not provide interface introspection
146
// functions, or if its type is not an InterfaceType, the checks below
147
// cannot be performed, so we create the binding directly:
148
bindFc(cItfType, clientItfName, serverItf);
149       return;
150     }
151
152     if (sItfType.isFcClientItf()) {
153       throw new ChainedIllegalBindingException(
154         null,
155         _this_weaveableC,
156         sItf.getFcItfOwner(),
157         clientItfName,
158         sItf.getFcItfName(),
159         "Cannot bind two client interfaces together");
160     }
161
162     try {
163       Class JavaDoc s = Class.forName(sItfType.getFcItfSignature());
164       Class JavaDoc c = Class.forName(cItfType.getFcItfSignature());
165       if (!c.isAssignableFrom(s)) {
166         throw new ChainedIllegalBindingException(
167           null,
168           _this_weaveableC,
169           sItf.getFcItfOwner(),
170           clientItfName,
171           sItf.getFcItfName(),
172           "The server interface type is not a subtype " +
173           "of the client interface type");
174       }
175     } catch (ClassNotFoundException JavaDoc ignored) {
176     }
177
178     if (!cItfType.isFcOptionalItf() && sItfType.isFcOptionalItf()) {
179       throw new ChainedIllegalBindingException(
180         null,
181         _this_weaveableC,
182         ((Interface)serverItf).getFcItfOwner(),
183         clientItfName,
184         ((Interface)serverItf).getFcItfName(),
185         "A mandatory interface cannot be bound to an optional interface");
186     }
187
188     bindFc(cItfType, clientItfName, sItf);
189   }
190
191   /**
192    * Checks the interface name with the component's type and then calls the
193    * {@link #unbindFc(InterfaceType,String) unbindFc} method.
194    *
195    * @param clientItfName the name of a client interface of the component to
196    * which this interface belongs.
197    * @throws NoSuchInterfaceException if there is no such client interface.
198    * @throws IllegalBindingException if the binding cannot be removed.
199    * @throws IllegalLifeCycleException if this component has a {@link
200    * LifeCycleController} interface, but it is not in an appropriate state
201    * to perform this operation.
202    */

203
204   public void unbindFc (final String JavaDoc clientItfName) throws
205     NoSuchInterfaceException,
206     IllegalBindingException,
207     IllegalLifeCycleException
208   {
209     ComponentType compType = (ComponentType)_this_weaveableC.getFcType();
210     InterfaceType clientItfType;
211     try {
212       clientItfType = compType.getFcInterfaceType(clientItfName);
213     } catch (NoSuchInterfaceException e) {
214       throw new ChainedNoSuchInterfaceException(
215         null, _this_weaveableC, clientItfName);
216     }
217     checkFcClientInterface(clientItfType);
218     unbindFc(clientItfType, clientItfName);
219   }
220
221   /**
222    * Returns the interface to which the given client interface is bound. More
223    * precisely, returns the server interface to which the client interface whose
224    * name is given is bound.
225    *
226    * @param clientItfType the type of the <tt>clientItfName</tt> interface.
227    * @param clientItfName the name of a client interface of the component to
228    * which this interface belongs.
229    * @return the server interface to which the given interface is bound, or <tt>
230    * null</tt> if it is not bound.
231    * @throws NoSuchInterfaceException if the component to which this interface
232    * belongs does not have a client interface whose name is equal to the
233    * given name.
234    */

235
236   public Object JavaDoc lookupFc (
237     final InterfaceType clientItfType,
238     final String JavaDoc clientItfName)
239     throws
240     NoSuchInterfaceException
241   {
242     return _super_lookupFc(clientItfName);
243   }
244
245   /**
246    * Binds the client interface whose name is given to a server interface. More
247    * precisely, binds the client interface of the component to which this
248    * interface belongs, and whose name is equal to the given name, to the given
249    * server interface.
250    *
251    * @param clientItfType the type of the <tt>clientItfName</tt> interface.
252    * @param clientItfName the name of a client interface of the component to
253    * which this interface belongs.
254    * @param serverItf a server interface.
255    * @throws NoSuchInterfaceException if there is no such client interface.
256    * @throws IllegalBindingException if the binding cannot be created.
257    * @throws IllegalLifeCycleException if this component has a {@link
258    * org.objectweb.fractal.api.control.LifeCycleController} interface, but
259    * it is not in an appropriate state to perform this operation.
260    */

261
262   public void bindFc (
263     final InterfaceType clientItfType,
264     final String JavaDoc clientItfName,
265     final Object JavaDoc serverItf)
266     throws
267     NoSuchInterfaceException,
268     IllegalBindingException,
269     IllegalLifeCycleException
270   {
271     _super_bindFc(clientItfName, serverItf);
272   }
273
274   /**
275    * Unbinds the given client interface. More precisely, unbinds the client
276    * interface of the component to which this interface belongs, and whose name
277    * is equal to the given name.
278    *
279    * @param clientItfType the type of the <tt>clientItfName</tt> interface.
280    * @param clientItfName the name of a client interface of the component to
281    * which this interface belongs.
282    * @throws NoSuchInterfaceException if there is no such client interface.
283    * @throws IllegalBindingException if the binding cannot be removed.
284    * @throws IllegalLifeCycleException if this component has a {@link
285    * org.objectweb.fractal.api.control.LifeCycleController} interface, but
286    * it is not in an appropriate state to perform this operation.
287    */

288
289   public void unbindFc (
290     final InterfaceType clientItfType,
291     final String JavaDoc clientItfName)
292     throws
293     NoSuchInterfaceException,
294     IllegalBindingException,
295     IllegalLifeCycleException
296   {
297     _super_unbindFc(clientItfName);
298   }
299
300   /**
301    * Checks that the given type corresponds to an external or internal client
302    * interface.
303    *
304    * @param itfType an interface type.
305    * @throws NoSuchInterfaceException if the given type does not correspdond to
306    * an external or internal client interface.
307    */

308
309   private void checkFcClientInterface (InterfaceType itfType)
310     throws NoSuchInterfaceException
311   {
312     try {
313       _this_weaveableC.getFcInterface("content-controller");
314       String JavaDoc name = itfType.getFcItfName();
315       if (!name.equals("component") && !name.endsWith("-controller")) {
316         return;
317       }
318     } catch (NoSuchInterfaceException e) {
319       if (itfType.isFcClientItf()) {
320         return;
321       }
322     }
323     throw new ChainedNoSuchInterfaceException(
324       null, _this_weaveableC, itfType.getFcItfName());
325   }
326
327   // -------------------------------------------------------------------------
328
// Fields and methods required by the mixin class in the base class
329
// -------------------------------------------------------------------------
330

331   /**
332    * The <tt>weaveableC</tt> field required by this mixin. This field is
333    * supposed to reference the {@link Component} interface of the component to
334    * which this controller object belongs.
335    */

336
337   public Component _this_weaveableC;
338
339   /**
340    * The {@link BindingController#lookupFc lookupFc} method overriden by this
341    * mixin.
342    *
343    * @param clientItfName the name of a client interface of the component to
344    * which this interface belongs.
345    * @return the server interface to which the given interface is bound, or <tt>
346    * null</tt> if it is not bound.
347    * @throws NoSuchInterfaceException if the component to which this interface
348    * belongs does not have a client interface whose name is equal to the
349    * given name.
350    */

351
352   public abstract Object JavaDoc _super_lookupFc (String JavaDoc clientItfName) throws
353     NoSuchInterfaceException;
354
355   /**
356    * The {@link BindingController#bindFc bindFc} method overriden by this
357    * mixin.
358    *
359    * @param clientItfName the name of a client interface of the component to
360    * which this interface belongs.
361    * @param serverItf a server interface.
362    * @throws NoSuchInterfaceException if there is no such client interface.
363    * @throws IllegalBindingException if the binding cannot be created.
364    * @throws IllegalLifeCycleException if this component has a {@link
365    * LifeCycleController} interface, but it is not in an appropriate state
366    * to perform this operation.
367    */

368
369   public abstract void _super_bindFc (String JavaDoc clientItfName, Object JavaDoc serverItf) throws
370     NoSuchInterfaceException,
371     IllegalBindingException,
372     IllegalLifeCycleException;
373
374   /**
375    * The {@link BindingController#unbindFc unbindFc} method overriden by this
376    * mixin.
377    *
378    * @param clientItfName the name of a client interface of the component to
379    * which this interface belongs.
380    * @throws NoSuchInterfaceException if there is no such client interface.
381    * @throws IllegalBindingException if the binding cannot be removed.
382    * @throws IllegalLifeCycleException if this component has a {@link
383    * LifeCycleController} interface, but it is not in an appropriate state
384    * to perform this operation.
385    */

386
387   public abstract void _super_unbindFc (String JavaDoc clientItfName) throws
388     NoSuchInterfaceException,
389     IllegalBindingException,
390     IllegalLifeCycleException;
391 }
392
Popular Tags