|
1 |
| package net.sf.crispy.impl.http; |
|
2 |
| |
|
3 |
| import java.io.IOException; |
|
4 |
| import java.lang.reflect.Method; |
|
5 |
| import java.util.Enumeration; |
|
6 |
| import java.util.Map; |
|
7 |
| import java.util.Vector; |
|
8 |
| |
|
9 |
| import javax.servlet.ServletConfig; |
|
10 |
| import javax.servlet.ServletException; |
|
11 |
| import javax.servlet.http.HttpServletRequest; |
|
12 |
| import javax.servlet.http.HttpServletResponse; |
|
13 |
| |
|
14 |
| import net.sf.crispy.InvocationException; |
|
15 |
| import net.sf.crispy.impl.ServiceManager; |
|
16 |
| import net.sf.crispy.server.HttpServiceEndpoint; |
|
17 |
| import net.sf.crispy.server.SingleServiceContainer; |
|
18 |
| import net.sf.crispy.server.SingleServiceContainerImpl; |
|
19 |
| import net.sf.crispy.util.Converter; |
|
20 |
| import net.sf.crispy.util.Invoker; |
|
21 |
| import net.sf.crispy.util.Util; |
|
22 |
| |
|
23 |
| import org.apache.commons.logging.Log; |
|
24 |
| import org.apache.commons.logging.LogFactory; |
|
25 |
| |
|
26 |
| public class HttpServlet extends HttpServiceEndpoint implements Constant, SingleServiceContainer { |
|
27 |
| |
|
28 |
| private static final long serialVersionUID = 5549791265449344427L; |
|
29 |
| protected static final Log log = LogFactory.getLog (HttpServlet.class); |
|
30 |
| |
|
31 |
| private SingleServiceContainer serviceContainer = new SingleServiceContainerImpl(); |
|
32 |
| |
|
33 |
4
| public void init(ServletConfig pvConfig) throws ServletException {
|
|
34 |
4
| super.init(pvConfig);
|
|
35 |
4
| Enumeration lvEnumeration = pvConfig.getInitParameterNames();
|
|
36 |
4
| while (lvEnumeration.hasMoreElements()) {
|
|
37 |
10
| String lvKey = (String) lvEnumeration.nextElement();
|
|
38 |
10
| String lvValue = getInitParameter(lvKey);
|
|
39 |
| |
|
40 |
10
| try {
|
|
41 |
10
| Object lvServiceObject = Util.createObject(lvValue);
|
|
42 |
9
| addService(lvKey, lvServiceObject);
|
|
43 |
| } catch (Exception e) { |
|
44 |
1
| throw new ServletException("Exception in init-method: " + e, e);
|
|
45 |
| } |
|
46 |
| } |
|
47 |
| } |
|
48 |
| |
|
49 |
61
| public void addService(String pvInterfaceName, Object pvServiceImpl) {
|
|
50 |
61
| serviceContainer.addService(pvInterfaceName, pvServiceImpl);
|
|
51 |
| } |
|
52 |
1
| public void addService(String pvServiceInterfaceName, String pvServiceImplName) {
|
|
53 |
1
| serviceContainer.addService(pvServiceInterfaceName, pvServiceImplName);
|
|
54 |
| } |
|
55 |
| |
|
56 |
1
| public void removeService(String pvServiceInterfaceName) {
|
|
57 |
1
| serviceContainer.removeService(pvServiceInterfaceName);
|
|
58 |
| } |
|
59 |
120
| public Object getService(String pvServiceInterfaceName) {
|
|
60 |
120
| return serviceContainer.getService(pvServiceInterfaceName);
|
|
61 |
| } |
|
62 |
0
| public int getServiceSize() {
|
|
63 |
0
| return serviceContainer.getServiceSize();
|
|
64 |
| } |
|
65 |
| |
|
66 |
| |
|
67 |
105
| public Object getService(HttpServletRequest pvRequest, HttpServletResponse pvResponse) throws Exception {
|
|
68 |
105
| String lvServiceAndMethodName[] = getServiceAndMethodName(pvRequest.getParameter(PARAM_CLASS), pvRequest.getParameter(PARAM_METHOD));
|
|
69 |
105
| String lvServiceClass = lvServiceAndMethodName[0];
|
|
70 |
105
| Object lvService = getService(lvServiceClass);
|
|
71 |
105
| if (lvService == null) {
|
|
72 |
0
| throw new InvocationException("For the service: " + lvServiceClass + " is no implementation by the HttpSerlet-servlet exist.");
|
|
73 |
| } |
|
74 |
105
| return lvService;
|
|
75 |
| } |
|
76 |
| |
|
77 |
| |
|
78 |
0
| protected void doGet(HttpServletRequest pvRequest, HttpServletResponse pvResponse) throws ServletException, IOException {
|
|
79 |
0
| doPost(pvRequest, pvResponse);
|
|
80 |
| } |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
58
| protected void doPost(HttpServletRequest pvRequest, HttpServletResponse pvResponse) throws ServletException, IOException {
|
|
85 |
58
| try {
|
|
86 |
58
| Object lvService = getService(pvRequest, pvResponse);
|
|
87 |
58
| Method lvMethod = getMethod(lvService, pvRequest.getParameterMap(), pvRequest.getParameter(PARAM_CLASS), pvRequest.getParameter(PARAM_METHOD));
|
|
88 |
| |
|
89 |
58
| Object lvParameters = Serializer.deserialize(pvRequest.getInputStream());
|
|
90 |
58
| Object lvArgs[] = getArgs(lvParameters);
|
|
91 |
| |
|
92 |
58
| Object lvResult = null;
|
|
93 |
58
| synchronized (lvService) {
|
|
94 |
58
| lvResult = doInvoke(lvService, lvMethod, lvArgs, createNewInterceptorHandlerInstance());
|
|
95 |
| } |
|
96 |
| |
|
97 |
| if (log.isDebugEnabled()) { |
|
98 |
| log.debug("Result from method " + pvRequest.getParameter(PARAM_METHOD) + " is: " + lvResult); |
|
99 |
| } |
|
100 |
| |
|
101 |
58
| Serializer.serialize(lvResult, pvResponse.getOutputStream());
|
|
102 |
| } catch (Exception e) { |
|
103 |
0
| Serializer.serialize(e, pvResponse.getOutputStream());
|
|
104 |
0
| throw new ServletException("Error by call Crispy-HttpServlet: " + e, e);
|
|
105 |
| } finally { |
|
106 |
| if (log.isDebugEnabled()) { log.debug("Call method " + pvRequest.getParameter(PARAM_METHOD)); } |
|
107 |
| } |
|
108 |
| } |
|
109 |
| |
|
110 |
58
| public Method getMethod (Object pvService, Map pvParameterMap, String pvServiceClassName, String pvMethodName) throws Exception {
|
|
111 |
| |
|
112 |
58
| String lvServiceAndMethodName[] = getServiceAndMethodName(pvServiceClassName, pvMethodName);
|
|
113 |
| |
|
114 |
58
| String lvMethodName = lvServiceAndMethodName[1];
|
|
115 |
| |
|
116 |
58
| String[] lvParamTypes = (String[]) pvParameterMap.get(PARAM_TYPES);
|
|
117 |
58
| Vector findParamVector = convertInParameterTypes(lvParamTypes);
|
|
118 |
58
| Method lvMethod = Invoker.findMethod (pvService.getClass(), lvMethodName, findParamVector);
|
|
119 |
58
| return lvMethod;
|
|
120 |
| } |
|
121 |
| |
|
122 |
58
| protected Object[] getArgs(Object pvParameters) {
|
|
123 |
58
| Object lvParamsArray[] = null;
|
|
124 |
58
| if (pvParameters == null) {
|
|
125 |
3
| lvParamsArray = null;
|
|
126 |
| } |
|
127 |
55
| else if (pvParameters.getClass().isArray()) {
|
|
128 |
55
| lvParamsArray = (Object[]) pvParameters;
|
|
129 |
| } else { |
|
130 |
0
| lvParamsArray = new Object[] { pvParameters };
|
|
131 |
| } |
|
132 |
58
| return lvParamsArray;
|
|
133 |
| } |
|
134 |
| |
|
135 |
| |
|
136 |
283
| protected String[] getServiceAndMethodName (String pvServiceClassName, String pvMethodName) throws Exception {
|
|
137 |
283
| String lvServiceClass = pvServiceClassName;
|
|
138 |
283
| String lvMethodName = pvMethodName;
|
|
139 |
| |
|
140 |
283
| if (lvMethodName == null) {
|
|
141 |
1
| throw new InvocationException("Error in HTTP-Servlet: The method-name was null.");
|
|
142 |
| } |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
282
| if (lvServiceClass == null) {
|
|
147 |
7
| int lvIndexPoint = lvMethodName.lastIndexOf(".");
|
|
148 |
7
| lvServiceClass = lvMethodName.substring(0, lvIndexPoint);
|
|
149 |
6
| lvMethodName = lvMethodName.substring(lvIndexPoint + 1);
|
|
150 |
| } |
|
151 |
| |
|
152 |
281
| return new String[] {lvServiceClass, lvMethodName};
|
|
153 |
| } |
|
154 |
| |
|
155 |
| |
|
156 |
117
| protected Vector convertInParameterTypes(String pvParamTypes[]) {
|
|
157 |
117
| if ((pvParamTypes == null) || (pvParamTypes.length == 0)) {
|
|
158 |
18
| return null;
|
|
159 |
| } else { |
|
160 |
99
| String lvParamTypes = pvParamTypes[0];
|
|
161 |
99
| String lvParamtypesArray[] = lvParamTypes.split(",");
|
|
162 |
99
| Vector v = new Vector(lvParamtypesArray.length);
|
|
163 |
99
| for (int i = 0; i < lvParamtypesArray.length; i++) {
|
|
164 |
137
| try {
|
|
165 |
137
| Object lvValue = Converter.convertClassString2Object(lvParamtypesArray[i]);
|
|
166 |
1
| if (lvValue == null) { return null; }
|
|
167 |
136
| v.add(lvValue);
|
|
168 |
| } catch (Exception e) { |
|
169 |
| if (ServiceManager.DEBUG_MODE_ON) { |
|
170 |
| e.printStackTrace(); |
|
171 |
| } |
|
172 |
| } |
|
173 |
| } |
|
174 |
98
| return v;
|
|
175 |
| } |
|
176 |
| } |
|
177 |
| |
|
178 |
| } |