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.resource;
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.Set;
023    import java.util.Map;
024    import java.util.HashMap;
025    import java.util.Collections ;
026    
027    /**
028     * java.util.ResourceBundle クラスをè¤?•°管ç�?�™るResourceManager をリソース毎に作æ?しますã?
029     * ResourceFactory#newInstance( String lang ) により?ŒResourceManager の要求毎に
030     * 新しくオブジェクトを作æ?するのではなã�?ロケール毎に ResourceManager を作æ?しますã?
031     * ResourceManagerは,ロケール毎に å†?ƒ¨のプã?ルに保存されてã�?�¾すã?
032     *
033     * リソース作æ?時にæŒ?®šするロケールは,ISO è¨?ªžコーãƒ?ISO-639 で定義されã‚?2 桁ã?小文å­?
034     * <a href ="http://www.ics.uci.edu/pub/ietf/http/related/iso639.txt">
035     * http://www.ics.uci.edu/pub/ietf/http/related/iso639.txt</a>を使用して下さã�??
036     * ただし,å?部çš?�« Locale を構築してã�?�¾すが,そã?正しさは,チェãƒ?‚¯されてã�?�¾せんので,
037     * æŒ?®šするロケールに応じã�?properties ファイルを用意しておいて下さã�??
038     *
039     * @og.group リソース管ç�?
040     *
041     * @version  4.0
042     * @author   Kazuhiko Hasegawa
043     * @since    JDK5.0,
044     */
045    public final class ResourceFactory {
046            private static final String SYSTEM_ID = HybsSystem.sys( "SYSTEM_ID" );
047    
048            // ãƒ?ƒ•ォルトシスãƒ?ƒ ?©?¤の日本èª?ja)は、特別扱ã�?�™るã?
049            private static final ResourceManager ja_Manager = new ResourceManager( SYSTEM_ID,"ja",true );
050    
051            private static final Map<String,ResourceManager> pool = Collections.synchronizedMap( new HashMap<String,ResourceManager>() );
052    
053            // 4.0.0 (2005/01/31) Cleanable インターフェースによる初期化å?ç�?
054            static {
055                    Cleanable clr = new Cleanable() {
056                            public void clear() {
057                                    ResourceFactory.clear();
058                            }
059                    };
060    
061                    SystemManager.addCleanable( clr );
062            }
063    
064            /**
065             *  ãƒ?ƒ•ォルトコンストラクターをprivateにしてã€?
066             *  オブジェクトã?生æ?をさせなã�?‚ˆã�?�«するã€?
067             *
068             */
069            private ResourceFactory() {
070            }
071    
072            /**
073             * ResourceManager オブジェクトを取得しますã?
074             * 引数のè¨?ªžコードに応じたリソースを1度ã�?�‘作æ?しますã?
075             * 作æ?したリソースオブジェクトã??Œå?部にプã?ルしておき?Œ同じリソース要求が
076             * あったときã??Œã?ールのリソースを返しますã?
077             *
078             * @param       lang    è¨?ªžコーãƒ?null の場合ã?ã€?ja" としますã?)
079             *
080             * @return      ResourceManagerオブジェクãƒ?
081             */
082            public static ResourceManager newInstance( final String lang ) {
083                    if( lang == null || "ja".equalsIgnoreCase( lang ) ) {
084                            return ja_Manager ;
085                    }
086                    return newInstance( SYSTEM_ID,lang,true );
087            }
088    
089            /**
090             * ResourceManager オブジェクトを取得しますã?
091             * 引数のè¨?ªžコードに応じたリソースを1度ã�?�‘作æ?しますã?
092             * 作æ?したリソースオブジェクトã??Œå?部にプã?ルしておき?Œ同じリソース要求が
093             * あったときã??Œã?ールのリソースを返しますã?
094             *
095             * @param       systemId        シスãƒ?ƒ ?©?¤(null の場合ã?、HybsSystem の SYSTEM_ID パラメータ)
096             * @param       lang            è¨?ªžコーãƒ?null の場合ã?ã€?ja" としますã?)
097             * @param       initLoad        リソースãƒ??タの先読み可否(true:先読みする)
098             *
099             * @return      ResourceManagerオブジェクãƒ?
100             */
101            public static ResourceManager newInstance( final String systemId,final String lang,final boolean initLoad ) {
102                    String sys = (systemId != null ) ? systemId : SYSTEM_ID ;
103                    String lg  = (lang     != null ) ? lang     : "ja" ;
104    
105                    if( SYSTEM_ID.equalsIgnoreCase( sys ) && "ja".equalsIgnoreCase( lg ) ) {
106                            return ja_Manager ;
107                    }
108    
109                    String key = sys + lg ;
110    
111                    ResourceManager resource = pool.get( key );
112    
113                    if( resource == null ) {
114                            resource = new ResourceManager( sys,lg,initLoad );
115                            pool.put( key,resource );
116                    }
117                    return resource;
118            }
119    
120            /**
121             * キャãƒ?‚·ュ(プã?ル)から、すべてのオブジェクトをクリアしますã?
122             * こã?時ã?poolされてã�?‚‹オブジェクトã?、ResourceManager#clear() メソãƒ?ƒ‰ã‚?
123             * 呼び出しますã?
124             *
125             * @og.rev 3.5.5.7 (2004/05/10) CodeSelectionFactoryをクリアしますã?
126             */
127            public static void clear() {
128                    ja_Manager.clear();
129    
130                    Set<String> keyset = pool.keySet();
131                    String[] keys = (keyset.toArray( new String[keyset.size()] )) ;
132    
133                    for( int i=0; i<keys.length; i++ ) {
134                            ResourceManager resource = pool.remove( keys[i] );
135                            resource.clear();
136                    }
137                    pool.clear();
138            }
139    
140            /**
141             * キャãƒ?‚·ュ(プã?ル)から、すべてのGUIæƒ??オブジェクトをクリアしますã?
142             *
143             * @og.rev 4.0.0.0 (2005/01/31) 新規追åŠ?
144             */
145            public static void guiClear() {
146                    ja_Manager.guiClear();
147    
148                    Set<String> keyset = pool.keySet();
149                    String[] keys = keyset.toArray( new String[keyset.size()] ) ;
150    
151                    ResourceManager resource ;
152                    for( int j=0; j<keys.length; j++ ) {
153                            resource = pool.get( keys[j] );
154                            resource.guiClear();
155                    }
156            }
157    }