KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > component > request > ComponentRequestImpl


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.request;
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.AttributeController;
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
43 import org.objectweb.proactive.Body;
44 import org.objectweb.proactive.core.body.UniversalBody;
45 import org.objectweb.proactive.core.body.request.Request;
46 import org.objectweb.proactive.core.body.request.RequestImpl;
47 import org.objectweb.proactive.core.body.request.ServeException;
48 import org.objectweb.proactive.core.component.Constants;
49 import org.objectweb.proactive.core.component.Fractive;
50 import org.objectweb.proactive.core.component.body.ComponentBodyImpl;
51 import org.objectweb.proactive.core.component.controller.ComponentParametersController;
52 import org.objectweb.proactive.core.mop.MethodCall;
53 import org.objectweb.proactive.core.mop.MethodCallExecutionFailedException;
54
55 import java.io.Serializable JavaDoc;
56
57
58 /**
59  * Method calls to components are actually reified calls, and ComponentRequest contains
60  * a reification of the call.
61  *
62  * This class allows for the tagging of the call (a component call), and the redispatching
63  * to the targeted component metaobject.
64  *
65  * @author Matthieu Morel
66  *
67  */

68 public class ComponentRequestImpl extends RequestImpl
69     implements ComponentRequest, Serializable JavaDoc {
70     protected static Logger logger = Logger.getLogger(ComponentRequestImpl.class.getName());
71
72     public ComponentRequestImpl(MethodCall methodCall, UniversalBody sender,
73         boolean isOneWay, long nextSequenceID) {
74         super(methodCall, sender, isOneWay, nextSequenceID);
75     }
76
77     public ComponentRequestImpl(Request request) {
78         super(request.getMethodCall(), request.getSender(), request.isOneWay(),
79             request.getSequenceNumber());
80     }
81
82     /**
83      * redirects the call to the adequate component metaobject
84      */

85     protected Object JavaDoc serveInternal(Body targetBody) throws ServeException {
86         // if (logger.isDebugEnabled()) {
87
// //logger.debug("ComponentRequestImpl.serveInternal : redirecting the call to the component metaobject");
88
// }
89
try {
90             Object JavaDoc result = null;
91             Class JavaDoc target_class = methodCall.getReifiedMethod()
92                                            .getDeclaringClass();
93             if (target_class.equals(BindingController.class)) {
94                 result = methodCall.execute(((ComponentBodyImpl) targetBody).getProActiveComponent()
95                                              .getFcInterface(Constants.BINDING_CONTROLLER));
96             } else if (target_class.equals(ContentController.class)) {
97                 result = methodCall.execute(((ComponentBodyImpl) targetBody).getProActiveComponent()
98                                              .getFcInterface(Constants.CONTENT_CONTROLLER));
99             } else if (target_class.equals(LifeCycleController.class)) {
100                 result = methodCall.execute(((ComponentBodyImpl) targetBody).getProActiveComponent()
101                                              .getFcInterface(Constants.LIFECYCLE_CONTROLLER));
102             } else if (target_class.equals(ComponentParametersController.class)) {
103                 result = methodCall.execute(((ComponentBodyImpl) targetBody).getProActiveComponent()
104                                              .getFcInterface(Constants.COMPONENT_PARAMETERS_CONTROLLER));
105             } else if (target_class.equals(AttributeController.class)) {
106                 // directly invoke on reified object, as AttributeController is only defined on primitive components
107
result = methodCall.execute(targetBody.getReifiedObject());
108             } else if (target_class.equals(Interface.class)) {
109                 result = methodCall.execute((Interface) ((ComponentBodyImpl) targetBody).getProActiveComponent());
110             } else if (target_class.equals(Component.class)) {
111                 result = methodCall.execute(((ComponentBodyImpl) targetBody).getProActiveComponent()
112                                              .getFcInterface(Constants.COMPONENT));
113             } else {
114                 if (((ComponentBodyImpl) targetBody).getProActiveComponent() != null) {
115                     String JavaDoc hierarchical_type = Fractive.getComponentParametersController(((ComponentBodyImpl) targetBody).getProActiveComponent())
116                                                        .getComponentParameters()
117                                                        .getHierarchicalType();
118
119                     // if the component is a composite OR A PARALLEL , forward to functional interface
120
if (hierarchical_type.equals(Constants.COMPOSITE) ||
121                             hierarchical_type.equals(Constants.PARALLEL)) {
122                         // // result =
123
// // methodCall.execute(
124
// // ((ComponentBodyImpl) targetBody).getProActiveComponentIdentity().getFcInterface(
125
// // methodCall.getReifiedMethod().getName()));
126
// // forward to functional interface whose name is given as a parameter in the method call
127
if (logger.isDebugEnabled()) {
128                             logger.debug(" forwarding the call : " +
129                                 methodCall.getFcFunctionalInterfaceName() +
130                                 " to : " +
131                                 ((ComponentParametersController) ((ComponentBodyImpl) targetBody)
132                                  .getProActiveComponent().getFcInterface(Constants.COMPONENT_PARAMETERS_CONTROLLER)).getComponentParameters()
133                                  .getName());
134                         }
135                         try {
136                             result = methodCall.execute(((ComponentBodyImpl) targetBody).getProActiveComponent()
137                                                          .getFcInterface(methodCall.getFcFunctionalInterfaceName()));
138                         } catch (Exception JavaDoc e) {
139                             e.printStackTrace();
140                         }
141                     } else {
142                         // the component is a primitive
143
// directly execute the method on the active object
144
// if (logger.isDebugEnabled()) {
145
// logger.debug("primitive component forwarding the call : " +
146
// methodCall.getFcFunctionalInterfaceName());
147
// }
148
if (logger.isDebugEnabled()) {
149                             logger.debug(" directly executing the call : " +
150                                 methodCall.getFcFunctionalInterfaceName());
151                         }
152                         result = methodCall.execute(targetBody.getReifiedObject());
153                     }
154                 } else {
155                     throw new ServeException(
156                         "trying to execute a component method on an object that is not a component");
157                 }
158             }
159             return result;
160         } catch (NoSuchInterfaceException nsie) {
161             nsie.printStackTrace();
162             throw new ServeException("cannot serve request : problem accessing a component controller",
163                 nsie);
164         } catch (MethodCallExecutionFailedException e) {
165             e.printStackTrace();
166             throw new ServeException("serve method " +
167                 methodCall.getReifiedMethod().toString() + " failed", e);
168         } catch (java.lang.reflect.InvocationTargetException JavaDoc e) {
169             Throwable JavaDoc t = e.getTargetException();
170
171             // t.printStackTrace();
172
if (isOneWay) {
173                 throw new ServeException("serve method " +
174                     methodCall.getReifiedMethod().toString() + " failed", t);
175             } else {
176                 return t;
177             }
178         }
179     }
180
181     /**
182      * @see org.objectweb.proactive.core.component.request.ComponentRequest#isControllerRequest()
183      */

184     public boolean isControllerRequest() {
185         Class JavaDoc declaring_class = methodCall.getReifiedMethod().getDeclaringClass();
186         return (declaring_class.equals(ComponentParametersController.class) ||
187         declaring_class.equals(ContentController.class) ||
188         declaring_class.equals(BindingController.class) ||
189         declaring_class.equals(LifeCycleController.class) ||
190         declaring_class.equals(Interface.class));
191     }
192 }
193
Popular Tags