01: /*
02: * argun 1.0
03: * Web 2.0 delivery framework
04: * Copyright (C) 2007 Hammurapi Group
05: *
06: * This program is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * version 2 of the License, or (at your option) any later version.
10: *
11: * This program is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: * You should have received a copy of the GNU Lesser General Public
17: * License along with this library; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19: *
20: * URL: http://www.hammurapi.biz
21: * e-Mail: support@hammurapi.biz
22: */
23: package biz.hammurapi.web.remoting;
24:
25: import java.io.InputStream;
26: import java.io.ObjectInputStream;
27: import java.io.ObjectOutputStream;
28: import java.io.OutputStream;
29: import java.lang.reflect.Method;
30: import java.util.zip.GZIPInputStream;
31: import java.util.zip.GZIPOutputStream;
32:
33: import biz.hammurapi.config.Context;
34:
35: /**
36: * Remote registry routes invocations to instances of this class.
37: * @author Pavel Vlasov
38: */
39: public abstract class Registry implements Context {
40:
41: /**
42: * Deserialized request, performs invocation, serializes response.
43: * @param request
44: * @return
45: */
46: public void invoke(InputStream request, OutputStream response) {
47: try {
48: Object ret = null;
49: boolean success = false;
50: GZIPOutputStream gos = new GZIPOutputStream(response);
51: ObjectOutputStream oos = new ObjectOutputStream(gos);
52: try {
53: GZIPInputStream gis = new GZIPInputStream(request);
54: ObjectInputStream ois = new ObjectInputStream(gis);
55: String name = (String) ois.readObject();
56: Object target = get(name);
57: if (target == null) {
58: ret = "Name not found: " + name;
59: } else {
60: ClassLoader classLoader = target.getClass()
61: .getClassLoader();
62: Class declaringClass = classLoader
63: .loadClass((String) ois.readObject());
64: String methodName = (String) ois.readObject();
65: String[] paramTypeNames = (String[]) ois
66: .readObject();
67: Class[] paramTypes = new Class[paramTypeNames.length];
68: for (int i = 0, l = paramTypeNames.length; i < l; ++i) {
69: paramTypes[i] = classLoader
70: .loadClass(paramTypeNames[i]);
71: }
72: Method method = declaringClass.getMethod(
73: methodName, paramTypes);
74: Object[] args = (Object[]) ois.readObject();
75: ret = method.invoke(target, args);
76: success = true;
77: }
78: ois.close();
79: } catch (Exception e) {
80: ret = e.toString();
81: }
82: oos.writeBoolean(success);
83: oos.writeObject(ret);
84: oos.close();
85: } catch (Exception e) {
86: e.printStackTrace();
87: }
88: }
89:
90: }
|