View Javadoc

1   package org.codehaus.xfire.plexus;
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.plexus.component.repository.exception.ComponentLookupException;
9   import org.codehaus.plexus.personality.plexus.lifecycle.phase.ServiceLocator;
10  import org.codehaus.xfire.MessageContext;
11  import org.codehaus.xfire.XFireRuntimeException;
12  import org.codehaus.xfire.fault.XFireFault;
13  import org.codehaus.xfire.service.object.ObjectInvoker;
14  
15  /***
16   * Invokes a Plexus service.
17   * 
18   * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
19   * @since Nov 18, 2004
20   */
21  public class ServiceInvoker
22  	extends ObjectInvoker
23  {
24      private static Log logger = LogFactory.getLog(ServiceInvoker.class.getName());
25      
26      private ServiceLocator locator;
27      
28      private boolean hasService = true;
29      private boolean hasChecked = false;
30      
31      public ServiceInvoker( ServiceLocator locator )
32      {
33          this.locator = locator;
34      }
35      
36      public Object invoke(Method method, Object[] args, MessageContext context)
37          throws XFireFault
38      {
39          if ( !hasChecked )
40          {
41              if ( locator.hasComponent(method.getDeclaringClass().getName(),
42                                        context.getServiceName()) )
43              {
44                  hasService = true;
45              }
46              else
47              {
48                  hasService = false;
49              }
50          }
51  
52          if ( hasService )
53          {
54              try
55              {
56  	            Object component = locator.lookup(
57  	                    method.getDeclaringClass().getName(),
58  	                    context.getServiceName());
59  	            
60  	            return method.invoke(component, args);
61              }
62              catch (IllegalArgumentException e)
63              {
64                  throw new XFireFault( "Illegal argument.", e, XFireFault.SENDER );
65              }
66              catch (InvocationTargetException e)
67              {
68                  Throwable t = e.getTargetException();
69                  t.getMessage();
70                  
71                  if ( t instanceof XFireFault )
72                  {
73                      throw (XFireFault) t;
74                  }
75                  else if ( t instanceof Exception )
76                  {
77                      logger.warn("Error invoking service.", t);
78                      throw new XFireFault( (Exception) t, XFireFault.SENDER );
79                  }
80                  else
81                  {
82                      logger.warn("Error invoking service.", e);
83                      throw new XFireRuntimeException( "Error invoking service.", e );
84                  }
85              }
86              catch (IllegalAccessException e)
87              {
88                  throw new XFireFault( "Couldn't access service object.", e, XFireFault.RECEIVER );
89              } 
90              catch (ComponentLookupException e)
91              {
92                  throw new XFireFault( "Couldn't find plexus service.", e, XFireFault.RECEIVER );
93              }
94          }
95          else
96          {
97              return super.invoke(method, args, context);
98          }
99      }
100 
101 }