View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.config.spring.factories;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.api.MuleException;
11  import org.mule.api.construct.FlowConstruct;
12  import org.mule.api.context.MuleContextAware;
13  import org.mule.api.endpoint.InboundEndpoint;
14  import org.mule.api.exception.MessagingExceptionHandler;
15  import org.mule.api.lifecycle.Initialisable;
16  import org.mule.api.lifecycle.InitialisationException;
17  import org.mule.api.processor.MessageProcessorChainBuilder;
18  import org.mule.api.source.MessageSource;
19  import org.mule.construct.AbstractFlowConstruct;
20  import org.mule.construct.builder.AbstractFlowConstructBuilder;
21  import org.mule.construct.builder.AbstractFlowConstructWithSingleInboundEndpointBuilder;
22  
23  import org.springframework.beans.BeansException;
24  import org.springframework.beans.factory.FactoryBean;
25  import org.springframework.beans.factory.InitializingBean;
26  import org.springframework.context.ApplicationContext;
27  import org.springframework.context.ApplicationContextAware;
28  
29  public abstract class AbstractFlowConstructFactoryBean implements FactoryBean<FlowConstruct>, 
30      InitializingBean, ApplicationContextAware, MuleContextAware, Initialisable
31  {
32      private static final NullFlowConstruct NULL_FLOW_CONSTRUCT = new NullFlowConstruct("noop", null);
33  
34      /*
35       * Shameful hack, read FIXME below
36       */
37      private static final class NullFlowConstruct extends AbstractFlowConstruct
38      {
39          public NullFlowConstruct(String name, MuleContext muleContext)
40          {
41              super(name, muleContext);
42          }
43  
44          @Override
45          protected void configureMessageProcessors(MessageProcessorChainBuilder builder)
46          {
47              // NOOP
48          }
49  
50          @Override
51          public String getConstructType()
52          {
53              return "NULL";
54          }
55      }
56  
57      protected ApplicationContext applicationContext;
58      protected MuleContext muleContext;
59  
60      // FIXME terrible hack to get around the first call to getObject that comes too
61      // soon (nothing is injected yet)
62      protected AbstractFlowConstruct flowConstruct = NULL_FLOW_CONSTRUCT;
63  
64      public boolean isSingleton()
65      {
66          return true;
67      }
68  
69      public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
70      {
71          this.applicationContext = applicationContext;
72      }
73  
74      public void setMuleContext(MuleContext muleContext)
75      {
76          this.muleContext = muleContext;
77      }
78  
79      protected abstract AbstractFlowConstructBuilder<? extends AbstractFlowConstructBuilder<?, ?>, ? extends AbstractFlowConstruct> getFlowConstructBuilder();
80  
81      public void setName(String name)
82      {
83          getFlowConstructBuilder().name(name);
84      }
85  
86      public void setInitialState(String initialState)
87      {
88          getFlowConstructBuilder().initialState(initialState);
89      }
90  
91      public void setMessageSource(MessageSource messageSource)
92      {
93          final AbstractFlowConstructBuilder<?, ?> flowConstructBuilder = getFlowConstructBuilder();
94  
95          if ((flowConstructBuilder instanceof AbstractFlowConstructWithSingleInboundEndpointBuilder<?, ?>)
96              && (messageSource instanceof InboundEndpoint))
97          {
98              ((AbstractFlowConstructWithSingleInboundEndpointBuilder<?, ?>) flowConstructBuilder).inboundEndpoint((InboundEndpoint) messageSource);
99          }
100         else
101         {
102             flowConstructBuilder.messageSource(messageSource);
103         }
104     }
105 
106     public void setExceptionListener(MessagingExceptionHandler exceptionListener)
107     {
108         getFlowConstructBuilder().exceptionStrategy(exceptionListener);
109     }
110 
111     public void afterPropertiesSet() throws Exception
112     {
113         flowConstruct = createFlowConstruct();
114     }
115 
116     public void initialise() throws InitialisationException
117     {
118         flowConstruct.initialise();
119     }
120 
121     public FlowConstruct getObject() throws Exception
122     {
123         return flowConstruct;
124     }
125 
126     protected AbstractFlowConstruct createFlowConstruct() throws MuleException
127     {
128         return getFlowConstructBuilder().build(muleContext);
129     }
130 }