View Javadoc

1   /*
2    * $Id: AbstractFlowConstructFactoryBean.java 20203 2010-11-17 01:54:38Z mike.schilling $
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 org.mule.api.MuleContext;
14  import org.mule.api.MuleException;
15  import org.mule.api.construct.FlowConstruct;
16  import org.mule.api.context.MuleContextAware;
17  import org.mule.api.endpoint.InboundEndpoint;
18  import org.mule.api.exception.MessagingExceptionHandler;
19  import org.mule.api.lifecycle.Initialisable;
20  import org.mule.api.lifecycle.InitialisationException;
21  import org.mule.api.processor.MessageProcessorChainBuilder;
22  import org.mule.api.source.MessageSource;
23  import org.mule.construct.AbstractFlowConstruct;
24  import org.mule.construct.builder.AbstractFlowConstructBuilder;
25  import org.mule.construct.builder.AbstractFlowConstructWithSingleInboundEndpointBuilder;
26  
27  import org.springframework.beans.BeansException;
28  import org.springframework.beans.factory.FactoryBean;
29  import org.springframework.beans.factory.InitializingBean;
30  import org.springframework.context.ApplicationContext;
31  import org.springframework.context.ApplicationContextAware;
32  
33  public abstract class AbstractFlowConstructFactoryBean implements FactoryBean<FlowConstruct>, 
34      InitializingBean, ApplicationContextAware, MuleContextAware, Initialisable
35  {
36      private static final NullFlowConstruct NULL_FLOW_CONSTRUCT = new NullFlowConstruct("noop", null);
37  
38      /*
39       * Shameful hack, read FIXME below
40       */
41      private static final class NullFlowConstruct extends AbstractFlowConstruct
42      {
43          public NullFlowConstruct(String name, MuleContext muleContext)
44          {
45              super(name, muleContext);
46          }
47  
48          @Override
49          protected void configureMessageProcessors(MessageProcessorChainBuilder builder)
50          {
51              // NOOP
52          }
53  
54          @Override
55          public String getConstructType()
56          {
57              return "NULL";
58          }
59      }
60  
61      protected ApplicationContext applicationContext;
62      protected MuleContext muleContext;
63  
64      // FIXME terrible hack to get around the first call to getObject that comes too
65      // soon (nothing is injected yet)
66      protected AbstractFlowConstruct flowConstruct = NULL_FLOW_CONSTRUCT;
67  
68      public boolean isSingleton()
69      {
70          return true;
71      }
72  
73      public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
74      {
75          this.applicationContext = applicationContext;
76      }
77  
78      public void setMuleContext(MuleContext muleContext)
79      {
80          this.muleContext = muleContext;
81      }
82  
83      protected abstract AbstractFlowConstructBuilder<? extends AbstractFlowConstructBuilder<?, ?>, ? extends AbstractFlowConstruct> getFlowConstructBuilder();
84  
85      public void setName(String name)
86      {
87          getFlowConstructBuilder().name(name);
88      }
89  
90      public void setMessageSource(MessageSource messageSource)
91      {
92          final AbstractFlowConstructBuilder<?, ?> flowConstructBuilder = getFlowConstructBuilder();
93  
94          if ((flowConstructBuilder instanceof AbstractFlowConstructWithSingleInboundEndpointBuilder<?, ?>)
95              && (messageSource instanceof InboundEndpoint))
96          {
97              ((AbstractFlowConstructWithSingleInboundEndpointBuilder<?, ?>) flowConstructBuilder).inboundEndpoint((InboundEndpoint) messageSource);
98          }
99          else
100         {
101             flowConstructBuilder.messageSource(messageSource);
102         }
103     }
104 
105     public void setExceptionListener(MessagingExceptionHandler exceptionListener)
106     {
107         getFlowConstructBuilder().exceptionStrategy(exceptionListener);
108     }
109 
110     public void afterPropertiesSet() throws Exception
111     {
112         flowConstruct = createFlowConstruct();
113     }
114 
115     public void initialise() throws InitialisationException
116     {
117         flowConstruct.initialise();
118     }
119 
120     public FlowConstruct getObject() throws Exception
121     {
122         return flowConstruct;
123     }
124 
125     protected AbstractFlowConstruct createFlowConstruct() throws MuleException
126     {
127         return getFlowConstructBuilder().build(muleContext);
128     }
129 }