Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fa61815194a574839eed3f6e12bb2993b738352c (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
/*
 * 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.examples.server.offline;

import org.eclipse.emf.cdo.common.revision.CDORevisionCache;
import org.eclipse.emf.cdo.common.revision.CDORevisionUtil;
import org.eclipse.emf.cdo.net4j.CDONet4jUtil;
import org.eclipse.emf.cdo.net4j.CDONet4jSessionConfiguration;
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.session.CDOSession;
import org.eclipse.emf.cdo.session.CDOSessionConfiguration.SessionOpenedEvent;
import org.eclipse.emf.cdo.session.CDOSessionConfigurationFactory;

import org.eclipse.net4j.Net4jUtil;
import org.eclipse.net4j.connector.IConnector;
import org.eclipse.net4j.util.event.IEvent;
import org.eclipse.net4j.util.event.IListener;
import org.eclipse.net4j.util.lifecycle.ILifecycle;
import org.eclipse.net4j.util.lifecycle.LifecycleEventAdapter;

import java.util.Map;

/**
 * @author Eike Stepper
 * @author Martin Fluegge
 * @since 4.0
 */
public class OfflineExampleClone extends AbstractOfflineExampleServer
{
  public final static String NAME = "clone";

  private static final int PORT = 2037;

  private static final int DB_BROWSER_PORT = 7778;

  public OfflineExampleClone()
  {
    super(NAME, PORT, DB_BROWSER_PORT);
  }

  @Override
  protected IRepository createRepository(IStore store, Map<String, String> props)
  {
    IRepositorySynchronizer synchronizer = createRepositorySynchronizer("localhost:" + OfflineExampleMaster.PORT,
        OfflineExampleMaster.NAME);
    return CDOServerUtil.createOfflineClone(name, store, props, synchronizer);
  }

  /**
   * Creates a repository synchronizer which connects to the master repository to synchronize between master and client.
   */
  protected IRepositorySynchronizer createRepositorySynchronizer(String connectorDescription, String repositoryName)
  {
    CDOSessionConfigurationFactory factory = createSessionConfigurationFactory(connectorDescription, repositoryName);

    IRepositorySynchronizer synchronizer = CDOServerUtil.createRepositorySynchronizer(factory);
    synchronizer.setRetryInterval(2);
    synchronizer.setRawReplication(true);
    synchronizer.setMaxRecommits(10);
    synchronizer.setRecommitInterval(2);
    return synchronizer;
  }

  /**
   * creates a CDOSessionConfigurationFactory for the offline clone. It instantiates a connection to the master
   * repository.
   */
  protected CDOSessionConfigurationFactory createSessionConfigurationFactory(final String connectorDescription,
      final String repositoryName)
  {
    return new CDOSessionConfigurationFactory()
    {
      public CDONet4jSessionConfiguration createSessionConfiguration()
      {
        IConnector connector = createConnector("localhost:" + OfflineExampleMaster.PORT);
        return OfflineExampleClone.this.createSessionConfiguration(connector, repositoryName);
      }
    };
  }

  protected CDONet4jSessionConfiguration createSessionConfiguration(IConnector connector, String repositoryName)
  {
    CDONet4jSessionConfiguration configuration = CDONet4jUtil.createNet4jSessionConfiguration();
    configuration.setConnector(connector);
    configuration.setRepositoryName(repositoryName);
    configuration.setRevisionManager(CDORevisionUtil.createRevisionManager(CDORevisionCache.NOOP));
    configuration.addListener(new IListener()
    {
      public void notifyEvent(IEvent event)
      {
        if (event instanceof SessionOpenedEvent)
        {
          SessionOpenedEvent e = (SessionOpenedEvent)event;
          CDOSession session = e.getOpenedSession();
          System.out.println("Opened " + session);

          session.addListener(new LifecycleEventAdapter()
          {
            @Override
            protected void onAboutToDeactivate(ILifecycle lifecycle)
            {
              System.out.println("Closing " + lifecycle);
            }
          });
        }
      }
    });

    return configuration;
  }

  protected IConnector createConnector(String description)
  {
    return Net4jUtil.getConnector(container, AbstractOfflineExampleServer.TRANSPORT_TYPE, description);
  }

  public static void main(String[] args) throws Exception
  {
    System.out.println("Clone repository starting...");
    OfflineExampleClone example = new OfflineExampleClone();
    example.init();
    example.run();
    example.done();
  }
}

Back to the top