Skip to main content
summaryrefslogtreecommitdiffstats
blob: e9d61461750cdbcf4eca1e86fbc6f8d3fa0862eb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * Copyright (c) 2008-2013 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.emf.cdo.server;

import org.eclipse.emf.cdo.server.IStoreAccessor.CommitContext;
import org.eclipse.emf.cdo.spi.server.InternalSession;

import org.eclipse.net4j.util.lifecycle.LifecycleUtil;

/**
 * Provides server-side consumers with the {@link IStoreAccessor store accessor} that is valid in the context of a
 * specific {@link ISession session} during read operations or a specific {@link CommitContext commit context} during
 * write operations.
 *
 * @author Eike Stepper
 * @since 2.0
 * @apiviz.exclude
 */
public final class StoreThreadLocal
{
  private static final ThreadLocal<InternalSession> SESSION = new InheritableThreadLocal<InternalSession>();

  private static final ThreadLocal<IStoreAccessor> ACCESSOR = new InheritableThreadLocal<IStoreAccessor>();

  private static final ThreadLocal<IStoreAccessor.CommitContext> COMMIT_CONTEXT = new InheritableThreadLocal<IStoreAccessor.CommitContext>();

  private StoreThreadLocal()
  {
  }

  /**
   * @since 3.0
   */
  public static void setSession(InternalSession session)
  {
    SESSION.set(session);
    ACCESSOR.remove();
    COMMIT_CONTEXT.remove();
  }

  /**
   * Returns the session associated with the current thread.
   *
   * @return Never <code>null</code>.
   * @throws IllegalStateException
   *           if no session is associated with the current thread.
   * @since 3.0
   */
  public static InternalSession getSession() throws NoSessionRegisteredException
  {
    InternalSession session = SESSION.get();
    if (session == null)
    {
      throw new NoSessionRegisteredException();
    }

    return session;
  }

  /**
   * @since 4.2
   */
  public static boolean hasSession()
  {
    return SESSION.get() != null;
  }

  public static void setAccessor(IStoreAccessor accessor)
  {
    SESSION.set(accessor == null ? null : accessor.getSession());
    ACCESSOR.set(accessor);
  }

  public static IStoreAccessor getAccessor() throws NoSessionRegisteredException
  {
    IStoreAccessor accessor = ACCESSOR.get();
    if (accessor == null)
    {
      ISession session = getSession();
      IStore store = session.getManager().getRepository().getStore();
      accessor = store.getReader(session);
      ACCESSOR.set(accessor);
    }

    return accessor;
  }

  public static void setCommitContext(IStoreAccessor.CommitContext commitContext)
  {
    COMMIT_CONTEXT.set(commitContext);
  }

  public static IStoreAccessor.CommitContext getCommitContext()
  {
    return COMMIT_CONTEXT.get();
  }

  /**
   * @since 4.2
   */
  public static void release()
  {
    try
    {
      IStoreAccessor accessor = ACCESSOR.get();
      if (accessor != null)
      {
        if (LifecycleUtil.isActive(accessor))
        {
          accessor.release();
        }
      }
    }
    finally
    {
      ACCESSOR.remove();
      SESSION.remove();
      COMMIT_CONTEXT.remove();
    }
  }

  /**
   * An {@link IllegalStateException} that can be thrown from {@link StoreThreadLocal#getSession()}.
   *
   * @author Eike Stepper
   * @since 4.2
   */
  public static final class NoSessionRegisteredException extends IllegalStateException
  {
    private static final long serialVersionUID = 1L;

    public NoSessionRegisteredException()
    {
      super("session == null");
    }
  }
}

Back to the top