KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > component > identity > ProActiveComponentImpl


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.identity;
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.Type;
39 import org.objectweb.fractal.api.type.InterfaceType;
40
41 import org.objectweb.proactive.Body;
42 import org.objectweb.proactive.core.ProActiveRuntimeException;
43 import org.objectweb.proactive.core.UniqueID;
44 import org.objectweb.proactive.core.component.ComponentParameters;
45 import org.objectweb.proactive.core.component.Constants;
46 import org.objectweb.proactive.core.component.ProActiveInterface;
47 import org.objectweb.proactive.core.component.asmgen.MetaObjectInterfaceClassGenerator;
48 import org.objectweb.proactive.core.component.controller.ComponentParametersController;
49 import org.objectweb.proactive.core.component.controller.ProActiveBindingController;
50 import org.objectweb.proactive.core.component.controller.ProActiveComponentParametersController;
51 import org.objectweb.proactive.core.component.controller.ProActiveContentController;
52 import org.objectweb.proactive.core.component.controller.ProActiveLifeCycleController;
53 import org.objectweb.proactive.core.component.representative.ProActiveComponentRepresentative;
54 import org.objectweb.proactive.core.component.request.ComponentRequestQueue;
55 import org.objectweb.proactive.core.group.Group;
56 import org.objectweb.proactive.core.group.ProActiveComponentGroup;
57 import org.objectweb.proactive.core.group.ProActiveGroup;
58
59 import java.io.Serializable JavaDoc;
60
61 import java.util.Iterator JavaDoc;
62 import java.util.Vector JavaDoc;
63
64
65 /**
66  * The base class for managing components.
67  * It builds the "membrane" in the Fractal terminology : the controllers of the
68  * components.
69  *
70  *
71  * @author Matthieu Morel
72  */

