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     */
016    package org.opengion.hayabusa.db;
017    
018    import org.opengion.hayabusa.common.HybsSystem ;
019    import org.opengion.hayabusa.common.SystemManager ;
020    import org.opengion.fukurou.util.Cleanable;
021    
022    import java.util.Map;
023    import java.util.WeakHashMap ;
024    
025    /**
026     * Selectionオブジェクトを取得する為に使用するファクトリクラスです?
027     *
028     * Selectionオブジェク?のキー(codeName)を?に、オブジェクトをキャ?ュ管?
029     * することが?主な機?です?
030     *
031     * @og.rev 3.5.5.7 (2004/05/10) 新規作?
032     * @og.group 選択データ制御
033     *
034     * @version  4.0
035     * @author   Kazuhiko Hasegawa
036     * @since    JDK5.0,
037     */
038    public final class SelectionFactory {
039            // private static final Map<String,Selection>  codeMap = new WeakHashMap<String,Selection>();
040            private static final Map<String,Selection>  dbMap   = new WeakHashMap<String,Selection>( HybsSystem.BUFFER_SMALL );
041            private static final Map<String,Selection>  dbRadioMap   = new WeakHashMap<String,Selection>( HybsSystem.BUFFER_SMALL ); // 4.3.3.6 (2008/11/15)
042    
043            // 4.0.0 (2005/01/31) Cleanable インターフェースによる初期化??
044            static {
045                    Cleanable clr = new Cleanable() {
046                            public void clear() {
047                                    SelectionFactory.clear();
048                            }
049                    };
050    
051                    SystemManager.addCleanable( clr );
052            }
053    
054            /**
055             *  ?ォルトコンストラクターをprivateにして?
056             *  オブジェクト?生?をさせな??する?
057             *
058             */
059            private SelectionFactory() {
060            }
061    
062            /**
063             * コードデータオブジェクトより?コードリソースSelectionオブジェクトを構築します?
064             *
065             * @og.rev 4.0.0.0 (2007/11/07) DBColumnにSelectionオブジェクトをキャ?ュするように変更
066             *
067             * @param   cdData CodeData コードデータ
068             *
069             * @return  Selectionオブジェク?
070             */
071    //      public static Selection newCodeSelection( final CodeData cdData ) {
072    //              String key = cdData.getColumn() ;
073    //              Selection select = codeMap.get( key );
074    //              if( select == null ) {
075    //                      synchronized( codeMap ) {
076    //                              select = new Selection_CODE( cdData );
077    //                              codeMap.put( key,select );
078    //                      }
079    //              }
080    //              return select;
081    //      }
082    
083            /**
084             * DB検索(SQL)??より、データベ?スSelectionオブジェクトを構築します?
085             * Selection_DB では、検索行毎?クエリーがあるため?name + query でキャ?ュします?
086             *
087             * @og.rev 4.0.0.0 (2006/11/15) lang 属?を追?ます?
088             *
089             * @param   query DB検索(SQL)??
090             * @param       dbid  ??タベ?ス接続?ID
091             * @param       lang  リソースを使用する場合???
092             *
093             * @return  Selectionオブジェク?
094             */
095            public static Selection newDBSelection( final String query,final String dbid,final String lang ) {
096                    String key = query+dbid+lang;
097                    Selection select = dbMap.get( key );
098    
099                    if( select == null || select.isTimeOver() ) {
100                            synchronized( dbMap ) {
101                                    select = new Selection_DB( query,dbid,lang );
102                                    dbMap.put( key,select );
103                            }
104                    }
105                    return select;
106            }
107    
108            /**
109             * DB検索(SQL)??より、データベ?スSelectionオブジェクトを構築します?
110             * Selection_DB では、検索行毎?クエリーがあるため?name + query でキャ?ュします?
111             *
112             * @og.rev 4.3.3.6 (2008/11/15) 新規作?
113             *
114             * @param   query DB検索(SQL)??
115             * @param       dbid  ??タベ?ス接続?ID
116             * @param       lang  リソースを使用する場合???
117             *
118             * @return  Selectionオブジェク?
119             */
120            public static Selection newDBRadioSelection( final String query,final String dbid,final String lang ) {
121                    String key = query+dbid+lang;
122                    Selection select = dbRadioMap.get( key );
123    
124                    if( select == null || select.isTimeOver() ) {
125                            synchronized( dbRadioMap ) {
126                                    select = new Selection_DBRADIO( query,dbid,lang );
127                                    dbRadioMap.put( key,select );
128                            }
129                    }
130                    return select;
131            }
132    
133            /**
134             * Selectionオブジェクトをプ?ルからすべて削除します?
135             * シス?全体を初期化するときや、動作が不安定になったときに行います?
136             * プ?ルの方法?体が,?のキャ?ュ?使?たしかして??,
137             * 実行中でも??でも?ールを?期化できます?
138             *
139             * @og.rev 4.3.3.6 (2008/11/15) DBRadioMap追?
140             */
141            public static void clear() {
142                    // synchronized( codeMap ) { codeMap.clear(); }
143                    synchronized( dbMap   ) { dbMap.clear(); }
144                    synchronized( dbRadioMap ) { dbRadioMap.clear(); } // 4.3.3.6 (2008/11/15)
145            }
146    }