00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 package org.objectweb.proactive.core.component.request;
00032
00033 import java.io.IOException;
00034 import java.io.Serializable;
00035 import java.util.Iterator;
00036 import java.util.List;
00037 import java.util.ListIterator;
00038
00039 import org.apache.log4j.Logger;
00040 import org.objectweb.fractal.api.NoSuchInterfaceException;
00041 import org.objectweb.fractal.api.control.LifeCycleController;
00042 import org.objectweb.proactive.Body;
00043 import org.objectweb.proactive.core.body.UniversalBody;
00044 import org.objectweb.proactive.core.body.future.FutureResult;
00045 import org.objectweb.proactive.core.body.request.Request;
00046 import org.objectweb.proactive.core.body.request.RequestImpl;
00047 import org.objectweb.proactive.core.body.request.ServeException;
00048 import org.objectweb.proactive.core.component.Constants;
00049 import org.objectweb.proactive.core.component.Fractive;
00050 import org.objectweb.proactive.core.component.ProActiveInterface;
00051 import org.objectweb.proactive.core.component.body.ComponentBody;
00052 import org.objectweb.proactive.core.component.body.ComponentBodyImpl;
00053 import org.objectweb.proactive.core.component.controller.ComponentParametersController;
00054 import org.objectweb.proactive.core.component.identity.ProActiveComponentImpl;
00055 import org.objectweb.proactive.core.component.interception.InputInterceptor;
00056 import org.objectweb.proactive.core.component.representative.ItfID;
00057 import org.objectweb.proactive.core.component.type.ProActiveInterfaceType;
00058 import org.objectweb.proactive.core.mop.MethodCall;
00059 import org.objectweb.proactive.core.mop.MethodCallExecutionFailedException;
00060 import org.objectweb.proactive.core.util.log.Loggers;
00061 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00062
00063
00075 public class ComponentRequestImpl extends RequestImpl
00076 implements ComponentRequest, Serializable {
00077 protected static Logger logger = ProActiveLogger.getLogger(Loggers.COMPONENTS_REQUESTS);
00078
00079
00080
00081 private final Class declaringClass;
00082 private final Class targetClass;
00083
00084
00085
00086 public ComponentRequestImpl(MethodCall methodCall, UniversalBody sender,
00087 boolean isOneWay, long nextSequenceID) {
00088 super(methodCall, sender, isOneWay, nextSequenceID);
00089 declaringClass = methodCall.getReifiedMethod().getDeclaringClass();
00090 targetClass = methodCall.getReifiedMethod().getDeclaringClass();
00091 }
00092
00093 public ComponentRequestImpl(Request request) {
00094 super(request.getMethodCall(), request.getSender(), request.isOneWay(),
00095 request.getSequenceNumber());
00096 declaringClass = methodCall.getReifiedMethod().getDeclaringClass();
00097 targetClass = methodCall.getReifiedMethod().getDeclaringClass();
00098 }
00099
00105 protected FutureResult serveInternal(Body targetBody)
00106 throws ServeException {
00107 Object result = null;
00108 Throwable exception = null;
00109
00110 if (logger.isDebugEnabled()) {
00111 try {
00112 logger.debug("invocation on method [" + methodCall.getName() +
00113 "] of interface [" +
00114 methodCall.getComponentMetadata().getComponentInterfaceName() +
00115 "] on component : [" +
00116 ((ComponentParametersController) ((ComponentBodyImpl) targetBody)
00117 .getProActiveComponentImpl().getFcInterface(Constants.COMPONENT_PARAMETERS_CONTROLLER)).getComponentParameters()
00118 .getName() + "]");
00119 } catch (NoSuchInterfaceException e) {
00120 e.printStackTrace();
00121 }
00122 }
00123
00124 try {
00125 if (isControllerRequest()) {
00126
00127 result = ((ProActiveComponentImpl) ((ComponentBodyImpl) targetBody)
00128 .getProActiveComponentImpl()).getControllerRequestHandler()
00129 .handleRequest(this);
00130
00131 } else {
00132
00133 if (((ComponentBodyImpl) targetBody).getProActiveComponentImpl() != null) {
00134 interceptBeforeInvocation(targetBody);
00135
00136 String hierarchical_type = Fractive.getComponentParametersController(((ComponentBodyImpl) targetBody).getProActiveComponentImpl())
00137 .getComponentParameters()
00138 .getHierarchicalType();
00139
00140
00141 ProActiveInterface itf = (ProActiveInterface)((ComponentBody)targetBody).getProActiveComponentImpl().getFcInterface(methodCall.getComponentMetadata().getComponentInterfaceName());
00142 ProActiveInterfaceType itfType = (ProActiveInterfaceType)itf.getFcItfType();
00143 if (itfType.isFcGathercastItf() && (!getMethodCall().getComponentMetadata().getSenderItfID().equals(new ItfID(itfType.getFcItfName(), targetBody.getID())))) {
00144
00145 result = Fractive.getGathercastController(((ComponentBodyImpl) targetBody).getProActiveComponentImpl()).handleRequestOnGatherItf(this);
00146
00147 }
00148
00149
00150 else if (hierarchical_type.equals(Constants.COMPOSITE)) {
00151
00152 try {
00153 if (getShortcut() != null) {
00154
00155 }
00156
00157 result = methodCall.execute((ProActiveInterface) (((ComponentBodyImpl) targetBody)
00158 .getProActiveComponentImpl())
00159 .getFcInterface(methodCall.getComponentMetadata().getComponentInterfaceName()));
00160 } catch (IllegalArgumentException e) {
00161 throw new ServeException("could not reify method call : ",
00162 e);
00163 } catch (Throwable e) {
00164 e.printStackTrace();
00165 throw new ServeException("could not reify method call : ",
00166 e);
00167 }
00168 } else {
00169
00170
00171 if (logger.isDebugEnabled()) {
00172 if (getShortcutLength() > 0) {
00173 logger.debug("request has crossed " +
00174 (getShortcutLength() - 1) +
00175 " membranes before reaching a primitive component");
00176 }
00177 }
00178 result = methodCall.execute(targetBody.getReifiedObject());
00179 }
00180 interceptAfterInvocation(targetBody);
00181 } else {
00182 throw new ServeException(
00183 "trying to execute a component method on an object that is not a component");
00184 }
00185 }
00186 } catch (NoSuchInterfaceException nsie) {
00187 throw new ServeException("cannot serve request : problem accessing a component controller",
00188 nsie);
00189 } catch (MethodCallExecutionFailedException e) {
00190 throw new ServeException("serve method " +
00191 methodCall.getReifiedMethod().toString() + " failed", e);
00192 } catch (java.lang.reflect.InvocationTargetException e) {
00193 exception = e.getTargetException();
00194 e.printStackTrace();
00195
00196 if (isOneWay) {
00197 throw new ServeException("serve method " +
00198 methodCall.getReifiedMethod().toString() + " failed",
00199 exception);
00200 }
00201 }
00202
00203
00204 return new FutureResult(result, exception, null);
00205 }
00206
00207
00208 private void interceptBeforeInvocation(Body targetBody) {
00209 if (methodCall.getReifiedMethod() != null) {
00210 List inputInterceptors = ((ComponentBodyImpl) targetBody).getProActiveComponentImpl()
00211 .getInputInterceptors();
00212 Iterator it = inputInterceptors.iterator();
00213 while (it.hasNext()) {
00214 try {
00215 InputInterceptor interceptor = (InputInterceptor) it.next();
00216 interceptor.beforeInputMethodInvocation(methodCall);
00217 } catch (NullPointerException e) {
00218 logger.error("could not intercept invocation : " +
00219 e.getMessage());
00220 }
00221 }
00222 }
00223 }
00224
00225
00226 private void interceptAfterInvocation(Body targetBody) {
00227 if (methodCall.getReifiedMethod() != null) {
00228 if (((ComponentBodyImpl) targetBody).getProActiveComponentImpl() != null) {
00229 List interceptors = ((ComponentBodyImpl) targetBody).getProActiveComponentImpl()
00230 .getInputInterceptors();
00231
00232
00233 ListIterator it = interceptors.listIterator();
00234
00235
00236 while (it.hasNext()) {
00237 it.next();
00238 }
00239 while (it.hasPrevious()) {
00240 ((InputInterceptor) it.previous()).afterInputMethodInvocation(methodCall);
00241 }
00242 }
00243 }
00244 }
00245
00246
00247
00248
00249 public boolean isControllerRequest() {
00250
00251 return ((methodCall.getComponentMetadata().getComponentInterfaceName() != null) &&
00252 (methodCall.getComponentMetadata().getComponentInterfaceName().endsWith("-controller") ||
00253 methodCall.getComponentMetadata().getComponentInterfaceName().equals("component")));
00254 }
00255
00256
00257
00258
00259 public boolean isStopFcRequest() {
00260 return (declaringClass.equals(LifeCycleController.class) &&
00261 "stopFc".equals(getMethodName()));
00262 }
00263
00264
00265
00266
00267 public boolean isStartFcRequest() {
00268 return (declaringClass.equals(LifeCycleController.class) &&
00269 "startFc".equals(getMethodName()));
00270 }
00271
00272 public void notifyReception(UniversalBody bodyReceiver)
00273 throws IOException {
00274 if (getShortcut() != null) {
00275 if (logger.isDebugEnabled()) {
00276 logger.debug("notifying reception of method " +
00277 methodCall.getName());
00278 }
00279 Shortcut shortcut = getShortcut();
00280 shortcut.updateDestination(bodyReceiver.getRemoteAdapter());
00281 shortcut.getSender().createShortcut(shortcut);
00282
00283 ((ComponentBodyImpl) bodyReceiver).keepShortcut(shortcut);
00284 }
00285 super.notifyReception(bodyReceiver);
00286 }
00287
00288 public void shortcutNotification(UniversalBody sender,
00289 UniversalBody intermediate) {
00290 methodCall.getComponentMetadata().shortcutNotification(sender, intermediate);
00291 }
00292
00293 public Shortcut getShortcut() {
00294 return methodCall.getComponentMetadata().getShortcut();
00295 }
00296
00297 public int getShortcutLength() {
00298 return ((getShortcut() == null) ? 0 : getShortcut().length());
00299 }
00300
00301
00302
00303
00304 public short getPriority() {
00305 return methodCall.getComponentMetadata().getPriority();
00306 }
00307
00308
00309
00310
00311 public Class getTargetClass() {
00312 return targetClass;
00313 }
00314 }