1 | /* |
2 | * Copyright 2006-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 | |
17 | package org.springframework.batch.core.repository.dao; |
18 | |
19 | import java.io.Serializable; |
20 | import java.util.Collection; |
21 | import java.util.concurrent.ConcurrentMap; |
22 | |
23 | import org.springframework.batch.core.JobExecution; |
24 | import org.springframework.batch.core.StepExecution; |
25 | import org.springframework.batch.item.ExecutionContext; |
26 | import org.springframework.batch.support.SerializationUtils; |
27 | import org.springframework.batch.support.transaction.TransactionAwareProxyFactory; |
28 | import org.springframework.util.Assert; |
29 | |
30 | /** |
31 | * In-memory implementation of {@link ExecutionContextDao} backed by maps. |
32 | * |
33 | * @author Robert Kasanicky |
34 | * @author Dave Syer |
35 | * @author David Turanski |
36 | */ |
37 | @SuppressWarnings("serial") |
38 | public class MapExecutionContextDao implements ExecutionContextDao { |
39 | |
40 | private final ConcurrentMap<ContextKey, ExecutionContext> contexts = TransactionAwareProxyFactory |
41 | .createAppendOnlyTransactionalMap(); |
42 | |
43 | private static final class ContextKey implements Comparable<ContextKey>, Serializable { |
44 | |
45 | private static enum Type { STEP, JOB; } |
46 | |
47 | private final Type type; |
48 | private final long id; |
49 | |
50 | private ContextKey(Type type, long id) { |
51 | if(type == null) { |
52 | throw new IllegalStateException("Need a non-null type for a context"); |
53 | } |
54 | this.type = type; |
55 | this.id = id; |
56 | } |
57 | |
58 | @Override |
59 | public int compareTo(ContextKey them) { |
60 | if(them == null) { |
61 | return 1; |
62 | } |
63 | final int idCompare = new Long(this.id).compareTo(new Long(them.id)); // JDK6 Make this Long.compare(x,y) |
64 | if(idCompare != 0) { |
65 | return idCompare; |
66 | } |
67 | final int typeCompare = this.type.compareTo(them.type); |
68 | if(typeCompare != 0) { |
69 | return typeCompare; |
70 | } |
71 | return 0; |
72 | } |
73 | |
74 | @Override |
75 | public boolean equals(Object them) { |
76 | if(them == null) { |
77 | return false; |
78 | } |
79 | if(them instanceof ContextKey) { |
80 | return this.equals((ContextKey)them); |
81 | } |
82 | return false; |
83 | } |
84 | |
85 | public boolean equals(ContextKey them) { |
86 | if(them == null) { |
87 | return false; |
88 | } |
89 | return this.id == them.id && this.type.equals(them.type); |
90 | } |
91 | |
92 | @Override |
93 | public int hashCode() { |
94 | int value = (int)(id^(id>>>32)); |
95 | switch(type) { |
96 | case STEP: return value; |
97 | case JOB: return ~value; |
98 | default: throw new IllegalStateException("Unknown type encountered in switch: " + type); |
99 | } |
100 | } |
101 | |
102 | public static ContextKey step(long id) { return new ContextKey(Type.STEP, id); } |
103 | |
104 | public static ContextKey job(long id) { return new ContextKey(Type.JOB, id); } |
105 | } |
106 | |
107 | public void clear() { |
108 | contexts.clear(); |
109 | } |
110 | |
111 | private static ExecutionContext copy(ExecutionContext original) { |
112 | return (ExecutionContext) SerializationUtils.deserialize(SerializationUtils.serialize(original)); |
113 | } |
114 | |
115 | @Override |
116 | public ExecutionContext getExecutionContext(StepExecution stepExecution) { |
117 | return copy(contexts.get(ContextKey.step(stepExecution.getId()))); |
118 | } |
119 | |
120 | @Override |
121 | public void updateExecutionContext(StepExecution stepExecution) { |
122 | ExecutionContext executionContext = stepExecution.getExecutionContext(); |
123 | if (executionContext != null) { |
124 | contexts.put(ContextKey.step(stepExecution.getId()), copy(executionContext)); |
125 | } |
126 | } |
127 | |
128 | @Override |
129 | public ExecutionContext getExecutionContext(JobExecution jobExecution) { |
130 | return copy(contexts.get(ContextKey.job(jobExecution.getId()))); |
131 | } |
132 | |
133 | @Override |
134 | public void updateExecutionContext(JobExecution jobExecution) { |
135 | ExecutionContext executionContext = jobExecution.getExecutionContext(); |
136 | if (executionContext != null) { |
137 | contexts.put(ContextKey.job(jobExecution.getId()), copy(executionContext)); |
138 | } |
139 | } |
140 | |
141 | @Override |
142 | public void saveExecutionContext(JobExecution jobExecution) { |
143 | updateExecutionContext(jobExecution); |
144 | } |
145 | |
146 | @Override |
147 | public void saveExecutionContext(StepExecution stepExecution) { |
148 | updateExecutionContext(stepExecution); |
149 | } |
150 | |
151 | |
152 | @Override |
153 | public void saveExecutionContexts(Collection<StepExecution> stepExecutions) { |
154 | Assert.notNull(stepExecutions,"Attempt to save a nulk collection of step executions"); |
155 | for (StepExecution stepExecution: stepExecutions) { |
156 | saveExecutionContext(stepExecution); |
157 | saveExecutionContext(stepExecution.getJobExecution()); |
158 | } |
159 | } |
160 | |
161 | } |