View Javadoc

1   /*
2    * $Id: AbstractReceiverResourceWorker.java 10489 2008-01-23 17:53:38Z dfeist $
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  package org.mule.transport;
11  
12  import java.io.OutputStream;
13  import java.util.ArrayList;
14  
15  /**
16   * This is a Message receiver worker used by transports that do not have a way for the underlying transport
17   * to call back to the receiver when a message is available such as Jms. This worker provides a
18   * callback {@link #getNextMessage(Object)} where the receiver can read the next message from the underlying
19   * transport.
20   */
21  public abstract class AbstractReceiverResourceWorker extends AbstractReceiverWorker
22  {
23      protected Object resource;
24  
25      public AbstractReceiverResourceWorker(Object resource, AbstractMessageReceiver receiver)
26      {
27          this(resource, receiver, null);
28      }
29  
30      public AbstractReceiverResourceWorker(Object resource, AbstractMessageReceiver receiver, OutputStream out)
31      {
32          super(new ArrayList(), receiver, out);
33          this.resource = resource;
34      }
35  
36  
37      /**
38      * (non-Javadoc)
39      *
40      */
41      //@Override
42      public void doRun()
43      {
44          try
45          {
46              Object message = null;
47              do 
48              {
49                  message = getNextMessage(resource);
50                  messages.add(message);
51                  super.doRun();
52              }
53              while (message != null && hasMoreMessages(message));
54          }
55          catch (Exception e)
56          {
57              handleException(e);
58          }
59      }
60  
61      protected boolean hasMoreMessages(Object message)
62      {
63          return true;
64      }
65      
66      /**
67       * The method used to read the next message from the underlying transport.
68       * @param resource the resource to read from, this may be a socket, a directory or some higher level
69       * representation.
70       * @return the message read from the resource.  This can be raw data such as a byte[] or a MessageAdapter.
71       * @throws Exception
72       */
73      protected abstract Object getNextMessage(Object resource) throws Exception;
74      
75  }