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.component;
8   
9   import org.mule.api.MuleException;
10  import org.mule.api.component.InterfaceBinding;
11  import org.mule.api.component.JavaComponent;
12  import org.mule.api.component.LifecycleAdapter;
13  import org.mule.api.lifecycle.InitialisationException;
14  import org.mule.api.model.EntryPointResolverSet;
15  import org.mule.api.object.ObjectFactory;
16  import org.mule.api.registry.ServiceException;
17  import org.mule.config.i18n.CoreMessages;
18  import org.mule.config.i18n.MessageFactory;
19  
20  import java.util.List;
21  
22  /**
23   * Default implementation of {@link JavaComponent}. Component lifecycle is
24   * propagated to the component object instance via the {@link LifecycleAdapter}.
25   */
26  public class DefaultJavaComponent extends AbstractJavaComponent
27  {
28  
29      protected LifecycleAdapter singletonComponentLifecycleAdapter;
30  
31      /**
32       * For spring only
33       */
34      public DefaultJavaComponent()
35      {
36          super();
37      }
38  
39      public DefaultJavaComponent(ObjectFactory objectFactory)
40      {
41          super(objectFactory);
42      }
43  
44      public DefaultJavaComponent(ObjectFactory objectFactory,
45                                  EntryPointResolverSet entryPointResolverSet,
46                                  List<InterfaceBinding> bindings)
47      {
48          super(objectFactory, entryPointResolverSet, bindings);
49      }
50  
51      @Override
52      protected void doStart() throws MuleException
53      {
54          super.doStart();
55  
56          // If this component is using a SingletonObjectFactory we should create
57          // LifecycleAdaptor wrapper just once now and not on each event. This also
58          // allows start/stop life-cycle methods to be propagated to singleton
59          // component instances.
60          if (objectFactory != null && objectFactory.isSingleton())
61          {
62              // On first call, create and initialise singleton instance
63              try
64              {
65                  if (singletonComponentLifecycleAdapter == null)
66                  {
67                      singletonComponentLifecycleAdapter = createLifecycleAdaptor();
68                  }
69              }
70              catch (Exception e)
71              {
72                  throw new InitialisationException(
73                      MessageFactory.createStaticMessage("Unable to create instance of POJO service"), e, this);
74  
75              }
76              // On all calls, start if not started.
77              if (!singletonComponentLifecycleAdapter.isStarted())
78              {
79                  try
80                  {
81                      singletonComponentLifecycleAdapter.start();
82                  }
83                  catch (Exception e)
84                  {
85                      throw new ServiceException(CoreMessages.failedToStart("Service '" + flowConstruct.getName() + "'"), e);
86                  }
87              }
88          }
89      }
90  
91      @Override
92      protected void doStop() throws MuleException
93      {
94          super.doStop();
95          // It only makes sense to propagate this life-cycle to singleton component
96          // implementations
97          if (singletonComponentLifecycleAdapter != null && singletonComponentLifecycleAdapter.isStarted())
98          {
99              try
100             {
101                 singletonComponentLifecycleAdapter.stop();
102             }
103             catch (Exception e)
104             {
105                 throw new ServiceException(CoreMessages.failedToStop("Service '" + flowConstruct.getName() + "'"), e);
106             }
107         }
108     }
109 
110     @Override
111     protected void doDispose()
112     {
113         super.doDispose();
114         // It only makes sense to propagating this life-cycle to singleton component
115         // implementations
116         if (singletonComponentLifecycleAdapter != null)
117         {
118             singletonComponentLifecycleAdapter.dispose();
119         }
120     }
121 
122     @Override
123     protected LifecycleAdapter borrowComponentLifecycleAdaptor() throws Exception
124     {
125         LifecycleAdapter componentLifecycleAdapter;
126         if (singletonComponentLifecycleAdapter != null)
127         {
128             componentLifecycleAdapter = singletonComponentLifecycleAdapter;
129         }
130         else
131         {
132             componentLifecycleAdapter = createLifecycleAdaptor();
133             componentLifecycleAdapter.start();
134         }
135         return componentLifecycleAdapter;
136     }
137 
138     @Override
139     protected void returnComponentLifecycleAdaptor(LifecycleAdapter lifecycleAdapter) throws Exception
140     {
141         if (singletonComponentLifecycleAdapter == null && lifecycleAdapter != null)
142         {
143             lifecycleAdapter.stop();
144             lifecycleAdapter.dispose();
145             lifecycleAdapter = null;
146         }
147     }
148 
149 }