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    import java.sql.SQLException;
021    import java.util.List;
022    
023    import com.ibatis.sqlmap.client.SqlMapClient;
024    import org.apache.camel.Message;
025    import org.apache.camel.PollingConsumer;
026    import org.apache.camel.Producer;
027    import org.apache.camel.impl.DefaultPollingEndpoint;
028    import org.apache.camel.util.ObjectHelper;
029    
030    /**
031     * An <a href="http://activemq.apache.org/camel/ibatis.html>iBatis Endpoint</a>
032     * for performing SQL operations using an XML mapping file to abstract away the SQL
033     *
034     * @version $Revision: 3500 $
035     */
036    public class IBatisEndpoint extends DefaultPollingEndpoint {
037        private final String entityName;
038        private StatementType statementType;
039    
040        public IBatisEndpoint(String endpointUri, IBatisComponent component, String entityName) {
041            super(endpointUri, component);
042            this.entityName = entityName;
043        }
044    
045        public IBatisEndpoint(String endpointUri, String entityName) {
046            super(endpointUri);
047            this.entityName = entityName;
048        }
049    
050        @Override
051        public IBatisComponent getComponent() {
052            return (IBatisComponent) super.getComponent();
053        }
054    
055        public boolean isSingleton() {
056            return true;
057        }
058    
059        public Producer createProducer() throws Exception {
060            ObjectHelper.notNull(statementType, "statementType", this);
061            return new IBatisProducer(this);
062        }
063    
064        @Override
065        public PollingConsumer createPollingConsumer() throws Exception {
066            return new IBatisPollingConsumer(this);
067        }
068    
069        /**
070         * Returns the iBatis SQL client
071         */
072        public SqlMapClient getSqlClient() throws IOException {
073            return getComponent().getSqlMapClient();
074        }
075    
076        public String getEntityName() {
077            return entityName;
078        }
079    
080        public void query(Message message) throws IOException, SQLException {
081            String name = getEntityName();
082            List list = getSqlClient().queryForList(name);
083            message.setBody(list);
084            message.setHeader("org.apache.camel.ibatis.queryName", name);
085        }
086    
087        public StatementType getStatementType() {
088            return statementType;
089        }
090    
091        public void setStatementType(StatementType statementType) {
092            this.statementType = statementType;
093        }
094    }