|
1 |
| package net.sf.crispy.impl.caucho; |
|
2 |
| |
|
3 |
| import java.lang.reflect.InvocationTargetException; |
|
4 |
| import java.lang.reflect.Method; |
|
5 |
| |
|
6 |
| import net.sf.crispy.util.Converter; |
|
7 |
| |
|
8 |
| import com.caucho.burlap.io.BurlapInput; |
|
9 |
| import com.caucho.burlap.io.BurlapOutput; |
|
10 |
| import com.caucho.services.server.AbstractSkeleton; |
|
11 |
| import com.caucho.services.server.ServiceContext; |
|
12 |
| |
|
13 |
| public class BurlapInvoker extends AbstractSkeleton { |
|
14 |
| |
|
15 |
| private Object serviceObject = null; |
|
16 |
| private boolean withConverter = false; |
|
17 |
| |
|
18 |
4
| public BurlapInvoker(Object service, Class pvServiceClass) {
|
|
19 |
4
| super(pvServiceClass);
|
|
20 |
4
| serviceObject = service;
|
|
21 |
| } |
|
22 |
| |
|
23 |
4
| public void setWithConverter(boolean pvWithConverter) {
|
|
24 |
4
| withConverter = pvWithConverter;
|
|
25 |
| } |
|
26 |
| |
|
27 |
3
| public boolean getWithConverter() {
|
|
28 |
3
| return withConverter;
|
|
29 |
| } |
|
30 |
| |
|
31 |
| |
|
32 |
5
| public void invoke(BurlapInput in, BurlapOutput out) throws Throwable
|
|
33 |
| { |
|
34 |
5
| in.startCall();
|
|
35 |
| |
|
36 |
3
| ServiceContext context = ServiceContext.getContext();
|
|
37 |
| |
|
38 |
3
| String header;
|
|
39 |
?
| while ((header = in.readHeader()) != null) {
|
|
40 |
0
| Object value = in.readObject();
|
|
41 |
| |
|
42 |
0
| context.addHeader(header, value);
|
|
43 |
| } |
|
44 |
| |
|
45 |
3
| String methodName = in.getMethod();
|
|
46 |
| |
|
47 |
3
| Method method = getMethod(methodName);
|
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
3
| Class []args = method.getParameterTypes();
|
|
77 |
3
| Object []values = new Object[args.length];
|
|
78 |
| |
|
79 |
3
| for (int i = 0; i < args.length; i++)
|
|
80 |
4
| values[i] = in.readObject(args[i]);
|
|
81 |
| |
|
82 |
3
| in.completeCall();
|
|
83 |
| |
|
84 |
3
| Object result = null;
|
|
85 |
| |
|
86 |
3
| try {
|
|
87 |
3
| result = method.invoke(serviceObject, values);
|
|
88 |
2
| if (getWithConverter()) {
|
|
89 |
2
| result = new Converter().makeSimple(result);
|
|
90 |
| } |
|
91 |
| |
|
92 |
| } catch (Exception e) { |
|
93 |
1
| Throwable lvThrowable = e;
|
|
94 |
1
| if (lvThrowable instanceof InvocationTargetException) {
|
|
95 |
1
| lvThrowable = ((InvocationTargetException) lvThrowable).getTargetException();
|
|
96 |
| } |
|
97 |
1
| writeFault("ServiceException: ", lvThrowable, out);
|
|
98 |
1
| return;
|
|
99 |
| } |
|
100 |
| |
|
101 |
2
| out.startReply();
|
|
102 |
2
| out.writeObject(result);
|
|
103 |
2
| out.completeReply();
|
|
104 |
| } |
|
105 |
| |
|
106 |
2
| public void writeFault(String pvMessage, Throwable t, BurlapOutput out) throws Exception {
|
|
107 |
2
| out.startReply();
|
|
108 |
2
| out.writeFault(pvMessage, (t == null ? "NoMessage" : t.getMessage()), t);
|
|
109 |
2
| out.completeReply();
|
|
110 |
| } |
|
111 |
| |
|
112 |
| } |