1
2
3
4
5
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
31
32
33
34
35
36 public class DefaultLifecycleEnabledObjectPool extends CommonsPoolObjectPool implements LifecyleEnabledObjectPool
37 {
38
39
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
49
50
51
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
89
90 class LifecycleEnabledPoolabeObjectFactoryAdapter implements PoolableObjectFactory
91 {
92
93 public void activateObject(Object obj) throws Exception
94 {
95
96 }
97
98 public void destroyObject(Object obj) throws Exception
99 {
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(muleContext);
118
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
133 }
134
135 public boolean validateObject(Object obj)
136 {
137 return true;
138 }
139 }
140 }