View Javadoc
1   /*
2    *  Copyright 2010, 2011 Christopher Pheby
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  package org.jadira.usertype.spi.shared;
17  
18  import java.io.Serializable;
19  
20  import org.hibernate.HibernateException;
21  import org.hibernate.engine.spi.SharedSessionContractImplementor;
22  import org.hibernate.type.SerializationException;
23  
24  public abstract class AbstractUserType implements Serializable {
25  
26      private static final long serialVersionUID = -3503387360213242237L;
27  
28      public boolean isMutable() {
29          return false;
30      }
31  
32      public boolean equals(Object x, Object y) throws HibernateException {
33      	if ((x == null) && (y == null)) {
34      		return true;
35      	}
36          if ((x == null) || (y == null)) {
37              return false;
38          }
39      	if (x == y) {
40              return true;
41          }
42          return x.equals(y);
43      }
44  
45      public int hashCode(Object x) throws HibernateException {
46          assert (x != null);
47          return x.hashCode();
48      }
49  
50      public Object assemble(Serializable cachedValue, Object owner) throws HibernateException {
51          return deepCopy(cachedValue);
52      }
53  
54      public Serializable disassemble(Object value) throws HibernateException {
55  
56          final Serializable result;
57  
58          if (value == null) {
59              result = null;
60          } else {
61              final Object deepCopy = deepCopy(value);
62              if (!(deepCopy instanceof Serializable)) {
63                  throw new SerializationException(String.format("deepCopy of %s is not serializable", value), null);
64              }
65              result = (Serializable) deepCopy;
66          }
67  
68          return result;
69      }
70  
71      public Object replace(Object originalValue, Object target, Object owner) throws HibernateException {
72          return deepCopy(originalValue);
73      }
74  
75      public Object deepCopy(Object value) throws HibernateException {
76          return value;
77      }
78      
79      /**
80       * Included to allow session state to be applied to the user type
81       * @param session The session
82       */
83      public void beforeNullSafeOperation(SharedSessionContractImplementor session) {
84      	
85      	ConfigurationHelper.setCurrentSessionFactory(session.getFactory());
86      	if (this instanceof IntegratorConfiguredType) {
87      		((IntegratorConfiguredType)this).applyConfiguration(session.getFactory());
88      	}
89      }
90      
91      /**
92       * Included to allow session state to be disassociated from the user type.
93       * This should be called from a finally block for surety.
94       * @param session The session
95       */
96      public void afterNullSafeOperation(SharedSessionContractImplementor session) {
97      	ConfigurationHelper.setCurrentSessionFactory(null);
98      }
99  }