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