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 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
public class TypeMapping { |
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
0 |
private static TypeMapping _instance = null; |
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
0 |
private Properties _typeMapping = null; |
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
0 |
private DeserializerMapping _deserializerMapping = null; |
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
protected TypeMapping() |
51 |
|
{ |
52 |
0 |
super(); |
53 |
0 |
} |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
public String getDataType( String resourceID ) |
62 |
|
{ |
63 |
0 |
return _typeMapping.getProperty( resourceID ); |
64 |
|
} |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
public ResourceDeserializer getResourceDeserializer( String resourceID ) |
72 |
|
{ |
73 |
0 |
String type = _typeMapping.getProperty( resourceID ); |
74 |
0 |
if( type != null ) { |
75 |
0 |
return _deserializerMapping.getResourceDeserializer( type ); |
76 |
|
} |
77 |
|
else { |
78 |
0 |
return null; |
79 |
|
} |
80 |
|
} |
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
public DeserializerMapping getDeserializerMapping() |
87 |
|
{ |
88 |
0 |
return _deserializerMapping; |
89 |
|
} |
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
protected void initialize() |
96 |
|
{ |
97 |
0 |
ClassLoader cl = getClass().getClassLoader(); |
98 |
0 |
InputStream is = cl.getResourceAsStream( "resources.def" ); |
99 |
0 |
if( is == null ) { |
100 |
0 |
throw new InitializationError( "resources.def not found." ); |
101 |
|
} |
102 |
|
|
103 |
0 |
Properties props = new Properties(); |
104 |
|
try { |
105 |
0 |
props.load( is ); |
106 |
|
} |
107 |
0 |
catch( IOException ioe ) { |
108 |
0 |
Logger.fatal( "[resource] fail to initialize TypeMapping.", ioe ); |
109 |
0 |
throw new InitializationError( "fail to initialize TypeMapping.", ioe ); |
110 |
|
} |
111 |
|
finally { |
112 |
0 |
try { |
113 |
0 |
is.close(); |
114 |
|
} |
115 |
0 |
catch( IOException ioe ) { |
116 |
0 |
Logger.fatal( "[resource] fail to initialize TypeMapping.", ioe ); |
117 |
0 |
} |
118 |
0 |
} |
119 |
0 |
_typeMapping = props; |
120 |
|
|
121 |
0 |
_deserializerMapping = DeserializerMapping.newInstance( "tsukuba_bunko.peko.resource.mapping" ); |
122 |
0 |
} |
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
public static TypeMapping getInstance() |
132 |
|
{ |
133 |
0 |
if( _instance == null ) { |
134 |
0 |
synchronized( TypeMapping.class ) { |
135 |
0 |
if( _instance == null ) { |
136 |
0 |
_instance = new TypeMapping(); |
137 |
0 |
_instance.initialize(); |
138 |
|
} |
139 |
0 |
} |
140 |
|
} |
141 |
0 |
return _instance; |
142 |
|
} |
143 |
|
} |