PageRenderTime 150ms CodeModel.GetById 18ms RepoModel.GetById 0ms 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. */
  14. package com.googlecode.gwtrpccommlayer.client.impl;
  15. import org.apache.http.cookie.Cookie;
  16. import java.lang.reflect.InvocationHandler;
  17. import java.lang.reflect.Proxy;
  18. import java.net.URL;
  19. import java.util.Collection;
  20. /**
  21. * This class is the main client-side hook for all RPC interactions.
  22. * It hides the proxy class slightly and makes it easy for the developer
  23. * to quickly initiate a RPC class
  24. * @author jeff mchugh
  25. *
  26. */
  27. public class GwtRpcCommLayerClient
  28. {
  29. /**
  30. * This is the default class used unless a runtime directive changes it (which is perfectly fine)
  31. */
  32. public static String DEFAULT_RPC_CLIENT_SIDE_PROXY_IMPL_CLASS
  33. = "com.googlecode.gwtrpccommlayer.client.impl.GwtRpcClientSideProxy";
  34. private URL url = null;
  35. private String gwtRpcClientSizeProxyImplClass = null;
  36. private Collection<Cookie> cookies;
  37. public GwtRpcCommLayerClient(URL url)
  38. {
  39. this.url = url;
  40. setGwtRpcClientSizeProxyImplClass(DEFAULT_RPC_CLIENT_SIDE_PROXY_IMPL_CLASS);
  41. }
  42. public GwtRpcCommLayerClient(URL url, Collection<Cookie> cookies) {
  43. this(url);
  44. this.cookies = cookies;
  45. }
  46. /**
  47. * This method is the main method through which a GWT interface shall be "wrapped" into a working pojo-proxy-client object.
  48. *
  49. * @param serviceInterface (this class is your basic interface class which the developer should have already defined according the GWT-RPC specification
  50. * @return a PojoProxy class that uses standard Java Object serialization instead of GWT-RPC servialization
  51. * @throws ClassNotFoundException
  52. * @throws IllegalAccessException
  53. * @throws InstantiationException
  54. */
  55. public <T> T createRemoteServicePojoProxy(Class<T> serviceInterface) throws ClassNotFoundException, IllegalAccessException, InstantiationException
  56. {
  57. //Get the Invocation Handler
  58. InvocationHandler handler = createGwtRpcClientSideProxy();
  59. T proxy = (T) Proxy.newProxyInstance(
  60. serviceInterface.getClassLoader(),
  61. new Class[] { serviceInterface },
  62. handler);
  63. return proxy;
  64. }
  65. /**
  66. * Method called internally. Can be overridden for future functionality
  67. * @return
  68. * @throws ClassNotFoundException
  69. * @throws IllegalAccessException
  70. * @throws InstantiationException
  71. */
  72. protected IGwtRpcClientSideProxy createGwtRpcClientSideProxy() throws ClassNotFoundException, IllegalAccessException, InstantiationException
  73. {
  74. //todo: This instance should be injected or created with a factory
  75. //use factory with cookie argument
  76. Class<?> clazz = Class.forName(getGwtRpcClientSizeProxyImplClass());
  77. IGwtRpcClientSideProxy proxy = (IGwtRpcClientSideProxy) clazz.newInstance();
  78. proxy.setUrl(getUrl());
  79. proxy.setShowResponseHeaders(false);
  80. return proxy;
  81. }
  82. /**
  83. * @return the url
  84. */
  85. public URL getUrl()
  86. {
  87. return url;
  88. }
  89. /**
  90. * @param gwtRpcClientSizeProxyImplClass the gwtRpcClientSizeProxyImplClass to set
  91. */
  92. public void setGwtRpcClientSizeProxyImplClass(String gwtRpcClientSizeProxyImplClass)
  93. {
  94. this.gwtRpcClientSizeProxyImplClass = gwtRpcClientSizeProxyImplClass;
  95. }
  96. /**
  97. * @return the gwtRpcClientSizeProxyImplClass
  98. */
  99. public String getGwtRpcClientSizeProxyImplClass()
  100. {
  101. return gwtRpcClientSizeProxyImplClass;
  102. }
  103. }