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.JBIException;
020    import javax.jbi.component.ComponentContext;
021    import javax.jbi.component.ComponentLifeCycle;
022    import javax.jbi.messaging.DeliveryChannel;
023    import javax.jbi.messaging.ExchangeStatus;
024    import javax.jbi.messaging.MessageExchange;
025    import javax.jbi.messaging.MessagingException;
026    import javax.jbi.messaging.NormalizedMessage;
027    import javax.management.ObjectName;
028    import javax.xml.namespace.QName;
029    
030    import org.apache.commons.logging.Log;
031    import org.apache.commons.logging.LogFactory;
032    
033    /**
034     * A simple POJO which just implements the {@link javax.jbi.component.ComponentLifeCycle}
035     * interface and is not dependent on any ServiceMix code.
036     *
037     * @version $Revision: 564607 $
038     */
039    public class AsyncReceiverPojo implements ComponentLifeCycle, Receiver, Runnable {
040    
041        public static final QName SERVICE = ReceiverPojo.SERVICE;
042        public static final String ENDPOINT = ReceiverPojo.ENDPOINT;
043    
044        private static final Log LOG = LogFactory.getLog(AsyncReceiverPojo.class);
045    
046        private ComponentContext context;
047        private MessageList messageList = new MessageList();
048        private Thread runner;
049        private boolean running;
050    
051    
052        // ComponentLifeCycle interface
053        //-------------------------------------------------------------------------
054        public void init(ComponentContext ctx) throws JBIException {
055            this.context = ctx;
056            ctx.activateEndpoint(SERVICE, ENDPOINT);
057        }
058    
059        public void shutDown() throws JBIException {
060        }
061    
062        public synchronized void start() throws JBIException {
063            if (!running) {
064                running = true;
065                runner = new Thread(this);
066                runner.start();
067            }
068        }
069    
070        public synchronized void stop() throws JBIException {
071            running = false;
072        }
073    
074        public ObjectName getExtensionMBeanName() {
075            return null;
076        }
077    
078    
079        // Receiver interface
080        //-------------------------------------------------------------------------
081        public MessageList getMessageList() {
082            return messageList;
083        }
084    
085        // Runnable interface
086        //-------------------------------------------------------------------------
087        public void run() {
088            while (running) {
089                try {
090                    DeliveryChannel deliveryChannel = context.getDeliveryChannel();
091                    LOG.info("about to do an accept on deliveryChannel: " + deliveryChannel);
092                    MessageExchange messageExchange = deliveryChannel.accept();
093                    LOG.info("received me: " + messageExchange);
094                    onMessageExchange(messageExchange);
095                } catch (MessagingException e) {
096                    LOG.error("Failed to process inbound messages: " + e, e);
097                }
098            }
099        }
100    
101        public void onMessageExchange(MessageExchange exchange) throws MessagingException {
102            NormalizedMessage inMessage = exchange.getMessage("in");
103            messageList.addMessage(inMessage);
104            exchange.setStatus(ExchangeStatus.DONE);
105            context.getDeliveryChannel().send(exchange);
106        }
107    
108        public ComponentContext getContext() {
109            return context;
110        }
111    }