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.lang.ref.WeakReference;
18: import java.util.Collections;
19: import java.util.List;
20: import java.util.Map;
21: import java.util.WeakHashMap;
22:
23: /**
24: * Uses XStream with a CachingMapper caching the aliasForAttribute method.
25: *
26: * @author Jörg Schaible
27: */
28: public class DefaultImplementationCache extends XStreamCache {
29:
30: protected List getMappers(JVM jvm) {
31: List list = super .getMappers(jvm);
32: list.add(CachingMapper.class);
33: return list;
34: }
35:
36: public String toString() {
37: return "Default Implementation Cache";
38: }
39:
40: public static class CachingMapper extends MapperWrapper {
41:
42: private transient Map defaultImplementationCache;
43:
44: public CachingMapper(Mapper wrapped) {
45: super (wrapped);
46: readResolve();
47: }
48:
49: public Class defaultImplementationOf(Class type) {
50: WeakReference reference = (WeakReference) defaultImplementationCache
51: .get(type);
52: if (reference != null) {
53: Class cached = (Class) reference.get();
54: if (cached != null) {
55: return cached;
56: }
57: }
58:
59: Class result = super .defaultImplementationOf(type);
60: defaultImplementationCache.put(type, new WeakReference(
61: result));
62: return result;
63: }
64:
65: private Object readResolve() {
66: defaultImplementationCache = Collections
67: .synchronizedMap(new WeakHashMap(128));
68: return this;
69: }
70:
71: }
72: }
|