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.common.endpoints;
018    
019    import javax.jbi.component.ComponentContext;
020    import javax.jbi.messaging.DeliveryChannel;
021    import javax.jbi.messaging.ExchangeStatus;
022    import javax.jbi.messaging.MessageExchange;
023    import javax.jbi.messaging.MessageExchangeFactory;
024    import javax.jbi.messaging.MessagingException;
025    import javax.jbi.servicedesc.ServiceEndpoint;
026    import javax.xml.namespace.QName;
027    
028    import org.apache.servicemix.common.DefaultComponent;
029    import org.apache.servicemix.common.Endpoint;
030    import org.apache.servicemix.common.EndpointComponentContext;
031    import org.apache.servicemix.common.ServiceUnit;
032    
033    public abstract class SimpleEndpoint extends AbstractEndpoint {
034    
035        private DeliveryChannel channel;
036        private MessageExchangeFactory exchangeFactory;
037        private ComponentContext context;
038    
039        public SimpleEndpoint() {
040        }
041    
042        public SimpleEndpoint(ServiceUnit serviceUnit, QName service, String endpoint) {
043            super(serviceUnit, service, endpoint);
044        }
045    
046        public SimpleEndpoint(DefaultComponent component, ServiceEndpoint endpoint) {
047            super(component.getServiceUnit(), endpoint.getServiceName(), endpoint.getEndpointName());
048        }
049    
050        public synchronized void activate() throws Exception {
051            context = new EndpointComponentContext(this);
052            channel = context.getDeliveryChannel();
053            exchangeFactory = channel.createExchangeFactory();
054        }
055    
056        public synchronized void deactivate() throws Exception {
057        }
058    
059        public synchronized void start() throws Exception {
060        }
061    
062        public synchronized void stop() throws Exception {
063        }
064    
065        protected void send(MessageExchange me) throws MessagingException {
066            channel.send(me);
067        }
068        
069        protected void sendSync(MessageExchange me) throws MessagingException {
070            if (!channel.sendSync(me)) {
071                throw new MessagingException("SendSync failed");
072            }
073        }
074        
075        protected void done(MessageExchange me) throws MessagingException {
076            me.setStatus(ExchangeStatus.DONE);
077            send(me);
078        }
079        
080        protected void fail(MessageExchange me, Exception error) throws MessagingException {
081            me.setError(error);
082            send(me);
083        }
084        
085        /**
086         * @return the exchangeFactory
087         */
088        public MessageExchangeFactory getExchangeFactory() {
089            return exchangeFactory;
090        }
091    
092        /**
093         * @return the channel
094         */
095        public DeliveryChannel getChannel() {
096            return channel;
097        }
098    
099        /**
100         * @return the context
101         */
102        public ComponentContext getContext() {
103            return context;
104        }
105    
106    }