View Javadoc

1   /*
2    * $Id: TestConnector.java 7976 2007-08-21 14:26:13Z dirk.olmes $
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.impl.ThreadSafeAccess;
14  import org.mule.providers.AbstractConnector;
15  import org.mule.providers.AbstractMessageAdapter;
16  import org.mule.providers.AbstractMessageDispatcherFactory;
17  import org.mule.providers.AbstractMessageReceiver;
18  import org.mule.umo.MessagingException;
19  import org.mule.umo.UMOComponent;
20  import org.mule.umo.UMOEvent;
21  import org.mule.umo.UMOException;
22  import org.mule.umo.endpoint.UMOEndpoint;
23  import org.mule.umo.endpoint.UMOImmutableEndpoint;
24  import org.mule.umo.lifecycle.InitialisationException;
25  import org.mule.umo.provider.OutputHandler;
26  import org.mule.umo.provider.UMOMessageAdapter;
27  import org.mule.umo.provider.UMOMessageDispatcher;
28  import org.mule.umo.provider.UMOMessageReceiver;
29  import org.mule.umo.provider.UMOStreamMessageAdapter;
30  
31  import java.io.IOException;
32  import java.io.InputStream;
33  import java.io.OutputStream;
34  
35  /**
36   * <code>TestConnector</code> use a mock connector
37   */
38  public class TestConnector extends AbstractConnector
39  {
40  
41      public TestConnector()
42      {
43          super();
44          setDispatcherFactory(new AbstractMessageDispatcherFactory()
45          {
46              public UMOMessageDispatcher create(UMOImmutableEndpoint endpoint) throws UMOException
47              {
48                  return new TestMessageDispatcher(endpoint);
49              }
50          });
51      }
52  
53      protected void doInitialise() throws InitialisationException
54      {
55          // template method
56      }
57  
58      protected void doDispose()
59      {
60          // template method
61      }
62  
63      protected void doConnect() throws Exception
64      {
65          // template method
66      }
67  
68      protected void doDisconnect() throws Exception
69      {
70          // template method
71      }
72  
73      protected void doStart() throws UMOException
74      {
75          // template method
76      }
77  
78      protected void doStop() throws UMOException
79      {
80          // template method
81      }
82  
83      public String getProtocol()
84      {
85          return "test";
86      }
87  
88      public UMOMessageAdapter getMessageAdapter(Object message) throws MessagingException
89      {
90          return new DummyMessageAdapter(message);
91      }
92  
93      public UMOStreamMessageAdapter getStreamMessageAdapter(InputStream in, OutputStream out)
94          throws MessagingException
95      {
96          return new DummyMessageAdapter(in);
97      }
98  
99      public UMOMessageReceiver createReceiver(UMOComponent component, UMOEndpoint endpoint) throws Exception
100     {
101         UMOMessageReceiver receiver = new AbstractMessageReceiver(this, component, endpoint)
102         {
103             protected void doConnect() throws Exception
104             {
105                 // nothing to do
106             }
107 
108             protected void doDisconnect() throws Exception
109             {
110                 // nothing to do
111             }
112 
113             protected void doStart() throws UMOException
114             {
115                 // nothing to do
116             }
117 
118             protected void doStop() throws UMOException
119             {
120                 // nothing to do
121             }
122 
123             protected void doDispose()
124             {
125                 // nothing to do               
126             }
127         };
128         return receiver;
129     }
130 
131     public void destroyReceiver(UMOMessageReceiver receiver, UMOEndpoint endpoint) throws Exception
132     {
133         // nothing to do
134     }
135 
136     public class DummyMessageAdapter extends AbstractMessageAdapter implements UMOStreamMessageAdapter
137     {
138         /**
139          * Serial version
140          */
141         private static final long serialVersionUID = -2304322766342059136L;
142 
143         private Object message = new String("DummyMessage");
144 
145         public DummyMessageAdapter(Object message)
146         {
147             this.message = message;
148         }
149 
150         public Object getPayload()
151         {
152             return message;
153         }
154 
155         public byte[] getPayloadAsBytes() throws Exception
156         {
157 
158             return message.toString().getBytes();
159         }
160 
161         public String getPayloadAsString(String encoding) throws Exception
162         {
163             return message.toString();
164         }
165 
166         public InputStream getInputStream()
167         {
168             return null;
169         }
170 
171         public OutputStream getOutputStream()
172         {
173             return null;
174         }
175 
176         public void write(UMOEvent event) throws IOException
177         {
178             // nothing to do
179         }
180 
181         public OutputHandler getOutputHandler()
182         {
183             return null;
184         }
185 
186         public void setOutputHandler(OutputHandler handler)
187         {
188             // nothing to do
189         }
190 
191         public void release()
192         {
193             // nothing to do
194         }
195 
196         public ThreadSafeAccess newThreadCopy()
197         {
198             return this;
199         }
200     }
201 
202 }