001/*
002 * Copyright 2006-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.batch.core.repository.dao;
018
019import java.io.BufferedReader;
020import java.io.IOException;
021import java.io.InputStream;
022import java.io.InputStreamReader;
023import java.io.OutputStream;
024import java.util.Map;
025
026import com.thoughtworks.xstream.XStream;
027import com.thoughtworks.xstream.converters.reflection.ReflectionProvider;
028import com.thoughtworks.xstream.io.HierarchicalStreamDriver;
029import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
030
031import org.springframework.batch.core.repository.ExecutionContextSerializer;
032import org.springframework.beans.factory.InitializingBean;
033import org.springframework.core.serializer.Deserializer;
034import org.springframework.core.serializer.Serializer;
035import org.springframework.util.Assert;
036
037/**
038 * Implementation that uses XStream and Jettison to provide serialization.
039 *
040 * @author Thomas Risberg
041 * @author Michael Minella
042 * @author Mahmoud Ben Hassine
043 * @since 2.0
044 * @see ExecutionContextSerializer
045 * @deprecated Due to the incompatibilities between current Jettison versions and XStream
046 *              versions, this serializer is deprecated in favor of
047 *              {@link Jackson2ExecutionContextStringSerializer}
048 */
049@Deprecated
050public class XStreamExecutionContextStringSerializer implements ExecutionContextSerializer, InitializingBean {
051
052        private ReflectionProvider reflectionProvider = null;
053
054        private HierarchicalStreamDriver hierarchicalStreamDriver;
055
056        private XStream xstream;
057
058        public void setReflectionProvider(ReflectionProvider reflectionProvider) {
059                this.reflectionProvider = reflectionProvider;
060        }
061
062        public void setHierarchicalStreamDriver(HierarchicalStreamDriver hierarchicalStreamDriver) {
063                this.hierarchicalStreamDriver = hierarchicalStreamDriver;
064        }
065
066        @Override
067        public void afterPropertiesSet() throws Exception {
068                init();
069        }
070
071        public synchronized void init() throws Exception {
072                if (hierarchicalStreamDriver == null) {
073                        this.hierarchicalStreamDriver = new JettisonMappedXmlDriver();
074                }
075                if (reflectionProvider == null) {
076                        xstream =  new XStream(hierarchicalStreamDriver);
077                }
078                else {
079                        xstream = new XStream(reflectionProvider, hierarchicalStreamDriver);
080                }
081        }
082
083        /**
084         * Serializes the passed execution context to the supplied OutputStream.
085         *
086         * @param context {@link Map} containing the context information.
087         * @param out {@link OutputStream} where the serialized context information
088         * will be written.
089         *
090         * @see Serializer#serialize(Object, OutputStream)
091         */
092        @Override
093        public void serialize(Map<String, Object> context, OutputStream out) throws IOException {
094                Assert.notNull(context, "context is required");
095                Assert.notNull(out, "An OutputStream is required");
096
097                out.write(xstream.toXML(context).getBytes());
098        }
099
100        /**
101         * Deserializes the supplied input stream into a new execution context.
102         *
103         * @param in {@link InputStream} containing the information to be deserialized.
104
105         * @return a reconstructed execution context
106         * @see Deserializer#deserialize(InputStream)
107         */
108        @SuppressWarnings("unchecked")
109        @Override
110        public Map<String, Object> deserialize(InputStream in) throws IOException {
111                BufferedReader br = new BufferedReader(new InputStreamReader(in));
112
113                StringBuilder sb = new StringBuilder();
114
115                String line;
116                while ((line = br.readLine()) != null) {
117                        sb.append(line);
118                }
119
120                return (Map<String, Object>) xstream.fromXML(sb.toString());
121        }
122}