kr.co.skysoft.framework.dbms.ibatis.FwSqlMapClientFactoryBean.java Source code

Java tutorial

Introduction

Here is the source code for kr.co.skysoft.framework.dbms.ibatis.FwSqlMapClientFactoryBean.java

Source

/*
 * Copyright 2002-2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package kr.co.skysoft.framework.dbms.ibatis;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.springframework.core.NestedIOException;
import org.springframework.core.io.Resource;
import org.springframework.orm.ibatis.SqlMapClientFactoryBean;
import org.springframework.util.ObjectUtils;

import com.ibatis.common.xml.NodeletException;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.engine.builder.xml.SqlMapConfigParser;
import com.ibatis.sqlmap.engine.builder.xml.SqlMapParser;
import com.ibatis.sqlmap.engine.builder.xml.XmlParserState;

/**-----------------------------------------------------------------------
 * skysoft Egovframework 2.0
 *------------------------------------------------------------------------
 * @Class FwSqlMapClientFactoryBean.java
 * @Description
 *
 * @author ?
 * @since 2012. 12. 3.
 * @version 1.0
 *
 * @Copyright (c) 2012 () 
 *------------------------------------------------------------------------
 * Modification Information
 *------------------------------------------------------------------------
 * ?          ?       
 * -----------   ---------  -----------------------------------------------
 * 2012. 12. 3. ?      ?
 */
public class FwSqlMapClientFactoryBean extends SqlMapClientFactoryBean {

    protected Logger logger = Logger.getLogger(this.getClass());

    protected SqlMapClient buildSqlMapClient(Resource[] configLocations, Resource[] mappingLocations,
            Properties properties) throws IOException {

        if (ObjectUtils.isEmpty(configLocations)) {
            throw new IllegalArgumentException("At least 1 'configLocation' entry is required");
        }

        SqlMapClient client = null;
        FwSqlMapConfigParser configParser = new FwSqlMapConfigParser();
        for (Resource configLocation : configLocations) {
            InputStream is = configLocation.getInputStream();
            try {
                client = configParser.parse(is, properties);
            } catch (RuntimeException ex) {
                //SqlMap? ?  ???? ?   ? ?  ?? 
                logger.error("##### Failed to parse config resource: " + configLocation, ex);
            }
        }

        if (mappingLocations != null) {
            SqlMapParser mapParser = SqlMapParserFactory.createSqlMapParser(configParser);
            for (Resource mappingLocation : mappingLocations) {
                try {
                    mapParser.parse(mappingLocation.getInputStream());
                } catch (NodeletException ex) {
                    throw new NestedIOException("Failed to parse mapping resource: " + mappingLocation, ex);
                }
            }
        }

        return client;
    }

    /**
     * Inner class to avoid hard-coded iBATIS 2.3.2 dependency (XmlParserState class).
     */
    private static class SqlMapParserFactory {

        public static SqlMapParser createSqlMapParser(SqlMapConfigParser configParser) {
            // Ideally: XmlParserState state = configParser.getState();
            // Should raise an enhancement request with iBATIS...
            XmlParserState state = null;
            try {
                Field stateField = SqlMapConfigParser.class.getDeclaredField("state");
                stateField.setAccessible(true);
                state = (XmlParserState) stateField.get(configParser);
            } catch (Exception ex) {
                throw new IllegalStateException(
                        "iBATIS 2.3.2 'state' field not found in SqlMapConfigParser class - "
                                + "please upgrade to IBATIS 2.3.2 or higher in order to use the new 'mappingLocations' feature. "
                                + ex);
            }
            return new SqlMapParser(state);
        }
    }

}