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.concurrent.ConcurrentMap;                                                      // 6.4.3.3 (2016/03/04)
023import java.util.concurrent.ConcurrentHashMap;                                          // 6.4.3.1 (2016/02/12) refactoring
024import java.util.Locale ;
025
026/**
027 * DBType オブジェクトを取得する為に使用する,ファクトリクラスです。
028 *
029 *  DBType オブジェクト の識別ID を元に、DBTypeFactory.newInstance( String id )
030 * メソッドで,DBType オブジェクトを取得します。
031 * このオブジェクトは、内部的にすべてキャッシュしておき、Webアプリケーション内で
032 * 同時アクセスされますが、このオブジェクトは読み取り専用の為,マルチスレッド対応
033 * していません。
034 * よって、DBTypeFactory.close() メソッドで,オブジェクトを返す必要も
035 * ありません。
036 *
037 * @og.group データ属性
038 *
039 * @version  4.0
040 * @author   Kazuhiko Hasegawa
041 * @since    JDK5.0,
042 */
043public final class DBTypeFactory {
044        /** 6.4.3.1 (2016/02/12) PMD refactoring. HashMap → ConcurrentHashMap に置き換え。  */
045        private static final ConcurrentMap<String,DBType> TYPE_MAP = new ConcurrentHashMap<>();
046
047        // 4.0.0 (2005/01/31) Cleanable インターフェースによる初期化処理
048        static {
049                final Cleanable clr = new Cleanable() {
050                        /**
051                         * 初期化(クリア)します。
052                         * 主に、キャッシュクリアで利用します。
053                         */
054                        public void clear() {
055                                DBTypeFactory.clear();
056                        }
057                };
058
059                SystemManager.addCleanable( clr );
060        }
061
062        /**
063         *  デフォルトコンストラクターをprivateにして、
064         *  オブジェクトの生成をさせないようにする。
065         *
066         */
067        private DBTypeFactory() {}
068
069        /**
070         * 識別id に応じた DBType オブジェクトを取得します。
071         * DBType オブジェクト はすべてのWebアプリケーション中で
072         * 共有して使用されます。
073         *
074         * @og.rev 3.4.0.2 (2003/09/05) DBType のデフォルト値を、'X' から 'XK' に変更します。
075         * @og.rev 3.5.6.0 (2004/06/18) 各種プラグイン関連付け設定を、システムパラメータ に記述します。
076         * @og.rev 4.0.0.0 (2005/01/31) キーの指定を、DBType. から、DBType_ に変更します。
077         * @og.rev 5.1.6.0 (2010/05/01) 初期タイプを DBType.DEF_TYPE を使用するように変更します(設定値は、XK のままです。)
078         * @og.rev 6.0.4.0 (2014/11/28) NullPointerException が発生するので、事前にチェックします。
079         * @og.rev 6.4.3.3 (2016/03/04) HybsSystem.newInstance(String,String) への置き換え。
080         *
081         * @param       id DBTypeインターフェースを実装したサブクラスの識別id
082         *
083         * @return      DBTypeオブジェクト
084         */
085        public static DBType newInstance( final String id ) {
086                final String type = ( id == null ) ? DBType.DEF_TYPE : id.toUpperCase(Locale.JAPAN);
087
088                // Map#computeIfAbsent : 戻り値は、既存の、または計算された値。追加有り、置換なし、削除なし
089                return TYPE_MAP.computeIfAbsent( type , k -> HybsSystem.newInstance( "DBType_" , k ) );
090        }
091
092        /**
093         * 内部キャッシュのすべての DBType オブジェクトを削除します。
094         */
095        public static void clear() {
096                TYPE_MAP.clear() ;
097        }
098}