| 1 | /* |
| 2 | * Copyright 2006-2007 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.repeat.context; |
| 18 | |
| 19 | import java.util.ArrayList; |
| 20 | import java.util.HashMap; |
| 21 | import java.util.HashSet; |
| 22 | import java.util.List; |
| 23 | import java.util.Map; |
| 24 | import java.util.Set; |
| 25 | |
| 26 | import org.springframework.batch.repeat.RepeatContext; |
| 27 | |
| 28 | public class RepeatContextSupport extends SynchronizedAttributeAccessor implements RepeatContext { |
| 29 | |
| 30 | private RepeatContext parent; |
| 31 | |
| 32 | private int count; |
| 33 | |
| 34 | private volatile boolean completeOnly; |
| 35 | |
| 36 | private volatile boolean terminateOnly; |
| 37 | |
| 38 | private Map<String, Set<Runnable>> callbacks = new HashMap<String, Set<Runnable>>(); |
| 39 | |
| 40 | /** |
| 41 | * Constructor for {@link RepeatContextSupport}. The parent can be null, but |
| 42 | * should be set to the enclosing repeat context if there is one, e.g. if |
| 43 | * this context is an inner loop. |
| 44 | * @param parent |
| 45 | */ |
| 46 | public RepeatContextSupport(RepeatContext parent) { |
| 47 | super(); |
| 48 | this.parent = parent; |
| 49 | } |
| 50 | |
| 51 | /* |
| 52 | * (non-Javadoc) |
| 53 | * |
| 54 | * @see org.springframework.batch.repeat.RepeatContext#isCompleteOnly() |
| 55 | */ |
| 56 | public boolean isCompleteOnly() { |
| 57 | return completeOnly; |
| 58 | } |
| 59 | |
| 60 | /* |
| 61 | * (non-Javadoc) |
| 62 | * |
| 63 | * @see org.springframework.batch.repeat.RepeatContext#setCompleteOnly() |
| 64 | */ |
| 65 | public void setCompleteOnly() { |
| 66 | completeOnly = true; |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * (non-Javadoc) |
| 71 | * |
| 72 | * @see org.springframework.batch.repeat.RepeatContext#isTerminateOnly() |
| 73 | */ |
| 74 | public boolean isTerminateOnly() { |
| 75 | return terminateOnly; |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * (non-Javadoc) |
| 80 | * |
| 81 | * @see org.springframework.batch.repeat.RepeatContext#setTerminateOnly() |
| 82 | */ |
| 83 | public void setTerminateOnly() { |
| 84 | terminateOnly = true; |
| 85 | setCompleteOnly(); |
| 86 | } |
| 87 | |
| 88 | /* |
| 89 | * (non-Javadoc) |
| 90 | * |
| 91 | * @see org.springframework.batch.repeat.RepeatContext#getParent() |
| 92 | */ |
| 93 | public RepeatContext getParent() { |
| 94 | return parent; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Used by clients to increment the started count. |
| 99 | */ |
| 100 | public synchronized void increment() { |
| 101 | count++; |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * (non-Javadoc) |
| 106 | * |
| 107 | * @see org.springframework.batch.repeat.RepeatContext#getStartedCount() |
| 108 | */ |
| 109 | public synchronized int getStartedCount() { |
| 110 | return count; |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | * (non-Javadoc) |
| 115 | * |
| 116 | * @see |
| 117 | * org.springframework.batch.repeat.RepeatContext#registerDestructionCallback |
| 118 | * (java.lang.String, java.lang.Runnable) |
| 119 | */ |
| 120 | public void registerDestructionCallback(String name, Runnable callback) { |
| 121 | synchronized (callbacks) { |
| 122 | Set<Runnable> set = callbacks.get(name); |
| 123 | if (set == null) { |
| 124 | set = new HashSet<Runnable>(); |
| 125 | callbacks.put(name, set); |
| 126 | } |
| 127 | set.add(callback); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * (non-Javadoc) |
| 133 | * |
| 134 | * @see org.springframework.batch.repeat.RepeatContext#close() |
| 135 | */ |
| 136 | public void close() { |
| 137 | |
| 138 | List<RuntimeException> errors = new ArrayList<RuntimeException>(); |
| 139 | |
| 140 | Set<Map.Entry<String, Set<Runnable>>> copy; |
| 141 | |
| 142 | synchronized (callbacks) { |
| 143 | copy = new HashSet<Map.Entry<String, Set<Runnable>>>(callbacks.entrySet()); |
| 144 | } |
| 145 | |
| 146 | for (Map.Entry<String, Set<Runnable>> entry : copy) { |
| 147 | |
| 148 | for (Runnable callback : entry.getValue()) { |
| 149 | /* |
| 150 | * Potentially we could check here if there is an attribute with |
| 151 | * the given name - if it has been removed, maybe the callback |
| 152 | * is invalid. On the other hand it is less surprising for the |
| 153 | * callback register if it is always executed. |
| 154 | */ |
| 155 | if (callback != null) { |
| 156 | /* |
| 157 | * The documentation of the interface says that these |
| 158 | * callbacks must not throw exceptions, but we don't trust |
| 159 | * them necessarily... |
| 160 | */ |
| 161 | try { |
| 162 | callback.run(); |
| 163 | } |
| 164 | catch (RuntimeException t) { |
| 165 | errors.add(t); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | if (errors.isEmpty()) { |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | throw (RuntimeException) errors.get(0); |
| 176 | } |
| 177 | |
| 178 | } |