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.util.pool;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.api.MuleException;
11  import org.mule.api.component.JavaComponent;
12  import org.mule.api.component.LifecycleAdapter;
13  import org.mule.api.lifecycle.Disposable;
14  import org.mule.api.lifecycle.Startable;
15  import org.mule.api.lifecycle.Stoppable;
16  import org.mule.api.object.ObjectFactory;
17  import org.mule.component.PooledJavaComponent;
18  import org.mule.config.PoolingProfile;
19  
20  import java.util.Iterator;
21  import java.util.LinkedList;
22  import java.util.List;
23  
24  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.commons.pool.PoolableObjectFactory;
28  
29  /**
30   * A LifecyleEnabledObjectPool implementation for pooling {@link LifecycleAdapter}
31   * instances for implementations of {@link JavaComponent} that require
32   * {@link LifecycleAdapter} pooling such as {@link PooledJavaComponent}.
33   * 
34   * @see PooledJavaComponent
35   */
36  public class DefaultLifecycleEnabledObjectPool extends CommonsPoolObjectPool implements LifecyleEnabledObjectPool
37  {
38      /**
39       * logger used by this class
40       */
41      protected static final Log logger = LogFactory.getLog(DefaultLifecycleEnabledObjectPool.class);
42  
43      protected AtomicBoolean started = new AtomicBoolean(false);
44  
45      private List items = new LinkedList();
46  
47      /**
48       * @param objectFactory The object factory that should be used to create new
49       *            {@link org.mule.api.component.LifecycleAdapter} instance for the pool
50       * @param poolingProfile The pooling progile ot be used to configure pool
51       * @param muleContext
52       */
53      public DefaultLifecycleEnabledObjectPool(ObjectFactory objectFactory, PoolingProfile poolingProfile, MuleContext muleContext)
54      {
55          super(objectFactory, poolingProfile, muleContext);
56      }
57  
58      protected PoolableObjectFactory getPooledObjectFactory()
59      {
60          return new LifecycleEnabledPoolabeObjectFactoryAdapter();
61      }
62  
63      public void start() throws MuleException
64      {
65          started.set(true);
66          synchronized (items)
67          {
68              for (Iterator i = items.iterator(); i.hasNext();)
69              {
70                  ((Startable) i.next()).start();
71              }
72          }
73      }
74  
75      public void stop() throws MuleException
76      {
77          started.set(false);
78          synchronized (items)
79          {
80              for (Iterator i = items.iterator(); i.hasNext();)
81              {
82                  ((Stoppable) i.next()).stop();
83              }
84          }
85      }
86  
87      /**
88       * Wraps org.mule.object.ObjectFactory with commons-pool PoolableObjectFactory
89       */
90      class LifecycleEnabledPoolabeObjectFactoryAdapter implements PoolableObjectFactory
91      {
92  
93          public void activateObject(Object obj) throws Exception
94          {
95              // nothing to do
96          }
97  
98          public void destroyObject(Object obj) throws Exception
99          {
100             // Only stop existing objects if they havn't already been stopped
101             if (started.get() && obj instanceof Stoppable)
102             {
103                 ((Stoppable) obj).stop();
104             }
105             if (obj instanceof Disposable)
106             {
107                 ((Disposable) obj).dispose();
108             }
109             synchronized (items)
110             {
111                 items.remove(obj);
112             }
113         }
114 
115         public Object makeObject() throws Exception
116         {
117             Object object = objectFactory.getInstance(muleContext);
118             // Only start newly created objects if pool is started
119             if (started.get() && object instanceof Startable)
120             {
121                 ((Startable) object).start();
122             }
123             synchronized (items)
124             {
125                 items.add(object);
126             }
127             return object;
128         }
129 
130         public void passivateObject(Object obj) throws Exception
131         {
132             // nothing to do
133         }
134 
135         public boolean validateObject(Object obj)
136         {
137             return true;
138         }
139     }
140 }