001: /*
002: * Copyright 2002-2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.remoting.jaxrpc;
018:
019: import java.io.File;
020:
021: import javax.servlet.ServletContext;
022: import javax.xml.rpc.ServiceException;
023: import javax.xml.rpc.server.ServiceLifecycle;
024: import javax.xml.rpc.server.ServletEndpointContext;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028:
029: import org.springframework.context.ApplicationContext;
030: import org.springframework.context.support.MessageSourceAccessor;
031: import org.springframework.web.context.WebApplicationContext;
032: import org.springframework.web.context.support.WebApplicationContextUtils;
033: import org.springframework.web.util.WebUtils;
034:
035: /**
036: * Convenience base class for JAX-RPC servlet endpoint implementations.
037: * Provides a reference to the current Spring application context,
038: * e.g. for bean lookup or resource loading.
039: *
040: * <p>The Web Service servlet needs to run in the same web application
041: * as the Spring context to allow for access to Spring's facilities.
042: * In case of Axis, copy the AxisServlet definition into your web.xml,
043: * and set up the endpoint in "server-config.wsdd" (or use the deploy tool).
044: *
045: * <p>This class does not extend WebApplicationContextSupport to not expose
046: * any public setters. For some reason, Axis tries to resolve public setters
047: * in a special way...
048: *
049: * <p>JAX-RPC service endpoints are usually required to implement an
050: * RMI port interface. However, many JAX-RPC implementations accept plain
051: * service endpoint classes too, avoiding the need to maintain an RMI port
052: * interface in addition to an existing non-RMI business interface.
053: * Therefore, implementing the business interface will usually be sufficient.
054: *
055: * @author Juergen Hoeller
056: * @since 16.12.2003
057: * @see #init
058: * @see #getWebApplicationContext
059: * @see org.springframework.web.context.support.WebApplicationObjectSupport
060: */
061: public abstract class ServletEndpointSupport implements
062: ServiceLifecycle {
063:
064: protected final Log logger = LogFactory.getLog(getClass());
065:
066: private ServletEndpointContext servletEndpointContext;
067:
068: private WebApplicationContext webApplicationContext;
069:
070: private MessageSourceAccessor messageSourceAccessor;
071:
072: /**
073: * Initialize this JAX-RPC servlet endpoint.
074: * Calls onInit after successful context initialization.
075: * @param context ServletEndpointContext
076: * @throws ServiceException if the context is not a ServletEndpointContext
077: * @see #onInit
078: */
079: public final void init(Object context) throws ServiceException {
080: if (!(context instanceof ServletEndpointContext)) {
081: throw new ServiceException(
082: "ServletEndpointSupport needs ServletEndpointContext, not ["
083: + context + "]");
084: }
085: this .servletEndpointContext = (ServletEndpointContext) context;
086: ServletContext servletContext = this .servletEndpointContext
087: .getServletContext();
088: this .webApplicationContext = WebApplicationContextUtils
089: .getRequiredWebApplicationContext(servletContext);
090: this .messageSourceAccessor = new MessageSourceAccessor(
091: this .webApplicationContext);
092: onInit();
093: }
094:
095: /**
096: * Return the current JAX-RPC ServletEndpointContext.
097: */
098: protected final ServletEndpointContext getServletEndpointContext() {
099: return servletEndpointContext;
100: }
101:
102: /**
103: * Return the current Spring ApplicationContext.
104: */
105: protected final ApplicationContext getApplicationContext() {
106: return this .webApplicationContext;
107: }
108:
109: /**
110: * Return the current Spring WebApplicationContext.
111: */
112: protected final WebApplicationContext getWebApplicationContext() {
113: return this .webApplicationContext;
114: }
115:
116: /**
117: * Return a MessageSourceAccessor for the application context
118: * used by this object, for easy message access.
119: */
120: protected final MessageSourceAccessor getMessageSourceAccessor() {
121: return this .messageSourceAccessor;
122: }
123:
124: /**
125: * Return the current ServletContext.
126: */
127: protected final ServletContext getServletContext() {
128: return this .webApplicationContext.getServletContext();
129: }
130:
131: /**
132: * Return the temporary directory for the current web application,
133: * as provided by the servlet container.
134: * @return the File representing the temporary directory
135: */
136: protected final File getTempDir() {
137: return WebUtils.getTempDir(getServletContext());
138: }
139:
140: /**
141: * Callback for custom initialization after the context has been set up.
142: * @throws ServiceException if initialization failed
143: */
144: protected void onInit() throws ServiceException {
145: }
146:
147: /**
148: * This implementation of destroy is empty.
149: * Can be overridden in subclasses.
150: */
151: public void destroy() {
152: }
153:
154: }
|