Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8125a4f56a86eb5d8662c3d5526baca1dd8893f6 (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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
/*
 * Copyright (c) 2012, 2013, 2015 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.examples.client.offline.nodes;

import org.eclipse.emf.cdo.common.CDOCommonRepository.State;
import org.eclipse.emf.cdo.common.branch.CDOBranch;
import org.eclipse.emf.cdo.common.revision.CDORevisionCache;
import org.eclipse.emf.cdo.common.revision.CDORevisionUtil;
import org.eclipse.emf.cdo.examples.client.offline.Application;
import org.eclipse.emf.cdo.examples.company.CompanyPackage;
import org.eclipse.emf.cdo.net4j.CDONet4jSessionConfiguration;
import org.eclipse.emf.cdo.net4j.CDONet4jUtil;
import org.eclipse.emf.cdo.server.CDOServerBrowser;
import org.eclipse.emf.cdo.server.CDOServerUtil;
import org.eclipse.emf.cdo.server.IRepository;
import org.eclipse.emf.cdo.server.IRepositorySynchronizer;
import org.eclipse.emf.cdo.server.IStore;
import org.eclipse.emf.cdo.server.ISynchronizableRepository;
import org.eclipse.emf.cdo.server.db.CDODBUtil;
import org.eclipse.emf.cdo.server.db.mapping.IMappingStrategy;
import org.eclipse.emf.cdo.server.net4j.FailoverAgent;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.session.CDOSessionConfigurationFactory;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.view.CDOAdapterPolicy;
import org.eclipse.emf.cdo.view.CDOViewTargetChangedEvent;

import org.eclipse.net4j.Net4jUtil;
import org.eclipse.net4j.acceptor.IAcceptor;
import org.eclipse.net4j.connector.IConnector;
import org.eclipse.net4j.db.IDBAdapter;
import org.eclipse.net4j.db.IDBConnectionProvider;
import org.eclipse.net4j.db.h2.H2Adapter;
import org.eclipse.net4j.util.container.IPluginContainer;
import org.eclipse.net4j.util.container.SetContainer;
import org.eclipse.net4j.util.event.IEvent;
import org.eclipse.net4j.util.event.IListener;
import org.eclipse.net4j.util.event.ThrowableEventAdapter;
import org.eclipse.net4j.util.lifecycle.LifecycleUtil;

import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.wb.swt.ExampleResourceManager;

import org.h2.jdbcx.JdbcDataSource;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * @author Eike Stepper
 */
public abstract class NodeType extends SetContainer<Node>implements IElement
{
  public static final String TYPE_PROPERTY = "Type";

  public static final String NAME_PROPERTY = "Name";

  public static final String PORT_PROPERTY = "Port";

  public static final String SERVER_PROPERTY = "Server";

  public static final String MONITOR_PROPERTY = "Monitor";

  public static final String BRANCH_PROPERTY = "Branch";

  public static final String BROWSER_PROPERTY = "BrowserPort";

  private static final String REPOSITORY_NAME = "repository";

  private final NodeManager manager;

  private final List<Property> properties = new ArrayList<Property>();

  private final Properties settings = new Properties();

  private final Composite[] detailsControls = { null, null };

  public NodeType(NodeManager manager)
  {
    super(Node.class);
    this.manager = manager;
    addProperty(new Property.Entry(this, NAME_PROPERTY));
    settings.setProperty(TYPE_PROPERTY, getTypeName());
    activate();
  }

  public NodeManager getManager()
  {
    return manager;
  }

  public List<Property> getProperties()
  {
    return properties;
  }

  public Properties getSettings()
  {
    return settings;
  }

  public void showSettings()
  {
    showSettings(null);
  }

  public void showSettings(Node node)
  {
    Composite composite = getDetailsControl(node);
    Control[] children = composite.getChildren();

    Properties settings = node == null ? this.settings : node.getSettings();
    for (Property property : properties)
    {
      String name = property.getName();
      Control control = getControl(children, name);
      if (control != null)
      {
        String value = settings.getProperty(name, "");
        property.showSetting(control, value);
      }
    }
  }

  public void configureWindow(IWorkbenchWindowConfigurer configurer)
  {
    configurer.setInitialSize(new Point(800, 500));
    configurer.setTitle(Application.NODE.getName());
    configurer.setShowCoolBar(false);
    configurer.setShowMenuBar(false);
    configurer.setShowStatusLine(false);
  }

  private Control getControl(Control[] children, String name)
  {
    for (Control control : children)
    {
      if (name.equals(control.getData("name")))
      {
        return control;
      }
    }

    return null;
  }

  public Image getImage()
  {
    return ExampleResourceManager.getPluginImage(Application.PLUGIN_ID, "icons/Folder.gif");
  }

  public Image getInstanceImage()
  {
    return ExampleResourceManager.getPluginImage(Application.PLUGIN_ID, "icons/" + getTypeName() + ".gif");
  }

  public String getTypeName()
  {
    String name = getClass().getSimpleName();
    int lastDot = name.lastIndexOf('.');
    if (lastDot != -1)
    {
      name = name.substring(lastDot + 1);
    }

    return name;
  }

  public Composite getDetailsControl()
  {
    return getDetailsControl(null);
  }

  public Composite getDetailsControl(Node node)
  {
    if (node == null)
    {
      return detailsControls[0];
    }

    return detailsControls[1];
  }

  public void start(Node node)
  {
    IRepository repository = createRepository(node);
    node.setObject(IRepository.class, repository);

    IAcceptor acceptor = createAcceptor(node);
    node.setObject(IAcceptor.class, acceptor);

    String browserPort = node.getSetting(BROWSER_PROPERTY);
    if (browserPort != null && browserPort.length() != 0)
    {
      CDOServerBrowser browser = (CDOServerBrowser)IPluginContainer.INSTANCE
          .getElement("org.eclipse.emf.cdo.server.browsers", "default", browserPort);
      node.setObject(CDOServerBrowser.class, browser);
    }
  }

  public void stop(Node node)
  {
    CDOServerBrowser browser = node.getObject(CDOServerBrowser.class);
    LifecycleUtil.deactivate(browser);

    IAcceptor acceptor = node.getObject(IAcceptor.class);
    LifecycleUtil.deactivate(acceptor);

    IRepository repository = node.getObject(IRepository.class);
    LifecycleUtil.deactivate(repository);
  }

  public Button createUI(final NodeManagerDialog dialog, int kind)
  {
    Composite composite = new Composite(dialog.getDetails(), SWT.NONE);
    composite.setLayout(new GridLayout(2, false));

    for (Property property : properties)
    {
      property.createField(dialog, composite);
    }

    new Label(composite, SWT.NONE);

    Button button = new Button(composite, SWT.PUSH);
    button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    if (kind == NodeManagerDialog.NODE)
    {
      button.setText("Delete");
      button.addSelectionListener(new SelectionAdapter()
      {
        @Override
        public void widgetSelected(SelectionEvent e)
        {
          dialog.onDelete(NodeType.this);
        }
      });

      composite.setEnabled(false);
      detailsControls[1] = composite;
    }
    else
    {
      button.setText("Create");
      button.addSelectionListener(new SelectionAdapter()
      {
        @Override
        public void widgetSelected(SelectionEvent e)
        {
          dialog.onCreate(NodeType.this);
        }
      });

      detailsControls[0] = composite;
    }

    return button;
  }

  protected void addProperty(Property property)
  {
    properties.add(property);
  }

  @Override
  protected Node[] sortElements(Node[] array)
  {
    Arrays.sort(array);
    return array;
  }

  protected IRepository createRepository(Node node)
  {
    JdbcDataSource dataSource = new JdbcDataSource();
    dataSource.setURL("jdbc:h2:" + node.getFolder() + "/db/repository");

    IMappingStrategy mappingStrategy = CDODBUtil.createHorizontalMappingStrategy(true, true);
    IDBAdapter dbAdapter = new H2Adapter();
    IDBConnectionProvider dbConnectionProvider = dbAdapter.createConnectionProvider(dataSource);
    IStore store = CDODBUtil.createStore(mappingStrategy, dbAdapter, dbConnectionProvider);

    Map<String, String> props = new HashMap<String, String>();
    props.put(IRepository.Props.OVERRIDE_UUID, REPOSITORY_NAME);
    props.put(IRepository.Props.SUPPORTING_AUDITS, "true");
    props.put(IRepository.Props.SUPPORTING_BRANCHES, "true");
    props.put(IRepository.Props.ID_GENERATION_LOCATION, "CLIENT");

    IRepository repository = createRepository(node, store, props);
    repository.setInitialPackages(CompanyPackage.eINSTANCE);
    activateRepository(repository);
    return repository;
  }

  protected IRepository createRepository(Node node, IStore store, Map<String, String> props)
  {
    return CDOServerUtil.createRepository(REPOSITORY_NAME, store, props);
  }

  protected void activateRepository(IRepository repository)
  {
    // Don't do this with failover participants!
    CDOServerUtil.addRepository(IPluginContainer.INSTANCE, repository);
  }

  protected IAcceptor createAcceptor(Node node)
  {
    String description = "0.0.0.0:" + node.getSetting(PORT_PROPERTY);
    return (IAcceptor)IPluginContainer.INSTANCE.getElement("org.eclipse.net4j.acceptors", "tcp", description);
  }

  /**
   * @author Eike Stepper
   */
  public static abstract class Property
  {
    private final NodeType container;

    private final String name;

    public Property(NodeType container, String name)
    {
      this.container = container;
      this.name = name;
    }

    public NodeType getContainer()
    {
      return container;
    }

    public String getName()
    {
      return name;
    }

    public abstract void createField(NodeManagerDialog dialog, Composite parent);

    public abstract void showSetting(Control control, String value);

    /**
     * @author Eike Stepper
     */
    public static abstract class Labelled extends Property
    {
      public Labelled(NodeType container, String name)
      {
        super(container, name);
      }

      @Override
      public void createField(NodeManagerDialog dialog, Composite parent)
      {
        String name = getName();
        Label label = new Label(parent, SWT.NONE);

        label.setText(name + ":");
        label.setLayoutData(new GridData(GridData.END));

        Control control = createControl(dialog, parent);
        control.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        control.setData("name", name);
      }

      protected abstract Control createControl(NodeManagerDialog dialog, Composite parent);
    }

    /**
     * @author Eike Stepper
     */
    public static class Entry extends Labelled
    {
      public Entry(NodeType container, String name)
      {
        super(container, name);
      }

      @Override
      protected Control createControl(final NodeManagerDialog dialog, Composite parent)
      {
        final Text text = new Text(parent, SWT.BORDER);
        text.addModifyListener(new ModifyListener()
        {
          public void modifyText(ModifyEvent e)
          {
            if (!dialog.isUpdatingDetails())
            {
              dialog.onModify(Entry.this, text.getText());
            }
          }
        });

        return text;
      }

      @Override
      public void showSetting(Control control, String value)
      {
        ((Text)control).setText(value);
      }
    }

    /**
     * @author Eike Stepper
     */
    public static class Selection extends Labelled
    {
      private final Class<? extends NodeType> type;

      public Selection(NodeType container, String name, Class<? extends NodeType> type)
      {
        super(container, name);
        this.type = type;
      }

      public Class<? extends NodeType> getType()
      {
        return type;
      }

      @Override
      protected Control createControl(final NodeManagerDialog dialog, Composite parent)
      {
        final ComboViewer comboViewer = new ComboViewer(parent, SWT.NONE);
        comboViewer.setLabelProvider(new LabelProvider());
        comboViewer.setContentProvider(new IStructuredContentProvider()
        {
          public Object[] getElements(Object inputElement)
          {
            List<Node> result = new ArrayList<Node>();
            Node[] nodes = getContainer().getManager().getNodes();
            for (Node node : nodes)
            {
              if (type.isAssignableFrom(node.getType().getClass()))
              {
                result.add(node);
              }
            }

            Collections.sort(result);
            return result.toArray(new Node[result.size()]);
          }

          public void inputChanged(Viewer viewer, Object oldInput, Object newInput)
          {
          }

          public void dispose()
          {
          }
        });

        comboViewer.addSelectionChangedListener(new ISelectionChangedListener()
        {
          public void selectionChanged(SelectionChangedEvent event)
          {
            if (!dialog.isUpdatingDetails())
            {
              IStructuredSelection selection = (IStructuredSelection)comboViewer.getSelection();
              Node node = (Node)selection.getFirstElement();
              if (node != null)
              {
                dialog.onModify(Selection.this, node.getName());
              }
            }
          }
        });

        comboViewer.setInput(new Object());

        Combo combo = comboViewer.getCombo();
        combo.setData("viewer", comboViewer);
        return combo;
      }

      @Override
      public void showSetting(Control control, String value)
      {
        ComboViewer comboViewer = (ComboViewer)control.getData("viewer");
        comboViewer.refresh(true);

        Node node = getContainer().getManager().getNode(value);
        if (node != null)
        {
          comboViewer.setSelection(new StructuredSelection(node));
        }
        else
        {
          comboViewer.setSelection(StructuredSelection.EMPTY);
        }
      }
    }
  }

  /**
   * @author Eike Stepper
   */
  public static class Client extends NodeType
  {
    public Client(NodeManager manager)
    {
      super(manager);
      addProperty(new Property.Selection(this, SERVER_PROPERTY, Server.class));
      addProperty(new Property.Entry(this, BROWSER_PROPERTY));
    }

    @Override
    public void start(final Node node)
    {
      super.start(node);

      final CDOSession session = (CDOSession)IPluginContainer.INSTANCE.getElement("org.eclipse.emf.cdo.sessions", "cdo",
          "jvm://example?repositoryName=" + REPOSITORY_NAME);
      session.getPackageRegistry().putEPackage(CompanyPackage.eINSTANCE);

      node.setObject(CDOSession.class, session);

      if (session.getRepositoryInfo().getState() == State.INITIAL)
      {
        session.addListener(new IListener()
        {
          public void notifyEvent(IEvent event)
          {
            if (session.getRepositoryInfo().getState() != State.INITIAL)
            {
              session.removeListener(this);
              createTransaction(node);
            }
          }
        });
      }
      else
      {
        createTransaction(node);
      }
    }

    @Override
    public void stop(Node node)
    {
      CDOSession session = node.getObject(CDOSession.class);
      LifecycleUtil.deactivate(session);

      super.stop(node);
    }

    @Override
    protected IRepository createRepository(Node node, IStore store, Map<String, String> props)
    {
      String serverName = node.getSetting(SERVER_PROPERTY);
      final Node serverNode = getManager().getNode(serverName);
      if (serverNode == null)
      {
        throw new IllegalStateException("Server not found: " + serverName);
      }

      CDOSessionConfigurationFactory factory = new CDOSessionConfigurationFactory()
      {
        public CDONet4jSessionConfiguration createSessionConfiguration()
        {
          String serverAddress = "localhost:" + serverNode.getSetting(PORT_PROPERTY);

          CDONet4jSessionConfiguration configuration;
          if (serverNode.getType() instanceof FailoverMonitor)
          {
            configuration = CDONet4jUtil.createFailoverSessionConfiguration(serverAddress, serverNode.getName());
          }
          else
          {
            IConnector connector = Net4jUtil.getConnector(IPluginContainer.INSTANCE, "tcp", serverAddress);

            configuration = CDONet4jUtil.createNet4jSessionConfiguration();
            configuration.setConnector(connector);
            configuration.setRepositoryName(REPOSITORY_NAME);
          }

          configuration.setRevisionManager(CDORevisionUtil.createRevisionManager(CDORevisionCache.NOOP));
          return configuration;
        }
      };

      IRepositorySynchronizer synchronizer = CDOServerUtil.createRepositorySynchronizer(factory);
      synchronizer.setRetryInterval(2);
      synchronizer.setMaxRecommits(10);
      synchronizer.setRecommitInterval(2);

      return CDOServerUtil.createOfflineClone(REPOSITORY_NAME, store, props, synchronizer);
    }

    @Override
    protected IAcceptor createAcceptor(Node node)
    {
      return (IAcceptor)IPluginContainer.INSTANCE.getElement("org.eclipse.net4j.acceptors", "jvm", "example");
    }

    protected void createTransaction(final Node node)
    {
      CDOSession session = node.getObject(CDOSession.class);

      String branchPath = node.getSettings().getProperty(BRANCH_PROPERTY, CDOBranch.MAIN_BRANCH_NAME);
      CDOBranch branch = session.getBranchManager().getBranch(branchPath);
      if (branch == null)
      {
        branch = session.getBranchManager().getMainBranch();
        node.getSettings().setProperty(BRANCH_PROPERTY, branch.getPathName());
        getManager().saveNode(node);
      }

      CDOTransaction transaction = session.openTransaction(branch);
      transaction.options().addChangeSubscriptionPolicy(CDOAdapterPolicy.ALL);
      transaction.addListener(new IListener()
      {
        public void notifyEvent(IEvent event)
        {
          if (event instanceof CDOViewTargetChangedEvent)
          {
            CDOViewTargetChangedEvent e = (CDOViewTargetChangedEvent)event;
            String branchPath = e.getBranchPoint().getBranch().getPathName();
            node.getSettings().setProperty(BRANCH_PROPERTY, branchPath);
            getManager().saveNode(node);
          }
        }
      });

      node.setObject(CDOTransaction.class, transaction);
    }

    @Override
    public String toString()
    {
      return "Clients";
    }
  }

  /**
   * @author Eike Stepper
   */
  public static abstract class Server extends NodeType
  {
    public Server(NodeManager manager)
    {
      super(manager);
      addProperty(new Property.Entry(this, PORT_PROPERTY));
    }

    @Override
    public void configureWindow(IWorkbenchWindowConfigurer configurer)
    {
      super.configureWindow(configurer);
      configurer.setInitialSize(new Point(500, 350));
    }

    public void setConnectedToNetwork(Node node, boolean on)
    {
      if (on)
      {
        IAcceptor acceptor = createAcceptor(node);
        node.setObject(IAcceptor.class, acceptor);
      }
      else
      {
        IAcceptor acceptor = node.getObject(IAcceptor.class);
        LifecycleUtil.deactivate(acceptor);
      }
    }
  }

  /**
   * @author Eike Stepper
   */
  public static abstract class Repository extends Server
  {
    public Repository(NodeManager manager)
    {
      super(manager);
    }

    @Override
    public String toString()
    {
      return "Normal Repositories";
    }
  }

  /**
   * @author Eike Stepper
   */
  public static class NormalRepository extends Repository
  {
    public NormalRepository(NodeManager manager)
    {
      super(manager);
      addProperty(new Property.Entry(this, BROWSER_PROPERTY));
    }

    @Override
    public String toString()
    {
      return "Normal Repositories";
    }
  }

  /**
   * @author Eike Stepper
   */
  public static class FailoverRepository extends Repository
  {
    private FailoverAgent agent;

    public FailoverRepository(NodeManager manager)
    {
      super(manager);
      addProperty(new Property.Selection(this, MONITOR_PROPERTY, FailoverMonitor.class));
      addProperty(new Property.Entry(this, BROWSER_PROPERTY));
    }

    @Override
    protected IRepository createRepository(Node node, IStore store, Map<String, String> props)
    {
      String monitorName = node.getSetting(MONITOR_PROPERTY);
      Node monitorNode = getManager().getNode(monitorName);
      if (monitorNode == null)
      {
        throw new IllegalStateException("Monitor not found: " + monitorName);
      }

      final String monitorAddress = "localhost:" + monitorNode.getSetting(PORT_PROPERTY);

      ISynchronizableRepository repository = CDOServerUtil.createFailoverParticipant(REPOSITORY_NAME, store, props);

      agent = new FailoverAgent()
      {
        @Override
        protected IRepositorySynchronizer createRepositorySynchronizer()
        {
          IRepositorySynchronizer synchronizer = super.createRepositorySynchronizer();
          synchronizer.addListener(ThrowableEventAdapter.ToPrintStream.CONSOLE);
          return synchronizer;
        }

        @Override
        protected CDONet4jSessionConfiguration createSessionConfiguration(String connectorDescription,
            String repositoryName)
        {
          IConnector connector = Net4jUtil.getConnector(IPluginContainer.INSTANCE, "tcp", connectorDescription);

          CDONet4jSessionConfiguration configuration = CDONet4jUtil.createNet4jSessionConfiguration();
          configuration.setConnector(connector);
          configuration.setRepositoryName(repositoryName);
          configuration.setRevisionManager(CDORevisionUtil.createRevisionManager(CDORevisionCache.NOOP));
          return configuration;
        }
      };

      agent.setMonitorConnector(Net4jUtil.getConnector(IPluginContainer.INSTANCE, "tcp", monitorAddress));
      agent.setConnectorDescription("localhost:" + node.getSetting(PORT_PROPERTY));
      agent.setRepository(repository);
      agent.setGroup(monitorNode.getName());
      agent.setRate(1500L);
      agent.setTimeout(3000L);

      return repository;
    }

    @Override
    protected void activateRepository(IRepository repository)
    {
      agent.activate();
    }

    @Override
    public String toString()
    {
      return "Failover Repositories";
    }
  }

  /**
   * @author Eike Stepper
   */
  public static class FailoverMonitor extends Server
  {
    public FailoverMonitor(NodeManager manager)
    {
      super(manager);
    }

    @Override
    public void start(Node node)
    {
      super.start(node);

      org.eclipse.emf.cdo.server.net4j.FailoverMonitor monitor = (org.eclipse.emf.cdo.server.net4j.FailoverMonitor)IPluginContainer.INSTANCE
          .getElement(org.eclipse.emf.cdo.server.net4j.FailoverMonitor.PRODUCT_GROUP,
              org.eclipse.emf.cdo.server.net4j.FailoverMonitor.Factory.TYPE, node.getName());
      node.setObject(org.eclipse.emf.cdo.server.net4j.FailoverMonitor.class, monitor);
    }

    @Override
    public void stop(Node node)
    {
      org.eclipse.emf.cdo.server.net4j.FailoverMonitor monitor = node
          .getObject(org.eclipse.emf.cdo.server.net4j.FailoverMonitor.class);
      LifecycleUtil.deactivate(monitor);

      super.stop(node);
    }

    @Override
    protected IRepository createRepository(Node node)
    {
      return null;
    }

    @Override
    public String toString()
    {
      return "Failover Monitors";
    }
  }
}

Back to the top