KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > eip > patterns > StaticRecipientList


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.eip.patterns;
18
19 import javax.jbi.management.DeploymentException;
20 import javax.jbi.messaging.ExchangeStatus;
21 import javax.jbi.messaging.InOnly;
22 import javax.jbi.messaging.MessageExchange;
23 import javax.jbi.messaging.NormalizedMessage;
24 import javax.jbi.messaging.RobustInOnly;
25
26 import org.apache.servicemix.eip.EIPEndpoint;
27 import org.apache.servicemix.eip.support.ExchangeTarget;
28 import org.apache.servicemix.jbi.util.MessageUtil;
29
30 /**
31  * The StaticRecipientList component will forward an input In-Only or Robust-In-Only
32  * exchange to a list of known recipients.
33  * This component implements the
34  * <a HREF="http://www.enterpriseintegrationpatterns.com/RecipientList.html">Recipient List</a>
35  * pattern, with the limitation that the recipient list is static.
36  *
37  * @author gnodet
38  * @version $Revision: 376451 $
39  * @org.apache.xbean.XBean element="static-recipient-list"
40  * description="A static Recipient List"
41  */

42 public class StaticRecipientList extends EIPEndpoint {
43
44     /**
45      * List of recipients
46      */

47     private ExchangeTarget[] recipients;
48     /**
49      * Indicates if faults and errors from recipients should be sent
50      * back to the consumer. In such a case, only the first fault or
51      * error received will be reported.
52      * Note that if the consumer is synchronous, it will be blocked
53      * until all recipients successfully acked the exchange, or
54      * a fault or error is reported, and the exchange will be kept in the
55      * store for recovery.
56      */

57     private boolean reportErrors;
58     /**
59      * The correlation property used by this component
60      */

61     private String JavaDoc correlation;
62
63     /**
64      * @return Returns the recipients.
65      */

66     public ExchangeTarget[] getRecipients() {
67         return recipients;
68     }
69
70     /**
71      * @param recipients The recipients to set.
72      */

73     public void setRecipients(ExchangeTarget[] recipients) {
74         this.recipients = recipients;
75     }
76
77     /**
78      * @return Returns the reportErrors.
79      */

80     public boolean isReportErrors() {
81         return reportErrors;
82     }
83
84     /**
85      * @param reportErrors The reportErrors to set.
86      */

87     public void setReportErrors(boolean reportErrors) {
88         this.reportErrors = reportErrors;
89     }
90
91     /* (non-Javadoc)
92      * @see org.apache.servicemix.eip.EIPEndpoint#validate()
93      */

94     public void validate() throws DeploymentException {
95         super.validate();
96         // Check recipients
97
if (recipients == null || recipients.length == 0) {
98             throw new IllegalArgumentException JavaDoc("recipients should contain at least one ExchangeTarget");
99         }
100         // Create correlation property
101
correlation = "StaticRecipientList.Correlation." + getService() + "." + getEndpoint();
102     }
103     
104     /* (non-Javadoc)
105      * @see org.apache.servicemix.eip.EIPEndpoint#processSync(javax.jbi.messaging.MessageExchange)
106      */

107     protected void processSync(MessageExchange exchange) throws Exception JavaDoc {
108         if (exchange instanceof InOnly == false &&
109             exchange instanceof RobustInOnly == false) {
110             fail(exchange, new UnsupportedOperationException JavaDoc("Use an InOnly or RobustInOnly MEP"));
111             return;
112         }
113         NormalizedMessage in = MessageUtil.copyIn(exchange);
114         for (int i = 0; i < recipients.length; i++) {
115             MessageExchange me = exchangeFactory.createExchange(exchange.getPattern());
116             recipients[i].configureTarget(me, getContext());
117             MessageUtil.transferToIn(in, me);
118             sendSync(me);
119             if (me.getStatus() == ExchangeStatus.ERROR && reportErrors) {
120                 fail(exchange, me.getError());
121                 return;
122             }
123         }
124         done(exchange);
125     }
126
127     /* (non-Javadoc)
128      * @see org.apache.servicemix.eip.EIPEndpoint#processAsync(javax.jbi.messaging.MessageExchange)
129      */

130     protected void processAsync(MessageExchange exchange) throws Exception JavaDoc {
131         // If we need to report errors, the behavior is really different,
132
// as we need to keep the incoming exchange in the store until
133
// all acks have been received
134
if (reportErrors) {
135             // TODO: implement this
136
throw new UnsupportedOperationException JavaDoc("Not implemented");
137         // We are in a simple fire-and-forget behaviour.
138
// This implementation is really efficient as we do not use
139
// the store at all.
140
} else {
141             if (exchange.getStatus() == ExchangeStatus.DONE) {
142                 return;
143             } else if (exchange.getStatus() == ExchangeStatus.ERROR) {
144                 return;
145             } else if (exchange instanceof InOnly == false &&
146                        exchange instanceof RobustInOnly == false) {
147                 fail(exchange, new UnsupportedOperationException JavaDoc("Use an InOnly or RobustInOnly MEP"));
148             } else if (exchange.getFault() != null) {
149                 done(exchange);
150             } else {
151                 NormalizedMessage in = MessageUtil.copyIn(exchange);
152                 for (int i = 0; i < recipients.length; i++) {
153                     MessageExchange me = exchangeFactory.createExchange(exchange.getPattern());
154                     recipients[i].configureTarget(me, getContext());
155                     MessageUtil.transferToIn(in, me);
156                     send(me);
157                 }
158                 done(exchange);
159             }
160         }
161     }
162
163 }
164
Popular Tags