View Javadoc

1   /*
2    * $Id: StaticRecipientList.java 19739 2010-09-27 14:28:40Z 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.routing.outbound;
12  
13  import org.mule.api.MuleEvent;
14  import org.mule.util.StringUtils;
15  
16  import java.util.ArrayList;
17  import java.util.Arrays;
18  import java.util.Collections;
19  import java.util.List;
20  
21  /**
22   * <code>StaticRecipientList</code> is used to dispatch a single event to multiple
23   * recipients over the same transport. The recipient targets for this router can be
24   * configured statically on the router itself.
25   */
26  
27  // TODO I think the ExpressionRecipientList router does everything this router does and more.
28  // Perhaps we should rename it to simply "RecipientList Router" and remove this one?
29  public class StaticRecipientList extends AbstractRecipientList
30  {
31      public static final String RECIPIENTS_PROPERTY = "recipients";
32      public static final String RECIPIENT_DELIMITER = ",";
33  
34      private volatile List recipients = Collections.EMPTY_LIST;
35  
36      protected List getRecipients(MuleEvent event)
37      {
38          Object msgRecipients = event.getMessage().removeProperty(RECIPIENTS_PROPERTY);
39  
40          if (msgRecipients == null)
41          {
42              return recipients;
43          }
44          else if (msgRecipients instanceof String)
45          {
46              return Arrays.asList(StringUtils.splitAndTrim(msgRecipients.toString(), this.getListDelimiter()));
47          }
48          else if (msgRecipients instanceof List)
49          {
50              return new ArrayList((List) msgRecipients);
51          }
52          else
53          {
54              logger.warn("Recipients on message are neither String nor List but: " + msgRecipients.getClass());
55              return Collections.EMPTY_LIST;
56          }
57      }
58  
59      public List getRecipients()
60      {
61          return recipients;
62      }
63  
64      public void setRecipients(List recipients)
65      {
66          if (recipients != null)
67          {
68              this.recipients = new ArrayList(recipients);
69          }
70          else
71          {
72              this.recipients = Collections.EMPTY_LIST;
73          }
74      }
75  
76      /**
77       * Overloading classes can change the delimiter used to separate entries in the
78       * recipient list. By default a ',' is used.
79       * 
80       * @return The list delimiter to use
81       */
82      protected String getListDelimiter()
83      {
84          return RECIPIENT_DELIMITER;
85      }
86  
87  }