1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.remoting.support;
  17. import org.apache.commons.logging.Log;
  18. import org.apache.commons.logging.LogFactory;
  19. import org.springframework.aop.framework.ProxyFactory;
  20. import org.springframework.beans.factory.InitializingBean;
  21. /**
  22. * Abstract base class for classes that export a remote service.
  23. * Provides a "serviceInterface" bean property.
  24. *
  25. * <p>Note that the service interface being used will show some signs of
  26. * remotability, like the granularity of method calls that it offers.
  27. * Furthermore, it has to require serializable arguments etc.
  28. *
  29. * @author Juergen Hoeller
  30. * @since 26.12.2003
  31. */
  32. public abstract class RemoteExporter implements InitializingBean {
  33. protected final Log logger = LogFactory.getLog(getClass());
  34. private Class serviceInterface;
  35. private Object service;
  36. /**
  37. * Set the interface of the service to export.
  38. * Typically optional: If not set, all implement interfaces will be exported.
  39. * The interface must be suitable for the particular service and remoting tool.
  40. */
  41. public void setServiceInterface(Class serviceInterface) {
  42. if (!serviceInterface.isInterface()) {
  43. throw new IllegalArgumentException("serviceInterface must be an interface");
  44. }
  45. this.serviceInterface = serviceInterface;
  46. }
  47. /**
  48. * Return the interface of the service to export.
  49. */
  50. protected Class getServiceInterface() {
  51. return serviceInterface;
  52. }
  53. /**
  54. * Set the service to export.
  55. * Typically populated via a bean reference.
  56. */
  57. public void setService(Object service) {
  58. this.service = service;
  59. }
  60. /**
  61. * Return the service to export.
  62. */
  63. protected Object getService() {
  64. return service;
  65. }
  66. public void afterPropertiesSet() throws Exception {
  67. if (this.serviceInterface == null) {
  68. throw new IllegalArgumentException("serviceInterface is required");
  69. }
  70. if (this.service == null) {
  71. throw new IllegalArgumentException("service is required");
  72. }
  73. if (!this.serviceInterface.isInstance(this.service)) {
  74. throw new IllegalArgumentException("serviceInterface [" + this.serviceInterface.getName() +
  75. "] needs to be implemented by service [" + this.service + "]");
  76. }
  77. }
  78. /**
  79. * Get a proxy for the given service object, implementing the specified
  80. * service interface.
  81. * <p>Used to export a proxy that does not expose any internals but just
  82. * a specific interface intended for remote access. Only applied if the
  83. * remoting tool itself does not offer such means itself.
  84. * @return the proxy
  85. * @see #setServiceInterface
  86. */
  87. protected Object getProxyForService() {
  88. ProxyFactory proxyFactory = new ProxyFactory();
  89. proxyFactory.addInterface(this.serviceInterface);
  90. proxyFactory.setTarget(this.service);
  91. return proxyFactory.getProxy();
  92. }
  93. }