1 /*
2 * Copyright 2012-2013 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.springframework.batch.core.repository.dao;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.OutputStream;
21
22 import org.springframework.batch.core.repository.ExecutionContextSerializer;
23 import org.springframework.core.serializer.DefaultDeserializer;
24 import org.springframework.core.serializer.DefaultSerializer;
25 import org.springframework.core.serializer.Deserializer;
26 import org.springframework.core.serializer.Serializer;
27 import org.springframework.util.Assert;
28
29 /**
30 * An implementation of the {@link ExecutionContextSerializer} using the default
31 * serialization implementations from Spring ({@link DefaultSerializer} and
32 * {@link DefaultDeserializer}).
33 *
34 * @author Michael Minella
35 * @since 2.2
36 */
37 @SuppressWarnings("rawtypes")
38 public class DefaultExecutionContextSerializer implements ExecutionContextSerializer {
39
40 private Serializer serializer = new DefaultSerializer();
41 private Deserializer deserializer = new DefaultDeserializer();
42
43 /**
44 * Serializes an execution context to the provided {@link OutputStream}. The
45 * stream is not closed prior to it's return.
46 *
47 * @param context
48 * @param out
49 */
50 @Override
51 @SuppressWarnings("unchecked")
52 public void serialize(Object context, OutputStream out) throws IOException {
53 Assert.notNull(context);
54 Assert.notNull(out);
55
56 serializer.serialize(context, out);
57 }
58
59 /**
60 * Deserializes an execution context from the provided {@link InputStream}.
61 *
62 * @param inputStream
63 * @return the object serialized in the provided {@link InputStream}
64 */
65 @Override
66 public Object deserialize(InputStream inputStream) throws IOException {
67 return deserializer.deserialize(inputStream);
68 }
69
70 }