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.io.InputStream;
021 import java.io.InputStreamReader;
022 import java.util.Map;
023
024 import com.ibatis.sqlmap.client.SqlMapClient;
025 import com.ibatis.sqlmap.client.SqlMapClientBuilder;
026 import org.apache.camel.component.ResourceBasedComponent;
027 import org.apache.commons.logging.Log;
028 import org.apache.commons.logging.LogFactory;
029 import org.springframework.core.io.Resource;
030
031 /**
032 * An <a href="http://camel.apache.org/ibatis.html>iBatis Component</a>
033 * for performing SQL operations using an XML mapping file to abstract away the SQL
034 *
035 * @version $Revision: 17070 $
036 *
037 * <pre>
038 * Ibatis Component used to read/write to a database.
039 *
040 * <u>Requires one of the following:</u>
041 *
042 * 1. A Sql Map config file either on the root of
043 * the classpath or explicitly set.
044 *
045 * <b>OR</b>
046 *
047 * 2. A SqlMapClient explicit set.
048 *
049 * Using Ibatis as a source of data (<from>) you can use this component
050 * to treat a database table as a logical queue.
051 * Details are available in the {@link IBatisPollingConsumer}
052 *
053 * Using Ibatis as a destination for data (<to>) you can use this
054 * component to run an insert statement either on a single message or if the
055 * delivered content contains a collection of messages it can iterate through
056 * the collection and run the insert on each element.
057 * Details are available in the {@link IBatisProducer}
058 * </pre>
059 *
060 * @see IBatisProducer
061 * @see IBatisPollingConsumer
062 */
063 public class IBatisComponent extends ResourceBasedComponent {
064 private static final String DEFAULT_CONFIG_URI = "classpath:SqlMapConfig.xml";
065 private SqlMapClient sqlMapClient;
066 private String sqlMapConfig = DEFAULT_CONFIG_URI;
067 private boolean useTransactions = true;
068
069 public IBatisComponent() {
070 }
071
072 public IBatisComponent(SqlMapClient sqlMapClient) {
073 this.sqlMapClient = sqlMapClient;
074 }
075
076 /**
077 * Creates an IbatisEndpoint for use by an IbatisConsumer or IbatisProducer.
078 */
079 @Override
080 protected IBatisEndpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
081 IBatisEndpoint answer = new IBatisEndpoint(uri, this, remaining);
082 setProperties(answer, parameters);
083 return answer;
084 }
085
086 private SqlMapClient createSqlMapClient() throws IOException {
087 Resource resource = resolveMandatoryResource(sqlMapConfig);
088 InputStream is = resource.getInputStream();
089 return SqlMapClientBuilder.buildSqlMapClient(new InputStreamReader(is));
090 }
091
092 // Properties
093 //-------------------------------------------------------------------------
094
095 /**
096 * Returns the configured SqlMapClient.
097 *
098 * @return com.ibatis.sqlmap.client.SqlMapClient
099 * @throws IOException If configured with a SqlMapConfig and there
100 * is a problem reading the resource.
101 */
102 public SqlMapClient getSqlMapClient() throws IOException {
103 if (sqlMapClient == null) {
104 sqlMapClient = createSqlMapClient();
105 }
106 return sqlMapClient;
107 }
108
109 /**
110 * Sets the SqlMapClient
111 */
112 public void setSqlMapClient(SqlMapClient sqlMapClient) {
113 this.sqlMapClient = sqlMapClient;
114 }
115
116 /**
117 * The Spring uri of the SqlMapConfig
118 */
119 public String getSqlMapConfig() {
120 return sqlMapConfig;
121 }
122
123 public void setSqlMapConfig(String sqlMapConfig) {
124 this.sqlMapConfig = sqlMapConfig;
125 }
126
127 public boolean isUseTransactions() {
128 return useTransactions;
129 }
130
131 public void setUseTransactions(boolean useTransactions) {
132 this.useTransactions = useTransactions;
133 }
134 }