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.resource; 017 018import org.opengion.hayabusa.common.HybsSystem; 019import org.opengion.hayabusa.common.SystemManager; 020import org.opengion.fukurou.util.Cleanable; 021 022import java.util.Set; 023import java.util.Map; 024import java.util.HashMap; 025import 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 */ 045public final class ResourceFactory { 046 private static final String SYSTEM_ID = HybsSystem.sys( "SYSTEM_ID" ); 047 048 // デフォルトシステムIDの日本語(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 システムID(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}