KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > component > representative > ProActiveComponentRepresentativeImpl


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.representative;
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.control.BindingController;
40 import org.objectweb.fractal.api.control.ContentController;
41 import org.objectweb.fractal.api.control.LifeCycleController;
42 import org.objectweb.fractal.api.type.ComponentType;
43 import org.objectweb.fractal.api.type.InterfaceType;
44
45 import org.objectweb.proactive.core.ProActiveRuntimeException;
46 import org.objectweb.proactive.core.UniqueID;
47 import org.objectweb.proactive.core.body.proxy.UniversalBodyProxy;
48 import org.objectweb.proactive.core.component.ComponentParameters;
49 import org.objectweb.proactive.core.component.Constants;
50 import org.objectweb.proactive.core.component.asmgen.RepresentativeInterfaceClassGenerator;
51 import org.objectweb.proactive.core.component.controller.ComponentParametersController;
52 import org.objectweb.proactive.core.component.identity.ProActiveComponent;
53 import org.objectweb.proactive.core.component.request.ComponentRequestQueue;
54 import org.objectweb.proactive.core.mop.MethodCall;
55 import org.objectweb.proactive.core.mop.Proxy;
56 import org.objectweb.proactive.core.mop.StubObject;
57
58 import java.io.Serializable JavaDoc;
59
60
61 public class ProActiveComponentRepresentativeImpl
62     implements ProActiveComponentRepresentative, BindingController,
63         LifeCycleController, ContentController, ComponentParametersController,
64         Interface, Serializable JavaDoc {
65     protected static Logger logger = Logger.getLogger(ProActiveComponentRepresentativeImpl.class.getName());
66     private Interface[] interfaceReferences;
67     private Proxy proxy;
68     //private ComponentParameters componentParameters;
69
private ComponentType componentType = null; // immutable
70
private Object JavaDoc stubOnBaseObject = null; // kept for possible direct invocations on the base object
71

72     // public ProActiveComponentRepresentativeImpl() {
73
// // required for reification in group mechanism
74
// }
75
public ProActiveComponentRepresentativeImpl(
76         ComponentParameters componentParameters, Proxy proxy) {
77         // set the reference to the proxy for delegating calls to the component metalevel of the active object
78
this.proxy = proxy;
79
80         componentType = componentParameters.getComponentType();
81         stubOnBaseObject = componentParameters.getStubOnReifiedObject();
82
83         // create the interface references tables
84
// the size is the addition of :
85
// - 1 for the current ItfRef (that is at the same time a binding controller, lifecycle controller,
86
// content controller and name controller
87
// - the number of client functional interfaces
88
// - the number of server functional interfaces
89
interfaceReferences = new Interface[1 +
90             (componentParameters.getClientInterfaceTypes().length +
91             componentParameters.getServerInterfaceTypes().length)];
92
93         int i = 0;
94
95         // add controllers
96
interfaceReferences[i] = (Interface) this;
97         i++;
98
99         // add functional interfaces
100
// functional interfaces are proxies on the corresponding meta-objects
101
// 3. external functional interfaces
102
InterfaceType[] interface_types = componentParameters.getComponentType()
103                                                              .getFcInterfaceTypes();
104         try {
105             for (int j = 0; j < interface_types.length; j++) {
106                 Interface interface_reference = RepresentativeInterfaceClassGenerator.instance()
107                                                                                      .generateInterface(interface_types[j].getFcItfName(),
108                         this, interface_types[j], false);
109
110                 ((StubObject) interface_reference).setProxy(proxy);
111
112                 // all calls are to be reified
113
interfaceReferences[i] = interface_reference;
114                 i++;
115             }
116         } catch (Exception JavaDoc e) {
117             e.printStackTrace();
118             throw new RuntimeException JavaDoc("cannot create interface references : " +
119                 e.getMessage());
120         }
121     }
122
123     /**
124      *see {@link org.objectweb.fractal.api.control.BindingController#lookupFc(String)}
125      */

126     public Object JavaDoc lookupFc(String JavaDoc clientItfName) {
127         return (Interface) reifyCall(BindingController.class.getName(),
128             "lookupFc", new Class JavaDoc[] { String JavaDoc.class },
129             new Object JavaDoc[] { clientItfName });
130     }
131
132     /**
133      *see {@link BindingController#bindFc(java.lang.String, java.lang.Object)}
134      */

135     public void bindFc(String JavaDoc clientItfName, Object JavaDoc serverItf) {
136         reifyCall(BindingController.class.getName(), "bindFc",
137             new Class JavaDoc[] { String JavaDoc.class, Object JavaDoc.class },
138             new Object JavaDoc[] { clientItfName, serverItf });
139     }
140
141     /**
142      *see {@link org.objectweb.fractal.api.control.BindingController#unbindFc(String)}
143      */

144     public void unbindFc(String JavaDoc clientItfName) {
145         reifyCall(BindingController.class.getName(), "unbindFc",
146             new Class JavaDoc[] { String JavaDoc.class }, new Object JavaDoc[] { clientItfName });
147     }
148
149     /**
150      *see {@link org.objectweb.fractal.api.control.LifeCycleController#getFcState()}
151      */

152     public String JavaDoc getFcState() {
153         return (String JavaDoc) reifyCall(LifeCycleController.class.getName(),
154             "getFcState", new Class JavaDoc[] { }, new Object JavaDoc[] { });
155     }
156
157     /**
158      *see {@link org.objectweb.fractal.api.control.LifeCycleController#startFc()}
159      */

160     public void startFc() {
161         reifyCall(LifeCycleController.class.getName(), "startFc",
162             new Class JavaDoc[] { }, new Object JavaDoc[] { });
163     }
164
165     /**
166      *see {@link org.objectweb.fractal.api.control.LifeCycleController#stopFc()}
167      */

168     public void stopFc() {
169         reifyCall(LifeCycleController.class.getName(), "stopFc",
170             new Class JavaDoc[] { }, new Object JavaDoc[] { });
171     }
172
173     /**
174      *see {@link org.objectweb.fractal.api.control.ContentController#getFcInternalInterfaces()}
175      */

176     public Object JavaDoc[] getFcInternalInterfaces() {
177         return (Object JavaDoc[]) reifyCall(ContentController.class.getName(),
178             "getFcInternalInterfaces", new Class JavaDoc[] { }, new Object JavaDoc[] { });
179     }
180
181     /**
182      * in this implementation, internal interfaces are also external interfaces.
183      *see {@link org.objectweb.fractal.api.control.ContentController#getFcInternalInterface(String)}
184      */

185     public Object JavaDoc getFcInternalInterface(String JavaDoc interfaceName)
186         throws NoSuchInterfaceException {
187         return getFcInterface(interfaceName);
188     }
189
190     /**
191      *see {@link org.objectweb.fractal.api.control.ContentController#getFcSubComponents()}
192      */

193     public Component[] getFcSubComponents() {
194         return (Component[]) reifyCall(ContentController.class.getName(),
195             "getFcSubComponents", new Class JavaDoc[] { }, new Object JavaDoc[] { });
196     }
197
198     /**
199      *see {@link org.objectweb.fractal.api.control.ContentController#addFcSubComponent(Component)}
200      */

201     public void addFcSubComponent(Component subComponent) {
202         reifyCall(ContentController.class.getName(), "addFcSubComponent",
203             new Class JavaDoc[] { Component.class }, new Object JavaDoc[] { subComponent });
204     }
205
206     /**
207      *see {@link org.objectweb.fractal.api.control.ContentController#removeFcSubComponent(Component)}
208      */

209     public void removeFcSubComponent(Component subComponent) {
210         reifyCall(ContentController.class.getName(), "removeFcSubComponent",
211             new Class JavaDoc[] { Component.class }, new Object JavaDoc[] { subComponent });
212     }
213
214     /**
215      *see {@link org.objectweb.fractal.api.Interface#getFcItfOwner()}
216      */

217     public Component getFcItfOwner() {
218         return (Component) reifyCall(Interface.class.getName(),
219             "getFcItfOwner", new Class JavaDoc[] { }, new Object JavaDoc[] { });
220     }
221
222     /**
223      *see {@link org.objectweb.fractal.api.Interface#getFcItfName()}
224      */

225     public String JavaDoc getFcItfName() {
226         // PB is that the current object implements several functional interfaces.
227
// Thus it has several names...
228
return null;
229     }
230
231     /**
232      *see {@link org.objectweb.fractal.api.Interface#getFcItfType()}
233      */

234     public Type getFcItfType() {
235         return getFcType();
236     }
237
238     /**
239      *see {@link org.objectweb.fractal.api.Interface#isFcInternalItf()}
240      */

241     public boolean isFcInternalItf() {
242         return false;
243     }
244
245     private Object JavaDoc reifyCall(String JavaDoc className, String JavaDoc methodName,
246         Class JavaDoc[] parameterTypes, Object JavaDoc[] effectiveParameters) {
247         try {
248             return proxy.reify((MethodCall) MethodCall.getComponentMethodCall(
249                     Class.forName(className).getDeclaredMethod(methodName,
250                         parameterTypes), effectiveParameters, null));
251
252             // functional interface name is null
253
} catch (NoSuchMethodException JavaDoc e) {
254             throw new ProActiveRuntimeException(e.toString());
255         } catch (ClassNotFoundException JavaDoc e) {
256             throw new ProActiveRuntimeException(e.toString());
257         } catch (Throwable JavaDoc e) {
258             e.printStackTrace();
259             throw new ProActiveRuntimeException(e.toString());
260         }
261     }
262
263     /**
264      *see {@link org.objectweb.fractal.api.Component#getFcInterface(String)}
265      */

266     public Object JavaDoc getFcInterface(String JavaDoc interfaceName)
267         throws NoSuchInterfaceException {
268         if (interfaceName.equals(Constants.BINDING_CONTROLLER) ||
269                 interfaceName.equals(Constants.CONTENT_CONTROLLER) ||
270                 interfaceName.equals(Constants.LIFECYCLE_CONTROLLER) ||
271                 interfaceName.equals(Constants.COMPONENT_PARAMETERS_CONTROLLER)) {
272             return this;
273         } else {
274             for (int i = 0; i < interfaceReferences.length; i++) {
275                 if (interfaceReferences[i].getFcItfName() != null) {
276                     if (interfaceReferences[i].getFcItfName().equals(interfaceName)) {
277                         return interfaceReferences[i];
278                     }
279                 }
280             }
281             throw new NoSuchInterfaceException(interfaceName);
282         }
283     }
284
285     /**
286      *see {@link org.objectweb.fractal.api.Component#getFcInterfaces()}
287      */

288     public Object JavaDoc[] getFcInterfaces() {
289         // TODO implementation
290
throw new ProActiveRuntimeException("not yet implemented");
291     }
292
293     /**
294      *see {@link org.objectweb.fractal.api.Component#getFcType()}
295      */

296     public Type getFcType() {
297         return componentType;
298     }
299
300     /**
301      *see {@link org.objectweb.proactive.core.mop.StubObject#getProxy()}
302      */

303     public Proxy getProxy() {
304         return proxy;
305     }
306
307     /**
308      *see {@link org.objectweb.proactive.core.mop.StubObject#setProxy(Proxy)}
309      */

310     public void setProxy(Proxy proxy) {
311         this.proxy = proxy;
312     }
313
314     /**
315      * The comparison of component references is actually a comparison of unique
316      * identifiers accross jvms.
317      */

318     public boolean equals(Object JavaDoc component) {
319         if (!(component instanceof ProActiveComponent)) {
320             logger.error(
321                 "can only compare proactive components to proactive components ");
322             return false;
323         }
324         return getID().equals(((ProActiveComponent) component).getID());
325     }
326
327     public int hashCode() {
328         // should be cached maybe
329
return ((UniversalBodyProxy) getProxy()).getBodyID().hashCode();
330     }
331
332     public UniqueID getID() {
333         return ((UniversalBodyProxy) getProxy()).getBodyID();
334     }
335
336     /**
337      *see {@link ComponentParametersController#setComponentParameters(ComponentParameters)}
338      */

339     public void setComponentParameters(ComponentParameters componentParameters) {
340         logger.error("only available in the meta-objects");
341     }
342
343     /**
344      *see {@link ComponentParametersController#getComponentParameters()}
345      */

346     public ComponentParameters getComponentParameters() {
347         return (ComponentParameters) reifyCall(ComponentParametersController.class.getName(),
348             "getComponentParameters", new Class JavaDoc[] { }, new Object JavaDoc[] { });
349     }
350
351     /**
352      *see {@link org.objectweb.fractal.api.control.BindingController#listFc()}
353      */

354     public String JavaDoc[] listFc() {
355         return null;
356     }
357     
358     /**
359      * see {@link org.objectweb.proactive.core.component.controller.ComponentParametersController#setComponentName(java.lang.String)}
360      */

361     public void setComponentName(String JavaDoc componentName) {
362         reifyCall(ComponentParametersController.class.getName(),
363             "setComponentName", new Class JavaDoc[] {String JavaDoc.class}, new Object JavaDoc[] {componentName});
364
365     }
366
367     /**
368      * @see org.objectweb.proactive.core.component.identity.ProActiveComponent#getReferenceOnBaseObject()
369      */

370     public Object JavaDoc getReferenceOnBaseObject() {
371         return null;
372     }
373
374     /** (non-Javadoc)
375      * only available in the meta-objects
376      */

377     public ComponentRequestQueue getRequestQueue() {
378         logger.error("only available in the meta-objects");
379         return null;
380     }
381
382 }
383
Popular Tags