1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.transport; |
8 | |
|
9 | |
import org.mule.api.MuleException; |
10 | |
import org.mule.api.endpoint.OutboundEndpoint; |
11 | |
import org.mule.api.lifecycle.Disposable; |
12 | |
import org.mule.api.lifecycle.Startable; |
13 | |
import org.mule.api.lifecycle.Stoppable; |
14 | |
import org.mule.api.transport.MessageDispatcher; |
15 | |
import org.mule.api.transport.MessageDispatcherFactory; |
16 | |
import org.mule.config.i18n.CoreMessages; |
17 | |
|
18 | |
import org.apache.commons.pool.KeyedPoolableObjectFactory; |
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
public class KeyedPoolMessageDispatcherFactoryAdapter |
29 | |
implements MessageDispatcherFactory, KeyedPoolableObjectFactory |
30 | |
{ |
31 | |
private final MessageDispatcherFactory factory; |
32 | |
|
33 | |
public KeyedPoolMessageDispatcherFactoryAdapter(MessageDispatcherFactory factory) |
34 | |
{ |
35 | 0 | super(); |
36 | |
|
37 | 0 | if (factory == null) |
38 | |
{ |
39 | 0 | throw new IllegalArgumentException(CoreMessages.objectIsNull("factory").toString()); |
40 | |
} |
41 | |
|
42 | 0 | this.factory = factory; |
43 | 0 | } |
44 | |
|
45 | |
public void activateObject(Object key, Object obj) throws Exception |
46 | |
{ |
47 | 0 | OutboundEndpoint endpoint = (OutboundEndpoint)key; |
48 | |
|
49 | 0 | applyLifecycle((MessageDispatcher)obj); |
50 | |
|
51 | 0 | factory.activate((OutboundEndpoint) key, (MessageDispatcher) obj); |
52 | 0 | } |
53 | |
|
54 | |
public void destroyObject(Object key, Object obj) throws Exception |
55 | |
{ |
56 | 0 | factory.destroy((OutboundEndpoint) key, (MessageDispatcher) obj); |
57 | 0 | } |
58 | |
|
59 | |
public Object makeObject(Object key) throws Exception |
60 | |
{ |
61 | 0 | OutboundEndpoint endpoint = (OutboundEndpoint) key; |
62 | 0 | MessageDispatcher dispatcher = factory.create(endpoint); |
63 | 0 | applyLifecycle(dispatcher); |
64 | 0 | return dispatcher; |
65 | |
} |
66 | |
|
67 | |
public void passivateObject(Object key, Object obj) throws Exception |
68 | |
{ |
69 | 0 | factory.passivate((OutboundEndpoint) key, (MessageDispatcher) obj); |
70 | 0 | } |
71 | |
|
72 | |
public boolean validateObject(Object key, Object obj) |
73 | |
{ |
74 | 0 | return factory.validate((OutboundEndpoint) key, (MessageDispatcher) obj); |
75 | |
} |
76 | |
|
77 | |
public boolean isCreateDispatcherPerRequest() |
78 | |
{ |
79 | 0 | return factory.isCreateDispatcherPerRequest(); |
80 | |
} |
81 | |
|
82 | |
public MessageDispatcher create(OutboundEndpoint endpoint) throws MuleException |
83 | |
{ |
84 | 0 | return factory.create(endpoint); |
85 | |
} |
86 | |
|
87 | |
public void activate(OutboundEndpoint endpoint, MessageDispatcher dispatcher) throws MuleException |
88 | |
{ |
89 | |
|
90 | 0 | applyLifecycle(dispatcher); |
91 | 0 | factory.activate(endpoint, dispatcher); |
92 | 0 | } |
93 | |
|
94 | |
public void destroy(OutboundEndpoint endpoint, MessageDispatcher dispatcher) |
95 | |
{ |
96 | 0 | factory.destroy(endpoint, dispatcher); |
97 | 0 | } |
98 | |
|
99 | |
public void passivate(OutboundEndpoint endpoint, MessageDispatcher dispatcher) |
100 | |
{ |
101 | 0 | factory.passivate(endpoint, dispatcher); |
102 | 0 | } |
103 | |
|
104 | |
public boolean validate(OutboundEndpoint endpoint, MessageDispatcher dispatcher) |
105 | |
{ |
106 | 0 | return factory.validate(endpoint, dispatcher); |
107 | |
} |
108 | |
|
109 | |
protected void applyLifecycle(MessageDispatcher dispatcher) throws MuleException |
110 | |
{ |
111 | 0 | String phase = ((AbstractConnector)dispatcher.getConnector()).getLifecycleManager().getCurrentPhase(); |
112 | 0 | if(phase.equals(Startable.PHASE_NAME) && !dispatcher.getLifecycleState().isStarted()) |
113 | |
{ |
114 | 0 | if(!dispatcher.getLifecycleState().isInitialised()) |
115 | |
{ |
116 | 0 | dispatcher.initialise(); |
117 | |
} |
118 | 0 | dispatcher.start(); |
119 | |
} |
120 | 0 | else if(phase.equals(Stoppable.PHASE_NAME) && dispatcher.getLifecycleState().isStarted()) |
121 | |
{ |
122 | 0 | dispatcher.stop(); |
123 | |
} |
124 | 0 | else if(Disposable.PHASE_NAME.equals(phase)) |
125 | |
{ |
126 | 0 | dispatcher.dispose(); |
127 | |
} |
128 | 0 | } |
129 | |
|
130 | |
} |