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.ibatis;
018    
019    import java.io.IOException;
020    
021    import com.ibatis.sqlmap.client.SqlMapClient;
022    import org.apache.camel.Processor;
023    import org.apache.camel.Producer;
024    import org.apache.camel.component.ibatis.strategy.DefaultIBatisProcessingStategy;
025    import org.apache.camel.component.ibatis.strategy.IBatisProcessingStrategy;
026    import org.apache.camel.impl.DefaultPollingEndpoint;
027    import org.apache.camel.util.ObjectHelper;
028    
029    /**
030     * An <a href="http://camel.apache.org/ibatis.html>iBatis Endpoint</a>
031     * for performing SQL operations using an XML mapping file to abstract away the SQL
032     *
033     * @version $Revision: 13164 $
034     */
035    public class IBatisEndpoint extends DefaultPollingEndpoint {
036        private IBatisProcessingStrategy strategy;
037        private boolean useTransactions;
038        private String statement;
039        private StatementType statementType;
040        private int maxMessagesPerPoll;
041    
042        public IBatisEndpoint() {
043        }
044    
045        public IBatisEndpoint(String uri, IBatisComponent component, String statement) throws Exception {
046            super(uri, component);
047            setUseTransactions(component.isUseTransactions());
048            setStatement(statement);
049        }
050    
051        @Override
052        public IBatisComponent getComponent() {
053            return (IBatisComponent) super.getComponent();
054        }
055        
056        public boolean isSingleton() {
057            return true;
058        }
059    
060        public Producer createProducer() throws Exception {
061            ObjectHelper.notNull(statementType, "statementType", this);
062            return new IBatisProducer(this);
063        }
064    
065        @Override
066        public IBatisPollingConsumer createConsumer(Processor processor) throws Exception {
067            IBatisPollingConsumer consumer = new IBatisPollingConsumer(this, processor);
068            consumer.setMaxMessagesPerPoll(getMaxMessagesPerPoll());
069            configureConsumer(consumer);
070            return consumer;
071        }
072    
073        /**
074         * Gets the iBatis SqlMapClient
075         */
076        public SqlMapClient getSqlMapClient() throws IOException {
077            return getComponent().getSqlMapClient();
078        }
079    
080        /**
081         * Gets the IbatisProcessingStrategy to to use when consuming messages from the database
082         */
083        public IBatisProcessingStrategy getProcessingStrategy() throws Exception {
084            if (strategy == null) {
085                strategy = new DefaultIBatisProcessingStategy();
086            }
087            return strategy;
088        }
089    
090        public void setStrategy(IBatisProcessingStrategy strategy) {
091            this.strategy = strategy;
092        }
093    
094        /**
095         * Statement to run when polling or processing
096        */
097        public String getStatement() {
098            return statement;
099        }
100        
101        /**
102         * Statement to run when polling or processing
103         */
104        public void setStatement(String statement) {
105            this.statement = statement;
106        }
107    
108        /**
109         * Indicates if transactions should be used when calling statements.  Useful if using a comma separated list when
110         * consuming records.
111         */
112        public boolean isUseTransactions() {
113            return useTransactions;
114        }
115    
116        /**
117         * Sets indicator to use transactions for consuming and error handling statements.
118         */
119        public void setUseTransactions(boolean useTransactions) {
120            this.useTransactions = useTransactions;
121        }
122    
123        public StatementType getStatementType() {
124            return statementType;
125        }
126    
127        public void setStatementType(StatementType statementType) {
128            this.statementType = statementType;
129        }
130    
131        public int getMaxMessagesPerPoll() {
132            return maxMessagesPerPoll;
133        }
134    
135        public void setMaxMessagesPerPoll(int maxMessagesPerPoll) {
136            this.maxMessagesPerPoll = maxMessagesPerPoll;
137        }
138    }