001/* 002 * Copyright 2012-2017 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 */ 016package org.springframework.batch.core.repository.dao; 017 018import java.io.IOException; 019import java.io.InputStream; 020import java.io.OutputStream; 021import java.io.Serializable; 022import java.util.Map; 023 024import org.springframework.batch.core.repository.ExecutionContextSerializer; 025import org.springframework.core.serializer.DefaultDeserializer; 026import org.springframework.core.serializer.DefaultSerializer; 027import org.springframework.core.serializer.Deserializer; 028import org.springframework.core.serializer.Serializer; 029import org.springframework.util.Assert; 030 031/** 032 * An implementation of the {@link ExecutionContextSerializer} using the default 033 * serialization implementations from Spring ({@link DefaultSerializer} and 034 * {@link DefaultDeserializer}). 035 * 036 * @author Michael Minella 037 * @since 2.2 038 */ 039@SuppressWarnings("rawtypes") 040public class DefaultExecutionContextSerializer implements ExecutionContextSerializer { 041 042 private Serializer serializer = new DefaultSerializer(); 043 private Deserializer deserializer = new DefaultDeserializer(); 044 045 /** 046 * Serializes an execution context to the provided {@link OutputStream}. The 047 * stream is not closed prior to it's return. 048 * 049 * @param context {@link Map} contents of the {@code ExecutionContext}. 050 * @param out {@link OutputStream} where the serialized context information 051 * will be written. 052 */ 053 @Override 054 @SuppressWarnings("unchecked") 055 public void serialize(Map<String, Object> context, OutputStream out) throws IOException { 056 Assert.notNull(context, "context is required"); 057 Assert.notNull(out, "OutputStream is required"); 058 059 for(Object value : context.values()) { 060 Assert.notNull(value, "A null value was found"); 061 if (!(value instanceof Serializable)) { 062 throw new IllegalArgumentException( 063 "Value: [ " + value + "must be serializable." 064 + "Object of class [" + value.getClass().getName() 065 + "] must be an instance of " + Serializable.class); 066 } 067 } 068 serializer.serialize(context, out); 069 } 070 071 /** 072 * Deserializes an execution context from the provided {@link InputStream}. 073 * 074 * @param inputStream {@link InputStream} containing the information to be deserialized. 075 * @return the object serialized in the provided {@link InputStream} 076 */ 077 @SuppressWarnings("unchecked") 078 @Override 079 public Map<String, Object> deserialize(InputStream inputStream) throws IOException { 080 return (Map<String, Object>) deserializer.deserialize(inputStream); 081 } 082 083}