001: /*
002: * Copyright (C) 2008 XStream Committers.
003: * All rights reserved.
004: *
005: * The software in this package is published under the terms of the BSD
006: * style license a copy of which has been included with this distribution in
007: * the LICENSE.txt file.
008: *
009: * Created on 01. January 2008 by Joerg Schaible
010: */
011: package com.thoughtworks.xstream.benchmark.cache.products;
012:
013: import com.thoughtworks.xstream.InitializationException;
014: import com.thoughtworks.xstream.XStream;
015: import com.thoughtworks.xstream.benchmark.cache.model.Five;
016: import com.thoughtworks.xstream.benchmark.cache.model.One;
017: import com.thoughtworks.xstream.benchmark.cache.model.SerializableFive;
018: import com.thoughtworks.xstream.benchmark.cache.model.SerializableOne;
019: import com.thoughtworks.xstream.converters.ConverterLookup;
020: import com.thoughtworks.xstream.core.DefaultConverterLookup;
021: import com.thoughtworks.xstream.core.JVM;
022: import com.thoughtworks.xstream.core.util.ClassLoaderReference;
023: import com.thoughtworks.xstream.core.util.CompositeClassLoader;
024: import com.thoughtworks.xstream.core.util.DependencyInjectionFactory;
025: import com.thoughtworks.xstream.core.util.TypedNull;
026: import com.thoughtworks.xstream.io.xml.XppDriver;
027: import com.thoughtworks.xstream.mapper.ArrayMapper;
028: import com.thoughtworks.xstream.mapper.AttributeAliasingMapper;
029: import com.thoughtworks.xstream.mapper.AttributeMapper;
030: import com.thoughtworks.xstream.mapper.ClassAliasingMapper;
031: import com.thoughtworks.xstream.mapper.DefaultImplementationsMapper;
032: import com.thoughtworks.xstream.mapper.DefaultMapper;
033: import com.thoughtworks.xstream.mapper.DynamicProxyMapper;
034: import com.thoughtworks.xstream.mapper.FieldAliasingMapper;
035: import com.thoughtworks.xstream.mapper.ImmutableTypesMapper;
036: import com.thoughtworks.xstream.mapper.ImplicitCollectionMapper;
037: import com.thoughtworks.xstream.mapper.LocalConversionMapper;
038: import com.thoughtworks.xstream.mapper.Mapper;
039: import com.thoughtworks.xstream.mapper.OuterClassMapper;
040: import com.thoughtworks.xstream.tools.benchmark.Product;
041:
042: import java.io.InputStream;
043: import java.io.OutputStream;
044: import java.util.ArrayList;
045: import java.util.Iterator;
046: import java.util.List;
047:
048: /**
049: * Uses XStream with the XPP driver.
050: *
051: * @author Jörg Schaible
052: */
053: public abstract class XStreamCache implements Product {
054:
055: private final XStream xstream;
056:
057: public XStreamCache() {
058: JVM jvm = new JVM();
059: ClassLoaderReference classLoaderReference = new ClassLoaderReference(
060: new CompositeClassLoader());
061: DefaultConverterLookup converterLookup = new DefaultConverterLookup();
062: xstream = new XStream(jvm.bestReflectionProvider(),
063: new XppDriver(), classLoaderReference, buildMapper(
064: getMappers(jvm), jvm, classLoaderReference,
065: converterLookup), converterLookup,
066: converterLookup);
067: xstream.alias("one", One.class);
068: xstream.alias("five", Five.class);
069: xstream.alias("ser-one", SerializableOne.class);
070: xstream.alias("ser-five", SerializableFive.class);
071: }
072:
073: public void serialize(Object object, OutputStream output)
074: throws Exception {
075: xstream.toXML(object, output);
076: }
077:
078: public Object deserialize(InputStream input) throws Exception {
079: return xstream.fromXML(input);
080: }
081:
082: protected List getMappers(JVM jvm) {
083: List mappers = new ArrayList();
084: mappers.add(DefaultMapper.class);
085: if (jvm.loadClass("net.sf.cglib.proxy.Enhancer") != null) {
086: mappers
087: .add(jvm
088: .loadClass("com.thoughtworks.xstream.mapper.CGLIBMapper"));
089: }
090: mappers.add(DynamicProxyMapper.class);
091: mappers.add(ClassAliasingMapper.class);
092: mappers.add(FieldAliasingMapper.class);
093: mappers.add(AttributeAliasingMapper.class);
094: mappers.add(ImplicitCollectionMapper.class);
095: mappers.add(OuterClassMapper.class);
096: mappers.add(ArrayMapper.class);
097: mappers.add(LocalConversionMapper.class);
098: mappers.add(DefaultImplementationsMapper.class);
099: if (JVM.is15()) {
100: mappers
101: .add(jvm
102: .loadClass("com.thoughtworks.xstream.mapper.EnumMapper"));
103: } else {
104: mappers.add(AttributeMapper.class);
105: }
106: mappers.add(ImmutableTypesMapper.class);
107: if (JVM.is15()) {
108: mappers
109: .add(jvm
110: .loadClass("com.thoughtworks.xstream.mapper.AnnotationMapper"));
111: }
112: return mappers;
113: }
114:
115: private Mapper buildMapper(List mappers, JVM jvm,
116: ClassLoader classLoader, ConverterLookup converterLookup) {
117: final Object[] arguments = new Object[] {
118: new TypedNull(Mapper.class), converterLookup,
119: classLoader, jvm.bestReflectionProvider(), jvm };
120: for (final Iterator iter = mappers.iterator(); iter.hasNext();) {
121: final Class mapperType = (Class) iter.next();
122: try {
123: arguments[0] = DependencyInjectionFactory.newInstance(
124: mapperType, arguments);
125: } catch (Exception e) {
126: throw new InitializationException(
127: "Could not instantiate mapper : "
128: + mapperType.getName(), e);
129: }
130: }
131: return (Mapper) arguments[0];
132: }
133: }
|