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