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 super();
36
37 if (factory == null)
38 {
39 throw new IllegalArgumentException(CoreMessages.objectIsNull("factory").toString());
40 }
41
42 this.factory = factory;
43 }
44
45 public void activateObject(Object key, Object obj) throws Exception
46 {
47 OutboundEndpoint endpoint = (OutboundEndpoint)key;
48
49 applyLifecycle((MessageDispatcher)obj);
50
51 factory.activate((OutboundEndpoint) key, (MessageDispatcher) obj);
52 }
53
54 public void destroyObject(Object key, Object obj) throws Exception
55 {
56 factory.destroy((OutboundEndpoint) key, (MessageDispatcher) obj);
57 }
58
59 public Object makeObject(Object key) throws Exception
60 {
61 OutboundEndpoint endpoint = (OutboundEndpoint) key;
62 MessageDispatcher dispatcher = factory.create(endpoint);
63 applyLifecycle(dispatcher);
64 return dispatcher;
65 }
66
67 public void passivateObject(Object key, Object obj) throws Exception
68 {
69 factory.passivate((OutboundEndpoint) key, (MessageDispatcher) obj);
70 }
71
72 public boolean validateObject(Object key, Object obj)
73 {
74 return factory.validate((OutboundEndpoint) key, (MessageDispatcher) obj);
75 }
76
77 public boolean isCreateDispatcherPerRequest()
78 {
79 return factory.isCreateDispatcherPerRequest();
80 }
81
82 public MessageDispatcher create(OutboundEndpoint endpoint) throws MuleException
83 {
84 return factory.create(endpoint);
85 }
86
87 public void activate(OutboundEndpoint endpoint, MessageDispatcher dispatcher) throws MuleException
88 {
89
90 applyLifecycle(dispatcher);
91 factory.activate(endpoint, dispatcher);
92 }
93
94 public void destroy(OutboundEndpoint endpoint, MessageDispatcher dispatcher)
95 {
96 factory.destroy(endpoint, dispatcher);
97 }
98
99 public void passivate(OutboundEndpoint endpoint, MessageDispatcher dispatcher)
100 {
101 factory.passivate(endpoint, dispatcher);
102 }
103
104 public boolean validate(OutboundEndpoint endpoint, MessageDispatcher dispatcher)
105 {
106 return factory.validate(endpoint, dispatcher);
107 }
108
109 protected void applyLifecycle(MessageDispatcher dispatcher) throws MuleException
110 {
111 String phase = ((AbstractConnector)dispatcher.getConnector()).getLifecycleManager().getCurrentPhase();
112 if(phase.equals(Startable.PHASE_NAME) && !dispatcher.getLifecycleState().isStarted())
113 {
114 if(!dispatcher.getLifecycleState().isInitialised())
115 {
116 dispatcher.initialise();
117 }
118 dispatcher.start();
119 }
120 else if(phase.equals(Stoppable.PHASE_NAME) && dispatcher.getLifecycleState().isStarted())
121 {
122 dispatcher.stop();
123 }
124 else if(Disposable.PHASE_NAME.equals(phase))
125 {
126 dispatcher.dispose();
127 }
128 }
129
130 }