Skip to main content
summaryrefslogtreecommitdiffstats
blob: a8a448486d8e85a454362e458aeac529b84e889d (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
/**
 * Copyright (c) 2004 - 2011 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.CDOCommonRepository;
import org.eclipse.emf.cdo.common.CDOCommonRepository.State;
import org.eclipse.emf.cdo.common.branch.CDOBranch;
import org.eclipse.emf.cdo.common.commit.CDOCommitInfo;
import org.eclipse.emf.cdo.examples.company.CompanyFactory;
import org.eclipse.emf.cdo.examples.company.Customer;
import org.eclipse.emf.cdo.net4j.CDONet4jSession;
import org.eclipse.emf.cdo.net4j.CDONet4jSessionConfiguration;
import org.eclipse.emf.cdo.net4j.CDONet4jUtil;
import org.eclipse.emf.cdo.session.CDORepositoryInfo;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CommitException;

import org.eclipse.net4j.Net4jUtil;
import org.eclipse.net4j.connector.IConnector;
import org.eclipse.net4j.util.container.IManagedContainer;
import org.eclipse.net4j.util.event.IEvent;
import org.eclipse.net4j.util.event.IListener;

import org.eclipse.emf.spi.cdo.DefaultCDOMerger;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * Following console parameters are allowed: <br>
 * -automerge provides an automatic merging of the offline changes to the master repository
 * 
 * @author Eike Stepper
 * @author Martin Fluegge
 * @since 4.0
 */
public class OfflineExampleClient
{
  public static final int PORT = 2037;

  private static CDOTransaction tx;

  private static void addObject(CDOTransaction tx)
  {
    try
    {
      Customer customer = CompanyFactory.eINSTANCE.createCustomer();
      tx.getOrCreateResource("/r1").getContents().add(customer);

      System.out.println("Committing an object to " + tx.getBranch().getPathName());
      CDOCommitInfo commitInfo = tx.commit();
      CDOBranch branch = commitInfo.getBranch();
      System.out.println("Committed an object to  " + branch.getPathName());
      tx.setBranch(branch);
    }
    catch (CommitException x)
    {
      throw new RuntimeException(x);
    }
  }

  private static boolean isAutoMerge(String[] args)
  {
    for (int i = 0; i < args.length; i++)
    {
      if (args[i].equals("-automerge"))
      {
        return true;
      }
    }

    return false;
  }

  private static void createSessionListener(final CDONet4jSession session, final boolean autoMerging)
  {
    session.addListener(new IListener()
    {
      private boolean wasOffline;

      public void notifyEvent(IEvent event)
      {
        if (event instanceof CDOCommonRepository.StateChangedEvent)
        {
          CDOCommonRepository.StateChangedEvent e = (CDOCommonRepository.StateChangedEvent)event;
          State newState = e.getNewState();
          System.out.println("State changed to " + newState);
          if (autoMerging)
          {
            merge(session, newState);
          }
        }
      }

      private void merge(final CDONet4jSession session, State newState)
      {
        if (newState == State.ONLINE && wasOffline)
        {
          try
          {
            CDOTransaction newTransaction = session.openTransaction(session.getBranchManager().getMainBranch());

            // CDOBranch mainBranch = session.getBranchManager().getMainBranch();
            newTransaction.merge(tx.getBranch().getHead(), new DefaultCDOMerger.PerFeature.ManyValued());

            newTransaction.commit();
            tx.close();
            tx = newTransaction;
          }
          catch (CommitException ex)
          {
            ex.printStackTrace();
          }
          finally
          {
            wasOffline = false;
          }
        }
        else if (newState == State.OFFLINE)
        {
          wasOffline = true;
        }
      }
    });
  }

  public static void main(String[] args) throws Exception
  {
    boolean autoMerging = isAutoMerge(args);

    System.out.println("Client starting...");
    IManagedContainer container = OfflineExampleUtil.createContainer();
    IConnector connector = Net4jUtil.getConnector(container, AbstractOfflineExampleServer.TRANSPORT_TYPE, "localhost:"
        + PORT);

    CDONet4jSessionConfiguration configuration = CDONet4jUtil.createNet4jSessionConfiguration();
    configuration.setConnector(connector);
    configuration.setRepositoryName(OfflineExampleClone.NAME);

    CDONet4jSession session = configuration.openNet4jSession();
    CDORepositoryInfo repositoryInfo = session.getRepositoryInfo();
    System.out.println("Connected to " + repositoryInfo.getName());

    tx = session.openTransaction();
    createSessionListener(session, autoMerging);

    for (;;)
    {
      new BufferedReader(new InputStreamReader(System.in)).readLine();
      addObject(tx);
    }
  }
}

Back to the top