Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 622f689ad674ff96bf3dbccff4c81c43ebb8c63f (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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
 * Copyright (c) 2010-2012, 2015, 2016 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.net4j;

import org.eclipse.emf.cdo.common.branch.CDOBranch;
import org.eclipse.emf.cdo.common.branch.CDOBranchPoint;
import org.eclipse.emf.cdo.util.CDOURIData;
import org.eclipse.emf.cdo.view.AbstractCDOViewProvider;
import org.eclipse.emf.cdo.view.CDOView;
import org.eclipse.emf.cdo.view.CDOViewProvider;

import org.eclipse.net4j.Net4jUtil;
import org.eclipse.net4j.channel.IChannel;
import org.eclipse.net4j.connector.IConnector;
import org.eclipse.net4j.util.container.FactoryNotFoundException;
import org.eclipse.net4j.util.container.IManagedContainer;
import org.eclipse.net4j.util.container.IPluginContainer;
import org.eclipse.net4j.util.security.CredentialsProviderFactory;
import org.eclipse.net4j.util.security.IPasswordCredentialsProvider;
import org.eclipse.net4j.util.security.PasswordCredentialsProvider;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.ResourceSet;

import org.eclipse.core.runtime.Path;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * A {@link CDOViewProvider view provider} that uses Net4j-specific CDO {@link CDONet4jSession sessions} to open views.
 *
 * @author Eike Stepper
 * @since 4.0
 */
public abstract class CDONet4jViewProvider extends AbstractCDOViewProvider
{
  private String transport;

  public CDONet4jViewProvider(String transport, int priority)
  {
    super("cdo\\.net4j\\." + transport + "://.*", priority);
    this.transport = transport;
  }

  public CDOView getView(URI uri, ResourceSet resourceSet)
  {
    CDOURIData data = new CDOURIData(uri);

    IConnector connector = getConnector(data.getAuthority());
    CDONet4jSession session = getNet4jSession(connector, data.getUserName(), data.getPassWord(), data.getRepositoryName());

    String viewID = data.getViewID();
    if (viewID != null)
    {
      if (data.isTransactional())
      {
        return session.openTransaction(viewID, resourceSet);
      }

      return session.openView(viewID, resourceSet);
    }

    String branchPath = data.getBranchPath().toPortableString();
    CDOBranch branch = session.getBranchManager().getBranch(branchPath);
    long timeStamp = data.getTimeStamp();

    if (data.isTransactional())
    {
      return session.openTransaction(branch, resourceSet);
    }

    return session.openView(branch, timeStamp, resourceSet);
  }

  @Override
  public String getPath(URI uri)
  {
    return new Path(uri.path()).makeAbsolute().removeFirstSegments(1).toString();
  }

  @Override
  public URI getViewURI(URI uri)
  {
    CDOURIData uriData = new CDOURIData(uri);
    uriData.setResourcePath(null);
    uriData.setExtraParameters(null);
    return uriData.toURI();
  }

  @Override
  public URI getResourceURI(CDOView view, String path)
  {
    StringBuilder builder = new StringBuilder();
    builder.append("cdo.net4j.");
    builder.append(transport);
    builder.append("://");

    CDONet4jSession session = (CDONet4jSession)view.getSession();

    // CDOAuthenticator authenticator = ((InternalCDOSession)session).getAuthenticator();
    // IPasswordCredentialsProvider credentialsProvider = authenticator.getCredentialsProvider();
    // if (credentialsProvider != null)
    // {
    // IPasswordCredentials credentials = credentialsProvider.getCredentials();
    // builder.append(credentials.getUserID());
    //
    // char[] password = credentials.getPassword();
    // if (password != null)
    // {
    // builder.append(":");
    // builder.append(password);
    // }
    //
    // builder.append("@");
    // }

    IChannel channel = session.options().getNet4jProtocol().getChannel();
    if (channel == null)
    {
      return null;
    }
    IConnector connector = (IConnector)channel.getMultiplexer();
    String repositoryName = session.getRepositoryInfo().getName();
    append(builder, connector, repositoryName);

    if (path != null)
    {
      if (!path.startsWith("/"))
      {
        builder.append("/");
      }

      builder.append(path);
    }

    int params = 0;

    String branchPath = view.getBranch().getPathName();
    if (!CDOBranch.MAIN_BRANCH_NAME.equalsIgnoreCase(branchPath))
    {
      builder.append(params++ == 0 ? "?" : "&");
      builder.append(CDOURIData.BRANCH_PARAMETER);
      builder.append("=");
      builder.append(branchPath);
    }

    long timeStamp = view.getTimeStamp();
    if (timeStamp != CDOBranchPoint.UNSPECIFIED_DATE)
    {
      builder.append(params++ == 0 ? "?" : "&");
      builder.append(CDOURIData.TIME_PARAMETER);
      builder.append("=");
      builder.append(new SimpleDateFormat().format(new Date(timeStamp)));
    }

    if (!view.isReadOnly())
    {
      builder.append(params++ == 0 ? "?" : "&");
      builder.append(CDOURIData.TRANSACTIONAL_PARAMETER);
      builder.append("=true");
    }

    return URI.createURI(builder.toString());
  }

  protected String getURIAuthority(IConnector connector)
  {
    String url = connector.getURL().toString();
    return URI.createURI(url).authority();
  }

  /**
   * @since 4.1
   */
  protected CDONet4jSession getNet4jSession(IConnector connector, String userName, String passWord, String repositoryName)
  {
    CDONet4jSessionConfiguration configuration = getNet4jSessionConfiguration(connector, userName, passWord, repositoryName);
    return configuration.openNet4jSession();
  }

  /**
   * @since 4.1
   */
  protected CDONet4jSessionConfiguration getNet4jSessionConfiguration(IConnector connector, String userName, String passWord, String repositoryName)
  {
    CDONet4jSessionConfiguration configuration = CDONet4jUtil.createNet4jSessionConfiguration();
    configuration.setConnector(connector);
    configuration.setRepositoryName(repositoryName);

    IPasswordCredentialsProvider credentialsProvider = null;
    if (userName != null && passWord != null)
    {
      credentialsProvider = new PasswordCredentialsProvider(userName, passWord);
    }
    else
    {
      StringBuilder builder = new StringBuilder();
      append(builder, connector, repositoryName);
      String resource = builder.toString();

      try
      {
        credentialsProvider = (IPasswordCredentialsProvider)getContainer().getElement(CredentialsProviderFactory.PRODUCT_GROUP, "password", resource);
      }
      catch (FactoryNotFoundException ex)
      {
        // Ignore
      }

      // The following is to stay compatible with the formerly wrong product group (".security" was missing).
      if (credentialsProvider == null)
      {
        try
        {
          credentialsProvider = (IPasswordCredentialsProvider)getContainer().getElement("org.eclipse.net4j.util.credentialsProviders", "password", resource);
        }
        catch (FactoryNotFoundException ex)
        {
          // Ignore
        }
      }
    }

    configuration.setCredentialsProvider(credentialsProvider);
    return configuration;
  }

  /**
   * @deprecated Use {@link #getNet4jSession(IConnector, String, String, String) getNet4jSession()}.
   */
  @Deprecated
  protected CDOSession getSession(IConnector connector, String userName, String passWord, String repositoryName)
  {
    return (CDOSession)getNet4jSession(connector, userName, passWord, repositoryName);
  }

  /**
   * @deprecated Use {@link #getNet4jSessionConfiguration(IConnector, String, String, String)
   *             getNet4jSessionConfiguration()}.
   */
  @Deprecated
  protected CDOSessionConfiguration getSessionConfiguration(IConnector connector, String userName, String passWord, String repositoryName)
  {
    return (CDOSessionConfiguration)getNet4jSessionConfiguration(connector, userName, passWord, repositoryName);
  }

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

  protected IConnector getConnector(String authority)
  {
    IManagedContainer container = getContainer();
    String description = getConnectorDescription(authority);
    return Net4jUtil.getConnector(container, transport, description);
  }

  protected String getConnectorDescription(String authority)
  {
    return authority;
  }

  private void append(StringBuilder builder, IConnector connector, String repositoryName)
  {
    String authority = getURIAuthority(connector);
    builder.append(authority);

    builder.append("/");
    builder.append(repositoryName);
  }

  /**
   * A TCP-based {@link CDONet4jViewProvider view provider}.
   *
   * @author Eike Stepper
   */
  public static class TCP extends CDONet4jViewProvider
  {
    public TCP(int priority)
    {
      super("tcp", priority);
    }

    public TCP()
    {
      this(DEFAULT_PRIORITY);
    }

  }

  /**
   * An SSL-based {@link CDONet4jViewProvider view provider}.
   *
   * @author Teerawat Chaiyakijpichet (No Magic Asia Ltd.)
   */
  public static class SSL extends CDONet4jViewProvider
  {
    public SSL(int priority)
    {
      super("ssl", priority);
    }

    public SSL()
    {
      this(DEFAULT_PRIORITY);
    }

  }

  /**
   * A JVM-based {@link CDONet4jViewProvider view provider}.
   *
   * @author Eike Stepper
   */
  public static class JVM extends CDONet4jViewProvider
  {
    public JVM(int priority)
    {
      super("jvm", priority);
    }

    public JVM()
    {
      this(DEFAULT_PRIORITY);
    }
  }
}

Back to the top