View Javadoc

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