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.camel.component.jbi;
018    
019    import org.apache.camel.Consumer;
020    import org.apache.camel.Endpoint;
021    import org.apache.camel.Exchange;
022    import org.apache.camel.Processor;
023    import org.apache.camel.Producer;
024    import org.apache.camel.impl.DefaultConsumer;
025    import org.apache.camel.impl.DefaultEndpoint;
026    import org.apache.camel.impl.DefaultProducer;
027    
028    /**
029     * Represents an {@link Endpoint} for interacting with JBI
030     *
031     * @version $Revision: 563665 $
032     */
033    public class JbiEndpoint extends DefaultEndpoint<Exchange> {
034        private Processor toJbiProcessor;
035        private final CamelJbiComponent jbiComponent;
036    
037        public JbiEndpoint(CamelJbiComponent jbiComponent, String uri) {
038            super(uri, jbiComponent);
039            this.jbiComponent = jbiComponent;
040            toJbiProcessor = new ToJbiProcessor(jbiComponent.getBinding(), jbiComponent.getComponentContext(), uri);
041        }
042    
043        public Producer<Exchange> createProducer() throws Exception {
044            return new DefaultProducer<Exchange>(this) {
045                public void process(Exchange exchange) throws Exception {
046                    toJbiProcessor.process(exchange);
047                }
048            };
049        }
050    
051        public Consumer<Exchange> createConsumer(final Processor processor) throws Exception {
052            return new DefaultConsumer<Exchange>(this, processor) {
053                CamelJbiEndpoint jbiEndpoint;
054    
055                @Override
056                protected void doStart() throws Exception {
057                    super.doStart();
058                    jbiEndpoint = jbiComponent.activateJbiEndpoint(JbiEndpoint.this, processor);
059                }
060    
061                @Override
062                protected void doStop() throws Exception {
063                    if (jbiEndpoint != null) {
064                        jbiComponent.deactivateJbiEndpoint(jbiEndpoint);
065                    }
066                    super.doStop();
067                }
068            };
069        }
070    
071    
072        public JbiExchange createExchange() {
073            return new JbiExchange(getContext(), getBinding());
074        }
075    
076        public JbiBinding getBinding() {
077            return jbiComponent.getBinding();
078        }
079    
080        public boolean isSingleton() {
081            return true;
082        }
083    }