1
2
3
4
5
6
7
8
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
35
36
37
38
39
40 public class DefaultLifecycleEnabledObjectPool extends CommonsPoolObjectPool implements LifecyleEnabledObjectPool
41 {
42
43
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
53
54
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
90
91 class LifecycleEnabledPoolabeObjectFactoryAdaptor implements PoolableObjectFactory
92 {
93
94 public void activateObject(Object obj) throws Exception
95 {
96
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
132 }
133
134 public boolean validateObject(Object obj)
135 {
136 return true;
137 }
138 }
139 }