All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jp.co.tis.gsp.tools.dba.dialect.DialectFactory Maven / Gradle / Ivy

Go to download

To automate the routine work of the DBA, it is a tool to be able to concentrate on the data modeling work.

There is a newer version: 5.0.0
Show newest version
/*
 * Copyright (C) 2015 coastland
 *
 * 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 jp.co.tis.gsp.tools.dba.dialect;

import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.Map;

public class DialectFactory {
	public static Dialect getDialect(String url, String driver) {
		String[] urlTokens = StringUtils.split(url, ':');
		if(urlTokens.length < 3) {
			throw new IllegalArgumentException("url isn't jdbc url format.");
		}
		Dialect dialect;
		try {
            Class dialectClass;
            if (classMap.containsKey(urlTokens[1])) {
                dialectClass = classMap.get(urlTokens[1]);

            } else {
                String dialectClassName;
                dialectClassName = "jp.co.tis.gsp.tools.dba.dialect."
                        +StringUtils.capitalize(urlTokens[1])+"Dialect";
                dialectClass = Class.forName(dialectClassName);
            }
			dialect = (Dialect)dialectClass.newInstance();
			dialect.setUrl(url);
			dialect.setDriver(driver);
		} catch(Exception e) {
			throw new IllegalArgumentException("Unsupported Database product:" + urlTokens[1], e);
		}

		return dialect;
	}

    private static Map> classMap = new HashMap>();
    public static void registerOptionalDialects(Map optionalDialectClassNames) {
        if (optionalDialectClassNames == null) {
            return;
        }
        try {
            for (String urlPrefix : optionalDialectClassNames.keySet()) {
                Class dialectClass = Class.forName(optionalDialectClassNames.get(urlPrefix));
                classMap.put(urlPrefix, dialectClass);
            }
        } catch (Exception e) {
            throw new IllegalArgumentException("Can't create dialect:" + optionalDialectClassNames, e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy