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