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