Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b5c76f29809f0d86eec6bf8972890f4bb3c60426 (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
/*
 * Copyright (c) 2004 - 2012 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.internal.efs;

import org.eclipse.emf.cdo.common.branch.CDOBranch;
import org.eclipse.emf.cdo.net4j.CDONet4jSessionConfiguration;
import org.eclipse.emf.cdo.net4j.CDONet4jUtil;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.session.CDOSessionConfiguration;
import org.eclipse.emf.cdo.util.CDOURIData;
import org.eclipse.emf.cdo.view.CDOView;

import org.eclipse.net4j.Net4jUtil;
import org.eclipse.net4j.connector.IConnector;
import org.eclipse.net4j.util.collection.Pair;
import org.eclipse.net4j.util.container.IManagedContainer;
import org.eclipse.net4j.util.container.IPluginContainer;

import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.filesystem.provider.FileSystem;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;

/**
 * @author Eike Stepper
 */
public abstract class CDOFileSystem extends FileSystem
{
  private Map<Pair<String, String>, CDOSession> sessions = new HashMap<Pair<String, String>, CDOSession>();

  private Map<URI, CDOView> views = new HashMap<URI, CDOView>();

  protected CDOFileSystem()
  {
  }

  @Override
  public int attributes()
  {
    return /* EFS.ATTRIBUTE_READ_ONLY | */EFS.ATTRIBUTE_OTHER_READ | EFS.ATTRIBUTE_OTHER_WRITE;
  }

  @Override
  public boolean isCaseSensitive()
  {
    return true;
  }

  @Override
  public IFileStore getStore(URI uri)
  {
    CDOURIData data = new CDOURIData(uri.toString());
    // String authority = uri.getAuthority();
    // IPath path = new Path(uri.getPath());
    // String repositoryName = path.segment(0);
    // path = path.removeFirstSegments(1);
    //
    // IPath branchPath = Path.EMPTY;
    // long timeStamp = CDOBranchPoint.UNSPECIFIED_DATE;
    //
    // while (path.segmentCount() != 0)
    // {
    // String segment = path.segment(0);
    // path = path.removeFirstSegments(1);
    //
    // if (segment.startsWith("@"))
    // {
    // if (segment.length() != 1)
    // {
    // if (!segment.equals("@HEAD"))
    // {
    // timeStamp = Long.parseLong(segment.substring(1));
    // }
    // }
    //
    // break;
    // }
    //
    // branchPath = branchPath.append(segment);
    // }
    //
    // int segments = branchPath.segmentCount();
    // if (segments == 0 || segments == 1 && !branchPath.segment(0).equals(CDOBranch.MAIN_BRANCH_NAME))
    // {
    // branchPath = new Path(CDOBranch.MAIN_BRANCH_NAME).append(branchPath);
    // }

    String authority = data.getAuthority();
    String repositoryName = data.getRepositoryName();
    IPath path = data.getResourcePath();
    IPath branchPath = data.getBranchPath();
    long timeStamp = data.getTimeStamp();
    CDOFileRoot root = new CDOFileRoot(this, authority, repositoryName, branchPath, timeStamp);
    if (path.isEmpty())
    {
      return root;
    }

    return root.getFileStore(path);
  }

  public CDOView getView(CDOFileRoot root, IProgressMonitor monitor)
  {
    URI uri = root.toURI();
    CDOView view = views.get(uri);
    if (view == null)
    {
      view = openView(root, monitor);
      views.put(uri, view);
    }

    return view;
  }

  protected CDOView openView(CDOFileRoot root, IProgressMonitor monitor)
  {
    String authority = root.getAuthority();
    String repositoryName = root.getRepositoryName();
    String branchPath = root.getBranchPath().toPortableString();
    final long timeStamp = root.getTimeStamp();

    final CDOSession session = getSession(authority, repositoryName, monitor);
    final CDOBranch branch = session.getBranchManager().getBranch(branchPath);

    return InfiniteProgress.call("Open view", new Callable<CDOView>()
    {
      public CDOView call() throws Exception
      {
        return session.openView(branch, timeStamp);
      }
    });
  }

  protected CDOSession getSession(String authority, String repositoryName, IProgressMonitor monitor)
  {
    Pair<String, String> sessionKey = new Pair<String, String>(authority, repositoryName);
    CDOSession session = sessions.get(sessionKey);
    if (session == null)
    {
      final CDOSessionConfiguration configuration = createSessionConfiguration(authority, repositoryName, monitor);
      session = InfiniteProgress.call("Open session", new Callable<CDOSession>()
      {
        public CDOSession call() throws Exception
        {
          return configuration.openSession();
        }
      });

      sessions.put(sessionKey, session);
    }

    return session;
  }

  protected IManagedContainer getContainer()
  {
    return IPluginContainer.INSTANCE;
  }

  protected abstract CDOSessionConfiguration createSessionConfiguration(String authority, String repositoryName,
      IProgressMonitor monitor);

  /**
   * @author Eike Stepper
   */
  public static abstract class Net4j extends CDOFileSystem
  {
    private String connectorType;

    protected Net4j(String connectorType)
    {
      this.connectorType = connectorType;
    }

    protected IConnector getConnector(final String authority, IProgressMonitor monitor)
    {
      return InfiniteProgress.call("Open connection", new Callable<IConnector>()
      {
        public IConnector call() throws Exception
        {
          return Net4jUtil.getConnector(getContainer(), connectorType, authority);
        }
      });
    }

    @Override
    protected CDOSessionConfiguration createSessionConfiguration(String authority, String repositoryName,
        IProgressMonitor monitor)
    {
      CDONet4jSessionConfiguration configuration = CDONet4jUtil.createNet4jSessionConfiguration();
      configuration.setConnector(getConnector(authority, monitor));
      configuration.setRepositoryName(repositoryName);
      return configuration;
    }

    /**
     * @author Eike Stepper
     */
    public static class TCP extends Net4j
    {
      /*
       * Must be public to be instantiatable by the extension registry.
       */
      public TCP()
      {
        super("tcp");
      }
    }
  }
}

Back to the top