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.util.Map;
022
023 import com.ibatis.sqlmap.client.SqlMapClient;
024 import com.ibatis.sqlmap.client.SqlMapClientBuilder;
025 import org.apache.camel.Endpoint;
026 import org.apache.camel.impl.DefaultComponent;
027 import org.apache.commons.logging.Log;
028 import org.apache.commons.logging.LogFactory;
029 import org.springframework.core.io.ClassPathResource;
030 import org.springframework.core.io.Resource;
031
032 /**
033 * An <a href="http://activemq.apache.org/camel/ibatis.html>iBatis Component</a>
034 * for performing SQL operations using an XML mapping file to abstract away the SQL
035 *
036 * @version $Revision: 3500 $
037 */
038 public class IBatisComponent extends DefaultComponent {
039 public static final String DEFAULT_CONFIG_URI = "SqlMapConfig.xml";
040 private static final transient Log LOG = LogFactory.getLog(IBatisComponent.class);
041
042
043 private SqlMapClient sqlMapClient;
044 private Resource sqlMapResource;
045
046 public IBatisComponent() {
047 }
048
049 public IBatisComponent(SqlMapClient sqlMapClient) {
050 this.sqlMapClient = sqlMapClient;
051 }
052
053 // Properties
054 //-------------------------------------------------------------------------
055 public SqlMapClient getSqlMapClient() throws IOException {
056 if (sqlMapClient == null) {
057 sqlMapClient = createSqlMapClient();
058 }
059 return sqlMapClient;
060 }
061
062 public void setSqlMapClient(SqlMapClient sqlMapClient) {
063 this.sqlMapClient = sqlMapClient;
064 }
065
066 public Resource getSqlMapResource() {
067 if (sqlMapResource == null) {
068 sqlMapResource = new ClassPathResource(DEFAULT_CONFIG_URI);
069 LOG.debug("Defaulting to use the iBatis configuration from: " + sqlMapResource);
070 }
071 return sqlMapResource;
072 }
073
074 public void setSqlMapResource(Resource sqlMapResource) {
075 this.sqlMapResource = sqlMapResource;
076 }
077
078 // Implementation methods
079 //-------------------------------------------------------------------------
080 protected Endpoint createEndpoint(String uri, String remaining, Map parameters) throws Exception {
081 IBatisEndpoint answer = new IBatisEndpoint(uri, this, remaining);
082 setProperties(answer, parameters);
083 return answer;
084 }
085
086 protected SqlMapClient createSqlMapClient() throws IOException {
087 InputStream in = getSqlMapResource().getInputStream();
088 return SqlMapClientBuilder.buildSqlMapClient(in);
089 }
090 }