1   /*
2    * $Id: TestConnector.java 11459 2008-03-21 00:01:32Z rossmason $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.MessagingException;
14  import org.mule.api.MuleException;
15  import org.mule.api.ThreadSafeAccess;
16  import org.mule.api.endpoint.InboundEndpoint;
17  import org.mule.api.endpoint.OutboundEndpoint;
18  import org.mule.api.lifecycle.InitialisationException;
19  import org.mule.api.service.Service;
20  import org.mule.api.transport.MessageAdapter;
21  import org.mule.api.transport.MessageDispatcher;
22  import org.mule.api.transport.MessageReceiver;
23  import org.mule.transport.AbstractConnector;
24  import org.mule.transport.AbstractMessageAdapter;
25  import org.mule.transport.AbstractMessageDispatcherFactory;
26  import org.mule.transport.AbstractMessageReceiver;
27  
28  /**
29   * <code>TestConnector</code> use a mock connector
30   */
31  public class TestConnector extends AbstractConnector
32  {
33  
34      public static final String TEST = "test";
35  
36      private String someProperty;
37  
38      public TestConnector()
39      {
40          super();
41          setDispatcherFactory(new AbstractMessageDispatcherFactory()
42          {
43              public MessageDispatcher create(OutboundEndpoint endpoint) throws MuleException
44              {
45                  return new TestMessageDispatcher(endpoint);
46              }
47          });
48      }
49  
50      protected void doInitialise() throws InitialisationException
51      {
52          // template method
53      }
54  
55      protected void doDispose()
56      {
57          // template method
58      }
59  
60      protected void doConnect() throws Exception
61      {
62          // template method
63      }
64  
65      protected void doDisconnect() throws Exception
66      {
67          // template method
68      }
69  
70      protected void doStart() throws MuleException
71      {
72          // template method
73      }
74  
75      protected void doStop() throws MuleException
76      {
77          // template method
78      }
79  
80      public String getProtocol()
81      {
82          return TEST;
83      }
84  
85      public MessageAdapter getMessageAdapter(Object message) throws MessagingException
86      {
87          return new DummyMessageAdapter(message);
88      }
89  
90      public String getSomeProperty()
91      {
92          return someProperty;
93      }
94  
95      public void setSomeProperty(String someProperty)
96      {
97          this.someProperty = someProperty;
98      }
99  
100     public MessageReceiver createReceiver(Service service, InboundEndpoint endpoint) throws Exception
101     {
102         MessageReceiver receiver = new AbstractMessageReceiver(this, service, endpoint)
103         {
104 
105             protected void doInitialise() throws InitialisationException
106             {
107                 //nothing to do
108             }
109 
110             protected void doConnect() throws Exception
111             {
112                 // nothing to do
113             }
114 
115             protected void doDisconnect() throws Exception
116             {
117                 // nothing to do
118             }
119 
120             protected void doStart() throws MuleException
121             {
122                 // nothing to do
123             }
124 
125             protected void doStop() throws MuleException
126             {
127                 // nothing to do
128             }
129 
130             protected void doDispose()
131             {
132                 // nothing to do               
133             }
134         };
135         return receiver;
136     }
137 
138     public void destroyReceiver(MessageReceiver receiver, InboundEndpoint endpoint) throws Exception
139     {
140         // nothing to do
141     }
142 
143     public class DummyMessageAdapter extends AbstractMessageAdapter
144     {
145         /**
146          * Serial version
147          */
148         private static final long serialVersionUID = -2304322766342059136L;
149 
150         private Object message = new String("DummyMessage");
151 
152         public DummyMessageAdapter(Object message)
153         {
154             this.message = message;
155         }
156 
157         public Object getPayload()
158         {
159             return message;
160         }
161 
162         public byte[] getPayloadAsBytes() throws Exception
163         {
164 
165             return message.toString().getBytes();
166         }
167 
168         public String getPayloadAsString(String encoding) throws Exception
169         {
170             return message.toString();
171         }
172 
173         public void setPayload(Object payload)
174         {
175             this.message = payload;
176         }
177 
178         public ThreadSafeAccess newThreadCopy()
179         {
180             return this;
181         }
182     }
183 
184 }