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.caucho;
  17. import java.lang.reflect.Constructor;
  18. import javax.servlet.ServletException;
  19. import javax.servlet.http.HttpServletRequest;
  20. import javax.servlet.http.HttpServletResponse;
  21. import com.caucho.hessian.io.HessianInput;
  22. import com.caucho.hessian.io.HessianOutput;
  23. import com.caucho.hessian.server.HessianSkeleton;
  24. import org.springframework.remoting.support.RemoteExporter;
  25. import org.springframework.web.servlet.ModelAndView;
  26. import org.springframework.web.servlet.mvc.Controller;
  27. /**
  28. * Web controller that exports the specified service bean as Hessian service
  29. * endpoint, accessible via a Hessian proxy.
  30. *
  31. * <p>Hessian is a slim, binary RPC protocol.
  32. * For information on Hessian, see the
  33. * <a href="http://www.caucho.com/hessian">Hessian website</a>
  34. *
  35. * <p>This exporter will work with both Hessian 2.x and 3.x (respectively
  36. * Resin 2.x and 3.x), auto-detecting the corresponding skeleton class.
  37. *
  38. * <p>Note: Hessian services exported with this class can be accessed by
  39. * any Hessian client, as there isn't any special handling involved.
  40. *
  41. * @author Juergen Hoeller
  42. * @since 13.05.2003
  43. * @see HessianProxyFactoryBean
  44. */
  45. public class HessianServiceExporter extends RemoteExporter implements Controller {
  46. private HessianSkeleton skeleton;
  47. public void afterPropertiesSet() throws Exception {
  48. super.afterPropertiesSet();
  49. try {
  50. // try Hessian 3.x (with service interface argument)
  51. Constructor ctor = HessianSkeleton.class.getConstructor(new Class[] {Object.class, Class.class});
  52. this.skeleton = (HessianSkeleton) ctor.newInstance(new Object[] {getService(), getServiceInterface()});
  53. logger.info("Created Hessian 2.x skeleton");
  54. }
  55. catch (NoSuchMethodException ex) {
  56. // fall back to Hessian 2.x (without service interface argument)
  57. Constructor ctor = HessianSkeleton.class.getConstructor(new Class[] {Object.class});
  58. this.skeleton = (HessianSkeleton) ctor.newInstance(new Object[] {getProxyForService()});
  59. }
  60. }
  61. /**
  62. * Process the incoming Hessian request and create a Hessian response.
  63. */
  64. public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
  65. HessianInput in = new HessianInput(request.getInputStream());
  66. HessianOutput out = new HessianOutput(response.getOutputStream());
  67. try {
  68. this.skeleton.invoke(in, out);
  69. }
  70. catch (Exception ex) {
  71. throw ex;
  72. }
  73. catch (Error ex) {
  74. throw ex;
  75. }
  76. catch (Throwable ex) {
  77. throw new ServletException(ex);
  78. }
  79. return null;
  80. }
  81. }