001: package net.sf.crispy.impl.http;
002:
003: import java.io.IOException;
004: import java.lang.reflect.Method;
005: import java.util.Enumeration;
006: import java.util.Map;
007: import java.util.Vector;
008:
009: import javax.servlet.ServletConfig;
010: import javax.servlet.ServletException;
011: import javax.servlet.http.HttpServletRequest;
012: import javax.servlet.http.HttpServletResponse;
013:
014: import net.sf.crispy.InvocationException;
015: import net.sf.crispy.impl.ServiceManager;
016: import net.sf.crispy.server.HttpServiceEndpoint;
017: import net.sf.crispy.server.SingleServiceContainer;
018: import net.sf.crispy.server.SingleServiceContainerImpl;
019: import net.sf.crispy.util.Converter;
020: import net.sf.crispy.util.Invoker;
021: import net.sf.crispy.util.Util;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025:
026: public class HttpServlet extends HttpServiceEndpoint implements
027: Constant, SingleServiceContainer {
028:
029: private static final long serialVersionUID = 5549791265449344427L;
030: protected static final Log log = LogFactory
031: .getLog(HttpServlet.class);
032:
033: private SingleServiceContainer serviceContainer = new SingleServiceContainerImpl();
034:
035: public void init(ServletConfig pvConfig) throws ServletException {
036: super .init(pvConfig);
037: Enumeration lvEnumeration = pvConfig.getInitParameterNames();
038: while (lvEnumeration.hasMoreElements()) {
039: String lvKey = (String) lvEnumeration.nextElement();
040: String lvValue = getInitParameter(lvKey);
041: // System.out.println("---> " + lvKey + " - " + lvValue);
042: try {
043: Object lvServiceObject = Util.createObject(lvValue);
044: addService(lvKey, lvServiceObject);
045: } catch (Exception e) {
046: throw new ServletException("Exception in init-method: "
047: + e, e);
048: }
049: }
050: }
051:
052: public void addService(String pvInterfaceName, Object pvServiceImpl) {
053: serviceContainer.addService(pvInterfaceName, pvServiceImpl);
054: }
055:
056: public void addService(String pvServiceInterfaceName,
057: String pvServiceImplName) {
058: serviceContainer.addService(pvServiceInterfaceName,
059: pvServiceImplName);
060: }
061:
062: public void removeService(String pvServiceInterfaceName) {
063: serviceContainer.removeService(pvServiceInterfaceName);
064: }
065:
066: public Object getService(String pvServiceInterfaceName) {
067: return serviceContainer.getService(pvServiceInterfaceName);
068: }
069:
070: public int getServiceSize() {
071: return serviceContainer.getServiceSize();
072: }
073:
074: public Object getService(HttpServletRequest pvRequest,
075: HttpServletResponse pvResponse) throws Exception {
076: String lvServiceAndMethodName[] = getServiceAndMethodName(
077: pvRequest.getParameter(PARAM_CLASS), pvRequest
078: .getParameter(PARAM_METHOD));
079: String lvServiceClass = lvServiceAndMethodName[0];
080: Object lvService = getService(lvServiceClass);
081: if (lvService == null) {
082: throw new InvocationException(
083: "For the service: "
084: + lvServiceClass
085: + " is no implementation by the HttpSerlet-servlet exist.");
086: }
087: return lvService;
088: }
089:
090: protected void doGet(HttpServletRequest pvRequest,
091: HttpServletResponse pvResponse) throws ServletException,
092: IOException {
093: doPost(pvRequest, pvResponse);
094: }
095:
096: protected void doPost(HttpServletRequest pvRequest,
097: HttpServletResponse pvResponse) throws ServletException,
098: IOException {
099: try {
100: Object lvService = getService(pvRequest, pvResponse);
101: Method lvMethod = getMethod(lvService, pvRequest
102: .getParameterMap(), pvRequest
103: .getParameter(PARAM_CLASS), pvRequest
104: .getParameter(PARAM_METHOD));
105:
106: Object lvParameters = Serializer.deserialize(pvRequest
107: .getInputStream());
108: Object lvArgs[] = getArgs(lvParameters);
109:
110: Object lvResult = null;
111: synchronized (lvService) {
112: lvResult = doInvoke(lvService, lvMethod, lvArgs,
113: createNewInterceptorHandlerInstance());
114: }
115:
116: if (log.isDebugEnabled()) {
117: log.debug("Result from method "
118: + pvRequest.getParameter(PARAM_METHOD)
119: + " is: " + lvResult);
120: }
121:
122: Serializer
123: .serialize(lvResult, pvResponse.getOutputStream());
124: } catch (Exception e) {
125: Serializer.serialize(e, pvResponse.getOutputStream());
126: throw new ServletException(
127: "Error by call Crispy-HttpServlet: " + e, e);
128: } finally {
129: if (log.isDebugEnabled()) {
130: log.debug("Call method "
131: + pvRequest.getParameter(PARAM_METHOD));
132: }
133: }
134: }
135:
136: public Method getMethod(Object pvService, Map pvParameterMap,
137: String pvServiceClassName, String pvMethodName)
138: throws Exception {
139:
140: String lvServiceAndMethodName[] = getServiceAndMethodName(
141: pvServiceClassName, pvMethodName);
142: // String lvServiceClassName = lvServiceAndMethodName[0];
143: String lvMethodName = lvServiceAndMethodName[1];
144:
145: String[] lvParamTypes = (String[]) pvParameterMap
146: .get(PARAM_TYPES);
147: Vector findParamVector = convertInParameterTypes(lvParamTypes);
148: Method lvMethod = Invoker.findMethod(pvService.getClass(),
149: lvMethodName, findParamVector);
150: return lvMethod;
151: }
152:
153: protected Object[] getArgs(Object pvParameters) {
154: Object lvParamsArray[] = null;
155: if (pvParameters == null) {
156: lvParamsArray = null;
157: } else if (pvParameters.getClass().isArray()) {
158: lvParamsArray = (Object[]) pvParameters;
159: } else {
160: lvParamsArray = new Object[] { pvParameters };
161: }
162: return lvParamsArray;
163: }
164:
165: protected String[] getServiceAndMethodName(
166: String pvServiceClassName, String pvMethodName)
167: throws Exception {
168: String lvServiceClass = pvServiceClassName;
169: String lvMethodName = pvMethodName;
170:
171: if (lvMethodName == null) {
172: throw new InvocationException(
173: "Error in HTTP-Servlet: The method-name was null.");
174: }
175:
176: // wenn keine Klasse angegeben, dann befindet sich im Methoden-namen der Servicename
177: // caltulator.add -> calculator ist der Service und add die Methode
178: if (lvServiceClass == null) {
179: int lvIndexPoint = lvMethodName.lastIndexOf(".");
180: lvServiceClass = lvMethodName.substring(0, lvIndexPoint);
181: lvMethodName = lvMethodName.substring(lvIndexPoint + 1);
182: }
183:
184: return new String[] { lvServiceClass, lvMethodName };
185: }
186:
187: protected Vector convertInParameterTypes(String pvParamTypes[]) {
188: if ((pvParamTypes == null) || (pvParamTypes.length == 0)) {
189: return null;
190: } else {
191: String lvParamTypes = pvParamTypes[0];
192: String lvParamtypesArray[] = lvParamTypes.split(",");
193: Vector v = new Vector(lvParamtypesArray.length);
194: for (int i = 0; i < lvParamtypesArray.length; i++) {
195: try {
196: Object lvValue = Converter
197: .convertClassString2Object(lvParamtypesArray[i]);
198: if (lvValue == null) {
199: return null;
200: }
201: v.add(lvValue);
202: } catch (Exception e) {
203: if (ServiceManager.DEBUG_MODE_ON) {
204: e.printStackTrace();
205: }
206: }
207: }
208: return v;
209: }
210: }
211:
212: }
|