View Javadoc

    1   package org.codehaus.xfire.picocontainer.util;
    2
    3   import java.lang.reflect.InvocationTargetException;
    4   import java.lang.reflect.Method;
    5
    6   import org.apache.commons.logging.Log;
    7   import org.apache.commons.logging.LogFactory;
    8   import org.codehaus.xfire.MessageContext;
    9   import org.codehaus.xfire.XFireRuntimeException;
    10  import org.codehaus.xfire.fault.XFireFault;
    11  import org.codehaus.xfire.service.binding.Invoker;
    12  import org.picocontainer.PicoContainer;
    13  import org.picocontainer.defaults.ObjectReference;
    14
    15  public class PicoObjectInvoker
    16      implements Invoker
    17  {
    18
    19      private static final Log logger = LogFactory.getLog(Invoker.class.getName());
    20
    21      private ObjectReference picoReference;
    22
    23      private Object componentKey;
    24
    25      public PicoObjectInvoker(ObjectReference picoReference, Object componentKey)
    26      {
    27          this.picoReference = picoReference;
    28          this.componentKey = componentKey;
    29      }
    30
    31      public Object invoke(final Method method, final Object[] params, final MessageContext context)
    32          throws XFireFault
    33      {
    34          try
    35          {
    36              PicoContainer pico = (PicoContainer) picoReference.get();
    37              final Object serviceObject = pico.getComponentInstance(componentKey);
    38
    39              return method.invoke(serviceObject, params);
    40          }
    41          catch (IllegalArgumentException e)
    42          {
    43              throw new XFireFault("Illegal argument.", e, XFireFault.SENDER);
    44          }
    45          catch (InvocationTargetException e)
    46          {
    47              final Throwable t = e.getTargetException();
    48
    49              if (t instanceof XFireFault)
    50              {
    51                  throw (XFireFault) t;
    52              }
    53              else if (t instanceof Exception)
    54              {
    55                  logger.warn("Error invoking service.", t);
    56                  throw new XFireFault(t, XFireFault.SENDER);
    57              }
    58              else
    59              {
    60                  logger.warn("Error invoking service.", e);
    61                  throw new XFireRuntimeException("Error invoking service.", e);
    62              }
    63          }
    64          catch (IllegalAccessException e)
    65          {
    66              throw new XFireFault("Couldn't access service object.", e, XFireFault.RECEIVER);
    67          }
    68      }
    69  }