View Javadoc

1   /*
2    * $Id: AbstractSelectiveRouterFactoryBean.java 22566 2011-07-27 22:52:53Z julien.eluard $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.config.spring.factories;
12  
13  import java.util.Collection;
14  import java.util.Collections;
15  import java.util.Map;
16  import java.util.concurrent.ConcurrentHashMap;
17  
18  import javax.xml.namespace.QName;
19  
20  import org.mule.api.AnnotatedObject;
21  import org.mule.api.processor.MessageProcessor;
22  import org.mule.routing.AbstractSelectiveRouter;
23  import org.mule.routing.MessageProcessorFilterPair;
24  import org.springframework.beans.factory.FactoryBean;
25  
26  public abstract class AbstractSelectiveRouterFactoryBean implements FactoryBean, AnnotatedObject
27  {
28      private MessageProcessor defaultProcessor;
29      private Collection<MessageProcessorFilterPair> conditionalMessageProcessors;
30      private final Map<QName, Object> annotations = new ConcurrentHashMap<QName, Object>();
31  
32      public AbstractSelectiveRouterFactoryBean()
33      {
34          super();
35      }
36  
37      public void setDefaultRoute(MessageProcessorFilterPair conditionalProcessor)
38      {
39          defaultProcessor = conditionalProcessor.getMessageProcessor();
40      }
41  
42      public void setRoutes(Collection<MessageProcessorFilterPair> conditionalMessageProcessors)
43      {
44          this.conditionalMessageProcessors = conditionalMessageProcessors;
45      }
46  
47      public Object getObject() throws Exception
48      {
49          final AbstractSelectiveRouter router = newAbstractSelectiveRouter();
50          router.setAnnotations(getAnnotations());
51          router.setDefaultRoute(defaultProcessor);
52  
53          for (final MessageProcessorFilterPair mpfp : conditionalMessageProcessors)
54          {
55              router.addRoute(mpfp.getMessageProcessor(), mpfp.getFilter());
56          }
57  
58          return router;
59      }
60  
61      protected abstract AbstractSelectiveRouter newAbstractSelectiveRouter();
62  
63      public boolean isSingleton()
64      {
65          return true;
66      }
67  
68      public final Object getAnnotation(QName name)
69      {
70          return annotations.get(name);
71      }
72  
73      public final Map<QName, Object> getAnnotations()
74      {
75          return Collections.unmodifiableMap(annotations);
76      }
77  
78      public synchronized final void setAnnotations(Map<QName, Object> newAnnotations)
79      {
80          annotations.clear();
81          annotations.putAll(newAnnotations);
82      }
83  }