View Javadoc

1   /*
2    * $Id: TestConnector.java 19264 2010-08-31 22:21:33Z tcarlson $
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     public MessageReceiver createReceiver(FlowConstruct flowConstuct, InboundEndpoint endpoint) throws Exception
133     {
134         MessageReceiver receiver = new AbstractMessageReceiver(this, flowConstuct, endpoint)
135         {
136 
137             @Override
138             protected void doInitialise() throws InitialisationException
139             {
140                 //nothing to do
141             }
142 
143             @Override
144             protected void doConnect() throws Exception
145             {
146                 // nothing to do
147             }
148 
149             @Override
150             protected void doDisconnect() throws Exception
151             {
152                 // nothing to do
153             }
154 
155             @Override
156             protected void doStart() throws MuleException
157             {
158                 // nothing to do
159             }
160 
161             @Override
162             protected void doStop() throws MuleException
163             {
164                 // nothing to do
165             }
166 
167             @Override
168             protected void doDispose()
169             {
170                 // nothing to do               
171             }
172         };
173         return receiver;
174     }
175 
176     /**
177      * Open up the access to the service descriptor for testing purposes.
178      */
179     @Override
180     public TransportServiceDescriptor getServiceDescriptor()
181     {
182         return super.getServiceDescriptor();
183     }
184 
185     public void destroyReceiver(MessageReceiver receiver, InboundEndpoint endpoint) throws Exception
186     {
187         // nothing to do
188     }
189 
190     public ConfigurableKeyedObjectPool getDispatchers()
191     {
192         return dispatchers;
193     }
194 
195     public int getInitialiseCount() 
196     {
197         return initialiseCount;
198     }
199     
200     public int getConnectCount() 
201     {
202         return connectCount;
203     }
204     
205     public int getStartCount() 
206     {
207         return startCount;
208     }
209     
210     public int getStopCount() 
211     {
212         return stopCount;
213     }
214     
215     public int getDisconnectCount() 
216     {
217         return disconnectCount;
218     }
219     
220     public int getDisposeCount() 
221     {
222         return disposeCount;
223     }
224 
225     public MessageProcessor getOutboundEndpointMessageProcessor(OutboundEndpoint endpoint)
226         throws MuleException
227     {
228         return ((AbstractEndpoint) endpoint).getMessageProcessorChain(null);
229     }
230 
231     public void setFailAtStartup(boolean failAtStartup)
232     {
233         this.failAtStartup = failAtStartup;
234     }
235 
236     public boolean isFailAtStartup()
237     {
238         return failAtStartup;
239     }    
240 }