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.jaxrpc;
  17. import java.io.File;
  18. import javax.servlet.ServletContext;
  19. import javax.xml.rpc.ServiceException;
  20. import javax.xml.rpc.server.ServiceLifecycle;
  21. import javax.xml.rpc.server.ServletEndpointContext;
  22. import org.apache.commons.logging.Log;
  23. import org.apache.commons.logging.LogFactory;
  24. import org.springframework.context.ApplicationContext;
  25. import org.springframework.context.support.MessageSourceAccessor;
  26. import org.springframework.web.context.WebApplicationContext;
  27. import org.springframework.web.context.support.WebApplicationContextUtils;
  28. import org.springframework.web.util.WebUtils;
  29. /**
  30. * Convenience base class for JAX-RPC servlet endpoint implementations.
  31. * Provides a reference to the current Spring application context,
  32. * e.g. for bean lookup or resource loading.
  33. *
  34. * <p>The Web Service servlet needs to run in the same web application
  35. * as the Spring context to allow for access to Spring's facilities.
  36. * In case of Axis, copy the AxisServlet definition into your web.xml,
  37. * and set up the endpoint in "server-config.wsdd" (or use the deploy tool).
  38. *
  39. * <p>This class does not extend WebApplicationContextSupport to not expose
  40. * any public setters. For some reason, Axis tries to resolve public setters
  41. * with WSDL means...
  42. *
  43. * @author Juergen Hoeller
  44. * @since 16.12.2003
  45. * @see #init
  46. * @see #getWebApplicationContext
  47. * @see org.springframework.web.context.support.WebApplicationObjectSupport
  48. */
  49. public class ServletEndpointSupport implements ServiceLifecycle {
  50. protected final Log logger = LogFactory.getLog(getClass());
  51. private ServletEndpointContext servletEndpointContext;
  52. private WebApplicationContext webApplicationContext;
  53. private MessageSourceAccessor messageSourceAccessor;
  54. /**
  55. * Initialize this JAX-RPC servlet endpoint.
  56. * Calls onInit after successful context initialization.
  57. * @param context ServletEndpointContext
  58. * @throws ServiceException if the context is not a ServletEndpointContext
  59. * @see #onInit
  60. */
  61. public final void init(Object context) throws ServiceException {
  62. if (!(context instanceof ServletEndpointContext)) {
  63. throw new ServiceException("ServletEndpointSupport needs ServletEndpointContext, not [" + context + "]");
  64. }
  65. this.servletEndpointContext = (ServletEndpointContext) context;
  66. ServletContext servletContext = this.servletEndpointContext.getServletContext();
  67. this.webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
  68. this.messageSourceAccessor = new MessageSourceAccessor(this.webApplicationContext);
  69. onInit();
  70. }
  71. /**
  72. * Return the current JAX-RPC ServletEndpointContext.
  73. */
  74. protected final ServletEndpointContext getServletEndpointContext() {
  75. return servletEndpointContext;
  76. }
  77. /**
  78. * Return the current Spring ApplicationContext.
  79. */
  80. protected final ApplicationContext getApplicationContext() {
  81. return this.webApplicationContext;
  82. }
  83. /**
  84. * Return the current Spring WebApplicationContext.
  85. */
  86. protected final WebApplicationContext getWebApplicationContext() {
  87. return this.webApplicationContext;
  88. }
  89. /**
  90. * Return a MessageSourceAccessor for the application context
  91. * used by this object, for easy message access.
  92. */
  93. protected final MessageSourceAccessor getMessageSourceAccessor() {
  94. return this.messageSourceAccessor;
  95. }
  96. /**
  97. * Return the current ServletContext.
  98. */
  99. protected final ServletContext getServletContext() {
  100. return this.webApplicationContext.getServletContext();
  101. }
  102. /**
  103. * Return the temporary directory for the current web application,
  104. * as provided by the servlet container.
  105. * @return the File representing the temporary directory
  106. */
  107. protected final File getTempDir() {
  108. return WebUtils.getTempDir(getServletContext());
  109. }
  110. /**
  111. * Callback for custom initialization after the context has been set up.
  112. * @throws ServiceException if initialization failed
  113. */
  114. protected void onInit() throws ServiceException {
  115. }
  116. /**
  117. * This implementation of destroy is empty.
  118. * Can be overridden in subclasses.
  119. */
  120. public void destroy() {
  121. }
  122. }