View Javadoc

1   /*
2    * Copyright 2004 Charles Blaxland
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 mnemosyne.core;
17  
18  import org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  
21  
22  /***
23   * The persistent context is a thread local that holds information about
24   * the transactional state of the current thread.
25   *
26   * Method in this class are protected (they are only called from within the core
27   * package) to prevent direct manipulation of transactions and versions.
28   *
29   * @version $Id: PersistentContext.java,v 1.1.1.1 2004/08/07 06:41:05 charlesblaxland Exp $
30   * @see java.lang.ThreadLocal
31   */
32  public class PersistentContext
33  {
34      private static Log log = LogFactory.getLog(PersistentContext.class);
35  
36      private static ThreadLocal thePersistentContext = new ThreadLocal();
37  
38      private Version version;
39      private Transaction transaction;
40  
41      protected PersistentContext(Version currentVersion)
42      {
43          this.version = currentVersion;
44      }
45  
46      public Version getVersion()
47      {
48          return version;
49      }
50  
51      public void setVersion(final Version version)
52      {
53          if (log.isDebugEnabled())
54              log.debug("Setting thead persistent graph version version to " + version);
55  
56          this.version = version;
57      }
58  
59      public Transaction getTransaction()
60      {
61          return transaction;
62      }
63  
64      public boolean isInTransaction()
65      {
66          return transaction != null;
67      }
68  
69      public void setTransaction(Transaction currentTransaction)
70      {
71          this.transaction = currentTransaction;
72      }
73  
74      public final static PersistentContext get(final VersionManager versionMgr)
75      {
76          // TODO support multiple persistent systems
77          Object context = thePersistentContext.get();
78          if (context == null)
79          {
80              context = new PersistentContext(versionMgr.getLatestVersion());
81              thePersistentContext.set(context);
82          }
83  
84          return (PersistentContext) context;
85      }
86  
87      public final static PersistentContext reset(final VersionManager versionMgr)
88      {
89          PersistentContext context = new PersistentContext(versionMgr.getLatestVersion());
90          thePersistentContext.set(context);
91          return context;
92      }
93  }