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 public BurlapInvoker(Object service, Class pvServiceClass) {
19 super(pvServiceClass);
20 serviceObject = service;
21 }
22
23 public void setWithConverter(boolean pvWithConverter) {
24 withConverter = pvWithConverter;
25 }
26
27 public boolean getWithConverter() {
28 return withConverter;
29 }
30
31
32 public void invoke(BurlapInput in, BurlapOutput out) throws Throwable
33 {
34 in.startCall();
35
36 ServiceContext context = ServiceContext.getContext();
37
38 String header;
39 while ((header = in.readHeader()) != null) {
40 Object value = in.readObject();
41
42 context.addHeader(header, value);
43 }
44
45 String methodName = in.getMethod();
46
47 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 Class []args = method.getParameterTypes();
77 Object []values = new Object[args.length];
78
79 for (int i = 0; i < args.length; i++)
80 values[i] = in.readObject(args[i]);
81
82 in.completeCall();
83
84 Object result = null;
85
86 try {
87 result = method.invoke(serviceObject, values);
88 if (getWithConverter()) {
89 result = new Converter().makeSimple(result);
90 }
91
92 } catch (Exception e) {
93 Throwable lvThrowable = e;
94 if (lvThrowable instanceof InvocationTargetException) {
95 lvThrowable = ((InvocationTargetException) lvThrowable).getTargetException();
96 }
97 writeFault("ServiceException: ", lvThrowable, out);
98 return;
99 }
100
101 out.startReply();
102 out.writeObject(result);
103 out.completeReply();
104 }
105
106 public void writeFault(String pvMessage, Throwable t, BurlapOutput out) throws Exception {
107 out.startReply();
108 out.writeFault(pvMessage, (t == null ? "NoMessage" : t.getMessage()), t);
109 out.completeReply();
110 }
111
112 }