View Javadoc

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