View Javadoc

1   /*
2    * $Id: PooledJavaComponent.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.LifecycleAdapter;
15  import org.mule.api.lifecycle.InitialisationCallback;
16  import org.mule.api.lifecycle.InitialisationException;
17  import org.mule.api.model.EntryPointResolverSet;
18  import org.mule.api.object.ObjectFactory;
19  import org.mule.api.routing.NestedRouterCollection;
20  import org.mule.config.PoolingProfile;
21  import org.mule.util.pool.DefaultLifecycleEnabledObjectPool;
22  import org.mule.util.pool.LifecyleEnabledObjectPool;
23  
24  /**
25   * <code>PooledJavaComponent</code> implements pooling.
26   */
27  public class PooledJavaComponent extends AbstractJavaComponent
28  {
29  
30      protected PoolingProfile poolingProfile;
31      protected LifecyleEnabledObjectPool lifecycleAdapterPool;
32  
33      public PooledJavaComponent()
34      {
35          super();
36      }
37  
38      public PooledJavaComponent(ObjectFactory objectFactory)
39      {
40          this(objectFactory, null);
41      }
42  
43      public PooledJavaComponent(ObjectFactory objectFactory, PoolingProfile poolingProfile)
44      {
45          super(objectFactory);
46          this.poolingProfile = poolingProfile;
47      }
48  
49      public PooledJavaComponent(ObjectFactory objectFactory,
50                                 PoolingProfile poolingProfile,
51                                 EntryPointResolverSet entryPointResolverSet,
52                                 NestedRouterCollection nestedRouterCollection)
53      {
54          super(objectFactory, entryPointResolverSet, nestedRouterCollection);
55          this.poolingProfile = poolingProfile;
56      }
57  
58      protected LifecycleAdapter borrowComponentLifecycleAdaptor() throws Exception
59      {
60          return (LifecycleAdapter) lifecycleAdapterPool.borrowObject();
61      }
62  
63      protected void returnComponentLifecycleAdaptor(LifecycleAdapter lifecycleAdapter)
64      {
65          lifecycleAdapterPool.returnObject(lifecycleAdapter);
66      }
67  
68      protected void doStart() throws MuleException
69      {
70          super.doStart();
71          // Wrap pool's objectFactory with a LifeCycleAdaptor factory so we pool
72          // LifeCycleAdaptor's and not just pojo instances.
73          lifecycleAdapterPool = new DefaultLifecycleEnabledObjectPool(new LifeCycleAdaptorFactory(), poolingProfile);
74          lifecycleAdapterPool.initialise();
75          lifecycleAdapterPool.start();
76      }
77  
78      protected void doStop() throws MuleException
79      {
80          super.doStop();
81          if (lifecycleAdapterPool != null)
82          {
83              lifecycleAdapterPool.stop();
84              lifecycleAdapterPool.close();
85              lifecycleAdapterPool = null;
86          }
87      }
88  
89      public void setPoolingProfile(PoolingProfile poolingProfile)
90      {
91          // TODO What happens if this is set while component is started? Should we i)
92          // do nothing ii) issue warning iii) stop/start the pool
93          // (!!) iv) throw exception?
94          this.poolingProfile = poolingProfile;
95      }
96  
97      public PoolingProfile getPoolingProfile()
98      {
99          return poolingProfile;
100     }
101 
102     /**
103      * <code>LifeCycleAdaptorFactory</code> wraps the Component' s
104      * {@link ObjectFactory}. The LifeCycleAdaptorFactory "getInstance()" method
105      * creates a new {@link LifecycleAdapter} wrapping the object instance obtained
106      * for the component instance {@link ObjectFactory} set on the {@link Component}.<br/>
107      * This allows us to keep {@link LifecycleAdapter} creation in the Component and
108      * out of the {@link DefaultLifecyleEnabledObjectPool} and to use the generic
109      * {@link ObjectPool} interface.
110      */
111     protected class LifeCycleAdaptorFactory implements ObjectFactory
112     {
113 
114         public Object getInstance() throws Exception
115         {
116             return createLifeCycleAdaptor();
117         }
118 
119         public Class getObjectClass()
120         {
121             return LifecycleAdapter.class;
122         }
123 
124         public void initialise() throws InitialisationException
125         {
126             objectFactory.initialise();
127         }
128 
129         public void dispose()
130         {
131             objectFactory.dispose();
132         }
133 
134         public void addObjectInitialisationCallback(InitialisationCallback callback)
135         {
136             objectFactory.addObjectInitialisationCallback(callback);
137         }
138 
139         public boolean isSingleton()
140         {
141             return false;
142         }
143     }
144 
145 }