1
2
3
4
5
6
7
8
9 package tsukuba_bunko.peko.resource;
10
11 import java.io.InputStream;
12 import java.io.IOException;
13
14 import java.util.Properties;
15
16 import tsukuba_bunko.peko.InitializationError;
17 import tsukuba_bunko.peko.Logger;
18
19 import tsukuba_bunko.resource.DeserializerMapping;
20 import tsukuba_bunko.resource.ResourceDeserializer;
21
22
23 /***
24 * リソース ID と データ型のマッピングを管理します。
25 * @author $Author: ppoi $
26 * @version $Revision: 1.1 $
27 */
28 public class TypeMapping {
29
30 /***
31 * 唯一のインスタンス
32 */
33 private static TypeMapping _instance = null;
34
35
36 /***
37 * リソース ID - データ型マップ
38 */
39 private Properties _typeMapping = null;
40
41 /***
42 * DeserializerMapping
43 */
44 private DeserializerMapping _deserializerMapping = null;
45
46
47 /***
48 * <code>TypeMapping</code> のインスタンスを作成します。
49 */
50 protected TypeMapping()
51 {
52 super();
53 }
54
55
56 /***
57 * 指定されたリソースのデータ型名を取得します。
58 * @param resourceID リソース ID
59 * @return データ型名
60 */
61 public String getDataType( String resourceID )
62 {
63 return _typeMapping.getProperty( resourceID );
64 }
65
66 /***
67 * 指定されたリソースをデシリアライズする ResourceDeserializer を取得します。
68 * @param resourceID リソース ID
69 * @return ResourceDeserializer
70 */
71 public ResourceDeserializer getResourceDeserializer( String resourceID )
72 {
73 String type = _typeMapping.getProperty( resourceID );
74 if( type != null ) {
75 return _deserializerMapping.getResourceDeserializer( type );
76 }
77 else {
78 return null;
79 }
80 }
81
82 /***
83 * DeserializerMapping を取得します。
84 * @return DeserializerMapping
85 */
86 public DeserializerMapping getDeserializerMapping()
87 {
88 return _deserializerMapping;
89 }
90
91 /***
92 * <cpde>TypeMapping</code> を初期化します。
93 * @throws InitializationError 初期化に失敗した場合
94 */
95 protected void initialize()
96 {
97 ClassLoader cl = getClass().getClassLoader();
98 InputStream is = cl.getResourceAsStream( "resources.def" );
99 if( is == null ) {
100 throw new InitializationError( "resources.def not found." );
101 }
102
103 Properties props = new Properties();
104 try {
105 props.load( is );
106 }
107 catch( IOException ioe ) {
108 Logger.fatal( "[resource] fail to initialize TypeMapping.", ioe );
109 throw new InitializationError( "fail to initialize TypeMapping.", ioe );
110 }
111 finally {
112 try {
113 is.close();
114 }
115 catch( IOException ioe ) {
116 Logger.fatal( "[resource] fail to initialize TypeMapping.", ioe );
117 }
118 }
119 _typeMapping = props;
120
121 _deserializerMapping = DeserializerMapping.newInstance( "tsukuba_bunko.peko.resource.mapping" );
122 }
123
124
125
126 /***
127 * 唯一の <code>TypeMapping</code> のインスタンスを取得します。
128 * @return 唯一の <code>TypeMapping</code> のインスタンス
129 * @throws InitializationError 初期化に失敗した場合
130 */
131 public static TypeMapping getInstance()
132 {
133 if( _instance == null ) {
134 synchronized( TypeMapping.class ) {
135 if( _instance == null ) {
136 _instance = new TypeMapping();
137 _instance.initialize();
138 }
139 }
140 }
141 return _instance;
142 }
143 }