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 | 104 | public class DefaultLifecycleEnabledObjectPool extends CommonsPoolObjectPool implements LifecyleEnabledObjectPool |
41 | |
{ |
42 | |
|
43 | |
|
44 | |
|
45 | 2 | protected static final Log logger = LogFactory.getLog(DefaultLifecycleEnabledObjectPool.class); |
46 | |
|
47 | 26 | protected AtomicBoolean started = new AtomicBoolean(false); |
48 | |
|
49 | 26 | private List items = new LinkedList(); |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
public DefaultLifecycleEnabledObjectPool(ObjectFactory objectFactory, PoolingProfile poolingProfile) |
57 | |
{ |
58 | 26 | super(objectFactory, poolingProfile); |
59 | 26 | } |
60 | |
|
61 | |
protected PoolableObjectFactory getPooledObjectFactory() |
62 | |
{ |
63 | 26 | return new LifecycleEnabledPoolabeObjectFactoryAdaptor(); |
64 | |
} |
65 | |
|
66 | |
public void start() throws MuleException |
67 | |
{ |
68 | 26 | synchronized (items) |
69 | |
{ |
70 | 26 | for (Iterator i = items.iterator(); i.hasNext();) |
71 | |
{ |
72 | 0 | ((Startable) i.next()).start(); |
73 | |
} |
74 | 26 | } |
75 | 26 | } |
76 | |
|
77 | |
public void stop() throws MuleException |
78 | |
{ |
79 | 8 | synchronized (items) |
80 | |
{ |
81 | 8 | for (Iterator i = items.iterator(); i.hasNext();) |
82 | |
{ |
83 | 8 | ((Stoppable) i.next()).stop(); |
84 | |
} |
85 | 8 | } |
86 | 8 | } |
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | 26 | class LifecycleEnabledPoolabeObjectFactoryAdaptor implements PoolableObjectFactory |
92 | |
{ |
93 | |
|
94 | |
public void activateObject(Object obj) throws Exception |
95 | |
{ |
96 | |
|
97 | 52 | } |
98 | |
|
99 | |
public void destroyObject(Object obj) throws Exception |
100 | |
{ |
101 | 4 | if (started.get() && obj instanceof Stoppable) |
102 | |
{ |
103 | 0 | ((Stoppable) obj).stop(); |
104 | |
} |
105 | 4 | if (obj instanceof Disposable) |
106 | |
{ |
107 | 4 | ((Disposable) obj).dispose(); |
108 | |
} |
109 | 4 | synchronized (items) |
110 | |
{ |
111 | 4 | items.remove(obj); |
112 | 4 | } |
113 | 4 | } |
114 | |
|
115 | |
public Object makeObject() throws Exception |
116 | |
{ |
117 | 48 | Object object = objectFactory.getInstance(); |
118 | 48 | if (!started.get() && object instanceof Startable) |
119 | |
{ |
120 | 48 | ((Startable) object).start(); |
121 | |
} |
122 | 48 | synchronized (items) |
123 | |
{ |
124 | 48 | items.add(object); |
125 | 48 | } |
126 | 48 | return object; |
127 | |
} |
128 | |
|
129 | |
public void passivateObject(Object obj) throws Exception |
130 | |
{ |
131 | |
|
132 | 8 | } |
133 | |
|
134 | |
public boolean validateObject(Object obj) |
135 | |
{ |
136 | 0 | return true; |
137 | |
} |
138 | |
} |
139 | |
} |