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 public void init(ServletConfig pvConfig) throws ServletException {
34 super.init(pvConfig);
35 Enumeration lvEnumeration = pvConfig.getInitParameterNames();
36 while (lvEnumeration.hasMoreElements()) {
37 String lvKey = (String) lvEnumeration.nextElement();
38 String lvValue = getInitParameter(lvKey);
39
40 try {
41 Object lvServiceObject = Util.createObject(lvValue);
42 addService(lvKey, lvServiceObject);
43 } catch (Exception e) {
44 throw new ServletException("Exception in init-method: " + e, e);
45 }
46 }
47 }
48
49 public void addService(String pvInterfaceName, Object pvServiceImpl) {
50 serviceContainer.addService(pvInterfaceName, pvServiceImpl);
51 }
52 public void addService(String pvServiceInterfaceName, String pvServiceImplName) {
53 serviceContainer.addService(pvServiceInterfaceName, pvServiceImplName);
54 }
55
56 public void removeService(String pvServiceInterfaceName) {
57 serviceContainer.removeService(pvServiceInterfaceName);
58 }
59 public Object getService(String pvServiceInterfaceName) {
60 return serviceContainer.getService(pvServiceInterfaceName);
61 }
62 public int getServiceSize() {
63 return serviceContainer.getServiceSize();
64 }
65
66
67 public Object getService(HttpServletRequest pvRequest, HttpServletResponse pvResponse) throws Exception {
68 String lvServiceAndMethodName[] = getServiceAndMethodName(pvRequest.getParameter(PARAM_CLASS), pvRequest.getParameter(PARAM_METHOD));
69 String lvServiceClass = lvServiceAndMethodName[0];
70 Object lvService = getService(lvServiceClass);
71 if (lvService == null) {
72 throw new InvocationException("For the service: " + lvServiceClass + " is no implementation by the HttpSerlet-servlet exist.");
73 }
74 return lvService;
75 }
76
77
78 protected void doGet(HttpServletRequest pvRequest, HttpServletResponse pvResponse) throws ServletException, IOException {
79 doPost(pvRequest, pvResponse);
80 }
81
82
83
84 protected void doPost(HttpServletRequest pvRequest, HttpServletResponse pvResponse) throws ServletException, IOException {
85 try {
86 Object lvService = getService(pvRequest, pvResponse);
87 Method lvMethod = getMethod(lvService, pvRequest.getParameterMap(), pvRequest.getParameter(PARAM_CLASS), pvRequest.getParameter(PARAM_METHOD));
88
89 Object lvParameters = Serializer.deserialize(pvRequest.getInputStream());
90 Object lvArgs[] = getArgs(lvParameters);
91
92 Object lvResult = null;
93 synchronized (lvService) {
94 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 Serializer.serialize(lvResult, pvResponse.getOutputStream());
102 } catch (Exception e) {
103 Serializer.serialize(e, pvResponse.getOutputStream());
104 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 public Method getMethod (Object pvService, Map pvParameterMap, String pvServiceClassName, String pvMethodName) throws Exception {
111
112 String lvServiceAndMethodName[] = getServiceAndMethodName(pvServiceClassName, pvMethodName);
113
114 String lvMethodName = lvServiceAndMethodName[1];
115
116 String[] lvParamTypes = (String[]) pvParameterMap.get(PARAM_TYPES);
117 Vector findParamVector = convertInParameterTypes(lvParamTypes);
118 Method lvMethod = Invoker.findMethod (pvService.getClass(), lvMethodName, findParamVector);
119 return lvMethod;
120 }
121
122 protected Object[] getArgs(Object pvParameters) {
123 Object lvParamsArray[] = null;
124 if (pvParameters == null) {
125 lvParamsArray = null;
126 }
127 else if (pvParameters.getClass().isArray()) {
128 lvParamsArray = (Object[]) pvParameters;
129 } else {
130 lvParamsArray = new Object[] { pvParameters };
131 }
132 return lvParamsArray;
133 }
134
135
136 protected String[] getServiceAndMethodName (String pvServiceClassName, String pvMethodName) throws Exception {
137 String lvServiceClass = pvServiceClassName;
138 String lvMethodName = pvMethodName;
139
140 if (lvMethodName == null) {
141 throw new InvocationException("Error in HTTP-Servlet: The method-name was null.");
142 }
143
144
145
146 if (lvServiceClass == null) {
147 int lvIndexPoint = lvMethodName.lastIndexOf(".");
148 lvServiceClass = lvMethodName.substring(0, lvIndexPoint);
149 lvMethodName = lvMethodName.substring(lvIndexPoint + 1);
150 }
151
152 return new String[] {lvServiceClass, lvMethodName};
153 }
154
155
156 protected Vector convertInParameterTypes(String pvParamTypes[]) {
157 if ((pvParamTypes == null) || (pvParamTypes.length == 0)) {
158 return null;
159 } else {
160 String lvParamTypes = pvParamTypes[0];
161 String lvParamtypesArray[] = lvParamTypes.split(",");
162 Vector v = new Vector(lvParamtypesArray.length);
163 for (int i = 0; i < lvParamtypesArray.length; i++) {
164 try {
165 Object lvValue = Converter.convertClassString2Object(lvParamtypesArray[i]);
166 if (lvValue == null) { return null; }
167 v.add(lvValue);
168 } catch (Exception e) {
169 if (ServiceManager.DEBUG_MODE_ON) {
170 e.printStackTrace();
171 }
172 }
173 }
174 return v;
175 }
176 }
177
178 }