View Javadoc

1   /*
2    * $Id: DefaultLifecycleEnabledObjectPool.java 11517 2008-03-31 21:34:19Z dirk.olmes $
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.util.pool;
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.Disposable;
17  import org.mule.api.lifecycle.Startable;
18  import org.mule.api.lifecycle.Stoppable;
19  import org.mule.api.object.ObjectFactory;
20  import org.mule.component.PooledJavaComponent;
21  import org.mule.config.PoolingProfile;
22  
23  import java.util.Iterator;
24  import java.util.LinkedList;
25  import java.util.List;
26  
27  import edu.emory.mathcs.backport.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 LifecycleAdapter} instance for the pool
54       * @param poolingProfile The pooling progile ot be used to configure pool
55       */
56      public DefaultLifecycleEnabledObjectPool(ObjectFactory objectFactory, PoolingProfile poolingProfile)
57      {
58          super(objectFactory, poolingProfile);
59      }
60  
61      protected PoolableObjectFactory getPooledObjectFactory()
62      {
63          return new LifecycleEnabledPoolabeObjectFactoryAdaptor();
64      }
65  
66      public void start() throws MuleException
67      {
68          synchronized (items)
69          {
70              for (Iterator i = items.iterator(); i.hasNext();)
71              {
72                  ((Startable) i.next()).start();
73              }
74          }
75      }
76  
77      public void stop() throws MuleException
78      {
79          synchronized (items)
80          {
81              for (Iterator i = items.iterator(); i.hasNext();)
82              {
83                  ((Stoppable) i.next()).stop();
84              }
85          }
86      }
87  
88      /**
89       * Wraps org.mule.object.ObjectFactory with commons-pool PoolableObjectFactory
90       */
91      class LifecycleEnabledPoolabeObjectFactoryAdaptor implements PoolableObjectFactory
92      {
93  
94          public void activateObject(Object obj) throws Exception
95          {
96              // nothing to do
97          }
98  
99          public void destroyObject(Object obj) throws Exception
100         {
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();
118             if (!started.get() && object instanceof Startable)
119             {
120                 ((Startable) object).start();
121             }
122             synchronized (items)
123             {
124                 items.add(object);
125             }
126             return object;
127         }
128 
129         public void passivateObject(Object obj) throws Exception
130         {
131             // nothing to do
132         }
133 
134         public boolean validateObject(Object obj)
135         {
136             return true;
137         }
138     }
139 }