|
| 1 | +/* |
| 2 | + * jabsorb - a Java to JavaScript Advanced Object Request Broker |
| 3 | + * http://www.jabsorb.org |
| 4 | + * |
| 5 | + * Copyright 2007-2009 The jabsorb team |
| 6 | + * |
| 7 | + * based on original code from |
| 8 | + * JSON-RPC-Client, a Java client extension to JSON-RPC-Java |
| 9 | + * (C) Copyright CodeBistro 2007, Sasha Ovsankin <sasha at codebistro dot com> |
| 10 | + * |
| 11 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 12 | + * you may not use this file except in compliance with the License. |
| 13 | + * You may obtain a copy of the License at |
| 14 | + * |
| 15 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 16 | + * |
| 17 | + * Unless required by applicable law or agreed to in writing, software |
| 18 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 19 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 20 | + * See the License for the specific language governing permissions and |
| 21 | + * limitations under the License. |
| 22 | + * |
| 23 | + */ |
| 24 | +package org.jabsorb.client.async; |
| 25 | + |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +import org.jabsorb.JSONSerializer; |
| 30 | +import org.jabsorb.client.ClientError; |
| 31 | +import org.jabsorb.serializer.request.fixups.FixupsCircularReferenceHandler; |
| 32 | +import org.jabsorb.serializer.response.fixups.FixupCircRefAndNonPrimitiveDupes; |
| 33 | + |
| 34 | +/** |
| 35 | + * A factory to create proxies for access to remote Jabsorb services. |
| 36 | + */ |
| 37 | +public class AsyncClient { |
| 38 | + |
| 39 | + /** |
| 40 | + * Maps proxy keys to proxies |
| 41 | + */ |
| 42 | + private final Map<Object, String> proxyMap; |
| 43 | + |
| 44 | + /** |
| 45 | + * The serializer instance to use. |
| 46 | + */ |
| 47 | + private final JSONSerializer serializer; |
| 48 | + |
| 49 | + /** |
| 50 | + * The transport session to use for this connection |
| 51 | + */ |
| 52 | + private final AsyncSession session; |
| 53 | + |
| 54 | + /** |
| 55 | + * Create a client given a session |
| 56 | + * |
| 57 | + * @param session |
| 58 | + * transport session to use for this connection |
| 59 | + */ |
| 60 | + public AsyncClient(final AsyncSession session) { |
| 61 | + try { |
| 62 | + this.session = session; |
| 63 | + this.proxyMap = new HashMap<Object, String>(); |
| 64 | + // TODO: this might need a better way of initialising it |
| 65 | + this.serializer = new JSONSerializer( |
| 66 | + FixupCircRefAndNonPrimitiveDupes.class, |
| 67 | + new FixupsCircularReferenceHandler()); |
| 68 | + this.serializer.registerDefaultSerializers(); |
| 69 | + } catch (final Exception e) { |
| 70 | + throw new ClientError(e); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Create a proxy for communicating with the remote service. |
| 76 | + * |
| 77 | + * @param key |
| 78 | + * the remote object key |
| 79 | + * @param klass |
| 80 | + * the class of the interface the remote object should adhere to |
| 81 | + * @return created proxy |
| 82 | + */ |
| 83 | + public Object openProxy(final String key, final Class<?> klass) { |
| 84 | + final Object result = java.lang.reflect.Proxy.newProxyInstance( |
| 85 | + klass.getClassLoader(), new Class[] { klass, AsyncProxy.class }, new AsyncProxyHandler(key, session, serializer)); |
| 86 | + |
| 87 | + proxyMap.put(result, key); |
| 88 | + return result; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Dispose of the proxy that is no longer needed |
| 93 | + * |
| 94 | + * @param proxy |
| 95 | + * The proxy to close |
| 96 | + */ |
| 97 | + public void closeProxy(final Object proxy) { |
| 98 | + proxyMap.remove(proxy); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Allow access to the serializer |
| 103 | + * |
| 104 | + * @return The serializer for this class |
| 105 | + */ |
| 106 | + public JSONSerializer getSerializer() { |
| 107 | + return serializer; |
| 108 | + } |
| 109 | +} |
0 commit comments