001/*
002 * Copyright (c) 2009 The openGion Project.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013 * either express or implied. See the License for the specific language
014 * governing permissions and limitations under the License.
015 */
016package org.opengion.hayabusa.db;
017
018import org.opengion.hayabusa.common.HybsSystem;
019import org.opengion.hayabusa.common.SystemManager;
020import org.opengion.fukurou.util.Cleanable;
021
022import java.util.Map;
023import java.util.HashMap;
024import java.util.Locale ;
025import java.util.Collections;
026
027/**
028 * DBType オブジェクトを取得する為に使用する,ファクトリクラスです。
029 *
030 *  DBType オブジェクト の識別ID を元に、DBTypeFactory.newInstance( String id )
031 * メソッドで,DBType オブジェクトを取得します。
032 * このオブジェクトは、内部的にすべてキャッシュしておき、Webアプリケーション内で
033 * 同時アクセスされますが、このオブジェクトは読み取り専用の為,マルチスレッド対応
034 * していません。
035 * よって、DBTypeFactory.close() メソッドで,オブジェクトを返す必要も
036 * ありません。
037 *
038 * @og.group データ属性
039 *
040 * @version  4.0
041 * @author   Kazuhiko Hasegawa
042 * @since    JDK5.0,
043 */
044public final class DBTypeFactory {
045        private static Map<String,DBType> map = Collections.synchronizedMap( new HashMap<String,DBType>() );
046
047        // 4.0.0 (2005/01/31) Cleanable インターフェースによる初期化処理
048        static {
049                Cleanable clr = new Cleanable() {
050                        public void clear() {
051                                DBTypeFactory.clear();
052                        }
053                };
054
055                SystemManager.addCleanable( clr );
056        }
057
058        /**
059         *  デフォルトコンストラクターをprivateにして、
060         *  オブジェクトの生成をさせないようにする。
061         *
062         */
063        private DBTypeFactory() {}
064
065        /**
066         * 識別id に応じた DBType オブジェクトを取得します。
067         * DBType オブジェクト はすべてのWebアプリケーション中で
068         * 共有して使用されます。
069         *
070         * @og.rev 3.4.0.2 (2003/09/05) DBType のデフォルト値を、'X' から 'XK' に変更します。
071         * @og.rev 3.5.6.0 (2004/06/18) 各種プラグイン関連付け設定を、システムパラメータ に記述します。
072         * @og.rev 4.0.0.0 (2005/01/31) キーの指定を、DBType. から、DBType_ に変更します。
073         * @og.rev 5.1.6.0 (2010/05/01) 初期タイプを DBType.DEF_TYPE を使用するように変更します(設定値は、XK のままです。)
074         *
075         * @param       id DBTypeインターフェースを実装したサブクラスの識別id
076         *
077         * @return      DBTypeオブジェクト
078         */
079        public static DBType newInstance( final String id ) {
080                String type = ( id == null ) ? DBType.DEF_TYPE : id.toUpperCase(Locale.JAPAN);
081                DBType dbType = map.get( type );
082                if( dbType == null ) {
083                        String cls = HybsSystem.sys( "DBType_" + type ) ;       // 4.0.0 (2005/01/31)
084                        dbType = (DBType)HybsSystem.newInstance( cls ); // 3.5.5.3 (2004/04/09)
085                        map.put( type,dbType );
086                }
087                return dbType;
088        }
089
090        /**
091         * 内部キャッシュのすべての DBType オブジェクトを削除します。
092         */
093        public static void clear() {
094                map.clear() ;
095        }
096}