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