KickJava   Java API By Example, From Geeks To Geeks.

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


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.control.BindingController;
27 import org.objectweb.fractal.api.control.IllegalBindingException;
28 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
29 import org.objectweb.fractal.api.NoSuchInterfaceException;
30
31 import org.objectweb.fractal.julia.ComponentInterface;
32
33 import java.util.Map JavaDoc;
34 import java.util.HashMap JavaDoc;
35
36 /**
37  * Provides an optimized container based implementation of the {@link
38  * BindingController} interface. This mixin is designed to override a basic
39  * container binding mixin: indeed it implements the {@link BindingController}
40  * methods by using a hash map, and calls the overriden method to optimized
41  * bindings in the encapsulated component (the bindings are optimized by binding
42  * the encapsulated component to
43  * "((ComponentInterface)serverItf).getFcItfImpl()", instead of "serverItf").
44  * <br>
45  * <br>
46  * <b>Requirements</b>
47  * <ul>
48  * <li>TODO.</li>
49  * </ul>
50  */

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

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

67   /**
68    * The map used to store the bindings of this component. This map associate
69    * server interface references to client interface names. A client interface
70    * name is associated with 'fcBindings' means that this interface is unbound.
71    */

72
73   public Map JavaDoc fcBindings;
74
75   /**
76    * Returns the names of the client interfaces of the component to which this
77    * interface belongs. This method returns the fcBindings keys.
78    *
79    * @return the names of the client interfaces of the component to which this
80    * interface belongs.
81    */

82   
83   public String JavaDoc[] listFc () {
84     Map JavaDoc fcBindings = getFcBindings();
85     return (String JavaDoc[])fcBindings.keySet().toArray(new String JavaDoc[fcBindings.size()]);
86   }
87
88   /**
89    * Returns the interface to which the given client interface is bound. More
90    * precisely, returns the server interface to which the client interface whose
91    * name is given is bound. This server interface is necessarily in the same
92    * address space as the client interface (see {@link #bindFc bindFc}). This
93    * method uses the fcBindings map to return its result.
94    *
95    * @param clientItfName the name of a client interface of the component to
96    * which this interface belongs.
97    * @return the server interface to which the given interface is bound, or <tt>
98    * null</tt> if it is not bound.
99    * @throws NoSuchInterfaceException if the component to which this interface
100    * belongs does not have a client interface whose name is equal to the
101    * given name.
102    */

103
104   public Object JavaDoc lookupFc (final String JavaDoc clientItfName)
105     throws NoSuchInterfaceException
106   {
107     Map JavaDoc fcBindings = getFcBindings();
108     Object JavaDoc result = fcBindings.get(clientItfName);
109     if (result == fcBindings) {
110       result = null;
111     }
112     return result;
113   }
114
115   /**
116    * Binds the client interface whose name is given to a server interface. More
117    * precisely, binds the client interface of the component to which this
118    * interface belongs, and whose name is equal to the given name, to the given
119    * server interface. The given server interface must be in the same address
120    * space as the client interface. This method updates the fcBindings map,
121    * and also called the overriden method, with serverItf.getFcItfImpl as second
122    * parameter, in order to save an indirection.
123    *
124    * @param clientItfName the name of a client interface of the component to
125    * which this interface belongs.
126    * @param serverItf a server interface.
127    * @throws NoSuchInterfaceException if there is no such client interface.
128    * @throws IllegalBindingException if the binding cannot be created.
129    * @throws IllegalLifeCycleException if this component has a {@link
130    * LifeCycleController} interface, but it is not in an appropriate state
131    * to perform this operation.
132    */

