com.pactera.edg.am.metamanager.extractor.util.GenSqlUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.pactera.edg.am.metamanager.extractor.util.GenSqlUtil.java

Source

/*
 * Copyright 2009 by pactera.edg.am Corporation. Address:HePingLi East Street No.11
 * 5-5, BeiJing,
 * 
 * All rights reserved.
 * 
 * This software is the confidential and proprietary information of pactera.edg.am
 * Corporation ("Confidential Information"). You shall not disclose such
 * Confidential Information and shall use it only in accordance with the terms
 * of the license agreement you entered into with pactera.edg.am.
 */

package com.pactera.edg.am.metamanager.extractor.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Element;

import com.pactera.edg.am.metamanager.extractor.ex.SQLFileNotLoadException;

/**
 * SQL??, ?:?,?SQL?
 * 
 * @author hqchen
 * @version 1.0 Date: Jul 9, 2009
 * @version 2.2 2011-02-23  - spring????SQL
 */
public class GenSqlUtil {

    private static Log log = LogFactory.getLog(GenSqlUtil.class);

    /**
     * ??SQLKey????ValueSQLMap?
     */
    private Map<String, Map<String, String>> sqlHolder = new HashMap<String, Map<String, String>>();

    /**
     * ?????SQL??????
     */
    private Map<String, String> locations = new HashMap<String, String>();

    private static final GenSqlUtil instance = new GenSqlUtil();

    /**
     * ?MM?
     */
    private String databaseName;

    /**
     * private creator
     */
    private GenSqlUtil() {

    }

    /**
     * factory Getter
     * 
     * @return this instance
     */
    public static GenSqlUtil getInstance() {
        return instance;
    }

    public Map<String, String> getLocations() {
        return locations;
    }

    public void setLocations(Map<String, String> locations) {
        this.locations = locations;
    }

    /**
     * ??SQLoracle,teradata,sqlserver
     * 
     * @param databaseName
     *            ????
     * @return null
     */
    public String getLocation(String databaseName) {
        if (locations.containsKey(databaseName)) { // ???
            return locations.get(databaseName);
        }
        for (Iterator<String> it = locations.keySet().iterator(); it.hasNext();) {
            String key = it.next();
            // ??DB2?????DB2/NT, DB2/LINUX, DB2/SUN
            if (databaseName.matches(key)) {
                return locations.get(key);
            }
        }
        return null;
    }

    /**
     * ????Oracle?Teradata?SQL
     * 
     * @return
     * @throws IOException
     *             ????Bean
     * @throws SQLException
     *             ?????
     */
    private static Map<String, String> findDatabaseSQL() {
        Connection conn = null;
        String databaseName = instance.databaseName;
        try {
            if (databaseName == null) {
                DataSource dataSource = ExtractorContextLoader.getDataSource();
                conn = dataSource.getConnection();
                DatabaseMetaData meta = conn.getMetaData();
                instance.databaseName = meta.getDatabaseProductName();
                databaseName = instance.databaseName;
            }
            String location = instance.getLocation(databaseName);
            if (location == null) {
                throw new SQLFileNotLoadException("???" + databaseName
                        + "SQLspring/context-service.xmlGenSqlUtil");
            }

            if (instance.sqlHolder.get(databaseName) == null) {
                Map<String, String> sqls = readSQL(location);
                instance.sqlHolder.put(databaseName, sqls);
            }
            return instance.sqlHolder.get(databaseName);
        } catch (SQLException e) {
            databaseName = (databaseName == null) ? "Unkown" : databaseName;
            String s = "??[" + databaseName + "]SQL";
            log.error(s, e);
            throw new SQLFileNotLoadException(s, e);
        } catch (IOException e) {
            databaseName = (databaseName == null) ? "Unkown" : databaseName;
            String s = "??[" + databaseName + "]SQL";
            log.error(s, e);
            throw new SQLFileNotLoadException(s, e);
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * ?SQL
     * 
     * @throws IOException
     *             XML?
     */
    private static Map<String, String> readSQL(String location) throws IOException {
        Map<String, String> sqls = new HashMap<String, String>();
        MessageFormat mf = new MessageFormat(Constants.SQL_STORE_PATH);
        String file = mf.format(new Object[] { location });

        InputStream in = null;
        Dom4jReader reader = null;
        try {
            in = GenSqlUtil.class.getClassLoader().getResourceAsStream(file);
            reader = new Dom4jReader();
            reader.initDocument(in);
            List<?> elements = reader.selectNodes(Constants.SQL_FLAG);
            for (int i = 0; i < elements.size(); i++) {
                Element element = (Element) elements.get(i);
                sqls.put(element.attributeValue("id"), element.getTextTrim());
            }
        } finally {
            if (reader != null) {
                reader.close();
            }
            if (in != null) {
                in.close();
            }
        }
        return sqls;
    }

    /**
     * ?SQLID?SQL,??SQL,?SQL<br>
     * ???SQL???BI.MetaManagerWeb
     * ?????MMWebSQL Server??????
     * Web??Oracle?MM????????SQL??
     * 
     * @param sqlId
     *            XMLID
     * @return SQL SQL?????SQL
     */
    public static String getSql(String sqlId) {
        return findDatabaseSQL().get(sqlId);
    }

    /**
     * ??????SQL
     */
    public static void restore() {
        instance.databaseName = null;
    }
}