1 package org.codehaus.xfire.java;
2
3 import java.lang.reflect.InvocationTargetException;
4 import java.lang.reflect.Method;
5 import java.util.List;
6
7 import javax.xml.namespace.QName;
8 import javax.xml.stream.XMLStreamReader;
9
10 import org.apache.log4j.Logger;
11 import org.codehaus.xfire.MessageContext;
12 import org.codehaus.xfire.XFireRuntimeException;
13 import org.codehaus.xfire.fault.XFireFault;
14 import org.codehaus.xfire.handler.AbstractHandler;
15 import org.codehaus.xfire.java.message.MessageBridge;
16 import org.codehaus.xfire.java.message.MessageBridgeFactory;
17
18 /***
19 * Handles java services.
20 *
21 * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
22 * @since Feb 18, 2004
23 */
24 public class JavaServiceHandler
25 extends AbstractHandler
26 {
27 private static Logger logger = Logger.getLogger(JavaServiceHandler.class.getName());
28
29 public static final String NAME = "java";
30
31 /***
32 * @see org.codehaus.xfire.handler.Handler#invoke(org.codehaus.xfire.MessageContext)
33 */
34 public void invoke( MessageContext context,
35 XMLStreamReader reader )
36 throws XFireFault
37 {
38 JavaService service = (JavaService) context.getService();
39
40 MessageBridge pipe = MessageBridgeFactory.createMessageBridge( service,
41 context,
42 reader,
43 getXMLStreamWriter(context) );
44
45 try
46 {
47
48 List params = pipe.read();
49
50
51
52 Operation operation = pipe.getOperation();
53
54
55 Object value = invokeService( operation, params, service, context );
56
57 pipe.write( value );
58 }
59 catch (XFireRuntimeException e)
60 {
61 logger.warn("Error invoking service.", e);
62 throw new XFireFault("Error invoking service.", e, XFireFault.SENDER);
63 }
64 }
65
66 /***
67 * @param methodName
68 * @param params
69 */
70 private Object invokeService( Operation operation,
71 List params,
72 JavaService service,
73 MessageContext context ) throws XFireFault
74 {
75 Method method = operation.getMethod();
76
77 try
78 {
79 Object serviceObject = service.getServiceObject( context );
80
81 return method.invoke( serviceObject, params.toArray() );
82 }
83 catch (IllegalArgumentException e)
84 {
85 throw new XFireFault( "Illegal argument.", e, XFireFault.SENDER );
86 }
87 catch (InvocationTargetException e)
88 {
89 Throwable t = e.getTargetException();
90 t.getMessage();
91
92 if ( t instanceof XFireFault )
93 {
94 throw (XFireFault) t;
95 }
96 else if ( t instanceof Exception )
97 {
98 logger.warn("Error invoking service.", t);
99 throw new XFireFault( (Exception) t, XFireFault.SENDER );
100 }
101 else
102 {
103 logger.warn("Error invoking service.", e);
104 throw new XFireRuntimeException( "Error invoking service.", e );
105 }
106 }
107 catch (IllegalAccessException e)
108 {
109 throw new XFireFault( "Couldn't access service object.", e, XFireFault.RECEIVER );
110 }
111 }
112
113 /***
114 * @see org.codehaus.xfire.handler.dom.DOMHandler#getUnderstoodHeaders()
115 */
116 public QName[] getUnderstoodHeaders()
117 {
118 return null;
119 }
120 }