001: /*
002: * Copyright 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.ws.server.endpoint.mapping;
018:
019: import org.springframework.context.support.ApplicationObjectSupport;
020: import org.springframework.core.Ordered;
021: import org.springframework.ws.context.MessageContext;
022: import org.springframework.ws.server.EndpointInterceptor;
023: import org.springframework.ws.server.EndpointInvocationChain;
024: import org.springframework.ws.server.EndpointMapping;
025:
026: /**
027: * Abstract base class for EndpointMapping implementations. Supports a default endpoint, and endpoint interceptors.
028: *
029: * @author Arjen Poutsma
030: * @see #getEndpointInternal(org.springframework.ws.context.MessageContext)
031: * @see org.springframework.ws.server.EndpointInterceptor
032: * @since 1.0.0
033: */
034: public abstract class AbstractEndpointMapping extends
035: ApplicationObjectSupport implements EndpointMapping, Ordered {
036:
037: private int order = Integer.MAX_VALUE; // default: same as non-Ordered
038:
039: private Object defaultEndpoint;
040:
041: private EndpointInterceptor[] interceptors;
042:
043: /**
044: * Returns the the endpoint interceptors to apply to all endpoints mapped by this endpoint mapping.
045: *
046: * @return array of endpoint interceptors, or <code>null</code> if none
047: */
048: public EndpointInterceptor[] getInterceptors() {
049: return interceptors;
050: }
051:
052: /**
053: * Sets the endpoint interceptors to apply to all endpoints mapped by this endpoint mapping.
054: *
055: * @param interceptors array of endpoint interceptors, or <code>null</code> if none
056: */
057: public final void setInterceptors(EndpointInterceptor[] interceptors) {
058: this .interceptors = interceptors;
059: }
060:
061: public final int getOrder() {
062: return order;
063: }
064:
065: /**
066: * Specify the order value for this mapping.
067: * <p/>
068: * Default value is {@link Integer#MAX_VALUE}, meaning that it's non-ordered.
069: *
070: * @see org.springframework.core.Ordered#getOrder()
071: */
072: public final void setOrder(int order) {
073: this .order = order;
074: }
075:
076: /**
077: * Look up an endpoint for the given message context, falling back to the default endpoint if no specific one is
078: * found.
079: *
080: * @return the looked up endpoint instance, or the default endpoint
081: * @see #getEndpointInternal(org.springframework.ws.context.MessageContext)
082: */
083: public final EndpointInvocationChain getEndpoint(
084: MessageContext messageContext) throws Exception {
085: Object endpoint = getEndpointInternal(messageContext);
086: if (endpoint == null) {
087: endpoint = defaultEndpoint;
088: }
089: if (endpoint == null) {
090: return null;
091: }
092: if (endpoint instanceof String) {
093: String endpointName = (String) endpoint;
094: endpoint = resolveStringEndpoint(endpointName);
095: if (endpoint == null) {
096: return null;
097: }
098: }
099: return createEndpointInvocationChain(messageContext, endpoint,
100: interceptors);
101: }
102:
103: /**
104: * Creates a new <code>EndpointInvocationChain</code> based on the given message context, endpoint, and
105: * interceptors. Default implementation creates a simple <code>EndpointInvocationChain</code> based on the set
106: * interceptors.
107: *
108: * @param endpoint the endpoint
109: * @param interceptors the endpoint interceptors
110: * @return the created invocation chain
111: * @see #setInterceptors(org.springframework.ws.server.EndpointInterceptor[])
112: */
113: protected EndpointInvocationChain createEndpointInvocationChain(
114: MessageContext messageContext, Object endpoint,
115: EndpointInterceptor[] interceptors) {
116: return new EndpointInvocationChain(endpoint, interceptors);
117: }
118:
119: /**
120: * Returns the default endpoint for this endpoint mapping.
121: *
122: * @return the default endpoint mapping, or null if none
123: */
124: protected final Object getDefaultEndpoint() {
125: return defaultEndpoint;
126: }
127:
128: /**
129: * Sets the default endpoint for this endpoint mapping. This endpoint will be returned if no specific mapping was
130: * found.
131: * <p/>
132: * Default is <code>null</code>, indicating no default endpoint.
133: *
134: * @param defaultEndpoint the default endpoint, or null if none
135: */
136: public final void setDefaultEndpoint(Object defaultEndpoint) {
137: this .defaultEndpoint = defaultEndpoint;
138: }
139:
140: /**
141: * Resolves an endpoint string. If the given string can is a bean name, it is resolved using the application
142: * context.
143: *
144: * @param endpointName the endpoint name
145: * @return the resolved enpoint, or <code>null</code> if the name could not be resolved
146: */
147: protected Object resolveStringEndpoint(String endpointName) {
148: if (getApplicationContext().containsBean(endpointName)) {
149: return getApplicationContext().getBean(endpointName);
150: } else {
151: return null;
152: }
153: }
154:
155: /**
156: * Lookup an endpoint for the given request, returning <code>null</code> if no specific one is found. This template
157: * method is called by getEndpoint, a <code>null</code> return value will lead to the default handler, if one is
158: * set.
159: * <p/>
160: * The returned endpoint can be a string, in which case it is resolved as a bean name. Also, it can take the form
161: * <code>beanName#method</code>, in which case the method is resolved.
162: *
163: * @return the looked up endpoint instance, or null
164: * @throws Exception if there is an error
165: */
166: protected abstract Object getEndpointInternal(
167: MessageContext messageContext) throws Exception;
168: }
|