01: /*
02: * Copyright (C) 2008 XStream Committers.
03: * All rights reserved.
04: *
05: * The software in this package is published under the terms of the BSD
06: * style license a copy of which has been included with this distribution in
07: * the LICENSE.txt file.
08: *
09: * Created on 04. January 2008 by Joerg Schaible
10: */
11: package com.thoughtworks.xstream.benchmark.cache.products;
12:
13: import com.thoughtworks.xstream.core.JVM;
14: import com.thoughtworks.xstream.mapper.Mapper;
15: import com.thoughtworks.xstream.mapper.MapperWrapper;
16:
17: import java.util.Collections;
18: import java.util.List;
19: import java.util.Map;
20: import java.util.WeakHashMap;
21:
22: /**
23: * Uses XStream with a CachingMapper caching the aliasForAttribute method.
24: *
25: * @author Jörg Schaible
26: */
27: public class SerializedClassCache extends XStreamCache {
28:
29: protected List getMappers(JVM jvm) {
30: List list = super .getMappers(jvm);
31: list.add(CachingMapper.class);
32: return list;
33: }
34:
35: public String toString() {
36: return "Serialized Class Cache";
37: }
38:
39: public static class CachingMapper extends MapperWrapper {
40:
41: private transient Map serializedClassCache;
42:
43: public CachingMapper(Mapper wrapped) {
44: super (wrapped);
45: readResolve();
46: }
47:
48: public String serializedClass(Class type) {
49: String alias = (String) serializedClassCache.get(type);
50: if (alias != null) {
51: return alias;
52: }
53:
54: String result = super .serializedClass(type);
55: serializedClassCache.put(type, alias);
56: return result;
57: }
58:
59: private Object readResolve() {
60: serializedClassCache = Collections
61: .synchronizedMap(new WeakHashMap(128));
62: return this;
63: }
64:
65: }
66: }
|