Package org.servicemix.components.util

Source Code of org.servicemix.components.util.ChainedComponent

/**
*
* Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**/
package org.servicemix.components.util;

import java.util.Iterator;
import java.util.Set;

import javax.jbi.messaging.MessageExchange;
import javax.jbi.messaging.MessagingException;
import javax.jbi.messaging.NormalizedMessage;
import javax.xml.namespace.QName;

import org.servicemix.components.util.TransformComponentSupport;

/**
* This class allows a series of componeents to be chained together. It will
* invoke the first service, then take the output of that first service as the
* input to the next service, and return the overall results when finished.
*
* All properties and attachments are maintained.
*
* @author birchfieldj
*
*/
public class ChainedComponent extends TransformComponentSupport {

    private QName[] services = new QName[0];

    protected boolean transform(MessageExchange exchange,
                                NormalizedMessage in,
                                NormalizedMessage out) throws MessagingException {
        NormalizedMessage curIn = in;
        MessageExchange curExchange = exchange;
        for (int i = 0; i < services.length; i++) {
            MessageExchange mexchange = this.getDeliveryChannel()
                    .createExchangeFactoryForService(services[i])
                    .createInOutExchange();
            copyProperties(curExchange, mexchange);
            curIn = invokeService(mexchange, curIn, services[i]);
            curExchange = mexchange;
        }
        getMessageTransformer().transform(exchange, curIn, out);
        copyProperties(curExchange, exchange);
        return true;
    }

    /**
     * Invokes the service with the given message, and returns the output
     *
     * @param exchange
     * @param in
     * @param service
     * @return the out message of the invoked service
     * @throws MessagingException
     */
    private NormalizedMessage invokeService(MessageExchange exchange,
                                            NormalizedMessage in,
                                            QName service) throws MessagingException {
        NormalizedMessage msg = exchange.createMessage();
        getMessageTransformer().transform(exchange, in, msg);
        exchange.setMessage(msg, "in");
        boolean result = this.getDeliveryChannel().sendSync(exchange);
        if (result) {
            return exchange.getMessage("out");
        }
        throw new MessagingException("Could not invoke service: " + service);
    }

    /**
     *
     * @param in
     *            echange to copy from
     * @param out
     *            excahnge to copy to
     */
    private void copyProperties(MessageExchange in, MessageExchange out) {
        Set propertyNames = in.getPropertyNames();
        for (Iterator iter = propertyNames.iterator(); iter.hasNext();) {
            String name = (String) iter.next();
            out.setProperty(name, in.getProperty(name));
        }
    }

    /**
     * Allows the services to be set
     *
     * @param services
     *            a collection of QNAmes representing the services to be
     *            invoked.
     */
    public void setServices(QName[] services) {
        this.services = services;
    }

}
TOP

Related Classes of org.servicemix.components.util.ChainedComponent

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.