Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d9f7a474832a6dd1e48b0234a6012b1fec46d69e (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * Copyright (c) 2009-2012 Eike Stepper (Loehne, 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.spi.server;

import org.eclipse.emf.cdo.internal.server.bundle.OM;
import org.eclipse.emf.cdo.server.ISession;
import org.eclipse.emf.cdo.server.IStore;
import org.eclipse.emf.cdo.server.IView;

import org.eclipse.net4j.util.lifecycle.LifecycleUtil;
import org.eclipse.net4j.util.om.log.OMLogger;

import java.util.LinkedList;

/**
 * If the meaning of this type isn't clear, there really should be more of a description here...
 *
 * @author Eike Stepper
 * @since 2.0
 */
public class StoreAccessorPool
{
  /**
   * @since 4.2
   */
  public static final int DEFAULT_CAPACITY = 15;

  /**
   * The {@link IStore store} instance that manages this pool.
   */
  private IStore store;

  /**
   * The pooling context of this pool. An instance of either {@link ISession} or {@link IView}, or <code>null</code> if
   * this pool is not contextual.
   */
  private Object context;

  private int capacity = DEFAULT_CAPACITY;

  private LinkedList<StoreAccessorBase> accessors = new LinkedList<StoreAccessorBase>();

  public StoreAccessorPool(IStore store, Object context)
  {
    this.store = store;
    this.context = context;
  }

  public IStore getStore()
  {
    return store;
  }

  public Object getContext()
  {
    return context;
  }

  /**
   * @since 4.2
   */
  public int getCapacity()
  {
    return capacity;
  }

  /**
   * @since 4.2
   */
  public void setCapacity(int capacity)
  {
    this.capacity = capacity;
    retainStoreAccessors(capacity);
  }

  /**
   * Passivates the given {@link StoreAccessor store accessor} and adds it to this pool if the pool size is smaller than the {@link #getCapacity() capacity},
   * or disposes of the store accessor otherwise.
   *
   * @since 4.0
   */
  public void addStoreAccessor(StoreAccessorBase storeAccessor)
  {
    try
    {
      storeAccessor.doPassivate();

      boolean full = false;
      synchronized (accessors)
      {
        if (accessors.size() >= capacity)
        {
          full = true;
        }
        else
        {
          accessors.addFirst(storeAccessor);
        }
      }

      if (full)
      {
        disposeStoreAccessor(storeAccessor);
      }
    }
    catch (Exception ex)
    {
      OM.LOG.error(ex);
    }
  }

  /**
   * Returns a {@link StoreAccessor store accessor} from this pool if one is available, or <code>null</code> otherwise.
   * If a store accessor is available it is removed from this pool and its unpassivate method is called.
   *
   * @since 4.0
   */
  public StoreAccessorBase removeStoreAccessor(Object context)
  {
    StoreAccessorBase accessor;
    synchronized (accessors)
    {
      accessor = accessors.poll();
    }

    if (accessor != null)
    {
      try
      {
        accessor.doUnpassivate();
        accessor.setContext(context);
      }
      catch (Exception ex)
      {
        OM.LOG.error(ex);
        return null;
      }
    }

    return accessor;
  }

  /**
   * Deactivates all contained {@link StoreAccessor store accessors} and clears this pool.
   */
  public void dispose()
  {
    retainStoreAccessors(0);

    context = null;
    store = null;
  }

  /**
   * @since 4.2
   */
  protected void retainStoreAccessors(int targetSize)
  {
    synchronized (accessors)
    {
      while (accessors.size() > targetSize)
      {
        StoreAccessorBase accessor = accessors.removeLast();
        disposeStoreAccessor(accessor);
      }
    }
  }

  /**
   * @since 4.2
   */
  protected void disposeStoreAccessor(StoreAccessorBase accessor)
  {
    LifecycleUtil.deactivate(accessor, OMLogger.Level.WARN);
  }
}

Back to the top