View Javadoc

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