Skip to main content
summaryrefslogtreecommitdiffstats
blob: eabb2a76c04213ffb4de2df4f3477e0f592fa77d (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/***************************************************************************
 * Copyright (c) 2004 - 2008 Eike Stepper, Germany.
 * 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
 *    Simon McDuff - http://bugs.eclipse.org/233490
 **************************************************************************/
package org.eclipse.emf.cdo.internal.server;

import org.eclipse.emf.cdo.common.CDOProtocolView;
import org.eclipse.emf.cdo.common.id.CDOID;
import org.eclipse.emf.cdo.server.IRepository;
import org.eclipse.emf.cdo.server.IStoreReader;
import org.eclipse.emf.cdo.server.IView;
import org.eclipse.emf.cdo.server.StoreThreadLocal;
import org.eclipse.emf.cdo.spi.common.InternalCDORevision;

import org.eclipse.net4j.util.StringUtil;

import java.text.MessageFormat;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;

/**
 * @author Eike Stepper
 */
public class View implements IView
{
  private Session session;

  private int viewID;

  private IRepository repository;

  private Set<CDOID> changeSubscriptionIDs = new HashSet<CDOID>();

  /**
   * @since 2.0
   */
  public View(Session session, int viewID)
  {
    this.session = session;
    this.viewID = viewID;
    repository = session.getSessionManager().getRepository();
  }

  public Session getSession()
  {
    return session;
  }

  public int getViewID()
  {
    return viewID;
  }

  public CDOProtocolView.Type getViewType()
  {
    return CDOProtocolView.Type.READONLY;
  }

  /**
   * @since 2.0
   */
  public IRepository getRepository()
  {
    checkOpen();
    return repository;
  }

  /**
   * @since 2.0
   */
  public CDOID getResourceID(String path)
  {
    checkOpen();
    long timeStamp = getTimeStamp();
    CDOID resourceID = null;

    StringTokenizer tokenizer = new StringTokenizer(path, "/");
    while (tokenizer.hasMoreTokens())
    {
      String token = tokenizer.nextToken();
      if (!StringUtil.isEmpty(token))
      {
        resourceID = getResourceID(resourceID, token, timeStamp);
        if (resourceID == null)
        {
          return null;
        }
      }
    }

    return resourceID;
  }

  private CDOID getResourceID(CDOID folderID, String name, long timeStamp)
  {
    CDOID id = repository.getRevisionManager().getResourceID(folderID, name, timeStamp);
    if (id == null)
    {
      IStoreReader storeReader = StoreThreadLocal.getStoreReader();
      id = storeReader.readResourceID(folderID, name, timeStamp);
    }

    return id;
  }

  /**
   * @since 2.0
   */
  public String getResourcePath(CDOID id)
  {
    checkOpen();
    long timeStamp = getTimeStamp();
    String path = repository.getRevisionManager().getResourcePath(id, timeStamp);
    if (path == null)
    {
      IStoreReader storeReader = StoreThreadLocal.getStoreReader();
      InternalCDORevision revision = (InternalCDORevision)storeReader.readRevisionByTime(id, 0, timeStamp);
      if (revision != null && revision.isResource())
      {
        path = (String)revision.getValue(resourcePathFeature);
      }
    }

    return path;
  }

  /**
   * The timeStamp of the view ({@link CDOProtocolView#UNSPECIFIED_DATE} if the view is an
   * {@link CDOProtocolView.Type#AUDIT audit} view.
   * 
   * @since 2.0
   */
  public long getTimeStamp()
  {
    return UNSPECIFIED_DATE;
  }

  /**
   * @since 2.0
   */
  public synchronized void subscribe(CDOID id)
  {
    checkOpen();
    changeSubscriptionIDs.add(id);
  }

  /**
   * @since 2.0
   */
  public synchronized void unsubscribe(CDOID id)
  {
    checkOpen();
    changeSubscriptionIDs.remove(id);
  }

  /**
   * @since 2.0
   */
  public synchronized boolean hasSubscription(CDOID id)
  {
    checkOpen();
    return changeSubscriptionIDs.contains(id);
  }

  /**
   * @since 2.0
   */
  public synchronized void clearChangeSubscription()
  {
    checkOpen();
    changeSubscriptionIDs.clear();
  }

  @Override
  public String toString()
  {
    return MessageFormat.format("View[{0}]", viewID);
  }

  /**
   * @since 2.0
   */
  public void close()
  {
    if (!isClosed())
    {
      session.viewClosed(this);
    }
  }

  /**
   * @since 2.0
   */
  public void doClose()
  {
    clearChangeSubscription();
    session = null;
    repository = null;
    changeSubscriptionIDs = null;
  }

  /**
   * @since 2.0
   */
  public boolean isClosed()
  {
    return session == null;
  }

  private void checkOpen()
  {
    if (isClosed())
    {
      throw new IllegalStateException("View closed");
    }
  }
}

Back to the top