View Javadoc

1   /* Copyright 2004, 2005 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  package org.acegisecurity.domain.dao;
17  
18  
19  /***
20   * <code>InheritableThreadLocal</code> which indicates whether a {@link Dao}
21   * implementation should be forced to return a detached instance.
22   * 
23   * <p>A detached instance is one which is no longer associated with the ORM
24   * mapper and changes will therefore not be transparently persisted to the database.
25   * 
26   * <p>Not all <code>Dao</code> implementations support the concept of detached
27   * instances.
28   *
29   * @author Ben Alex
30   * @version $Id: DetachmentContextHolder.java,v 1.4 2005/11/17 00:55:47 benalex Exp $
31   *
32   * @see java.lang.InheritableThreadLocal
33   */
34  public class DetachmentContextHolder {
35      //~ Static fields/initializers =============================================
36  
37      private static InheritableThreadLocal<Boolean> contextHolder = new InheritableThreadLocal<Boolean>();
38  
39      //~ Methods ================================================================
40  
41      /***
42       * Sets whether or not detached domain object instances should be returned
43       * within the current thread of execution.
44       *
45       * @param alwaysReturnDetached if true then detached instances should be returned.
46       */
47      public static void setForceReturnOfDetachedInstances(boolean alwaysReturnDetached) {
48          contextHolder.set(new Boolean(alwaysReturnDetached));
49      }
50  
51      /***
52       * Returns the boolean value detachment policy which has been set for the current
53       * thread (defaults to false).
54       *
55       */
56      public static boolean isForceReturnOfDetachedInstances() {
57          if (contextHolder.get() == null) {
58              contextHolder.set(Boolean.FALSE);
59          }
60  
61          return contextHolder.get().booleanValue();
62      }
63  }