KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > hessian > io > SerializerFactory


1 /*
2  * Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved.
3  *
4  * The Apache Software License, Version 1.1
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution, if
19  * any, must include the following acknowlegement:
20  * "This product includes software developed by the
21  * Caucho Technology (http://www.caucho.com/)."
22  * Alternately, this acknowlegement may appear in the software itself,
23  * if and wherever such third-party acknowlegements normally appear.
24  *
25  * 4. The names "Burlap", "Resin", and "Caucho" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * info@caucho.com.
29  *
30  * 5. Products derived from this software may not be called "Resin"
31  * nor may "Resin" appear in their names without prior written
32  * permission of Caucho Technology.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37  * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
38  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
39  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
40  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
42  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
43  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
44  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  * @author Scott Ferguson
47  */

48
49 package com.caucho.hessian.io;
50
51 import com.caucho.burlap.io.BurlapRemoteObject;
52
53 import java.io.File JavaDoc;
54 import java.io.IOException JavaDoc;
55 import java.io.InputStream JavaDoc;
56 import java.math.BigDecimal JavaDoc;
57 import java.util.*;
58 import java.util.logging.Level JavaDoc;
59 import java.util.logging.Logger JavaDoc;
60
61 /**
62  * Factory for returning serialization methods.
63  */

