View Javadoc

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  //	    System.out.println("BURLAP-METHOD-Name: " + methodName);
49  //	    System.out.println("BURLAP-METHOD: " + method);
50  //	    if ((method != null) && ( "_burlap_getAttribute".equals(in.getMethod()))) {
51  //	      String attrName = in.readString();
52  //	      in.completeCall();
53  //
54  //	      String value = null;
55  //
56  //	      if ("java.api.class".equals(attrName))
57  //	    	  value = getAPIClassName();
58  //	      else if ("java.home.class".equals(attrName))
59  //	    	  value = getHomeClassName();
60  //	      else if ("java.object.class".equals(attrName))
61  //	    	  value = getObjectClassName();
62  //
63  //	      out.startReply();
64  //
65  //	      out.writeObject(value);
66  //
67  //	      out.completeReply();
68  //	      System.err.println("BurlapInvoker with method: " + methodName);
69  //	      return;
70  //	    }
71  //	    else if (method == null) {
72  //	    	writeFault("The service has no method named: " + in.getMethod(), null, out);
73  //	    	return;
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 }