View Javadoc
1   /*
2    * Copyright 2002-2012 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.orm;
18  
19  import org.springframework.dao.OptimisticLockingFailureException;
20  
21  /**
22   * Exception thrown on an optimistic locking violation for a mapped object.
23   * Provides information about the persistent class and the identifier.
24   *
25   * @author Juergen Hoeller
26   * @since 13.10.2003
27   */
28  @SuppressWarnings("serial")
29  public class ObjectOptimisticLockingFailureException extends OptimisticLockingFailureException {
30  
31  	private Object persistentClass;
32  
33  	private Object identifier;
34  
35  
36  	/**
37  	 * Create a general ObjectOptimisticLockingFailureException with the given message,
38  	 * without any information on the affected object.
39  	 * @param msg the detail message
40  	 * @param cause the source exception
41  	 */
42  	public ObjectOptimisticLockingFailureException(String msg, Throwable cause) {
43  		super(msg, cause);
44  	}
45  
46  	/**
47  	 * Create a new ObjectOptimisticLockingFailureException for the given object,
48  	 * with the default "optimistic locking failed" message.
49  	 * @param persistentClass the persistent class
50  	 * @param identifier the ID of the object for which the locking failed
51  	 */
52  	public ObjectOptimisticLockingFailureException(Class<?> persistentClass, Object identifier) {
53  		this(persistentClass, identifier, null);
54  	}
55  
56  	/**
57  	 * Create a new ObjectOptimisticLockingFailureException for the given object,
58  	 * with the default "optimistic locking failed" message.
59  	 * @param persistentClass the persistent class
60  	 * @param identifier the ID of the object for which the locking failed
61  	 * @param cause the source exception
62  	 */
63  	public ObjectOptimisticLockingFailureException(
64  			Class<?> persistentClass, Object identifier, Throwable cause) {
65  
66  		this(persistentClass, identifier,
67  				"Object of class [" + persistentClass.getName() + "] with identifier [" + identifier +
68  				"]: optimistic locking failed", cause);
69  	}
70  
71  	/**
72  	 * Create a new ObjectOptimisticLockingFailureException for the given object,
73  	 * with the given explicit message.
74  	 * @param persistentClass the persistent class
75  	 * @param identifier the ID of the object for which the locking failed
76  	 * @param msg the detail message
77  	 * @param cause the source exception
78  	 */
79  	public ObjectOptimisticLockingFailureException(
80  			Class<?> persistentClass, Object identifier, String msg, Throwable cause) {
81  
82  		super(msg, cause);
83  		this.persistentClass = persistentClass;
84  		this.identifier = identifier;
85  	}
86  
87  	/**
88  	 * Create a new ObjectOptimisticLockingFailureException for the given object,
89  	 * with the default "optimistic locking failed" message.
90  	 * @param persistentClassName the name of the persistent class
91  	 * @param identifier the ID of the object for which the locking failed
92  	 */
93  	public ObjectOptimisticLockingFailureException(String persistentClassName, Object identifier) {
94  		this(persistentClassName, identifier, null);
95  	}
96  
97  	/**
98  	 * Create a new ObjectOptimisticLockingFailureException for the given object,
99  	 * with the default "optimistic locking failed" message.
100 	 * @param persistentClassName the name of the persistent class
101 	 * @param identifier the ID of the object for which the locking failed
102 	 * @param cause the source exception
103 	 */
104 	public ObjectOptimisticLockingFailureException(
105 			String persistentClassName, Object identifier, Throwable cause) {
106 
107 		this(persistentClassName, identifier,
108 				"Object of class [" + persistentClassName + "] with identifier [" + identifier +
109 				"]: optimistic locking failed", cause);
110 	}
111 
112 	/**
113 	 * Create a new ObjectOptimisticLockingFailureException for the given object,
114 	 * with the given explicit message.
115 	 * @param persistentClassName the name of the persistent class
116 	 * @param identifier the ID of the object for which the locking failed
117 	 * @param msg the detail message
118 	 * @param cause the source exception
119 	 */
120 	public ObjectOptimisticLockingFailureException(
121 			String persistentClassName, Object identifier, String msg, Throwable cause) {
122 
123 		super(msg, cause);
124 		this.persistentClass = persistentClassName;
125 		this.identifier = identifier;
126 	}
127 
128 
129 	/**
130 	 * Return the persistent class of the object for which the locking failed.
131 	 * If no Class was specified, this method returns null.
132 	 */
133 	public Class<?> getPersistentClass() {
134 		return (this.persistentClass instanceof Class ? (Class<?>) this.persistentClass : null);
135 	}
136 
137 	/**
138 	 * Return the name of the persistent class of the object for which the locking failed.
139 	 * Will work for both Class objects and String names.
140 	 */
141 	public String getPersistentClassName() {
142 		if (this.persistentClass instanceof Class) {
143 			return ((Class<?>) this.persistentClass).getName();
144 		}
145 		return (this.persistentClass != null ? this.persistentClass.toString() : null);
146 	}
147 
148 	/**
149 	 * Return the identifier of the object for which the locking failed.
150 	 */
151 	public Object getIdentifier() {
152 		return identifier;
153 	}
154 
155 }