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    }