1717package org .springframework .ws .server ;
1818
1919import java .util .ArrayList ;
20+ import java .util .Collections ;
2021import java .util .Iterator ;
2122import java .util .List ;
23+ import java .util .Map ;
2224
2325import org .apache .commons .logging .Log ;
2426import org .apache .commons .logging .LogFactory ;
27+ import org .springframework .beans .BeansException ;
28+ import org .springframework .beans .factory .BeanFactoryUtils ;
2529import org .springframework .beans .factory .BeanNameAware ;
30+ import org .springframework .context .ApplicationContext ;
31+ import org .springframework .context .ApplicationContextAware ;
32+ import org .springframework .core .OrderComparator ;
33+ import org .springframework .core .io .ClassPathResource ;
34+ import org .springframework .core .io .Resource ;
35+ import org .springframework .util .ClassUtils ;
2636import org .springframework .util .ObjectUtils ;
2737import org .springframework .ws .NoEndpointFoundException ;
2838import org .springframework .ws .context .MessageContext ;
29- import org .springframework .ws .server .endpoint .MessageEndpointAdapter ;
30- import org .springframework .ws .server .endpoint .PayloadEndpointAdapter ;
3139import org .springframework .ws .transport .WebServiceMessageReceiver ;
40+ import org .springframework .ws .transport .support .DefaultStrategiesHelper ;
3241
3342/**
3443 * Central dispatcher for use withing Spring-WS. Dispatches Web service messages to registered endoints.
5059 * @see EndpointExceptionResolver
5160 * @see org.springframework.web.servlet.DispatcherServlet
5261 */
53- public class MessageDispatcher implements WebServiceMessageReceiver , BeanNameAware {
62+ public class MessageDispatcher implements WebServiceMessageReceiver , BeanNameAware , ApplicationContextAware {
5463
5564 /**
5665 * Log category to use when no mapped endpoint is found for a request.
@@ -67,6 +76,8 @@ public class MessageDispatcher implements WebServiceMessageReceiver, BeanNameAwa
6776 */
6877 protected final Log logger = LogFactory .getLog (getClass ());
6978
79+ private final DefaultStrategiesHelper defaultStrategiesHelper ;
80+
7081 /**
7182 * The registered bean name for this dispatcher.
7283 */
@@ -91,7 +102,8 @@ public class MessageDispatcher implements WebServiceMessageReceiver, BeanNameAwa
91102 * Initializes a new instance of the <code>MessageDispatcher</code>.
92103 */
93104 public MessageDispatcher () {
94- initDefaultStrategies ();
105+ Resource resource = new ClassPathResource (ClassUtils .getShortName (getClass ()) + ".properties" , getClass ());
106+ defaultStrategiesHelper = new DefaultStrategiesHelper (resource );
95107 }
96108
97109 /**
@@ -140,6 +152,12 @@ public void setBeanName(String beanName) {
140152 this .beanName = beanName ;
141153 }
142154
155+ public void setApplicationContext (ApplicationContext applicationContext ) throws BeansException {
156+ initEndpointAdapters (applicationContext );
157+ initEndpointExceptionResolvers (applicationContext );
158+ initEndpointMappings (applicationContext );
159+ }
160+
143161 public void receive (MessageContext messageContext ) throws Exception {
144162 if (logger .isDebugEnabled ()) {
145163 logger .debug ("MessageDispatcher with name '" + beanName + "' received request [" +
@@ -260,21 +278,6 @@ protected boolean handleRequest(EndpointInvocationChain mappedEndpoint, MessageC
260278 return true ;
261279 }
262280
263- /**
264- * Initialize the default implementations for the dispatcher's strategies: <code>MessageEndpointAdapter</code> and
265- * <code>PayloadEndpointAdapter</code>.
266- *
267- * @see #setEndpointAdapters(java.util.List)
268- * @see org.springframework.ws.server.endpoint.MessageEndpointAdapter
269- * @see org.springframework.ws.server.endpoint.PayloadEndpointAdapter
270- */
271- protected void initDefaultStrategies () {
272- List defaultEndpointAdapters = new ArrayList ();
273- defaultEndpointAdapters .add (new MessageEndpointAdapter ());
274- defaultEndpointAdapters .add (new PayloadEndpointAdapter ());
275- setEndpointAdapters (defaultEndpointAdapters );
276- }
277-
278281 /**
279282 * Determine an error <code>SOAPMessage</code> respone via the registered <code>EndpointExceptionResolvers</code>.
280283 * Most likely, the response contains a <code>SOAPFault</code>. If no suitable resolver was found, the exception is
@@ -323,4 +326,76 @@ protected void triggerHandleResponse(EndpointInvocationChain mappedEndpoint,
323326 }
324327 }
325328 }
329+
330+ /**
331+ * Initialize the <code>EndpointAdapters</code> used by this class. If no adapter beans are explictely set by using
332+ * the <code>endpointAdapters</code> property, we use the default strategies.
333+ *
334+ * @see #setEndpointAdapters(java.util.List)
335+ */
336+ private void initEndpointAdapters (ApplicationContext applicationContext ) throws BeansException {
337+ if (endpointAdapters == null ) {
338+ Map matchingBeans = BeanFactoryUtils
339+ .beansOfTypeIncludingAncestors (applicationContext , EndpointAdapter .class , true , false );
340+ if (!matchingBeans .isEmpty ()) {
341+ endpointAdapters = new ArrayList (matchingBeans .values ());
342+ Collections .sort (endpointAdapters , new OrderComparator ());
343+ }
344+ else {
345+ endpointAdapters =
346+ defaultStrategiesHelper .getDefaultStrategies (EndpointAdapter .class , applicationContext );
347+ if (logger .isInfoEnabled () && !endpointAdapters .isEmpty ()) {
348+ logger .info ("No EndpointAdapters found, using defaults" );
349+ }
350+ }
351+ }
352+ }
353+
354+ /**
355+ * Initialize the <code>EndpointExceptionResolver</code> used by this class. If no resolver beans are explictely set
356+ * by using the <code>endpointExceptionResolvers</code> property, we use the default strategies.
357+ *
358+ * @see #setEndpointExceptionResolvers(java.util.List)
359+ */
360+ private void initEndpointExceptionResolvers (ApplicationContext applicationContext ) throws BeansException {
361+ if (endpointExceptionResolvers == null ) {
362+ Map matchingBeans = BeanFactoryUtils
363+ .beansOfTypeIncludingAncestors (applicationContext , EndpointExceptionResolver .class , true , false );
364+ if (!matchingBeans .isEmpty ()) {
365+ endpointExceptionResolvers = new ArrayList (matchingBeans .values ());
366+ Collections .sort (endpointExceptionResolvers , new OrderComparator ());
367+ }
368+ else {
369+ endpointExceptionResolvers = defaultStrategiesHelper
370+ .getDefaultStrategies (EndpointExceptionResolver .class , applicationContext );
371+ if (logger .isInfoEnabled () && !endpointExceptionResolvers .isEmpty ()) {
372+ logger .info ("No EndpointExceptionResolvers found, using defaults" );
373+ }
374+ }
375+ }
376+ }
377+
378+ /**
379+ * Initialize the <code>EndpointMappings</code> used by this class. If no mapping beans are explictely set by using
380+ * the <code>endpointMappings</code> property, we use the default strategies.
381+ *
382+ * @see #setEndpointMappings(java.util.List)
383+ */
384+ private void initEndpointMappings (ApplicationContext applicationContext ) throws BeansException {
385+ if (endpointMappings == null ) {
386+ Map matchingBeans = BeanFactoryUtils
387+ .beansOfTypeIncludingAncestors (applicationContext , EndpointMapping .class , true , false );
388+ if (!matchingBeans .isEmpty ()) {
389+ endpointMappings = new ArrayList (matchingBeans .values ());
390+ Collections .sort (endpointMappings , new OrderComparator ());
391+ }
392+ else {
393+ endpointMappings = defaultStrategiesHelper
394+ .getDefaultStrategies (EndpointMapping .class , applicationContext );
395+ if (logger .isInfoEnabled () && !endpointMappings .isEmpty ()) {
396+ logger .info ("No EndpointMappings found, using defaults" );
397+ }
398+ }
399+ }
400+ }
326401}
0 commit comments