KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > component > controller > ProActiveBindingController


1 /*
2  * ################################################################
3  *
4  * ProActive: The Java(TM) library for Parallel, Distributed,
5  * Concurrent computing with Security and Mobility
6  *
7  * Copyright (C) 1997-2004 INRIA/University of Nice-Sophia Antipolis
8  * Contact: proactive-support@inria.fr
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23  * USA
24  *
25  * Initial developer(s): The ProActive Team
26  * http://www.inria.fr/oasis/ProActive/contacts.html
27  * Contributor(s):
28  *
29  * ################################################################
30  */

31 package org.objectweb.proactive.core.component.controller;
32
33 import org.apache.log4j.Logger;
34
35 import org.objectweb.fractal.api.Component;
36 import org.objectweb.fractal.api.Interface;
37 import org.objectweb.fractal.api.NoSuchInterfaceException;
38 import org.objectweb.fractal.api.control.BindingController;
39 import org.objectweb.fractal.api.control.IllegalBindingException;
40 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
41 import org.objectweb.fractal.api.control.LifeCycleController;
42 import org.objectweb.fractal.api.type.InterfaceType;
43 import org.objectweb.fractal.util.Fractal;
44
45 import org.objectweb.proactive.ProActive;
46 import org.objectweb.proactive.core.ProActiveRuntimeException;
47 import org.objectweb.proactive.core.component.Binding;
48 import org.objectweb.proactive.core.component.Bindings;
49 import org.objectweb.proactive.core.component.Constants;
50 import org.objectweb.proactive.core.component.Fractive;
51 import org.objectweb.proactive.core.component.ProActiveInterface;
52 import org.objectweb.proactive.core.component.identity.ProActiveComponent;
53 import org.objectweb.proactive.core.component.type.ProActiveInterfaceType;
54 import org.objectweb.proactive.core.group.Group;
55 import org.objectweb.proactive.core.group.ProActiveGroup;
56
57 import java.io.Serializable JavaDoc;
58
59 import java.util.Collection JavaDoc;
60 import java.util.Hashtable JavaDoc;
61
62
63 /**
64  * Abstract implementation of BindingController.
65  *
66  * It defines common operations of both primitive and composite binding controllers.
67  *
68  * @author Matthieu Morel
69  *
70  */

