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    }