1 48 49 package com.caucho.hessian.io; 50 51 import com.caucho.burlap.io.BurlapRemoteObject; 52 53 import java.io.File ; 54 import java.io.IOException ; 55 import java.io.InputStream ; 56 import java.math.BigDecimal ; 57 import java.util.*; 58 import java.util.logging.Level ; 59 import java.util.logging.Logger ; 60 61 64 public class SerializerFactory extends AbstractSerializerFactory 65 { 66 private static final Logger log 67 = Logger.getLogger(SerializerFactory.class.getName()); 68 69 private static Class _enumClass; 70 71 private static HashMap _serializerMap; 72 private static HashMap _deserializerMap; 73 private static HashMap _typeMap; 74 75 protected Serializer _defaultSerializer; 76 77 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 90 public void setSendCollectionType(boolean isSendType) 91 { 92 if (_collectionSerializer == null) 93 _collectionSerializer = new CollectionSerializer(); 94 95 _collectionSerializer.setSendJavaType(isSendType); 96 } 97 98 101 public void addFactory(AbstractSerializerFactory factory) 102 { 103 _factories.add(factory); 104 } 105 106 113 public Serializer getSerializer(Class 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 .class.isAssignableFrom(cl)) 163 serializer = new ThrowableSerializer(cl); 164 165 else if (InputStream .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 200 protected Serializer getDefaultSerializer(Class cl) 201 { 202 if (_defaultSerializer != null) 203 return _defaultSerializer; 204 205 return new JavaSerializer(cl); 206 } 207 208 215 public Deserializer getDeserializer(Class 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 279 protected Deserializer getDefaultDeserializer(Class cl) 280 { 281 return new JavaDeserializer(cl); 282 } 283 284 287 public Object readList(AbstractHessianInput in, int length, String type) 288 throws HessianProtocolException, IOException 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 301 public Object readMap(AbstractHessianInput in, String type) 302 throws HessianProtocolException, IOException 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 320 public Object readObject(AbstractHessianInput in, 321 String type, 322 String []fieldNames) 323 throws HessianProtocolException, IOException 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 341 public Deserializer getObjectDeserializer(String 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 360 public Deserializer getDeserializer(String 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 loader = Thread.currentThread().getContextClassLoader(); 387 388 Class cl = Class.forName(type, false, loader); 389 390 deserializer = getDeserializer(cl); 391 } catch (Exception 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 cl, String 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 .class, "boolean", BasicSerializer.BOOLEAN); 423 addBasic(Byte .class, "byte", BasicSerializer.BYTE); 424 addBasic(Short .class, "short", BasicSerializer.SHORT); 425 addBasic(Integer .class, "int", BasicSerializer.INTEGER); 426 addBasic(Long .class, "long", BasicSerializer.LONG); 427 addBasic(Float .class, "float", BasicSerializer.FLOAT); 428 addBasic(Double .class, "double", BasicSerializer.DOUBLE); 429 addBasic(Character .class, "char", BasicSerializer.CHARACTER); 430 addBasic(String .class, "string", BasicSerializer.STRING); 431 addBasic(Object .class, "object", BasicSerializer.OBJECT); 432 addBasic(java.util.Date .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 [].class, "[string", BasicSerializer.STRING_ARRAY); 452 addBasic(Object [].class, "[object", BasicSerializer.OBJECT_ARRAY); 453 454 _serializerMap.put(Class .class, new ClassSerializer()); 455 _deserializerMap.put(Class .class, new ClassDeserializer()); 456 457 _deserializerMap.put(Number .class, new BasicDeserializer(BasicSerializer.NUMBER)); 458 459 _serializerMap.put(BigDecimal .class, new StringValueSerializer()); 460 try { 461 _deserializerMap.put(BigDecimal .class, 462 new StringValueDeserializer(BigDecimal .class)); 463 } catch (Throwable e) { 464 } 465 466 _serializerMap.put(File .class, new StringValueSerializer()); 467 try { 468 _deserializerMap.put(File .class, 469 new StringValueDeserializer(File .class)); 470 } catch (Throwable e) { 471 } 472 473 _serializerMap.put(java.sql.Date .class, new SqlDateSerializer()); 474 _serializerMap.put(java.sql.Time .class, new SqlDateSerializer()); 475 _serializerMap.put(java.sql.Timestamp .class, new SqlDateSerializer()); 476 477 try { 478 _deserializerMap.put(java.sql.Date .class, 479 new SqlDateDeserializer(java.sql.Date .class)); 480 _deserializerMap.put(java.sql.Time .class, 481 new SqlDateDeserializer(java.sql.Time .class)); 482 _deserializerMap.put(java.sql.Timestamp .class, 483 new SqlDateDeserializer(java.sql.Timestamp .class)); 484 } catch (Throwable e) { 485 e.printStackTrace(); 486 } 487 488 try { 489 Class stackTrace = Class.forName("java.lang.StackTraceElement"); 490 491 _deserializerMap.put(stackTrace, new StackTraceElementDeserializer()); 492 } catch (Throwable e) { 493 } 494 495 try { 496 _enumClass = Class.forName("java.lang.Enum"); 497 } catch (Throwable e) { 498 } 499 } 500 } 501 | Popular Tags |