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 org.springframework.batch.core.repository.ExecutionContextSerializer; |
19 | import org.springframework.core.serializer.DefaultDeserializer; |
20 | import org.springframework.core.serializer.DefaultSerializer; |
21 | import org.springframework.core.serializer.Deserializer; |
22 | import org.springframework.core.serializer.Serializer; |
23 | import org.springframework.util.Assert; |
24 | |
25 | import java.io.IOException; |
26 | import java.io.InputStream; |
27 | import java.io.OutputStream; |
28 | import java.io.Serializable; |
29 | import java.util.Map; |
30 | |
31 | /** |
32 | * An implementation of the {@link ExecutionContextSerializer} using the default |
33 | * serialization implementations from Spring ({@link DefaultSerializer} and |
34 | * {@link DefaultDeserializer}). |
35 | * |
36 | * @author Michael Minella |
37 | * @since 2.2 |
38 | */ |
39 | @SuppressWarnings("rawtypes") |
40 | public class DefaultExecutionContextSerializer implements ExecutionContextSerializer { |
41 | |
42 | private Serializer serializer = new DefaultSerializer(); |
43 | private Deserializer deserializer = new DefaultDeserializer(); |
44 | |
45 | /** |
46 | * Serializes an execution context to the provided {@link OutputStream}. The |
47 | * stream is not closed prior to it's return. |
48 | * |
49 | * @param context |
50 | * @param out |
51 | */ |
52 | @Override |
53 | @SuppressWarnings("unchecked") |
54 | public void serialize(Object context, OutputStream out) throws IOException { |
55 | Assert.notNull(context); |
56 | Assert.notNull(out); |
57 | for(Object value : ((Map)context).values()) { |
58 | Assert.isInstanceOf(Serializable.class, value, "Value: [ " + value + "must be serializable."); |
59 | } |
60 | |
61 | serializer.serialize(context, out); |
62 | } |
63 | |
64 | /** |
65 | * Deserializes an execution context from the provided {@link InputStream}. |
66 | * |
67 | * @param inputStream |
68 | * @return the object serialized in the provided {@link InputStream} |
69 | */ |
70 | @Override |
71 | public Object deserialize(InputStream inputStream) throws IOException { |
72 | return deserializer.deserialize(inputStream); |
73 | } |
74 | |
75 | } |