PageRenderTime 20ms CodeModel.GetById 2ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/gwtrpccommlayer/src/main/java/com/googlecode/gwtrpccommlayer/client/impl/GwtRpcCommLayerClient.java

https://code.google.com/p/gwtrpccommlayer/
Java | 117 lines | 52 code | 14 blank | 51 comment | 0 complexity | 72a29b76ada6cbb8e34381cc29db7109 MD5 | raw file
  1/*
  2 * Copyright 2010 Jeff McHugh (Segue Development LLC)
  3 * 
  4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
  5 * in compliance with the License. You may obtain a copy of the License at
  6 * 
  7 * http://www.apache.org/licenses/LICENSE-2.0
  8 * 
  9 * Unless required by applicable law or agreed to in writing, software distributed under the License
 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 11 * or implied. See the License for the specific language governing permissions and limitations under
 12 * the License.
 13 */
 14package com.googlecode.gwtrpccommlayer.client.impl;
 15
 16import org.apache.http.cookie.Cookie;
 17
 18import java.lang.reflect.InvocationHandler;
 19import java.lang.reflect.Proxy;
 20import java.net.URL;
 21import java.util.Collection;
 22
 23/**
 24 * This class is the main client-side hook for all RPC interactions.
 25 * It hides the proxy class slightly and makes it easy for the developer
 26 * to quickly initiate a RPC class
 27 * @author jeff mchugh
 28 *
 29 */
 30public class GwtRpcCommLayerClient
 31{
 32	/**
 33	 * This is the default class used unless a runtime directive changes it (which is perfectly fine)
 34	 */
 35	public static String DEFAULT_RPC_CLIENT_SIDE_PROXY_IMPL_CLASS
 36            = "com.googlecode.gwtrpccommlayer.client.impl.GwtRpcClientSideProxy";
 37	
 38	private URL url = null;
 39	private String gwtRpcClientSizeProxyImplClass = null;
 40    private Collection<Cookie> cookies;
 41
 42    public GwtRpcCommLayerClient(URL url)
 43	{
 44        this.url = url;
 45		setGwtRpcClientSizeProxyImplClass(DEFAULT_RPC_CLIENT_SIDE_PROXY_IMPL_CLASS);
 46	}
 47
 48    public GwtRpcCommLayerClient(URL url, Collection<Cookie> cookies) {
 49        this(url);
 50        this.cookies = cookies;
 51    }
 52	/**
 53	 * This method is the main method through which a GWT interface shall be "wrapped" into a working pojo-proxy-client object.
 54	 * 
 55	 * @param serviceInterface (this class is your basic interface class which the developer should have already defined according the GWT-RPC specification 
 56	 * @return a PojoProxy class that uses standard Java Object serialization instead of GWT-RPC servialization
 57	 * @throws ClassNotFoundException
 58	 * @throws IllegalAccessException
 59	 * @throws InstantiationException
 60	 */
 61	public <T> T createRemoteServicePojoProxy(Class<T> serviceInterface) throws ClassNotFoundException, IllegalAccessException, InstantiationException
 62	{
 63        //Get the Invocation Handler
 64		InvocationHandler handler = createGwtRpcClientSideProxy();
 65		T proxy = (T) Proxy.newProxyInstance(
 66                serviceInterface.getClassLoader(),
 67                new Class[] { serviceInterface },
 68                handler);
 69		return proxy;
 70	}
 71	
 72	/**
 73	 * Method called internally. Can be overridden for future functionality
 74	 * @return
 75	 * @throws ClassNotFoundException
 76	 * @throws IllegalAccessException
 77	 * @throws InstantiationException
 78	 */
 79	protected IGwtRpcClientSideProxy createGwtRpcClientSideProxy() throws ClassNotFoundException, IllegalAccessException, InstantiationException
 80	{
 81        //todo:  This instance should be injected or created with a factory
 82        //use factory with cookie argument
 83		Class<?> clazz = Class.forName(getGwtRpcClientSizeProxyImplClass());
 84		IGwtRpcClientSideProxy proxy = (IGwtRpcClientSideProxy) clazz.newInstance();
 85		proxy.setUrl(getUrl());
 86		proxy.setShowResponseHeaders(false);
 87		
 88		return proxy;
 89	}
 90
 91
 92	/**
 93	 * @return the url
 94	 */
 95	public URL getUrl()
 96	{
 97		return url;
 98	}
 99
100	/**
101	 * @param gwtRpcClientSizeProxyImplClass the gwtRpcClientSizeProxyImplClass to set
102	 */
103	public void setGwtRpcClientSizeProxyImplClass(String gwtRpcClientSizeProxyImplClass)
104	{
105		this.gwtRpcClientSizeProxyImplClass = gwtRpcClientSizeProxyImplClass;
106	}
107
108	/**
109	 * @return the gwtRpcClientSizeProxyImplClass
110	 */
111	public String getGwtRpcClientSizeProxyImplClass()
112	{
113		return gwtRpcClientSizeProxyImplClass;
114	}
115
116
117}