View Javadoc

1   /*
2    * $Id: TestConnector.java 21307 2011-02-17 15:35:44Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.tck.testmodels.mule;
12  
13  import org.mule.api.MuleContext;
14  import org.mule.api.MuleException;
15  import org.mule.api.config.MuleProperties;
16  import org.mule.api.construct.FlowConstruct;
17  import org.mule.api.endpoint.InboundEndpoint;
18  import org.mule.api.endpoint.OutboundEndpoint;
19  import org.mule.api.lifecycle.InitialisationException;
20  import org.mule.api.processor.MessageProcessor;
21  import org.mule.api.retry.RetryPolicyTemplate;
22  import org.mule.api.transport.MessageDispatcher;
23  import org.mule.api.transport.MessageReceiver;
24  import org.mule.api.transport.MessageRequester;
25  import org.mule.endpoint.AbstractEndpoint;
26  import org.mule.transport.AbstractConnector;
27  import org.mule.transport.AbstractMessageDispatcherFactory;
28  import org.mule.transport.AbstractMessageReceiver;
29  import org.mule.transport.AbstractMessageRequesterFactory;
30  import org.mule.transport.ConfigurableKeyedObjectPool;
31  import org.mule.transport.service.TransportServiceDescriptor;
32  
33  /**
34   * <code>TestConnector</code> use a mock connector
35   */
36  public class TestConnector extends AbstractConnector
37  {
38      public static final String TEST = "test";
39  
40      private String someProperty;
41  
42      private int initialiseCount = 0;
43      private int connectCount = 0;
44      private int startCount = 0;
45      private int stopCount = 0;
46      private int disconnectCount = 0;
47      private int disposeCount = 0;
48      
49      private boolean failAtStartup = false;
50      
51      public TestConnector(MuleContext context)
52      {
53          super(context);
54          setDispatcherFactory(new AbstractMessageDispatcherFactory()
55          {
56              @Override
57              public MessageDispatcher create(OutboundEndpoint endpoint) throws MuleException
58              {
59                  return new TestMessageDispatcher(endpoint);
60              }
61          });
62  
63          setRequesterFactory(new AbstractMessageRequesterFactory()
64          {
65              @Override
66              public MessageRequester create(InboundEndpoint endpoint) throws MuleException
67              {
68                  return new TestMessageRequester(endpoint);
69              }
70          });
71  
72          setRetryPolicyTemplate((RetryPolicyTemplate) muleContext.getRegistry().lookupObject(
73                                  MuleProperties.OBJECT_DEFAULT_RETRY_POLICY_TEMPLATE));
74      }
75  
76      public String getProtocol()
77      {
78          return TEST;
79      }
80  
81      @Override
82      protected void doInitialise() 
83      {
84          initialiseCount++;
85      }
86  
87      @Override
88      protected void doConnect() 
89      {
90          connectCount++;
91      }
92  
93      @Override
94      protected void doStart() 
95      {
96          if (isFailAtStartup())
97          {
98              throw new RuntimeException("Startup failure");
99          }
100         startCount++;
101     }
102 
103     @Override
104     protected void doStop() 
105     {
106         stopCount++;
107     }
108 
109     @Override
110     protected void doDisconnect() 
111     {
112         disconnectCount++;
113     }
114 
115     @Override
116     protected void doDispose() 
117     {
118         disposeCount++;
119     }
120 
121     public String getSomeProperty()
122     {
123         return someProperty;
124     }
125 
126     public void setSomeProperty(String someProperty)
127     {
128         this.someProperty = someProperty;
129     }
130     
131     @Override
132     protected Object getReceiverKey(FlowConstruct flowConstruct, InboundEndpoint endpoint)
133     {
134         if (endpoint.getProperty("competingConsumers") != null)
135         {
136             return flowConstruct.getName() + "~" + endpoint.getEndpointURI().getAddress();
137         }
138         else
139         {
140             return super.getReceiverKey(flowConstruct, endpoint);
141         }
142     }
143 
144     @Override
145     public MessageReceiver createReceiver(FlowConstruct flowConstuct, InboundEndpoint endpoint) throws Exception
146     {
147         MessageReceiver receiver = new AbstractMessageReceiver(this, flowConstuct, endpoint)
148         {
149 
150             @Override
151             protected void doInitialise() throws InitialisationException
152             {
153                 //nothing to do
154             }
155 
156             @Override
157             protected void doConnect() throws Exception
158             {
159                 // nothing to do
160             }
161 
162             @Override
163             protected void doDisconnect() throws Exception
164             {
165                 // nothing to do
166             }
167 
168             @Override
169             protected void doStart() throws MuleException
170             {
171                 // nothing to do
172             }
173 
174             @Override
175             protected void doStop() throws MuleException
176             {
177                 // nothing to do
178             }
179 
180             @Override
181             protected void doDispose()
182             {
183                 // nothing to do               
184             }
185         };
186         return receiver;
187     }
188 
189     /**
190      * Open up the access to the service descriptor for testing purposes.
191      */
192     @Override
193     public TransportServiceDescriptor getServiceDescriptor()
194     {
195         return super.getServiceDescriptor();
196     }
197 
198     public void destroyReceiver(MessageReceiver receiver, InboundEndpoint endpoint) throws Exception
199     {
200         // nothing to do
201     }
202 
203     public ConfigurableKeyedObjectPool getDispatchers()
204     {
205         return dispatchers;
206     }
207 
208     public int getInitialiseCount() 
209     {
210         return initialiseCount;
211     }
212     
213     public int getConnectCount() 
214     {
215         return connectCount;
216     }
217     
218     public int getStartCount() 
219     {
220         return startCount;
221     }
222     
223     public int getStopCount() 
224     {
225         return stopCount;
226     }
227     
228     public int getDisconnectCount() 
229     {
230         return disconnectCount;
231     }
232     
233     public int getDisposeCount() 
234     {
235         return disposeCount;
236     }
237 
238     public MessageProcessor getOutboundEndpointMessageProcessor(OutboundEndpoint endpoint)
239         throws MuleException
240     {
241         return ((AbstractEndpoint) endpoint).getMessageProcessorChain(null);
242     }
243 
244     public void setFailAtStartup(boolean failAtStartup)
245     {
246         this.failAtStartup = failAtStartup;
247     }
248 
249     public boolean isFailAtStartup()
250     {
251         return failAtStartup;
252     }    
253 }