001: /*
002: * $Id: DefaultLifecycleEnabledObjectPool.java 11379 2008-03-17 02:46:56Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.util.pool;
012:
013: import org.mule.api.MuleException;
014: import org.mule.api.component.JavaComponent;
015: import org.mule.api.component.LifecycleAdapter;
016: import org.mule.api.lifecycle.Disposable;
017: import org.mule.api.lifecycle.LifecycleTransitionResult;
018: import org.mule.api.lifecycle.Startable;
019: import org.mule.api.lifecycle.Stoppable;
020: import org.mule.api.object.ObjectFactory;
021: import org.mule.config.PoolingProfile;
022:
023: import java.util.Iterator;
024: import java.util.LinkedList;
025: import java.util.List;
026:
027: import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031: import org.apache.commons.pool.PoolableObjectFactory;
032:
033: /**
034: * A LifecyleEnabledObjectPool implementation for pooling {@link LifecycleAdapter}
035: * instances for implementations of {@link JavaComponent} that require
036: * {@link LifecycleAdapter} pooling such as {@link PooledJavaComponent}.
037: *
038: * @see PooledJavaComponent
039: */
040: public class DefaultLifecycleEnabledObjectPool extends
041: CommonsPoolObjectPool implements LifecyleEnabledObjectPool {
042: /**
043: * logger used by this class
044: */
045: protected static final Log logger = LogFactory
046: .getLog(DefaultLifecycleEnabledObjectPool.class);
047:
048: protected AtomicBoolean started = new AtomicBoolean(false);
049:
050: private List items = new LinkedList();
051:
052: /**
053: * @param objectFactory The object factory that should be used to create new
054: * {@link LifecycleAdapter} instance for the pool
055: * @param poolingProfile The pooling progile ot be used to configure pool
056: */
057: public DefaultLifecycleEnabledObjectPool(
058: ObjectFactory objectFactory, PoolingProfile poolingProfile) {
059: super (objectFactory, poolingProfile);
060: }
061:
062: protected PoolableObjectFactory getPooledObjectFactory() {
063: return new LifecycleEnabledPoolabeObjectFactoryAdaptor();
064: }
065:
066: public LifecycleTransitionResult start() throws MuleException {
067: synchronized (items) {
068: for (Iterator i = items.iterator(); i.hasNext();) {
069: ((Startable) i.next()).start();
070: }
071: }
072: return LifecycleTransitionResult.OK;
073: }
074:
075: public LifecycleTransitionResult stop() throws MuleException {
076: synchronized (items) {
077: for (Iterator i = items.iterator(); i.hasNext();) {
078: ((Stoppable) i.next()).stop();
079: }
080: }
081: return LifecycleTransitionResult.OK;
082: }
083:
084: /**
085: * Wraps org.mule.object.ObjectFactory with commons-pool PoolableObjectFactory
086: */
087: class LifecycleEnabledPoolabeObjectFactoryAdaptor implements
088: PoolableObjectFactory {
089:
090: public void activateObject(Object obj) throws Exception {
091: // nothing to do
092: }
093:
094: public void destroyObject(Object obj) throws Exception {
095: if (started.get() && obj instanceof Stoppable) {
096: ((Stoppable) obj).stop();
097: }
098: if (obj instanceof Disposable) {
099: ((Disposable) obj).dispose();
100: }
101: synchronized (items) {
102: items.remove(obj);
103: }
104: }
105:
106: public Object makeObject() throws Exception {
107: Object object = objectFactory.getInstance();
108: if (!started.get() && object instanceof Startable) {
109: ((Startable) object).start();
110: }
111: synchronized (items) {
112: items.add(object);
113: }
114: return object;
115: }
116:
117: public void passivateObject(Object obj) throws Exception {
118: // nothing to do
119: }
120:
121: public boolean validateObject(Object obj) {
122: return true;
123: }
124: }
125: }
|