73 public class ProActiveComponentImpl implements ProActiveComponent, Interface,
74     Serializable JavaDoc {
75     protected static Logger logger = Logger.getLogger(ProActiveComponentImpl.class.getName());
76
77     //private ComponentParameters componentParameters;
78
private Interface[] interfaceReferences;
79     private Body body;
80
81     public ProActiveComponentImpl() {
82     }
83
84     /** Constructor for ProActiveComponent.
85      * @param componentParameters
86      * @param myBody a reference on the body (required notably to get a reference on the request
87      * queue, used to control the life cycle of the component)
88      */

89     public ProActiveComponentImpl(ComponentParameters componentParameters,
90         Body myBody) {
91         this.body = myBody;
92
93         //this.body = new RemoteBodyAdapter(myBody);
94
//this.body = myBody;
95
//this.componentParameters = componentParameters;
96
boolean component_is_primitive = componentParameters.getHierarchicalType()
97                                                             .equals(Constants.PRIMITIVE);
98
99         // add interface references
100
Vector JavaDoc interface_references_vector = new Vector JavaDoc(4);
101
102         // 1. component identity
103
interface_references_vector.addElement(this);
104
105         //2. control interfaces
106
interface_references_vector.add(new ProActiveLifeCycleController(this));
107
108         // add component parameters controller
109
ComponentParametersController component_parameters_controller = new ProActiveComponentParametersController(this);
110         component_parameters_controller.setComponentParameters(componentParameters);
111         interface_references_vector.add(component_parameters_controller);
112
113         // add a binding controller
114
// only exception : primitive without any client interface
115
if (!(componentParameters.getHierarchicalType().equals(Constants.PRIMITIVE) &&
116                 (componentParameters.getClientInterfaceTypes().length == 0))) {
117             interface_references_vector.add(new ProActiveBindingController(this));
118         } else {
119             //bindingController = null;
120
if (logger.isDebugEnabled()) {
121                 logger.debug("user component class of '" +
122                     componentParameters.getName() +
123                     "' does not have any client interface. It will have no BindingController");
124             }
125         }
126
127         // create a content controller only if the component is a composite (or a parallel composite)
128
if (componentParameters.getHierarchicalType().equals(Constants.COMPOSITE) ||
129                 (componentParameters.getHierarchicalType().equals(Constants.PARALLEL))) {
130             // add and resize
131
interface_references_vector.addElement(new ProActiveContentController(
132                     this));
133         }
134
135         // 3. external functional interfaces
136
InterfaceType[] interface_types = componentParameters.getComponentType()
137                                                              .getFcInterfaceTypes();
138         try {
139             for (int i = 0; i < interface_types.length; i++) {
140                 ProActiveInterface itf_ref = null;
141
142                 // if we have a COLLECTION CLIENT interface, we should see the delegatee ("impl" field) as a group
143
if (interface_types[i].isFcClientItf() &&
144                         interface_types[i].isFcCollectionItf()) {
145                     //Component itfg = ProActiveComponentGroup.newActiveComponentGroup(interface_types[i]);
146
itf_ref = createInterfaceOnGroupOfDelegatees(interface_types[i],
147                             component_is_primitive);
148                 }
149                 // if we have a server port of a PARALLEL component, we also create a group proxy on the delegatee field
150
else if (componentParameters.getHierarchicalType().equals(Constants.PARALLEL) &&
151                         (!interface_types[i].isFcClientItf())) {
152                     // parallel component have a collective port on their server interfaces
153
itf_ref = createInterfaceOnGroupOfDelegatees(interface_types[i],
154                             component_is_primitive);
155                 } else {
156                     itf_ref = MetaObjectInterfaceClassGenerator.instance()
157                                                                .generateInterface(interface_types[i].getFcItfName(),
158                             this, interface_types[i], true,
159                             component_is_primitive);
160                     // all interfaces are also internal
161
//component_is_composite ? true : false); // interfaces are both external and internal for composite components
162
}
163
164                 // set delegation link
165
if (componentParameters.getHierarchicalType().equals(Constants.PRIMITIVE)) {
166                     // no group case
167
if (!interface_types[i].isFcCollectionItf()) {
168                         if (!interface_types[i].isFcClientItf()) {
169                             (itf_ref).setFcItfImpl(getReferenceOnBaseObject());
170                         } else if (interface_types[i].isFcClientItf()) {
171                             (itf_ref).setFcItfImpl(null);
172                         }
173                     }
174                 } else {
175                     // we have a composite component
176
// thus the reified object is not the one implementing the functional interface
177
// the designation of the delegation object (impl) is deferred to binding time
178
}
179                 interface_references_vector.addElement(itf_ref);
180             }
181         } catch (Exception JavaDoc e) {
182             if (logger.isDebugEnabled()) {
183                 logger.debug("cannot create interface references : " +
184                     e.getMessage());
185             }
186             e.printStackTrace();
187             throw new RuntimeException JavaDoc("cannot create interface references : " +
188                 e.getMessage());
189         }
190
191         // put all in a table
192
interfaceReferences = (Interface[]) interface_references_vector.toArray(new Interface[interface_references_vector.size()]);
193     }
194
195     // returns a generated interface reference, whose impl field is a group
196
// It is able to handle multiple bindings
197
private ProActiveInterface createInterfaceOnGroupOfDelegatees(
198         InterfaceType itfType, boolean isPrimitive) throws Exception JavaDoc {
199         ProActiveInterface itf_ref = MetaObjectInterfaceClassGenerator.instance()
200                                                                       .generateInterface(itfType.getFcItfName(),
201                 this, itfType, true, isPrimitive);
202
203         // create a group of impl target objects
204
// // should not have a ProActiveComponentRepresentativeImpl, but a sub-type of the functional interface
205
// Component ci_group = ProActiveComponentGroup.newActiveComponentGroup(itfType);
206
// itf_ref.setFcItfImpl(ci_group);
207
ProActiveInterface itf_ref_group = ProActiveComponentGroup.newActiveComponentInterfaceGroup(itfType);
208         itf_ref.setFcItfImpl(itf_ref_group);
209         return itf_ref;
210     }
211
212     /**
213      * see {@link org.objectweb.fractal.api.Component#getFcInterface(String)}
214      */

215     public Object JavaDoc getFcInterface(String JavaDoc interfaceName)
216         throws NoSuchInterfaceException {
217         if (interfaceReferences != null) {
218             for (int i = 0; i < interfaceReferences.length; i++) {
219                 if (ProActiveGroup.isGroup(interfaceReferences[i])) {
220                     // need to find at least one occurence of the interface
221
// if exists but not for all elements of the group -> throw error
222
// if does not exist -> do nothing
223
int count = 0;
224                     Group itf_ref_group = ProActiveGroup.getGroup(interfaceReferences[i]);
225                     Iterator JavaDoc iterator = itf_ref_group.iterator();
226
227                     // ensure groups are coherent (ie : if 1 interface of the given name exists,
228
// all of the group elements should be of this kind
229
while (iterator.hasNext()) {
230                         Interface group_element = (Interface) iterator.next();
231                         if (group_element.getFcItfName().equals(interfaceName)) {
232                             count++;
233                         }
234                     }
235                     if (count > 0) {
236                         if (count == itf_ref_group.size()) {
237                             return interfaceReferences[i];
238                         } else {
239                             throw new NoSuchInterfaceException(
240                                 "some elements of the collection are not named " +
241                                 interfaceName);
242                         }
243                     }
244                 } else {
245                     //looking into single interface
246
if (interfaceReferences[i].getFcItfName().equals(interfaceName)) {
247                         return interfaceReferences[i];
248                     }
249                 }
250             }
251         }
252         throw new NoSuchInterfaceException(interfaceName);
253     }
254
255     /**
256      * see {@link org.objectweb.fractal.api.Component#getFcInterfaces()}
257      */

258     public Object JavaDoc[] getFcInterfaces() {
259         Vector JavaDoc external_interfaces = new Vector JavaDoc(interfaceReferences.length);
260         for (int i = 0; i < interfaceReferences.length; i++) {
261             if (!interfaceReferences[i].isFcInternalItf()) {
262                 external_interfaces.add(interfaceReferences[i]);
263             }
264         }
265         external_interfaces.trimToSize();
266         return external_interfaces.toArray();
267     }
268
269     /**
270      * see {@link org.objectweb.fractal.api.Component#getFcType()}
271      */

272     public Type getFcType() {
273         try {
274             return ((ComponentParametersController) getFcInterface(Constants.COMPONENT_PARAMETERS_CONTROLLER)).getComponentParameters()
275                     .getComponentType();
276         } catch (NoSuchInterfaceException nsie) {
277             nsie.printStackTrace();
278             throw new ProActiveRuntimeException("cannot retreive the type of the component",
279                 nsie);
280         }
281     }
282
283     /**
284      * see {@link org.objectweb.fractal.api.Interface#getFcItfName()}
285      */

286     public String JavaDoc getFcItfName() {
287         return Constants.COMPONENT;
288     }
289
290     /**
291      * see {@link org.objectweb.fractal.api.Interface#getFcItfOwner()}
292      */

293     public Component getFcItfOwner() {
294         return (Component) this;
295     }
296
297     /**
298      * see {@link org.objectweb.fractal.api.Interface#getFcItfType()}
299      */

300     public Type getFcItfType() {
301         return getFcType();
302     }
303
304     /**
305      * see {@link org.objectweb.fractal.api.Interface#isFcInternalItf()}
306      */

307     public boolean isFcInternalItf() {
308         return false;
309     }
310
311     /**
312      * Returns the base object.
313      * If the component is a composite, a basic do-nothing instance of class Composite
314      * is returned.
315      * @return the base object underneath
316      */

317     public Object JavaDoc getReferenceOnBaseObject() {
318         return getBody().getReifiedObject();
319     }
320
321     /**
322      * @return the request queue of the current active object.
323      */

324     public ComponentRequestQueue getRequestQueue() {
325         return (ComponentRequestQueue) getBody().getRequestQueue();
326     }
327
328     /**
329      * @return a ComponentParameters instance, corresponding to the configuration of the current component
330      */

331     public ComponentParameters getComponentParameters()
332         throws NoSuchInterfaceException {
333         //return componentParameters;
334
return ((ComponentParametersController) getFcInterface(Constants.COMPONENT_PARAMETERS_CONTROLLER)).getComponentParameters();
335     }
336
337     /**
338      * @return the body of the current active object
339      */

340     public Body getBody() {
341         return body;
342     }
343
344     /**
345      * Components are equal if they have the same UniqueID.
346      * However, it is only possible to compare the current component with a reference
347      * on another component, in other words with a ProActiveComponentRepresentative object.
348      *
349      * see {@link ProActiveComponent#equals(Object)}
350      */

351     public boolean equals(Object JavaDoc component) {
352         if (component instanceof ProActiveComponentRepresentative) {
353             return getBody().getID().equals(((ProActiveComponentRepresentative) component).getID());
354         } else {
355             logger.error(
356                 "can only compare the current component with component representatives");
357             return false;
358         }
359     }
360
361     /**
362      * hashCode override
363      * @return a hashCode for this object
364      */

365     public int hashCode() {
366         return getBody().hashCode();
367     }
368
369     /**
370      * see {@link org.objectweb.proactive.core.component.identity.ProActiveComponent#getID()}
371      */

372     public UniqueID getID() {
373         return getBody().getID();
374     }
375 }
376
Popular Tags