View Javadoc

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