001 /*
002 * Copyright (C) 2008-2009 Holger Arndt, A. Naegele and M. Bundschus
003 *
004 * This file is part of the Java Data Mining Package (JDMP).
005 * See the NOTICE file distributed with this work for additional
006 * information regarding copyright ownership and licensing.
007 *
008 * JDMP is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation; either version 2
011 * of the License, or (at your option) any later version.
012 *
013 * JDMP is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016 * GNU Lesser General Public License for more details.
017 *
018 * You should have received a copy of the GNU Lesser General Public
019 * License along with JDMP; if not, write to the
020 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021 * Boston, MA 02110-1301 USA
022 */
023
024 package org.jdmp.jetty;
025
026 import java.io.IOException;
027 import java.io.InputStream;
028 import java.io.ObjectInputStream;
029 import java.io.ObjectOutputStream;
030 import java.io.OutputStream;
031 import java.lang.reflect.Method;
032 import java.util.Arrays;
033
034 import javax.servlet.ServletException;
035 import javax.servlet.http.HttpServlet;
036 import javax.servlet.http.HttpServletRequest;
037 import javax.servlet.http.HttpServletResponse;
038
039 public class JettyObjectServlet extends HttpServlet {
040 private static final long serialVersionUID = -3704890184419166703L;
041
042 private Object object = null;
043
044 public JettyObjectServlet(Object o) {
045 this.object = o;
046 }
047
048 @Override
049 protected void doGet(HttpServletRequest request,
050 HttpServletResponse response) throws ServletException, IOException {
051 doPost(request, response);
052 }
053
054 @Override
055 protected void doPost(HttpServletRequest request,
056 HttpServletResponse response) throws ServletException, IOException {
057
058 InputStream is = request.getInputStream();
059
060 String methodName = null;
061 Object[] parameters = null;
062 Object result = null;
063
064 // read parameters
065 try {
066 ObjectInputStream ois = new ObjectInputStream(is);
067 methodName = (String) ois.readObject();
068 parameters = (Object[]) ois.readObject();
069 ois.close();
070 } catch (Exception e) {
071 e.printStackTrace();
072 }
073
074 is.close();
075
076 // execute method
077 try {
078 Class<?>[] classes = new Class<?>[parameters.length];
079 for (int i = 0; i < parameters.length; i++) {
080 classes[i] = parameters[i].getClass();
081 }
082
083 Method method = null;
084 for (Method m : object.getClass().getMethods()) {
085 if (m.getParameterTypes().length == parameters.length
086 && m.getName().equals(methodName)) {
087 method = m;
088 break;
089 }
090 }
091
092 if (method == null) {
093 throw new RuntimeException("method not found: " + methodName
094 + Arrays.asList(parameters));
095 }
096
097 result = method.invoke(object, parameters);
098
099 } catch (Exception e) {
100 e.printStackTrace();
101 }
102
103 OutputStream os = response.getOutputStream();
104
105 // write result
106 try {
107 ObjectOutputStream oos = new ObjectOutputStream(os);
108 oos.writeObject(result);
109 oos.flush();
110 oos.close();
111 } catch (Exception e) {
112 e.printStackTrace();
113 }
114
115 os.flush();
116 os.close();
117 }
118
119 }