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 org.springframework.core.AttributeAccessor; |
20 | import org.springframework.core.AttributeAccessorSupport; |
21 | |
22 | /** |
23 | * An {@link AttributeAccessor} that synchronizes on a mutex (not this) before |
24 | * modifying or accessing the underlying attributes. |
25 | * |
26 | * @author Dave Syer |
27 | * |
28 | */ |
29 | public class SynchronizedAttributeAccessor implements AttributeAccessor { |
30 | |
31 | /** |
32 | * All methods are delegated to this support object. |
33 | */ |
34 | AttributeAccessorSupport support = new AttributeAccessorSupport() { |
35 | /** |
36 | * Generated serial UID. |
37 | */ |
38 | private static final long serialVersionUID = -7664290016506582290L; |
39 | }; |
40 | |
41 | /* |
42 | * (non-Javadoc) |
43 | * @see org.springframework.core.AttributeAccessor#attributeNames() |
44 | */ |
45 | public String[] attributeNames() { |
46 | synchronized (support) { |
47 | return support.attributeNames(); |
48 | } |
49 | } |
50 | |
51 | /* |
52 | * (non-Javadoc) |
53 | * @see java.lang.Object#equals(java.lang.Object) |
54 | */ |
55 | public boolean equals(Object other) { |
56 | if (this == other) { |
57 | return true; |
58 | } |
59 | AttributeAccessorSupport that; |
60 | if (other instanceof SynchronizedAttributeAccessor) { |
61 | that = ((SynchronizedAttributeAccessor) other).support; |
62 | } |
63 | else if (other instanceof AttributeAccessorSupport) { |
64 | that = (AttributeAccessorSupport) other; |
65 | } |
66 | else { |
67 | return false; |
68 | } |
69 | synchronized (support) { |
70 | return support.equals(that); |
71 | } |
72 | } |
73 | |
74 | /* |
75 | * (non-Javadoc) |
76 | * @see org.springframework.core.AttributeAccessor#getAttribute(java.lang.String) |
77 | */ |
78 | public Object getAttribute(String name) { |
79 | synchronized (support) { |
80 | return support.getAttribute(name); |
81 | } |
82 | } |
83 | |
84 | /* |
85 | * (non-Javadoc) |
86 | * @see org.springframework.core.AttributeAccessor#hasAttribute(java.lang.String) |
87 | */ |
88 | public boolean hasAttribute(String name) { |
89 | synchronized (support) { |
90 | return support.hasAttribute(name); |
91 | } |
92 | } |
93 | |
94 | /* |
95 | * (non-Javadoc) |
96 | * @see java.lang.Object#hashCode() |
97 | */ |
98 | public int hashCode() { |
99 | return support.hashCode(); |
100 | } |
101 | |
102 | /* |
103 | * (non-Javadoc) |
104 | * @see org.springframework.core.AttributeAccessor#removeAttribute(java.lang.String) |
105 | */ |
106 | public Object removeAttribute(String name) { |
107 | synchronized (support) { |
108 | return support.removeAttribute(name); |
109 | } |
110 | } |
111 | |
112 | /* |
113 | * (non-Javadoc) |
114 | * @see org.springframework.core.AttributeAccessor#setAttribute(java.lang.String, |
115 | * java.lang.Object) |
116 | */ |
117 | public void setAttribute(String name, Object value) { |
118 | synchronized (support) { |
119 | support.setAttribute(name, value); |
120 | } |
121 | } |
122 | |
123 | /** |
124 | * Additional support for atomic put if absent. |
125 | * @param name the key for the attribute name |
126 | * @param value the value of the attribute |
127 | * @return null if the attribute was not already set, the existing value |
128 | * otherwise. |
129 | */ |
130 | public Object setAttributeIfAbsent(String name, Object value) { |
131 | synchronized (support) { |
132 | Object old = getAttribute(name); |
133 | if (old != null) { |
134 | return old; |
135 | } |
136 | setAttribute(name, value); |
137 | } |
138 | return null; |
139 | } |
140 | |
141 | /* |
142 | * (non-Javadoc) |
143 | * @see java.lang.Object#toString() |
144 | */ |
145 | public String toString() { |
146 | StringBuffer buffer = new StringBuffer("SynchronizedAttributeAccessor: ["); |
147 | synchronized (support) { |
148 | String[] names = attributeNames(); |
149 | for (int i = 0; i < names.length; i++) { |
150 | String name = names[i]; |
151 | buffer.append(names[i] + "=" + getAttribute(name)); |
152 | if (i < names.length - 1) { |
153 | buffer.append(", "); |
154 | } |
155 | } |
156 | buffer.append("]"); |
157 | return buffer.toString(); |
158 | } |
159 | } |
160 | |
161 | } |