KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > tools > remote > caucho > CauchoServlet


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.tools.remote.caucho;
10
11 import java.io.BufferedInputStream JavaDoc;
12 import java.io.BufferedOutputStream JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.io.OutputStream JavaDoc;
16 import java.lang.reflect.InvocationTargetException JavaDoc;
17 import java.lang.reflect.Method JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import javax.servlet.ServletException JavaDoc;
22 import javax.servlet.http.HttpServlet JavaDoc;
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
25
26 import mx4j.tools.remote.http.HTTPConnection;
27
28 /**
29  * @version $Revision: 1.4 $
30  */

31 public abstract class CauchoServlet extends HttpServlet JavaDoc
32 {
33    private Map JavaDoc methods;
34
35    public void init() throws ServletException JavaDoc
36    {
37       methods = new HashMap JavaDoc();
38       mapMethods(HTTPConnection.class, methods);
39    }
40
41    protected void mapMethods(Class JavaDoc cls, Map JavaDoc methods)
42    {
43       Method JavaDoc[] mthds = cls.getMethods();
44       for (int i = 0; i < mthds.length; ++i)
45       {
46          Method JavaDoc mthd = mthds[i];
47          String JavaDoc key = mangleMethodName(mthd);
48          methods.put(key, mthd);
49       }
50    }
51
52    protected Method JavaDoc findMethod(String JavaDoc methodName)
53    {
54       return (Method JavaDoc) methods.get(methodName);
55    }
56
57    protected String JavaDoc mangleMethodName(Method JavaDoc method)
58    {
59       return CauchoService.mangleMethodName(method);
60    }
61
62    protected void service(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc
63    {
64       if (!"POST".equalsIgnoreCase(request.getMethod())) throw new ServletException JavaDoc("Caucho protocol requires POST");
65
66       BufferedInputStream JavaDoc is = new BufferedInputStream JavaDoc(request.getInputStream(), 48);
67       CauchoInput input = createCauchoInput(is);
68       BufferedOutputStream JavaDoc bos = new BufferedOutputStream JavaDoc(response.getOutputStream(), 48);
69       CauchoOutput output = createCauchoOutput(bos);
70
71       invoke(request, input, output);
72
73       bos.flush();
74    }
75
76    protected abstract CauchoInput createCauchoInput(InputStream JavaDoc stream);
77
78    protected abstract CauchoOutput createCauchoOutput(OutputStream JavaDoc stream);
79
80    protected abstract Object JavaDoc getService();
81
82    protected void invoke(HttpServletRequest JavaDoc request, CauchoInput input, CauchoOutput output) throws IOException JavaDoc
83    {
84       input.startCall();
85       Map JavaDoc headers = readHeaders(input);
86       String JavaDoc methodName = input.readMethod();
87       Method JavaDoc method = findMethod(methodName);
88       if (method == null)
89       {
90          output.startReply();
91          NoSuchMethodException JavaDoc x = new NoSuchMethodException JavaDoc(methodName);
92          output.writeFault(x);
93          output.completeReply();
94       } else
95       {
96          Object JavaDoc[] values = readArguments(input, method);
97          input.completeCall();
98
99          Object JavaDoc result = null;
100          try
101          {
102             result = invoke(request.getRequestURL().toString(), getService(), method, headers, values);
103          } catch (Throwable JavaDoc x)
104          {
105             output.startReply();
106             output.writeFault(x);
107             output.completeReply();
108             return;
109          }
110          output.startReply();
111          output.writeObject(result);
112          output.completeReply();
113       }
114    }
115
116    protected Map JavaDoc readHeaders(CauchoInput input) throws IOException JavaDoc
117    {
118       Map JavaDoc headers = new HashMap JavaDoc();
119       String JavaDoc header = null;
120       while ((header = input.readHeader()) != null) headers.put(header, input.readObject(null));
121       return headers;
122    }
123
124    protected Object JavaDoc[] readArguments(CauchoInput input, Method JavaDoc method) throws IOException JavaDoc
125    {
126       Class JavaDoc[] types = method.getParameterTypes();
127       Object JavaDoc[] values = new Object JavaDoc[types.length];
128       for (int i = 0; i < types.length; ++i) values[i] = input.readObject(types[i]);
129       return values;
130    }
131
132    protected Object JavaDoc invoke(String JavaDoc url, Object JavaDoc target, Method JavaDoc method, Map JavaDoc headers, Object JavaDoc[] values) throws Exception JavaDoc
133    {
134       if (target == null) throw new IOException JavaDoc("Service is not available");
135       String JavaDoc connectionId = (String JavaDoc) headers.get(CauchoService.CONNECTION_ID_HEADER_NAME);
136       CauchoService.setConnectionContext(url, connectionId);
137       try
138       {
139          return method.invoke(target, values);
140       } catch (InvocationTargetException JavaDoc x)
141       {
142          Throwable JavaDoc t = x.getTargetException();
143          if (t instanceof Exception JavaDoc) throw (Exception JavaDoc) t;
144          throw (Error JavaDoc) t;
145       } finally
146       {
147          CauchoService.resetConnectionContext();
148       }
149    }
150 }
151
Popular Tags