View Javadoc

1   /*
2    * $Id: AbstractFlowConstructFactoryBean.java 20013 2010-10-27 11:24:22Z dirk.olmes $
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.source.MessageSource;
22  import org.mule.construct.AbstractFlowConstruct;
23  import org.mule.construct.builder.AbstractFlowConstructBuilder;
24  import org.mule.construct.builder.AbstractFlowConstructWithSingleInboundEndpointBuilder;
25  import org.mule.processor.builder.InterceptingChainMessageProcessorBuilder;
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(InterceptingChainMessageProcessorBuilder builder)
50          {
51              // NOOP
52          }
53      }
54  
55      protected ApplicationContext applicationContext;
56      protected MuleContext muleContext;
57  
58      // FIXME terrible hack to get around the first call to getObject that comes too
59      // soon (nothing is injected yet)
60      protected AbstractFlowConstruct flowConstruct = NULL_FLOW_CONSTRUCT;
61  
62      public boolean isSingleton()
63      {
64          return true;
65      }
66  
67      public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
68      {
69          this.applicationContext = applicationContext;
70      }
71  
72      public void setMuleContext(MuleContext muleContext)
73      {
74          this.muleContext = muleContext;
75      }
76  
77      protected abstract AbstractFlowConstructBuilder<? extends AbstractFlowConstructBuilder<?, ?>, ? extends AbstractFlowConstruct> getFlowConstructBuilder();
78  
79      public void setName(String name)
80      {
81          getFlowConstructBuilder().name(name);
82      }
83  
84      public void setMessageSource(MessageSource messageSource)
85      {
86          final AbstractFlowConstructBuilder<?, ?> flowConstructBuilder = getFlowConstructBuilder();
87  
88          if ((flowConstructBuilder instanceof AbstractFlowConstructWithSingleInboundEndpointBuilder<?, ?>)
89              && (messageSource instanceof InboundEndpoint))
90          {
91              ((AbstractFlowConstructWithSingleInboundEndpointBuilder<?, ?>) flowConstructBuilder).inboundEndpoint((InboundEndpoint) messageSource);
92          }
93          else
94          {
95              flowConstructBuilder.messageSource(messageSource);
96          }
97      }
98  
99      public void setExceptionListener(MessagingExceptionHandler exceptionListener)
100     {
101         getFlowConstructBuilder().exceptionStrategy(exceptionListener);
102     }
103 
104     public void afterPropertiesSet() throws Exception
105     {
106         flowConstruct = createFlowConstruct();
107     }
108 
109     public void initialise() throws InitialisationException
110     {
111         flowConstruct.initialise();
112     }
113 
114     public FlowConstruct getObject() throws Exception
115     {
116         return flowConstruct;
117     }
118 
119     protected AbstractFlowConstruct createFlowConstruct() throws MuleException
120     {
121         return getFlowConstructBuilder().build(muleContext);
122     }
123 }