64 public class SerializerFactory extends AbstractSerializerFactory
65 {
66   private static final Logger JavaDoc log
67     = Logger.getLogger(SerializerFactory.class.getName());
68   
69   private static Class JavaDoc _enumClass;
70   
71   private static HashMap _serializerMap;
72   private static HashMap _deserializerMap;
73   private static HashMap _typeMap;
74
75   protected Serializer _defaultSerializer;
76
77   // Additional factories
78
protected ArrayList _factories = new ArrayList();
79   
80   protected CollectionSerializer _collectionSerializer;
81   
82   private Deserializer _hashMapDeserializer;
83   private HashMap _cachedSerializerMap;
84   private HashMap _cachedDeserializerMap;
85   private HashMap _cachedTypeDeserializerMap;
86
87   /**
88    * Set true if the collection serializer should send the java type.
89    */

90   public void setSendCollectionType(boolean isSendType)
91   {
92     if (_collectionSerializer == null)
93       _collectionSerializer = new CollectionSerializer();
94
95     _collectionSerializer.setSendJavaType(isSendType);
96   }
97
98   /**
99    * Adds a factory.
100    */

101   public void addFactory(AbstractSerializerFactory factory)
102   {
103     _factories.add(factory);
104   }
105
106   /**
107    * Returns the serializer for a class.
108    *
109    * @param cl the class of the object that needs to be serialized.
110    *
111    * @return a serializer object for the serialization.
112    */

113   public Serializer getSerializer(Class JavaDoc cl)
114     throws HessianProtocolException
115   {
116     Serializer serializer;
117
118     serializer = (Serializer) _serializerMap.get(cl);
119     if (serializer != null)
120       return serializer;
121
122     if (_cachedSerializerMap != null) {
123       serializer = (Serializer) _cachedSerializerMap.get(cl);
124       
125       if (serializer != null)
126     return serializer;
127     }
128
129     for (int i = 0;
130      serializer == null && _factories != null && i < _factories.size();
131      i++) {
132       AbstractSerializerFactory factory;
133
134       factory = (AbstractSerializerFactory) _factories.get(i);
135
136       serializer = factory.getSerializer(cl);
137     }
138
139     if (serializer != null) {
140     }
141
142     else if (HessianRemoteObject.class.isAssignableFrom(cl))
143       serializer = new RemoteSerializer();
144
145     else if (BurlapRemoteObject.class.isAssignableFrom(cl))
146       serializer = new RemoteSerializer();
147
148     else if (Map.class.isAssignableFrom(cl))
149       serializer = new MapSerializer();
150
151     else if (Collection.class.isAssignableFrom(cl)) {
152       if (_collectionSerializer == null) {
153     _collectionSerializer = new CollectionSerializer();
154       }
155
156       serializer = _collectionSerializer;
157     }
158
159     else if (cl.isArray())
160       serializer = new ArraySerializer();
161
162     else if (Throwable JavaDoc.class.isAssignableFrom(cl))
163       serializer = new ThrowableSerializer(cl);
164
165     else if (InputStream JavaDoc.class.isAssignableFrom(cl))
166       serializer = new InputStreamSerializer();
167
168     else if (Iterator.class.isAssignableFrom(cl))
169       serializer = IteratorSerializer.create();
170
171     else if (Enumeration.class.isAssignableFrom(cl))
172       serializer = EnumerationSerializer.create();
173     
174     else if (Calendar.class.isAssignableFrom(cl))
175       serializer = CalendarSerializer.create();
176     
177     else if (_enumClass != null && _enumClass.isAssignableFrom(cl))
178       serializer = new EnumSerializer(cl);
179
180     if (serializer == null)
181       serializer = getDefaultSerializer(cl);
182
183     if (_cachedSerializerMap == null)
184       _cachedSerializerMap = new HashMap(8);
185
186     _cachedSerializerMap.put(cl, serializer);
187
188     return serializer;
189   }
190
191   /**
192    * Returns the default serializer for a class that isn't matched
193    * directly. Application can override this method to produce
194    * bean-style serialization instead of field serialization.
195    *
196    * @param cl the class of the object that needs to be serialized.
197    *
198    * @return a serializer object for the serialization.
199    */

200   protected Serializer getDefaultSerializer(Class JavaDoc cl)
201   {
202     if (_defaultSerializer != null)
203       return _defaultSerializer;
204
205     return new JavaSerializer(cl);
206   }
207   
208   /**
209    * Returns the deserializer for a class.
210    *
211    * @param cl the class of the object that needs to be deserialized.
212    *
213    * @return a deserializer object for the serialization.
214    */

215   public Deserializer getDeserializer(Class JavaDoc cl)
216     throws HessianProtocolException
217   {
218     Deserializer deserializer;
219
220     deserializer = (Deserializer) _deserializerMap.get(cl);
221     if (deserializer != null)
222       return deserializer;
223
224     if (_cachedDeserializerMap != null) {
225       deserializer = (Deserializer) _cachedDeserializerMap.get(cl);
226       
227       if (deserializer != null)
228     return deserializer;
229     }
230
231
232     for (int i = 0;
233      deserializer == null && _factories != null && i < _factories.size();
234      i++) {
235       AbstractSerializerFactory factory;
236       factory = (AbstractSerializerFactory) _factories.get(i);
237
238       deserializer = factory.getDeserializer(cl);
239     }
240
241     if (deserializer != null) {
242     }
243
244     else if (Collection.class.isAssignableFrom(cl))
245       deserializer = new CollectionDeserializer(cl);
246
247     else if (Map.class.isAssignableFrom(cl))
248       deserializer = new MapDeserializer(cl);
249     
250     else if (cl.isArray())
251       deserializer = new ArrayDeserializer(getDeserializer(cl.getComponentType()));
252
253     else if (Enumeration.class.isAssignableFrom(cl))
254       deserializer = EnumerationDeserializer.create();
255
256     else if (_enumClass != null && _enumClass.isAssignableFrom(cl))
257       deserializer = new EnumDeserializer(cl);
258     
259     else
260       deserializer = getDefaultDeserializer(cl);
261
262     if (_cachedDeserializerMap == null)
263       _cachedDeserializerMap = new HashMap(8);
264
265     _cachedDeserializerMap.put(cl, deserializer);
266
267     return deserializer;
268   }
269
270   /**
271    * Returns the default serializer for a class that isn't matched
272    * directly. Application can override this method to produce
273    * bean-style serialization instead of field serialization.
274    *
275    * @param cl the class of the object that needs to be serialized.
276    *
277    * @return a serializer object for the serialization.
278    */

279   protected Deserializer getDefaultDeserializer(Class JavaDoc cl)
280   {
281     return new JavaDeserializer(cl);
282   }
283
284   /**
285    * Reads the object as a list.
286    */

287   public Object JavaDoc readList(AbstractHessianInput in, int length, String JavaDoc type)
288     throws HessianProtocolException, IOException JavaDoc
289   {
290     Deserializer deserializer = getDeserializer(type);
291
292     if (deserializer != null)
293       return deserializer.readList(in, length);
294     else
295       return new CollectionDeserializer(ArrayList.class).readList(in, length);
296   }
297   
298   /**
299    * Reads the object as a map.
300    */

301   public Object JavaDoc readMap(AbstractHessianInput in, String JavaDoc type)
302     throws HessianProtocolException, IOException JavaDoc
303   {
304     Deserializer deserializer = getDeserializer(type);
305
306     if (deserializer != null)
307       return deserializer.readMap(in);
308     else if (_hashMapDeserializer != null)
309       return _hashMapDeserializer.readMap(in);
310     else {
311       _hashMapDeserializer = new MapDeserializer(HashMap.class);
312       
313       return _hashMapDeserializer.readMap(in);
314     }
315   }
316   
317   /**
318    * Reads the object as a map.
319    */

320   public Object JavaDoc readObject(AbstractHessianInput in,
321                String JavaDoc type,
322                String JavaDoc []fieldNames)
323     throws HessianProtocolException, IOException JavaDoc
324   {
325     Deserializer deserializer = getDeserializer(type);
326
327     if (deserializer != null)
328       return deserializer.readObject(in, fieldNames);
329     else if (_hashMapDeserializer != null)
330       return _hashMapDeserializer.readObject(in, fieldNames);
331     else {
332       _hashMapDeserializer = new MapDeserializer(HashMap.class);
333       
334       return _hashMapDeserializer.readObject(in, fieldNames);
335     }
336   }
337
338   /**
339    * Reads the object as a map.
340    */

341   public Deserializer getObjectDeserializer(String JavaDoc type)
342     throws HessianProtocolException
343   {
344     Deserializer deserializer = getDeserializer(type);
345
346     if (deserializer != null)
347       return deserializer;
348     else if (_hashMapDeserializer != null)
349       return _hashMapDeserializer;
350     else {
351       _hashMapDeserializer = new MapDeserializer(HashMap.class);
352       
353       return _hashMapDeserializer;
354     }
355   }
356
357   /**
358    * Returns a deserializer based on a string type.
359    */

360   public Deserializer getDeserializer(String JavaDoc type)
361     throws HessianProtocolException
362   {
363     if (type == null || type.equals(""))
364       return null;
365
366     Deserializer deserializer;
367     
368     if (_cachedTypeDeserializerMap != null) {
369       deserializer = (Deserializer) _cachedTypeDeserializerMap.get(type);
370
371       if (deserializer != null)
372     return deserializer;
373     }
374
375
376     deserializer = (Deserializer) _typeMap.get(type);
377     if (deserializer != null)
378       return deserializer;
379
380     if (type.startsWith("[")) {
381       Deserializer subDeserializer = getDeserializer(type.substring(1));
382       deserializer = new ArrayDeserializer(subDeserializer);
383     }
384     else {
385       try {
386     ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
387       
388     Class JavaDoc cl = Class.forName(type, false, loader);
389
390     deserializer = getDeserializer(cl);
391       } catch (Exception JavaDoc e) {
392     log.log(Level.FINER, e.toString(), e);
393       }
394     }
395
396     if (deserializer != null) {
397       if (_cachedTypeDeserializerMap == null)
398     _cachedTypeDeserializerMap = new HashMap(8);
399
400       _cachedTypeDeserializerMap.put(type, deserializer);
401     }
402
403     return deserializer;
404   }
405
406   private static void addBasic(Class JavaDoc cl, String JavaDoc typeName, int type)
407   {
408     _serializerMap.put(cl, new BasicSerializer(type));
409
410     Deserializer deserializer = new BasicDeserializer(type);
411     _deserializerMap.put(cl, deserializer);
412     _typeMap.put(typeName, deserializer);
413   }
414
415   static {
416     _serializerMap = new HashMap();
417     _deserializerMap = new HashMap();
418     _typeMap = new HashMap();
419
420     addBasic(void.class, "void", BasicSerializer.NULL);
421     
422     addBasic(Boolean JavaDoc.class, "boolean", BasicSerializer.BOOLEAN);
423     addBasic(Byte JavaDoc.class, "byte", BasicSerializer.BYTE);
424     addBasic(Short JavaDoc.class, "short", BasicSerializer.SHORT);
425     addBasic(Integer JavaDoc.class, "int", BasicSerializer.INTEGER);
426     addBasic(Long JavaDoc.class, "long", BasicSerializer.LONG);
427     addBasic(Float JavaDoc.class, "float", BasicSerializer.FLOAT);
428     addBasic(Double JavaDoc.class, "double", BasicSerializer.DOUBLE);
429     addBasic(Character JavaDoc.class, "char", BasicSerializer.CHARACTER);
430     addBasic(String JavaDoc.class, "string", BasicSerializer.STRING);
431     addBasic(Object JavaDoc.class, "object", BasicSerializer.OBJECT);
432     addBasic(java.util.Date JavaDoc.class, "date", BasicSerializer.DATE);
433
434     addBasic(boolean.class, "boolean", BasicSerializer.BOOLEAN);
435     addBasic(byte.class, "byte", BasicSerializer.BYTE);
436     addBasic(short.class, "short", BasicSerializer.SHORT);
437     addBasic(int.class, "int", BasicSerializer.INTEGER);
438     addBasic(long.class, "long", BasicSerializer.LONG);
439     addBasic(float.class, "float", BasicSerializer.FLOAT);
440     addBasic(double.class, "double", BasicSerializer.DOUBLE);
441     addBasic(char.class, "char", BasicSerializer.CHARACTER);
442     
443     addBasic(boolean[].class, "[boolean", BasicSerializer.BOOLEAN_ARRAY);
444     addBasic(byte[].class, "[byte", BasicSerializer.BYTE_ARRAY);
445     addBasic(short[].class, "[short", BasicSerializer.SHORT_ARRAY);
446     addBasic(int[].class, "[int", BasicSerializer.INTEGER_ARRAY);
447     addBasic(long[].class, "[long", BasicSerializer.LONG_ARRAY);
448     addBasic(float[].class, "[float", BasicSerializer.FLOAT_ARRAY);
449     addBasic(double[].class, "[double", BasicSerializer.DOUBLE_ARRAY);
450     addBasic(char[].class, "[char", BasicSerializer.CHARACTER_ARRAY);
451     addBasic(String JavaDoc[].class, "[string", BasicSerializer.STRING_ARRAY);
452     addBasic(Object JavaDoc[].class, "[object", BasicSerializer.OBJECT_ARRAY);
453
454     _serializerMap.put(Class JavaDoc.class, new ClassSerializer());
455     _deserializerMap.put(Class JavaDoc.class, new ClassDeserializer());
456
457     _deserializerMap.put(Number JavaDoc.class, new BasicDeserializer(BasicSerializer.NUMBER));
458     
459     _serializerMap.put(BigDecimal JavaDoc.class, new StringValueSerializer());
460     try {
461       _deserializerMap.put(BigDecimal JavaDoc.class,
462                new StringValueDeserializer(BigDecimal JavaDoc.class));
463     } catch (Throwable JavaDoc e) {
464     }
465     
466     _serializerMap.put(File JavaDoc.class, new StringValueSerializer());
467     try {
468       _deserializerMap.put(File JavaDoc.class,
469                new StringValueDeserializer(File JavaDoc.class));
470     } catch (Throwable JavaDoc e) {
471     }
472     
473     _serializerMap.put(java.sql.Date JavaDoc.class, new SqlDateSerializer());
474     _serializerMap.put(java.sql.Time JavaDoc.class, new SqlDateSerializer());
475     _serializerMap.put(java.sql.Timestamp JavaDoc.class, new SqlDateSerializer());
476
477     try {
478       _deserializerMap.put(java.sql.Date JavaDoc.class,
479               new SqlDateDeserializer(java.sql.Date JavaDoc.class));
480       _deserializerMap.put(java.sql.Time JavaDoc.class,
481               new SqlDateDeserializer(java.sql.Time JavaDoc.class));
482       _deserializerMap.put(java.sql.Timestamp JavaDoc.class,
483               new SqlDateDeserializer(java.sql.Timestamp JavaDoc.class));
484     } catch (Throwable JavaDoc e) {
485       e.printStackTrace();
486     }
487
488     try {
489       Class JavaDoc stackTrace = Class.forName("java.lang.StackTraceElement");
490       
491       _deserializerMap.put(stackTrace, new StackTraceElementDeserializer());
492     } catch (Throwable JavaDoc e) {
493     }
494
495     try {
496       _enumClass = Class.forName("java.lang.Enum");
497     } catch (Throwable JavaDoc e) {
498     }
499   }
500 }
501
Popular Tags