133
134   public void bindFc (final String JavaDoc clientItfName, final Object JavaDoc serverItf) throws
135     NoSuchInterfaceException,
136     IllegalBindingException,
137     IllegalLifeCycleException
138   {
139     Object JavaDoc o = serverItf;
140     if (o instanceof ComponentInterface) {
141       o = ((ComponentInterface)o).getFcItfImpl();
142     }
143     if (o != null) {
144       _super_bindFc(clientItfName, o);
145     }
146     Map JavaDoc fcBindings = getFcBindings();
147     fcBindings.put(clientItfName, serverItf);
148   }
149
150   /**
151    * Unbinds the given client interface. More precisely, unbinds the client
152    * interface of the component to which this interface belongs, and whose name
153    * is equal to the given name. This method updates the fcBindings map, and
154    * also calls the overriden method.
155    *
156    * @param clientItfName the name of a client interface of the component to
157    * which this interface belongs.
158    * @throws NoSuchInterfaceException if there is no such client interface.
159    * @throws IllegalBindingException if the binding cannot be removed.
160    * @throws IllegalLifeCycleException if this component has a {@link
161    * LifeCycleController} interface, but it is not in an appropriate state
162    * to perform this operation.
163    */

164
165   public void unbindFc (final String JavaDoc clientItfName) throws
166     NoSuchInterfaceException,
167     IllegalBindingException,
168     IllegalLifeCycleException
169   {
170     _super_unbindFc(clientItfName);
171     if (fcBindings != null) {
172       fcBindings.put(clientItfName, fcBindings);
173     }
174   }
175
176   /**
177    * Returns the fcBindings map. This map is created and initialized if
178    * necessary.
179    *
180    * @return the fcBindings map.
181    */

182
183   private Map JavaDoc getFcBindings () {
184     if (fcBindings == null) {
185       fcBindings = new HashMap JavaDoc();
186       String JavaDoc[] names = _super_listFc();
187       for (int i = 0; i < names.length; ++i) {
188         fcBindings.put(names[i], fcBindings);
189       }
190     }
191     return fcBindings;
192   }
193
194   // -------------------------------------------------------------------------
195
// Fields and methods required by the mixin class in the base class
196
// -------------------------------------------------------------------------
197

198   /**
199    * The {@link BindingController#listFc listFc} method overriden by this
200    * mixin.
201    *
202    * @return the names of the client interfaces of the component to which this
203    * interface belongs.
204    */

205
206   public abstract String JavaDoc[] _super_listFc ();
207
208   /**
209    * The {@link BindingController#lookupFc lookupFc} method overriden by this
210    * mixin.
211    *
212    * @param clientItfName the name of a client interface of the component to
213    * which this interface belongs.
214    * @return the server interface to which the given interface is bound, or <tt>
215    * null</tt> if it is not bound.
216    * @throws NoSuchInterfaceException if the component to which this interface
217    * belongs does not have a client interface whose name is equal to the
218    * given name.
219    */

220
221   public abstract Object JavaDoc _super_lookupFc (String JavaDoc clientItfName) throws
222     NoSuchInterfaceException;
223
224   /**
225    * The {@link BindingController#bindFc bindFc} method overriden by this mixin.
226    *
227    * @param clientItfName the name of a client interface of the component to
228    * which this interface belongs.
229    * @param serverItf a server interface.
230    * @throws NoSuchInterfaceException if there is no such client interface.
231    * @throws IllegalBindingException if the binding cannot be created.
232    * @throws IllegalLifeCycleException if this component has a {@link
233    * LifeCycleController} interface, but it is not in an appropriate state
234    * to perform this operation.
235    */

236
237   public abstract void _super_bindFc (String JavaDoc clientItfName, Object JavaDoc serverItf) throws
238     NoSuchInterfaceException,
239     IllegalBindingException,
240     IllegalLifeCycleException;
241
242   /**
243    * The {@link BindingController#unbindFc unbindFc} method overriden by this
244    * mixin.
245    *
246    * @param clientItfName the name of a client interface of the component to
247    * which this interface belongs.
248    * @throws NoSuchInterfaceException if there is no such client interface.
249    * @throws IllegalBindingException if the binding cannot be removed.
250    * @throws IllegalLifeCycleException if this component has a {@link
251    * LifeCycleController} interface, but it is not in an appropriate state
252    * to perform this operation.
253    */

254
255   public abstract void _super_unbindFc (String JavaDoc clientItfName) throws
256     NoSuchInterfaceException,
257     IllegalBindingException,
258     IllegalLifeCycleException;
259 }
260
Popular Tags