001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.servicemix.tck;
018    
019    import javax.jbi.component.ComponentLifeCycle;
020    import javax.jbi.messaging.MessageExchange;
021    import javax.jbi.messaging.MessagingException;
022    import javax.jbi.messaging.NormalizedMessage;
023    import javax.xml.namespace.QName;
024    
025    import org.apache.servicemix.MessageExchangeListener;
026    import org.apache.servicemix.components.util.CopyTransformer;
027    import org.apache.servicemix.components.util.MessageTransformer;
028    import org.apache.servicemix.components.util.PojoSupport;
029    import org.apache.servicemix.jbi.NoInMessageAvailableException;
030    
031    /**
032     * A simple POJO which just implements the {@link ComponentLifeCycle}
033     * interface and is not dependent on any ServiceMix code.
034     *
035     * @version $Revision: 426415 $
036     */
037    public class ReceiverPojo extends PojoSupport implements ComponentLifeCycle, MessageExchangeListener, Receiver {
038    
039        public static final QName SERVICE = new QName("http://servicemix.org/example/", "receiver");
040        public static final String ENDPOINT = "receiver";
041    
042        private MessageList messageList = new MessageList();
043        private MessageTransformer messageTransformer = CopyTransformer.getInstance();
044    
045        public ReceiverPojo() {
046            this(SERVICE, ENDPOINT);
047        }
048    
049        public ReceiverPojo(QName service, String endpoint) {
050            super(service, endpoint);
051        }
052    
053        // MessageExchangeListener interface
054        //-------------------------------------------------------------------------
055        public void onMessageExchange(MessageExchange exchange) throws MessagingException {
056            NormalizedMessage inMessage = getInMessage(exchange);
057            // Copy message to avoid possible closed stream exceptions
058            // when using StreamSource
059            NormalizedMessage copyMessage = exchange.createMessage();
060            getMessageTransformer().transform(exchange, inMessage, copyMessage);
061            messageList.addMessage(copyMessage);
062            done(exchange);
063        }
064    
065        /**
066         * Returns the in message or throws an exception if there is no in message.
067         */
068        protected NormalizedMessage getInMessage(MessageExchange exchange) throws NoInMessageAvailableException {
069            NormalizedMessage message = exchange.getMessage("in");
070            if (message == null) {
071                throw new NoInMessageAvailableException(exchange);
072            }
073            return message;
074        }
075    
076        public MessageTransformer getMessageTransformer() {
077            return messageTransformer;
078        }
079        
080        // Receiver interface
081        //-------------------------------------------------------------------------
082        public MessageList getMessageList() {
083            return messageList;
084        }
085    }