1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.component; |
12 | |
|
13 | |
import org.mule.api.MuleContext; |
14 | |
import org.mule.api.MuleException; |
15 | |
import org.mule.api.component.Component; |
16 | |
import org.mule.api.component.InterfaceBinding; |
17 | |
import org.mule.api.component.LifecycleAdapter; |
18 | |
import org.mule.api.lifecycle.InitialisationCallback; |
19 | |
import org.mule.api.lifecycle.InitialisationException; |
20 | |
import org.mule.api.model.EntryPointResolverSet; |
21 | |
import org.mule.api.object.ObjectFactory; |
22 | |
import org.mule.config.PoolingProfile; |
23 | |
import org.mule.util.pool.DefaultLifecycleEnabledObjectPool; |
24 | |
import org.mule.util.pool.LifecyleEnabledObjectPool; |
25 | |
import org.mule.util.pool.ObjectPool; |
26 | |
|
27 | |
import java.util.List; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
public class PooledJavaComponent extends AbstractJavaComponent |
33 | |
{ |
34 | |
|
35 | |
protected PoolingProfile poolingProfile; |
36 | |
protected LifecyleEnabledObjectPool lifecycleAdapterPool; |
37 | |
|
38 | |
public PooledJavaComponent() |
39 | |
{ |
40 | 0 | super(); |
41 | 0 | } |
42 | |
|
43 | |
public PooledJavaComponent(ObjectFactory objectFactory) |
44 | |
{ |
45 | 0 | this(objectFactory, null); |
46 | 0 | } |
47 | |
|
48 | |
public PooledJavaComponent(ObjectFactory objectFactory, PoolingProfile poolingProfile) |
49 | |
{ |
50 | 0 | super(objectFactory); |
51 | 0 | this.poolingProfile = poolingProfile; |
52 | 0 | } |
53 | |
|
54 | |
public PooledJavaComponent(ObjectFactory objectFactory, |
55 | |
PoolingProfile poolingProfile, |
56 | |
EntryPointResolverSet entryPointResolverSet, |
57 | |
List<InterfaceBinding> bindings) |
58 | |
{ |
59 | 0 | super(objectFactory, entryPointResolverSet, bindings); |
60 | 0 | this.poolingProfile = poolingProfile; |
61 | 0 | } |
62 | |
|
63 | |
@Override |
64 | |
protected LifecycleAdapter borrowComponentLifecycleAdaptor() throws Exception |
65 | |
{ |
66 | 0 | return (LifecycleAdapter) lifecycleAdapterPool.borrowObject(); |
67 | |
} |
68 | |
|
69 | |
@Override |
70 | |
protected void returnComponentLifecycleAdaptor(LifecycleAdapter lifecycleAdapter) |
71 | |
{ |
72 | 0 | lifecycleAdapterPool.returnObject(lifecycleAdapter); |
73 | 0 | } |
74 | |
|
75 | |
@Override |
76 | |
protected void doStart() throws MuleException |
77 | |
{ |
78 | 0 | super.doStart(); |
79 | |
|
80 | |
|
81 | 0 | lifecycleAdapterPool = new DefaultLifecycleEnabledObjectPool(new LifeCycleAdapterFactory(), poolingProfile, muleContext); |
82 | 0 | lifecycleAdapterPool.initialise(); |
83 | 0 | lifecycleAdapterPool.start(); |
84 | 0 | } |
85 | |
|
86 | |
@Override |
87 | |
protected void doStop() throws MuleException |
88 | |
{ |
89 | 0 | super.doStop(); |
90 | 0 | if (lifecycleAdapterPool != null) |
91 | |
{ |
92 | 0 | lifecycleAdapterPool.stop(); |
93 | 0 | lifecycleAdapterPool.close(); |
94 | 0 | lifecycleAdapterPool = null; |
95 | |
} |
96 | 0 | } |
97 | |
|
98 | |
public void setPoolingProfile(PoolingProfile poolingProfile) |
99 | |
{ |
100 | |
|
101 | |
|
102 | |
|
103 | 0 | this.poolingProfile = poolingProfile; |
104 | 0 | } |
105 | |
|
106 | |
public PoolingProfile getPoolingProfile() |
107 | |
{ |
108 | 0 | return poolingProfile; |
109 | |
} |
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | 0 | protected class LifeCycleAdapterFactory implements ObjectFactory |
122 | |
{ |
123 | |
public Object getInstance(MuleContext context) throws Exception |
124 | |
{ |
125 | 0 | return createLifecycleAdaptor(); |
126 | |
} |
127 | |
|
128 | |
public Class<?> getObjectClass() |
129 | |
{ |
130 | 0 | return LifecycleAdapter.class; |
131 | |
} |
132 | |
|
133 | |
public void initialise() throws InitialisationException |
134 | |
{ |
135 | 0 | objectFactory.initialise(); |
136 | 0 | } |
137 | |
|
138 | |
public void dispose() |
139 | |
{ |
140 | 0 | objectFactory.dispose(); |
141 | 0 | } |
142 | |
|
143 | |
public void addObjectInitialisationCallback(InitialisationCallback callback) |
144 | |
{ |
145 | 0 | objectFactory.addObjectInitialisationCallback(callback); |
146 | 0 | } |
147 | |
|
148 | |
public boolean isSingleton() |
149 | |
{ |
150 | 0 | return false; |
151 | |
} |
152 | |
|
153 | |
public boolean isExternallyManagedLifecycle() |
154 | |
{ |
155 | 0 | return objectFactory.isExternallyManagedLifecycle(); |
156 | |
} |
157 | |
|
158 | |
public boolean isAutoWireObject() |
159 | |
{ |
160 | 0 | return objectFactory.isAutoWireObject(); |
161 | |
} |
162 | |
} |
163 | |
} |