KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > util > ChainedComponent


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.components.util;
18
19 import java.util.Iterator JavaDoc;
20 import java.util.Set JavaDoc;
21
22 import javax.jbi.messaging.ExchangeStatus;
23 import javax.jbi.messaging.InOut;
24 import javax.jbi.messaging.MessageExchange;
25 import javax.jbi.messaging.MessagingException;
26 import javax.jbi.messaging.NormalizedMessage;
27 import javax.xml.namespace.QName JavaDoc;
28
29 import org.apache.servicemix.components.util.TransformComponentSupport;
30 import org.apache.servicemix.jbi.FaultException;
31
32 /**
33  * This class allows a series of componeents to be chained together. It will
34  * invoke the first service, then take the output of that first service as the
35  * input to the next service, and return the overall results when finished.
36  *
37  * All properties and attachments are maintained.
38  *
39  * @author birchfieldj
40  * @deprecated use the StaticRoutingSlip pattern from the EIP component instead
41  *
42  */

43 public class ChainedComponent extends TransformComponentSupport {
44
45     private QName JavaDoc[] services = new QName JavaDoc[0];
46
47     protected boolean transform(MessageExchange exchange,
48                                 NormalizedMessage in,
49                                 NormalizedMessage out) throws MessagingException {
50         NormalizedMessage curIn = in;
51         MessageExchange curExchange = exchange;
52         for (int i = 0; i < services.length; i++) {
53             InOut mexchange = this.getDeliveryChannel()
54                     .createExchangeFactoryForService(services[i])
55                     .createInOutExchange();
56             copyProperties(curExchange, mexchange);
57             curIn = invokeService(mexchange, curIn, services[i]);
58             curExchange = mexchange;
59         }
60         getMessageTransformer().transform(exchange, curIn, out);
61         copyProperties(curExchange, exchange);
62         return true;
63     }
64
65     /**
66      * Invokes the service with the given message, and returns the output
67      *
68      * @param exchange
69      * @param in
70      * @param service
71      * @return the out message of the invoked service
72      * @throws MessagingException
73      */

74     private NormalizedMessage invokeService(InOut exchange,
75                                             NormalizedMessage in,
76                                             QName JavaDoc service) throws MessagingException {
77         NormalizedMessage msg = exchange.createMessage();
78         getMessageTransformer().transform(exchange, in, msg);
79         exchange.setMessage(msg, "in");
80         boolean result = this.getDeliveryChannel().sendSync(exchange);
81         if (result) {
82             if (exchange.getStatus() == ExchangeStatus.ERROR) {
83                 exchange.setStatus(ExchangeStatus.DONE);
84                 getDeliveryChannel().send(exchange);
85                 if (exchange.getError() != null) {
86                     throw new MessagingException("Received error", exchange.getError());
87                 } else if (exchange.getFault() != null) {
88                     throw new FaultException("Received fault", exchange, exchange.getFault());
89                 } else {
90                     throw new MessagingException("Received unknown error");
91                 }
92             } else {
93                 NormalizedMessage out = exchange.getOutMessage();
94                 exchange.setStatus(ExchangeStatus.DONE);
95                 getDeliveryChannel().send(exchange);
96                 return out;
97             }
98         }
99         throw new MessagingException("Could not invoke service: " + service);
100     }
101
102     /**
103      *
104      * @param in
105      * echange to copy from
106      * @param out
107      * excahnge to copy to
108      */

109     private void copyProperties(MessageExchange in, MessageExchange out) {
110         Set JavaDoc propertyNames = in.getPropertyNames();
111         for (Iterator JavaDoc iter = propertyNames.iterator(); iter.hasNext();) {
112             String JavaDoc name = (String JavaDoc) iter.next();
113             out.setProperty(name, in.getProperty(name));
114         }
115     }
116
117     /**
118      * Allows the services to be set
119      *
120      * @param services
121      * a collection of QNAmes representing the services to be
122      * invoked.
123      */

124     public void setServices(QName JavaDoc[] services) {
125         this.services = services;
126     }
127
128 }
129
Popular Tags