71 public class ProActiveBindingController extends ProActiveController
72     implements BindingController, Serializable JavaDoc {
73     //private Vector bindings; // contains Binding objects
74
//private Hashtable bindings; // key = clientInterfaceName ; value = Binding
75
protected static Logger logger = Logger.getLogger(ProActiveBindingController.class.getName());
76     private Bindings bindings; // key = clientInterfaceName ; value = Binding
77
protected Hashtable JavaDoc groupBindings;
78
79     public ProActiveBindingController(Component owner) {
80         super(owner, Constants.BINDING_CONTROLLER);
81         bindings = new Bindings();
82     }
83
84     public void addBinding(Binding binding) {
85         //bindings.put(binding.getClientInterface().getFcItfName(), binding);
86
bindings.add(binding);
87     }
88
89     protected boolean existsBinding(String JavaDoc clientItfName) {
90         return bindings.containsBindingOn(clientItfName);
91     }
92
93     protected boolean existsClientInterface(String JavaDoc clientItfName) {
94         try {
95             return (getFcItfOwner().getFcInterface(clientItfName) != null);
96         } catch (NoSuchInterfaceException nsie) {
97             logger.error("interface not found : " + nsie.getMessage());
98             throw new ProActiveRuntimeException(nsie);
99         }
100     }
101     
102
103     protected void checkBindability(String JavaDoc clientItfName, Interface serverItf)
104         throws NoSuchInterfaceException, IllegalBindingException,
105             IllegalLifeCycleException {
106         if (!existsClientInterface(clientItfName)) {
107             throw new NoSuchInterfaceException(clientItfName +
108                 " is not a client interface");
109         }
110         if (existsBinding(clientItfName)) {
111             if (!((ProActiveInterfaceType) ((Interface) getFcItfOwner()
112                                                                 .getFcInterface(clientItfName)).getFcItfType()).isFcCollectionItf()) {
113                 // binding from a single client interface : only 1 binding is allowed
114
logger.warn(((ComponentParametersController) getFcItfOwner()
115                              .getFcInterface(Constants.COMPONENT_PARAMETERS_CONTROLLER)).getComponentParameters()
116                              .getName() + "." + clientItfName +
117                     " is already bound");
118
119                 throw new IllegalBindingException(clientItfName +
120                     " is already bound");
121             } else {
122                 // binding from a collective interface
123
if (((InterfaceType) serverItf.getFcItfType()).isFcClientItf()) {
124                     // binding to a client(external) interface --> not OK
125
throw new IllegalBindingException(serverItf.getFcItfName() + " is not a server interface");
126                 }
127             }
128         }
129         
130         // TODO : check bindings between external client interfaces
131

132         
133
134         // see next, but need to consider internal interfaces (i.e. valid if server AND internal)
135
// if (((InterfaceType) serverItf.getFcItfType()).isFcClientItf()) {
136
// throw new IllegalBindingException(serverItf.getFcItfName() + " is not a server interface");
137
// }
138
// TODO : other checks are to be performed (viability of the bindings)
139
if (((LifeCycleController) getFcItfOwner().getFcInterface(Constants.LIFECYCLE_CONTROLLER)).getFcState() != LifeCycleController.STOPPED) {
140             throw new IllegalLifeCycleException(
141                 "component has to be stopped to perform binding operations");
142         }
143     }
144
145     protected void checkUnbindability(String JavaDoc clientItfName)
146         throws NoSuchInterfaceException, IllegalBindingException,
147             IllegalLifeCycleException {
148         checkLifeCycleIsStopped();
149         if (!existsClientInterface(clientItfName)) {
150             throw new NoSuchInterfaceException(clientItfName +
151                 " is not a client interface");
152         }
153         if (!existsBinding(clientItfName)) {
154             throw new IllegalBindingException(clientItfName +
155                 " is not yet bound");
156         }
157     }
158
159     /**
160      *
161      * @param the name of the client interface
162      * @return a Binding object if single binding, Vector of Binding objects otherwise
163      */

164     public Object JavaDoc removeBinding(String JavaDoc clientItfName) {
165         return bindings.remove(clientItfName);
166     }
167
168     /**
169      *
170      * @param the name of the client interface
171      * @return a Binding object if single binding, Vector of Binding objects otherwise
172      */

173     public Object JavaDoc getBinding(String JavaDoc clientItfName) {
174         return bindings.get(clientItfName);
175     }
176
177     /**
178              * see {@link org.objectweb.fractal.api.control.BindingController#lookupFc(String)}
179              */

180     public Object JavaDoc lookupFc(String JavaDoc clientItfName)
181         throws NoSuchInterfaceException {
182         if (!existsBinding(clientItfName)) {
183             throw new NoSuchInterfaceException(clientItfName);
184         } else {
185             if (getBinding(clientItfName) instanceof Collection JavaDoc) {
186                 logger.error(
187                     "you are looking up a collection of bindings. This method cannot return one single Interface object");
188                 return null;
189             } else {
190                 return ((Binding) getBinding(clientItfName)).getServerInterface();
191             }
192         }
193     }
194
195     /**
196      * implementation of the interface BindingController
197      * see {@link BindingController#bindFc(java.lang.String, java.lang.Object)}
198      */

199     public void bindFc(String JavaDoc clientItfName, Object JavaDoc serverItf)
200         throws NoSuchInterfaceException, IllegalBindingException,
201             IllegalLifeCycleException {
202         checkBindability(clientItfName, (Interface) serverItf);
203         String JavaDoc hierarchical_type = (Fractive.getComponentParametersController(getFcItfOwner())).getComponentParameters()
204                                     .getHierarchicalType();
205         if (hierarchical_type.equals(Constants.PRIMITIVE)) {
206             primitiveBindFc(clientItfName, (Interface) serverItf);
207         }
208         if (hierarchical_type.equals(Constants.COMPOSITE)) {
209             compositeBindFc(clientItfName, (Interface) serverItf);
210         }
211         if (hierarchical_type.equals(Constants.PARALLEL)) {
212             parallelBindFc(clientItfName, (Interface) serverItf);
213         }
214     }
215
216     private void primitiveBindFc(String JavaDoc clientItfName, Interface serverItf)
217         throws NoSuchInterfaceException, IllegalBindingException,
218             IllegalLifeCycleException {
219         // delegate binding operation to the reified object
220
BindingController user_binding_controller = (BindingController) ((ProActiveComponent) getFcItfOwner()).getReferenceOnBaseObject();
221
222         // serverItf cannot be a Future (because it has to be casted) => make sure if binding to a composite's internal interface
223
serverItf = (Interface) ProActive.getFutureValue(serverItf);
224         user_binding_controller.bindFc(clientItfName, serverItf);
225         addBinding(new Binding(
226                 (Interface) getFcItfOwner().getFcInterface(clientItfName),
227                 serverItf));
228     }
229
230     private void parallelBindFc(String JavaDoc clientItfName, Interface serverItf)
231         throws NoSuchInterfaceException, IllegalBindingException,
232             IllegalLifeCycleException {
233         ProActiveInterface clientItf = (ProActiveInterface) getFcItfOwner()
234                                                                 .getFcInterface(clientItfName);
235
236         // 1. parallel.serverItf -- > subcomponent.serverItf
237
// check :
238
// - whether the client interface is actually a server interface for the parallel component
239
boolean condition1 = !((InterfaceType) clientItf.getFcItfType()).isFcClientItf();
240
241         // - whether the targeted server interface belongs to an internal component
242
boolean condition2 = ((ProActiveContentController) (Fractal.getContentController(getFcItfOwner()))).isSubComponent(serverItf.getFcItfOwner());
243         if (condition1 && condition2) {
244             if (ProActiveGroup.isGroup(clientItf.getFcItfImpl())) {
245                 // we do not set the delegatee to null, the delegatee being a group,
246
// but we remove all the elements of this group
247
Group group = ProActiveGroup.getGroup(clientItf.getFcItfImpl());
248                 group.add(serverItf);
249             } else {
250                 throw new IllegalBindingException(
251                     "illegal binding : server interface " + clientItfName +
252                     " of parallel component " +
253                     Fractive.getComponentParametersController(getFcItfOwner())
254                            .getComponentParameters().getName() +
255                     " should be a collective interface");
256             }
257         } else if (!condition1 && !condition2) {
258             // 2. parallel.clientItf --> othercomponent.serverItf
259
// it is a standard composite binding
260
compositeBindFc(clientItfName, serverItf);
261         } else {
262             throw new IllegalBindingException("illegal binding of " +
263                 Fractive.getComponentParametersController(getFcItfOwner())
264                        .getComponentParameters().getName() + '.' +
265                 clientItfName);
266         }
267     }
268
269     /**
270      * binding method enforcing Interface type for the server interface, for composite components
271      */

272     private void compositeBindFc(String JavaDoc clientItfName, Interface serverItf)
273         throws NoSuchInterfaceException, IllegalBindingException,
274             IllegalLifeCycleException {
275         ProActiveInterface clientItf = (ProActiveInterface) getFcItfOwner()
276                                                                 .getFcInterface(clientItfName);
277
278         InterfaceType client_itf_type = ((ComponentParametersController) getFcItfOwner()
279                                          .getFcInterface(Constants.COMPONENT_PARAMETERS_CONTROLLER)).getComponentParameters()
280                                          .getComponentType().getFcInterfaceType(clientItfName);
281
282         // if we have a collection interface, the impl object is actually a group of references to interfaces
283
// Thus we have to add the link to the new interface in this group
284
// same for client interfaces of parallel components
285
if (client_itf_type.isFcCollectionItf() ||
286                 (((ComponentParametersController) getFcItfOwner()
287                       .getFcInterface(Constants.COMPONENT_PARAMETERS_CONTROLLER)).getComponentParameters()
288                       .getHierarchicalType().equals(Constants.PARALLEL) &&
289                 !client_itf_type.isFcClientItf())) {
290             if (ProActiveGroup.isGroup(clientItf.getFcItfImpl())) {
291                 Group itf_ref_group = ProActiveGroup.getGroup(clientItf.getFcItfImpl());
292
293                 //itf_ref_group.add(((Interface)serverItf).getFcItfOwner());
294
itf_ref_group.add((Interface) serverItf);
295                 if (logger.isDebugEnabled()) {
296                     logger.debug("performed collective binding : " +
297                         ((ComponentParametersController) getFcItfOwner()
298                          .getFcInterface(Constants.COMPONENT_PARAMETERS_CONTROLLER)).getComponentParameters()
299                          .getName() + '.' + clientItfName + " --> " +
300                         ((ComponentParametersController) ((Interface) serverItf)
301                          .getFcItfOwner().getFcInterface(Constants.COMPONENT_PARAMETERS_CONTROLLER)).getComponentParameters()
302                          .getName() + '.' +
303                         ((Interface) serverItf).getFcItfName());
304                 }
305             }
306         } else {
307             clientItf.setFcItfImpl(serverItf);
308         }
309         addBinding(new Binding(clientItf, (Interface) serverItf));
310     }
311
312     /**
313              * @see org.objectweb.fractal.api.control.BindingController#unbindFc(String)
314              *
315              * CAREFUL : unbinding action on collective interfaces will remove all the bindings to this interface.
316              * This is also the case when removing bindings from the server interface of a parallel component
317              * (yes you can do unbindFc(parallelServerItfName) !)
318             */

319     public void unbindFc(String JavaDoc clientItfName)
320         throws NoSuchInterfaceException, IllegalBindingException,
321             IllegalLifeCycleException { // remove from bindings and set impl object to null
322

323         checkUnbindability(clientItfName);
324         ProActiveInterface clientItf = (ProActiveInterface) getFcItfOwner()
325                                                                 .getFcInterface(clientItfName);
326         if (clientItf == null) {
327             throw new NoSuchInterfaceException(clientItfName);
328         }
329
330         // in the case of a collection interface, we actually remove ALL THE BINDINGS to this interface
331
if (ProActiveGroup.isGroup(clientItf.getFcItfImpl())) {
332             // we do not set the delegatee to null, the delegatee being a group,
333
// but we remove all the elements of this group
334
Group group = ProActiveGroup.getGroup(clientItf.getFcItfImpl());
335             group.clear();
336             removeBinding(clientItfName);
337         } else {
338             Binding binding = (Binding) (removeBinding(clientItfName));
339             ((ProActiveInterface) (binding.getClientInterface())).setFcItfImpl(null);
340         }
341         
342     }
343
344     /* (non-Javadoc)
345      * @see org.objectweb.fractal.api.control.BindingController#listFc()
346      */

347     public String JavaDoc[] listFc() {
348         return bindings.getExternalClientBindings();
349     }
350 }
351
Popular Tags