View Javadoc

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      @Override
46  	public String[] attributeNames() {
47  		synchronized (support) {
48  			return support.attributeNames();
49  		}
50  	}
51  
52  	/*
53  	 * (non-Javadoc)
54  	 * @see java.lang.Object#equals(java.lang.Object)
55  	 */
56      @Override
57  	public boolean equals(Object other) {
58  		if (this == other) {
59  			return true;
60  		}
61  		AttributeAccessorSupport that;
62  		if (other instanceof SynchronizedAttributeAccessor) {
63  			that = ((SynchronizedAttributeAccessor) other).support;
64  		}
65  		else if (other instanceof AttributeAccessorSupport) {
66  			that = (AttributeAccessorSupport) other;
67  		}
68  		else {
69  			return false;
70  		}
71  		synchronized (support) {
72  			return support.equals(that);
73  		}
74  	}
75  
76  	/*
77  	 * (non-Javadoc)
78  	 * @see org.springframework.core.AttributeAccessor#getAttribute(java.lang.String)
79  	 */
80      @Override
81  	public Object getAttribute(String name) {
82  		synchronized (support) {
83  			return support.getAttribute(name);
84  		}
85  	}
86  
87  	/*
88  	 * (non-Javadoc)
89  	 * @see org.springframework.core.AttributeAccessor#hasAttribute(java.lang.String)
90  	 */
91      @Override
92  	public boolean hasAttribute(String name) {
93  		synchronized (support) {
94  			return support.hasAttribute(name);
95  		}
96  	}
97  
98  	/*
99  	 * (non-Javadoc)
100 	 * @see java.lang.Object#hashCode()
101 	 */
102     @Override
103 	public int hashCode() {
104 		return support.hashCode();
105 	}
106 
107 	/*
108 	 * (non-Javadoc)
109 	 * @see org.springframework.core.AttributeAccessor#removeAttribute(java.lang.String)
110 	 */
111     @Override
112 	public Object removeAttribute(String name) {
113 		synchronized (support) {
114 			return support.removeAttribute(name);
115 		}
116 	}
117 
118 	/*
119 	 * (non-Javadoc)
120 	 * @see org.springframework.core.AttributeAccessor#setAttribute(java.lang.String,
121 	 * java.lang.Object)
122 	 */
123     @Override
124 	public void setAttribute(String name, Object value) {
125 		synchronized (support) {
126 			support.setAttribute(name, value);
127 		}
128 	}
129 
130 	/**
131 	 * Additional support for atomic put if absent.
132 	 * @param name the key for the attribute name
133 	 * @param value the value of the attribute
134 	 * @return null if the attribute was not already set, the existing value
135 	 * otherwise.
136 	 */
137 	public Object setAttributeIfAbsent(String name, Object value) {
138 		synchronized (support) {
139 			Object old = getAttribute(name);
140 			if (old != null) {
141 				return old;
142 			}
143 			setAttribute(name, value);
144 		}
145 		return null;
146 	}
147 
148 	/*
149 	 * (non-Javadoc)
150 	 * @see java.lang.Object#toString()
151 	 */
152     @Override
153 	public String toString() {
154 		StringBuffer buffer = new StringBuffer("SynchronizedAttributeAccessor: [");
155 		synchronized (support) {
156 			String[] names = attributeNames();
157 			for (int i = 0; i < names.length; i++) {
158 				String name = names[i];
159 				buffer.append(names[i]).append("=").append(getAttribute(name));
160 				if (i < names.length - 1) {
161 					buffer.append(", ");
162 				}
163 			}
164 			buffer.append("]");
165 			return buffer.toString();
166 		}
167 	}
168 
169 }