1
2
3
4
5
6
7
8
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
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
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
93
94 class LifecycleEnabledPoolabeObjectFactoryAdapter implements PoolableObjectFactory
95 {
96
97 public void activateObject(Object obj) throws Exception
98 {
99
100 }
101
102 public void destroyObject(Object obj) throws Exception
103 {
104
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
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
137 }
138
139 public boolean validateObject(Object obj)
140 {
141 return true;
142 }
143 }
144 }