001: /*
002: * argun 1.0
003: * Web 2.0 delivery framework
004: * Copyright (C) 2007 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.web.remoting;
024:
025: import java.io.ByteArrayOutputStream;
026: import java.io.InputStream;
027: import java.io.ObjectInputStream;
028: import java.io.ObjectOutputStream;
029: import java.lang.reflect.InvocationHandler;
030: import java.lang.reflect.Method;
031: import java.lang.reflect.Proxy;
032: import java.util.Map;
033: import java.util.WeakHashMap;
034: import java.util.zip.GZIPInputStream;
035: import java.util.zip.GZIPOutputStream;
036:
037: import biz.hammurapi.web.HammurapiWebRuntimeException;
038:
039: /**
040: * Client-side class.
041: * @author Pavel Vlasov
042: */
043: public abstract class RemoteRegistry {
044:
045: /**
046: *
047: * @param invocation Serialized invocation
048: * @return Return value or exception
049: */
050: protected abstract InputStream invoke(byte[] request);
051:
052: private Map registry = new WeakHashMap();
053:
054: public synchronized Object register(final String name,
055: Class theInterface) {
056: return register(name, new Class[] { theInterface },
057: theInterface.getClassLoader());
058: }
059:
060: /**
061: * Creates a proxy object for remote invocations. No server-side checks are done.
062: * @param name
063: * @param interfaces
064: * @return
065: */
066: public synchronized Object register(final String name,
067: Class[] interfaces, ClassLoader classLoader) {
068: if (registry.containsKey(name)) {
069: throw new HammurapiWebRuntimeException(
070: "This name already registered");
071: }
072:
073: InvocationHandler ih = new InvocationHandler() {
074:
075: public Object invoke(Object proxy, Method method,
076: Object[] args) throws Throwable {
077: ByteArrayOutputStream baos = new ByteArrayOutputStream();
078: GZIPOutputStream gos = new GZIPOutputStream(baos);
079: ObjectOutputStream oos = new ObjectOutputStream(gos);
080: oos.writeObject(name);
081: oos.writeObject(method.getDeclaringClass().getName());
082: oos.writeObject(method.getName());
083: Class[] mpt = method.getParameterTypes();
084: String[] paramTypes = new String[mpt.length];
085: for (int i = 0, l = mpt.length; i < l; ++i) {
086: paramTypes[i] = mpt[i].getName();
087: }
088: oos.writeObject(paramTypes);
089: oos.writeObject(args);
090: oos.close();
091:
092: GZIPInputStream gis = new GZIPInputStream(
093: RemoteRegistry.this .invoke(baos.toByteArray()));
094: ObjectInputStream ois = new ObjectInputStream(gis);
095: if (ois.readBoolean()) {
096: Object ret = ois.readObject();
097: ois.close();
098: return ret;
099: }
100:
101: String errorMessage = (String) ois.readObject();
102: ois.close();
103: throw new HammurapiWebRuntimeException(errorMessage);
104: }
105:
106: };
107:
108: Object ret = Proxy.newProxyInstance(classLoader == null ? this
109: .getClass().getClassLoader() : classLoader, interfaces,
110: ih);
111: registry.put(name, ret);
112: return ret;
113: }
114:
115: /**
116: * Looks up existing remote object in the registry.
117: * @param name
118: * @return
119: */
120: public Object lookup(String name) {
121: return registry.get(name);
122: }
123:
124: }
|