Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8894d4de2baebf68300a066d076a45ae35117d51 (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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/*
 * 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
 *    Christian W. Damus (CEA) - don't remove statically registered packages from registry
 */
package org.eclipse.emf.cdo.tests.config.impl;

import org.eclipse.emf.cdo.net4j.CDONet4jSessionConfiguration;
import org.eclipse.emf.cdo.net4j.CDONet4jUtil;
import org.eclipse.emf.cdo.net4j.CDONet4jViewProvider;
import org.eclipse.emf.cdo.server.CDOServerUtil;
import org.eclipse.emf.cdo.server.IRepository;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.session.CDOSessionConfiguration;
import org.eclipse.emf.cdo.spi.common.CDOLobStoreImpl;
import org.eclipse.emf.cdo.tests.config.IConfig;
import org.eclipse.emf.cdo.tests.config.IRepositoryConfig;
import org.eclipse.emf.cdo.tests.config.ISessionConfig;
import org.eclipse.emf.cdo.tests.util.TestRevisionManager;
import org.eclipse.emf.cdo.view.CDOFetchRuleManager;
import org.eclipse.emf.cdo.view.CDOViewProvider;
import org.eclipse.emf.cdo.view.CDOViewProviderRegistry;

import org.eclipse.net4j.acceptor.IAcceptor;
import org.eclipse.net4j.connector.IConnector;
import org.eclipse.net4j.jvm.JVMUtil;
import org.eclipse.net4j.tcp.TCPUtil;
import org.eclipse.net4j.tcp.ssl.SSLUtil;
import org.eclipse.net4j.util.container.IManagedContainer;
import org.eclipse.net4j.util.event.IListener;
import org.eclipse.net4j.util.io.IOUtil;
import org.eclipse.net4j.util.lifecycle.ILifecycle;
import org.eclipse.net4j.util.lifecycle.LifecycleEventAdapter;
import org.eclipse.net4j.util.lifecycle.LifecycleUtil;
import org.eclipse.net4j.util.security.IPasswordCredentialsProvider;

import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EPackage.Registry;
import org.eclipse.emf.ecore.impl.EPackageImpl;

import java.io.File;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * @author Eike Stepper
 */
public abstract class SessionConfig extends Config implements ISessionConfig
{
  public static final String PROP_TEST_SESSION_CONFIGURATION = "test.session.SessionConfiguration";

  public static final String PROP_TEST_CREDENTIALS_PROVIDER = "test.session.CredentialsProvider";

  public static final String PROP_TEST_FETCH_RULE_MANAGER = "test.session.FetchRuleManager";

  private static final Registry GLOBAL_REGISTRY = EPackage.Registry.INSTANCE;

  private static final long serialVersionUID = 1L;

  private transient Set<CDOSession> sessions;

  private transient IListener sessionListener;

  private transient Set<String> globallyRegisteredPackageURIs;

  public SessionConfig(String name)
  {
    super(name);
  }

  public void startTransport() throws Exception
  {
  }

  public void stopTransport() throws Exception
  {
  }

  public CDOSession openSession()
  {
    return openSession(IRepositoryConfig.REPOSITORY_NAME);
  }

  public CDOSession openSession(String repositoryName)
  {
    if (RepositoryConfig.REPOSITORY_NAME.equals(repositoryName))
    {
      // Start default repository
      getCurrentTest().getRepository(RepositoryConfig.REPOSITORY_NAME);
    }

    CDOSessionConfiguration configuration = getTestSessionConfiguration();
    if (configuration == null)
    {
      configuration = createSessionConfiguration(repositoryName);
    }

    IPasswordCredentialsProvider credentialsProvider = getTestCredentialsProvider();
    if (credentialsProvider != null)
    {
      configuration.setCredentialsProvider(credentialsProvider);
    }

    CDOSession session = configuration.openSession();
    configureSession(session);
    session.addListener(sessionListener);

    synchronized (sessions)
    {
      sessions.add(session);
    }

    return session;
  }

  @Override
  public void setUp() throws Exception
  {
    super.setUp();
    sessions = new HashSet<CDOSession>();
    sessionListener = new LifecycleEventAdapter()
    {
      @Override
      protected void onDeactivated(ILifecycle session)
      {
        synchronized (sessions)
        {
          sessions.remove(session);
        }
      }
    };

    globallyRegisteredPackageURIs = captureGlobalPackageRegistry();
  }

  @Override
  public void tearDown() throws Exception
  {
    try
    {
      if (sessions != null)
      {
        CDOSession[] array;
        synchronized (sessions)
        {
          array = sessions.toArray(new CDOSession[sessions.size()]);
        }

        for (CDOSession session : array)
        {
          session.removeListener(sessionListener);
          LifecycleUtil.deactivate(session);
        }

        synchronized (sessions)
        {
          sessions.clear();
        }
      }

      sessions = null;
      sessionListener = null;
      stopTransport();
      super.tearDown();
    }
    finally
    {
      removeDynamicPackagesFromGlobalRegistry(globallyRegisteredPackageURIs);
      globallyRegisteredPackageURIs = null;
    }
  }

  protected CDOSessionConfiguration getTestSessionConfiguration()
  {
    return (CDOSessionConfiguration)getTestProperty(PROP_TEST_SESSION_CONFIGURATION);
  }

  protected IPasswordCredentialsProvider getTestCredentialsProvider()
  {
    return (IPasswordCredentialsProvider)getTestProperty(PROP_TEST_CREDENTIALS_PROVIDER);
  }

  protected CDOFetchRuleManager getTestFetchRuleManager()
  {
    return (CDOFetchRuleManager)getTestProperty(PROP_TEST_FETCH_RULE_MANAGER);
  }

  protected abstract CDOSessionConfiguration createSessionConfiguration(String repositoryName);

  protected void configureSession(CDOSession session)
  {
    final File lobCache = getCurrentTest().createTempFolder("lobs_" + new Date().getTime() + "_", ".tmp");
    session.options().setLobCache(new CDOLobStoreImpl(lobCache));
    session.addListener(new LifecycleEventAdapter()
    {
      @Override
      protected void onDeactivated(ILifecycle lifecycle)
      {
        IOUtil.delete(lobCache);
      }
    });
  }

  private Set<String> captureGlobalPackageRegistry()
  {
    return new HashSet<String>(GLOBAL_REGISTRY.keySet());
  }

  private void removeDynamicPackagesFromGlobalRegistry(Set<String> urisToProtect)
  {
    for (String uri : GLOBAL_REGISTRY.keySet().toArray(new String[GLOBAL_REGISTRY.size()]))
    {
      if (urisToProtect == null || !urisToProtect.contains(uri))
      {
        Object object = GLOBAL_REGISTRY.get(uri); // Prevent resolving descriptors
        if (isDynamicPackage(object))
        {
          GLOBAL_REGISTRY.remove(uri);
        }
      }
    }
  }

  private boolean isDynamicPackage(Object object)
  {
    return object != null && object.getClass() == EPackageImpl.class;
  }

  /**
   * @author Eike Stepper
   * @deprecated Not yet supported.
   */
  @Deprecated
  public static final class Embedded extends SessionConfig
  {
    public static final String NAME = "Embedded";

    public static final Embedded INSTANCE = new Embedded();

    private static final long serialVersionUID = 1L;

    public Embedded()
    {
      super(NAME);
    }

    public void initCapabilities(Set<String> capabilities)
    {
      capabilities.add(CAPABILITY_EMBEDDED);
    }

    public String getURIProtocol()
    {
      throw new UnsupportedOperationException();
    }

    public String getURIPrefix()
    {
      throw new UnsupportedOperationException();
    }

    @Override
    protected CDOSessionConfiguration createSessionConfiguration(String repositoryName)
    {
      IRepository repository = getCurrentTest().getRepository(repositoryName);

      org.eclipse.emf.cdo.server.embedded.CDOSessionConfiguration configuration = CDOServerUtil
          .createSessionConfiguration();
      configuration.setRepository(repository);
      return configuration;
    }
  }

  /**
   * @author Eike Stepper
   */
  public static abstract class Net4j extends SessionConfig
  {
    private static final long serialVersionUID = 1L;

    private transient CDOViewProvider viewProvider;

    public Net4j(String name)
    {
      super(name);
    }

    public void initCapabilities(Set<String> capabilities)
    {
      capabilities.add(CAPABILITY_NET4J);
    }

    public String getTransportType()
    {
      return getName().toLowerCase();
    }

    @Override
    public void startTransport() throws Exception
    {
      IAcceptor acceptor = getAcceptor();
      LifecycleUtil.activate(acceptor);

      IConnector connector = getConnector();
      LifecycleUtil.activate(connector);
    }

    @Override
    public void stopTransport() throws Exception
    {
      ConfigTest currentTest = getCurrentTest();

      try
      {
        if (currentTest.hasClientContainer())
        {
          IConnector connector = getConnector();
          connector.close();
        }
      }
      catch (Exception ex)
      {
        IOUtil.print(ex);
      }

      try
      {
        if (currentTest.hasServerContainer())
        {
          IAcceptor acceptor = getAcceptor();
          acceptor.close();
        }
      }
      catch (Exception ex)
      {
        IOUtil.print(ex);
      }
    }

    public String getURIProtocol()
    {
      return "cdo.net4j." + getTransportType();
    }

    @Override
    protected CDOSessionConfiguration createSessionConfiguration(String repositoryName)
    {
      CDONet4jSessionConfiguration configuration = CDONet4jUtil.createNet4jSessionConfiguration();
      configuration.setConnector(getConnector());
      configuration.setRepositoryName(repositoryName);
      configuration.setRevisionManager(new TestRevisionManager());
      return configuration;
    }

    @Override
    protected void configureSession(CDOSession session)
    {
      super.configureSession(session);
      ((org.eclipse.emf.cdo.net4j.CDONet4jSession)session).options().getNet4jProtocol().setTimeout(-1);
    }

    protected abstract CDOViewProvider createViewProvider(IManagedContainer container);

    @Override
    public void setUp() throws Exception
    {
      super.setUp();

      viewProvider = createViewProvider(getCurrentTest().getClientContainer());
      if (viewProvider != null)
      {
        CDOViewProviderRegistry.INSTANCE.addViewProvider(viewProvider);
      }
    }

    @Override
    public void tearDown() throws Exception
    {
      if (viewProvider != null)
      {
        CDOViewProviderRegistry.INSTANCE.removeViewProvider(viewProvider);
      }

      super.tearDown();
    }

    public abstract IAcceptor getAcceptor();

    public abstract IConnector getConnector();

    /**
     * @author Eike Stepper
     */
    public static final class TCP extends SessionConfig.Net4j
    {
      public static final String NAME = "TCP";

      public static final TCP INSTANCE = new TCP();

      public static final String CONNECTOR_HOST = "localhost";

      private static final long serialVersionUID = 1L;

      public TCP()
      {
        super(NAME);
      }

      @Override
      public void initCapabilities(Set<String> capabilities)
      {
        super.initCapabilities(capabilities);
        capabilities.add(CAPABILITY_NET4J_TCP);
      }

      public String getURIPrefix()
      {
        return getURIProtocol() + "://" + CONNECTOR_HOST;
      }

      @Override
      public IAcceptor getAcceptor()
      {
        return TCPUtil.getAcceptor(getCurrentTest().getServerContainer(), null);
      }

      @Override
      public IConnector getConnector()
      {
        return TCPUtil.getConnector(getCurrentTest().getClientContainer(), CONNECTOR_HOST);
      }

      @Override
      public void setUp() throws Exception
      {
        super.setUp();

        IManagedContainer clientContainer = getCurrentTest().getClientContainer();
        TCPUtil.prepareContainer(clientContainer);

        IManagedContainer serverContainer = getCurrentTest().getServerContainer();
        if (serverContainer != clientContainer)
        {
          TCPUtil.prepareContainer(serverContainer);
        }
      }

      @Override
      protected CDOViewProvider createViewProvider(final IManagedContainer container)
      {
        return new CDONet4jViewProvider.TCP()
        {
          @Override
          protected IManagedContainer getContainer()
          {
            return container;
          }
        };
      }
    }

    /**
     * @author Teerawat Chaiyakijpichet (No Magic Asia Ltd.)
     */
    public static final class SSL extends SessionConfig.Net4j
    {
      public static final String NAME = "SSL";

      public static final SSL INSTANCE = new SSL();

      public static final String CONNECTOR_HOST = "localhost";

      private static final long serialVersionUID = 1L;

      public SSL()
      {
        super(NAME);
      }

      @Override
      public void initCapabilities(Set<String> capabilities)
      {
        super.initCapabilities(capabilities);
        capabilities.add(CAPABILITY_NET4J_SSL);
      }

      public String getURIPrefix()
      {
        return getURIProtocol() + "://" + CONNECTOR_HOST;
      }

      @Override
      public IAcceptor getAcceptor()
      {
        return SSLUtil.getAcceptor(getCurrentTest().getServerContainer(), null);
      }

      @Override
      public IConnector getConnector()
      {
        return SSLUtil.getConnector(getCurrentTest().getClientContainer(), CONNECTOR_HOST);
      }

      @Override
      public void setUp() throws Exception
      {
        super.setUp();

        IManagedContainer clientContainer = getCurrentTest().getClientContainer();
        SSLUtil.prepareContainer(clientContainer);

        IManagedContainer serverContainer = getCurrentTest().getServerContainer();
        if (serverContainer != clientContainer)
        {
          SSLUtil.prepareContainer(serverContainer);
        }
      }

      @Override
      protected CDOViewProvider createViewProvider(final IManagedContainer container)
      {
        return new CDONet4jViewProvider.SSL()
        {
          @Override
          protected IManagedContainer getContainer()
          {
            return container;
          }
        };
      }
    }

    /**
     * @author Eike Stepper
     */
    public static final class JVM extends SessionConfig.Net4j
    {
      public static final String NAME = "JVM";

      public static final JVM INSTANCE = new JVM();

      public static final String ACCEPTOR_NAME = "default";

      private static final long serialVersionUID = 1L;

      public JVM()
      {
        super(NAME);
      }

      @Override
      public void initCapabilities(Set<String> capabilities)
      {
        super.initCapabilities(capabilities);
        capabilities.add(CAPABILITY_NET4J_JVM);
      }

      public String getURIPrefix()
      {
        return getURIProtocol() + "://" + ACCEPTOR_NAME;
      }

      @Override
      public IAcceptor getAcceptor()
      {
        return JVMUtil.getAcceptor(getCurrentTest().getServerContainer(), ACCEPTOR_NAME);
      }

      @Override
      public IConnector getConnector()
      {
        return JVMUtil.getConnector(getCurrentTest().getClientContainer(), ACCEPTOR_NAME);
      }

      @Override
      public void setUp() throws Exception
      {
        super.setUp();
        JVMUtil.prepareContainer(getCurrentTest().getClientContainer());
        JVMUtil.prepareContainer(getCurrentTest().getServerContainer());
      }

      @Override
      public boolean isValid(Set<IConfig> configs)
      {
        return !configs.contains(ContainerConfig.Separated.INSTANCE);
      }

      @Override
      protected CDOViewProvider createViewProvider(final IManagedContainer container)
      {
        return new CDONet4jViewProvider.JVM()
        {
          @Override
          protected IManagedContainer getContainer()
          {
            return container;
          }
        };
      }
    }
  }
}

Back to the top