Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEike Stepper2012-06-11 11:21:27 +0000
committerEike Stepper2012-06-11 11:21:27 +0000
commit0423cb0a781011898af15805b9e57dd5af503954 (patch)
tree8d26511e311a08e22e68ea3a761a8af6a68c9b98 /plugins/org.eclipse.emf.cdo.examples/src
parent9aa5e39806741becc197d51710632b81365d8eea (diff)
downloadcdo-0423cb0a781011898af15805b9e57dd5af503954.tar.gz
cdo-0423cb0a781011898af15805b9e57dd5af503954.tar.xz
cdo-0423cb0a781011898af15805b9e57dd5af503954.zip
[382225] Provide CDO example installers
https://bugs.eclipse.org/bugs/show_bug.cgi?id=382225
Diffstat (limited to 'plugins/org.eclipse.emf.cdo.examples/src')
-rw-r--r--plugins/org.eclipse.emf.cdo.examples/src/org/eclipse/emf/cdo/examples/server/offline/AbstractOfflineExampleServer.java479
-rw-r--r--plugins/org.eclipse.emf.cdo.examples/src/org/eclipse/emf/cdo/examples/server/offline/OfflineExampleClient.java468
-rw-r--r--plugins/org.eclipse.emf.cdo.examples/src/org/eclipse/emf/cdo/examples/server/offline/OfflineExampleClone.java14
-rw-r--r--plugins/org.eclipse.emf.cdo.examples/src/org/eclipse/emf/cdo/examples/server/offline/OfflineExampleMaster.java98
-rw-r--r--plugins/org.eclipse.emf.cdo.examples/src/org/eclipse/emf/cdo/examples/server/offline/OfflineExampleUtil.java98
5 files changed, 578 insertions, 579 deletions
diff --git a/plugins/org.eclipse.emf.cdo.examples/src/org/eclipse/emf/cdo/examples/server/offline/AbstractOfflineExampleServer.java b/plugins/org.eclipse.emf.cdo.examples/src/org/eclipse/emf/cdo/examples/server/offline/AbstractOfflineExampleServer.java
index d65cfc583d..7e7e314f1e 100644
--- a/plugins/org.eclipse.emf.cdo.examples/src/org/eclipse/emf/cdo/examples/server/offline/AbstractOfflineExampleServer.java
+++ b/plugins/org.eclipse.emf.cdo.examples/src/org/eclipse/emf/cdo/examples/server/offline/AbstractOfflineExampleServer.java
@@ -1,240 +1,239 @@
-/*
- * 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.CDOCommonRepository.Type;
-import org.eclipse.emf.cdo.common.util.RepositoryStateChangedEvent;
-import org.eclipse.emf.cdo.common.util.RepositoryTypeChangedEvent;
-import org.eclipse.emf.cdo.examples.company.CompanyPackage;
-import org.eclipse.emf.cdo.server.CDOServerUtil;
-import org.eclipse.emf.cdo.server.IRepository;
-import org.eclipse.emf.cdo.server.ISession;
-import org.eclipse.emf.cdo.server.IStore;
-import org.eclipse.emf.cdo.server.db.CDODBUtil;
-import org.eclipse.emf.cdo.server.db.mapping.IMappingStrategy;
-
-import org.eclipse.net4j.acceptor.IAcceptor;
-import org.eclipse.net4j.db.DBUtil;
-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.ContainerEventAdapter;
-import org.eclipse.net4j.util.container.IContainer;
-import org.eclipse.net4j.util.container.IManagedContainer;
-import org.eclipse.net4j.util.event.IEvent;
-import org.eclipse.net4j.util.event.IListener;
-import org.eclipse.net4j.util.lifecycle.LifecycleUtil;
-
-import org.h2.jdbcx.JdbcDataSource;
-
-import java.io.BufferedReader;
-import java.io.InputStreamReader;
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- * @author Eike Stepper
- * @author Martin Fluegge
- * @since 4.0
- */
-public abstract class AbstractOfflineExampleServer
-{
- public static final String TRANSPORT_TYPE = "tcp";
-
- protected int port;
-
- protected String name;
-
- protected transient IManagedContainer container;
-
- protected transient IRepository repository;
-
- protected transient IAcceptor acceptor;
-
- static
- {
- // OMPlatform.INSTANCE.setDebugging(true);
- // OMPlatform.INSTANCE.addTraceHandler(PrintTraceHandler.CONSOLE);
- // OMPlatform.INSTANCE.addLogHandler(PrintLogHandler.CONSOLE);
-
- CompanyPackage.eINSTANCE.getClass(); // Register EPackage in standalone
- }
-
- public AbstractOfflineExampleServer(String name, int port, int dbBrowserPort)
- {
- this.name = name;
- this.port = port;
-
- container = OfflineExampleUtil.createContainer();
- container.getElement("org.eclipse.emf.cdo.server.browsers", "default", dbBrowserPort + ""); //$NON-NLS-1$ //$NON-NLS-2$
- }
-
- public void init()
- {
- IStore store = createStore();
- Map<String, String> props = createProperties();
-
- repository = createRepository(store, props);
- CDOServerUtil.addRepository(container, repository);
-
- repository.addListener(new IListener()
- {
- public void notifyEvent(IEvent event)
- {
- if (event instanceof RepositoryTypeChangedEvent)
- {
- RepositoryTypeChangedEvent e = (RepositoryTypeChangedEvent)event;
- System.out.println("Type changed to " + e.getNewType());
- }
- else if (event instanceof RepositoryStateChangedEvent)
- {
- RepositoryStateChangedEvent e = (RepositoryStateChangedEvent)event;
- System.out.println("State changed to " + e.getNewState());
- }
- }
- });
-
- repository.getSessionManager().addListener(new ContainerEventAdapter<ISession>()
- {
- @Override
- protected void onAdded(IContainer<ISession> sessionManager, ISession session)
- {
- System.out.println("Registered " + session);
- }
-
- @Override
- protected void onRemoved(IContainer<ISession> sessionManager, ISession session)
- {
- System.out.println("Unregistered " + session);
- }
- });
-
- connect();
- }
-
- public void run() throws Exception
- {
- for (;;)
- {
- System.out.println();
- System.out.println("Enter a command:");
- showMenu();
- System.out.println();
-
- String command = new BufferedReader(new InputStreamReader(System.in)).readLine();
- if (handleCommand(command))
- {
- break;
- }
- }
- }
-
- public void done()
- {
- LifecycleUtil.deactivate(acceptor);
- LifecycleUtil.deactivate(repository);
- container.deactivate();
- }
-
- protected void showMenu()
- {
- System.out.println("0 - exit");
- System.out.println("1 - connect repository to network");
- System.out.println("2 - disconnect repository from network");
- System.out.println("3 - dump repository infos");
- }
-
- protected boolean handleCommand(String command)
- {
- if ("1".equals(command))
- {
- if (acceptor == null)
- {
- connect();
- }
- else
- {
- System.out.println("Already connected");
- }
- }
- else if ("2".equals(command))
- {
- if (acceptor != null)
- {
- disconnect();
- }
- else
- {
- System.out.println("Already disconnected");
- }
- }
- else if ("3".equals(command))
- {
- System.out.println();
- System.out.println(repository.getName() + ": " + repository.getType()
- + (repository.getType() == Type.BACKUP ? "|" + repository.getState() : ""));
- }
- else if ("0".equals(command))
- {
- System.out.println("Exiting...");
- return true;
- }
- else
- {
- System.out.println("Unknown command");
- }
-
- return false;
- }
-
- protected void connect()
- {
- System.out.println("Connecting to network...");
- acceptor = createAcceptor();
- System.out.println("Connected");
- }
-
- protected IAcceptor createAcceptor()
- {
- return (IAcceptor)container.getElement("org.eclipse.net4j.acceptors", AbstractOfflineExampleServer.TRANSPORT_TYPE,
- "0.0.0.0:" + port);
- }
-
- protected void disconnect()
- {
- System.out.println("Disconnecting from network...");
- LifecycleUtil.deactivate(acceptor);
- acceptor = null;
- System.out.println("Disconnected");
- }
-
- protected IStore createStore()
- {
- JdbcDataSource dataSource = new JdbcDataSource();
- dataSource.setURL("jdbc:h2:_database/" + name);
-
- IMappingStrategy mappingStrategy = CDODBUtil.createHorizontalMappingStrategy(true, true);
- IDBAdapter dbAdapter = new H2Adapter();
- IDBConnectionProvider dbConnectionProvider = DBUtil.createConnectionProvider(dataSource);
- return CDODBUtil.createStore(mappingStrategy, dbAdapter, dbConnectionProvider);
- }
-
- protected Map<String, String> createProperties()
- {
- Map<String, String> props = new HashMap<String, String>();
- props.put(IRepository.Props.OVERRIDE_UUID, name);
- props.put(IRepository.Props.SUPPORTING_AUDITS, "true");
- props.put(IRepository.Props.SUPPORTING_BRANCHES, "true");
- return props;
- }
-
- protected abstract IRepository createRepository(IStore store, Map<String, String> props);
-}
+/*
+ * 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.CDOCommonRepository.Type;
+import org.eclipse.emf.cdo.common.util.RepositoryStateChangedEvent;
+import org.eclipse.emf.cdo.common.util.RepositoryTypeChangedEvent;
+import org.eclipse.emf.cdo.server.CDOServerUtil;
+import org.eclipse.emf.cdo.server.IRepository;
+import org.eclipse.emf.cdo.server.ISession;
+import org.eclipse.emf.cdo.server.IStore;
+import org.eclipse.emf.cdo.server.db.CDODBUtil;
+import org.eclipse.emf.cdo.server.db.mapping.IMappingStrategy;
+
+import org.eclipse.net4j.acceptor.IAcceptor;
+import org.eclipse.net4j.db.DBUtil;
+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.ContainerEventAdapter;
+import org.eclipse.net4j.util.container.IContainer;
+import org.eclipse.net4j.util.container.IManagedContainer;
+import org.eclipse.net4j.util.event.IEvent;
+import org.eclipse.net4j.util.event.IListener;
+import org.eclipse.net4j.util.lifecycle.LifecycleUtil;
+
+import org.h2.jdbcx.JdbcDataSource;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author Eike Stepper
+ * @author Martin Fluegge
+ * @since 4.0
+ */
+public abstract class AbstractOfflineExampleServer
+{
+ public static final String TRANSPORT_TYPE = "tcp";
+
+ protected int port;
+
+ protected String name;
+
+ protected transient IManagedContainer container;
+
+ protected transient IRepository repository;
+
+ protected transient IAcceptor acceptor;
+
+ static
+ {
+ // OMPlatform.INSTANCE.setDebugging(true);
+ // OMPlatform.INSTANCE.addTraceHandler(PrintTraceHandler.CONSOLE);
+ // OMPlatform.INSTANCE.addLogHandler(PrintLogHandler.CONSOLE);
+
+ // CompanyPackage.eINSTANCE.getClass(); // Register EPackage in standalone
+ }
+
+ public AbstractOfflineExampleServer(String name, int port, int dbBrowserPort)
+ {
+ this.name = name;
+ this.port = port;
+
+ container = OfflineExampleUtil.createContainer();
+ container.getElement("org.eclipse.emf.cdo.server.browsers", "default", dbBrowserPort + ""); //$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void init()
+ {
+ IStore store = createStore();
+ Map<String, String> props = createProperties();
+
+ repository = createRepository(store, props);
+ CDOServerUtil.addRepository(container, repository);
+
+ repository.addListener(new IListener()
+ {
+ public void notifyEvent(IEvent event)
+ {
+ if (event instanceof RepositoryTypeChangedEvent)
+ {
+ RepositoryTypeChangedEvent e = (RepositoryTypeChangedEvent)event;
+ System.out.println("Type changed to " + e.getNewType());
+ }
+ else if (event instanceof RepositoryStateChangedEvent)
+ {
+ RepositoryStateChangedEvent e = (RepositoryStateChangedEvent)event;
+ System.out.println("State changed to " + e.getNewState());
+ }
+ }
+ });
+
+ repository.getSessionManager().addListener(new ContainerEventAdapter<ISession>()
+ {
+ @Override
+ protected void onAdded(IContainer<ISession> sessionManager, ISession session)
+ {
+ System.out.println("Registered " + session);
+ }
+
+ @Override
+ protected void onRemoved(IContainer<ISession> sessionManager, ISession session)
+ {
+ System.out.println("Unregistered " + session);
+ }
+ });
+
+ connect();
+ }
+
+ public void run() throws Exception
+ {
+ for (;;)
+ {
+ System.out.println();
+ System.out.println("Enter a command:");
+ showMenu();
+ System.out.println();
+
+ String command = new BufferedReader(new InputStreamReader(System.in)).readLine();
+ if (handleCommand(command))
+ {
+ break;
+ }
+ }
+ }
+
+ public void done()
+ {
+ LifecycleUtil.deactivate(acceptor);
+ LifecycleUtil.deactivate(repository);
+ container.deactivate();
+ }
+
+ protected void showMenu()
+ {
+ System.out.println("0 - exit");
+ System.out.println("1 - connect repository to network");
+ System.out.println("2 - disconnect repository from network");
+ System.out.println("3 - dump repository infos");
+ }
+
+ protected boolean handleCommand(String command)
+ {
+ if ("1".equals(command))
+ {
+ if (acceptor == null)
+ {
+ connect();
+ }
+ else
+ {
+ System.out.println("Already connected");
+ }
+ }
+ else if ("2".equals(command))
+ {
+ if (acceptor != null)
+ {
+ disconnect();
+ }
+ else
+ {
+ System.out.println("Already disconnected");
+ }
+ }
+ else if ("3".equals(command))
+ {
+ System.out.println();
+ System.out.println(repository.getName() + ": " + repository.getType()
+ + (repository.getType() == Type.BACKUP ? "|" + repository.getState() : ""));
+ }
+ else if ("0".equals(command))
+ {
+ System.out.println("Exiting...");
+ return true;
+ }
+ else
+ {
+ System.out.println("Unknown command");
+ }
+
+ return false;
+ }
+
+ protected void connect()
+ {
+ System.out.println("Connecting to network...");
+ acceptor = createAcceptor();
+ System.out.println("Connected");
+ }
+
+ protected IAcceptor createAcceptor()
+ {
+ return (IAcceptor)container.getElement("org.eclipse.net4j.acceptors", AbstractOfflineExampleServer.TRANSPORT_TYPE,
+ "0.0.0.0:" + port);
+ }
+
+ protected void disconnect()
+ {
+ System.out.println("Disconnecting from network...");
+ LifecycleUtil.deactivate(acceptor);
+ acceptor = null;
+ System.out.println("Disconnected");
+ }
+
+ protected IStore createStore()
+ {
+ JdbcDataSource dataSource = new JdbcDataSource();
+ dataSource.setURL("jdbc:h2:database/" + name);
+
+ IMappingStrategy mappingStrategy = CDODBUtil.createHorizontalMappingStrategy(true, true);
+ IDBAdapter dbAdapter = new H2Adapter();
+ IDBConnectionProvider dbConnectionProvider = DBUtil.createConnectionProvider(dataSource);
+ return CDODBUtil.createStore(mappingStrategy, dbAdapter, dbConnectionProvider);
+ }
+
+ protected Map<String, String> createProperties()
+ {
+ Map<String, String> props = new HashMap<String, String>();
+ props.put(IRepository.Props.OVERRIDE_UUID, name);
+ props.put(IRepository.Props.SUPPORTING_AUDITS, "true");
+ props.put(IRepository.Props.SUPPORTING_BRANCHES, "true");
+ return props;
+ }
+
+ protected abstract IRepository createRepository(IStore store, Map<String, String> props);
+}
diff --git a/plugins/org.eclipse.emf.cdo.examples/src/org/eclipse/emf/cdo/examples/server/offline/OfflineExampleClient.java b/plugins/org.eclipse.emf.cdo.examples/src/org/eclipse/emf/cdo/examples/server/offline/OfflineExampleClient.java
index 24b0b8f2d0..912b47a48c 100644
--- a/plugins/org.eclipse.emf.cdo.examples/src/org/eclipse/emf/cdo/examples/server/offline/OfflineExampleClient.java
+++ b/plugins/org.eclipse.emf.cdo.examples/src/org/eclipse/emf/cdo/examples/server/offline/OfflineExampleClient.java
@@ -1,234 +1,234 @@
-/*
- * 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.CDOObject;
-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.CDOUtil;
-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.net4j.util.lifecycle.LifecycleUtil;
-
-import org.eclipse.emf.common.util.EList;
-import org.eclipse.emf.ecore.EObject;
-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 void lockObject(CDOTransaction tx)
- {
- EList<EObject> contents = tx.getOrCreateResource("/r1").getContents();
- int size = contents.size();
- if (size < 1)
- {
- System.out.println("There are no objects; can't lock anything.");
- }
-
- System.out.println("Locking last object");
- CDOObject firstObject = CDOUtil.getCDOObject(contents.get(size - 1));
- firstObject.cdoWriteLock().lock();
- System.out.println("Locked last object");
- }
-
- private static void unlockObject(CDOTransaction tx)
- {
- EList<EObject> contents = tx.getOrCreateResource("/r1").getContents();
- int size = contents.size();
- if (size < 1)
- {
- System.out.println("There are no objects; can't lock anything.");
- }
-
- System.out.println("Unlocking last object");
- CDOObject firstObject = CDOUtil.getCDOObject(contents.get(size - 1));
- firstObject.cdoWriteLock().unlock();
- System.out.println("Unlocked last object");
- }
-
- private static void createBranch(CDOTransaction tx)
- {
- CDOBranch subBranch = tx.getBranch().createBranch("sub.1");
- tx.setBranch(subBranch);
- }
-
- 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();
- tx.enableDurableLocking();
- createSessionListener(session, autoMerging);
-
- for (;;)
- {
- System.out.println();
- System.out.println("Enter a command:");
- System.out.println("0 - exit");
- System.out.println("1 - add an object to the repository");
- System.out.println("2 - lock the last object in the repository");
- System.out.println("3 - unlock the last object in the repository");
- System.out.println("4 - create a branch");
-
- String command = new BufferedReader(new InputStreamReader(System.in)).readLine();
- if ("0".equals(command))
- {
- break;
- }
-
- if ("1".equals(command))
- {
- addObject(tx);
- }
- else if ("2".equals(command))
- {
- lockObject(tx);
- }
- else if ("3".equals(command))
- {
- unlockObject(tx);
- }
- else if ("4".equals(command))
- {
- createBranch(tx);
- }
- }
-
- session.close();
- LifecycleUtil.deactivate(container);
- }
-}
+/*
+ * 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.CDOObject;
+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.CDOUtil;
+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.net4j.util.lifecycle.LifecycleUtil;
+
+import org.eclipse.emf.common.util.EList;
+import org.eclipse.emf.ecore.EObject;
+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 void lockObject(CDOTransaction tx)
+ {
+ EList<EObject> contents = tx.getOrCreateResource("/r1").getContents();
+ int size = contents.size();
+ if (size < 1)
+ {
+ System.out.println("There are no objects; can't lock anything.");
+ }
+
+ System.out.println("Locking last object");
+ CDOObject firstObject = CDOUtil.getCDOObject(contents.get(size - 1));
+ firstObject.cdoWriteLock().lock();
+ System.out.println("Locked last object");
+ }
+
+ private static void unlockObject(CDOTransaction tx)
+ {
+ EList<EObject> contents = tx.getOrCreateResource("/r1").getContents();
+ int size = contents.size();
+ if (size < 1)
+ {
+ System.out.println("There are no objects; can't lock anything.");
+ }
+
+ System.out.println("Unlocking last object");
+ CDOObject firstObject = CDOUtil.getCDOObject(contents.get(size - 1));
+ firstObject.cdoWriteLock().unlock();
+ System.out.println("Unlocked last object");
+ }
+
+ private static void createBranch(CDOTransaction tx)
+ {
+ CDOBranch subBranch = tx.getBranch().createBranch("sub.1");
+ tx.setBranch(subBranch);
+ }
+
+ 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(OfflineExampleUtil.CLONE_NAME);
+
+ CDONet4jSession session = configuration.openNet4jSession();
+ CDORepositoryInfo repositoryInfo = session.getRepositoryInfo();
+ System.out.println("Connected to " + repositoryInfo.getName());
+
+ tx = session.openTransaction();
+ tx.enableDurableLocking();
+ createSessionListener(session, autoMerging);
+
+ for (;;)
+ {
+ System.out.println();
+ System.out.println("Enter a command:");
+ System.out.println("0 - exit");
+ System.out.println("1 - add an object to the repository");
+ System.out.println("2 - lock the last object in the repository");
+ System.out.println("3 - unlock the last object in the repository");
+ System.out.println("4 - create a branch");
+
+ String command = new BufferedReader(new InputStreamReader(System.in)).readLine();
+ if ("0".equals(command))
+ {
+ break;
+ }
+
+ if ("1".equals(command))
+ {
+ addObject(tx);
+ }
+ else if ("2".equals(command))
+ {
+ lockObject(tx);
+ }
+ else if ("3".equals(command))
+ {
+ unlockObject(tx);
+ }
+ else if ("4".equals(command))
+ {
+ createBranch(tx);
+ }
+ }
+
+ session.close();
+ LifecycleUtil.deactivate(container);
+ }
+}
diff --git a/plugins/org.eclipse.emf.cdo.examples/src/org/eclipse/emf/cdo/examples/server/offline/OfflineExampleClone.java b/plugins/org.eclipse.emf.cdo.examples/src/org/eclipse/emf/cdo/examples/server/offline/OfflineExampleClone.java
index d889d72f34..b5a58fb95a 100644
--- a/plugins/org.eclipse.emf.cdo.examples/src/org/eclipse/emf/cdo/examples/server/offline/OfflineExampleClone.java
+++ b/plugins/org.eclipse.emf.cdo.examples/src/org/eclipse/emf/cdo/examples/server/offline/OfflineExampleClone.java
@@ -38,22 +38,18 @@ import java.util.Map;
*/
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);
+ super(OfflineExampleUtil.CLONE_NAME, OfflineExampleUtil.CLONE_PORT, DB_BROWSER_PORT);
}
@Override
protected IRepository createRepository(IStore store, Map<String, String> props)
{
- IRepositorySynchronizer synchronizer = createRepositorySynchronizer("localhost:" + OfflineExampleMaster.PORT,
- OfflineExampleMaster.NAME);
+ IRepositorySynchronizer synchronizer = createRepositorySynchronizer("localhost:" + OfflineExampleUtil.MASTER_PORT,
+ OfflineExampleUtil.MASTER_NAME);
return CDOServerUtil.createOfflineClone(name, store, props, synchronizer);
}
@@ -82,7 +78,7 @@ public class OfflineExampleClone extends AbstractOfflineExampleServer
{
public CDONet4jSessionConfiguration createSessionConfiguration()
{
- IConnector connector = createConnector("localhost:" + OfflineExampleMaster.PORT);
+ IConnector connector = createConnector("localhost:" + OfflineExampleUtil.MASTER_PORT);
return OfflineExampleClone.this.createSessionConfiguration(connector, repositoryName);
}
};
@@ -121,7 +117,7 @@ public class OfflineExampleClone extends AbstractOfflineExampleServer
protected IConnector createConnector(String description)
{
- return Net4jUtil.getConnector(container, AbstractOfflineExampleServer.TRANSPORT_TYPE, description);
+ return Net4jUtil.getConnector(container, TRANSPORT_TYPE, description);
}
public static void main(String[] args) throws Exception
diff --git a/plugins/org.eclipse.emf.cdo.examples/src/org/eclipse/emf/cdo/examples/server/offline/OfflineExampleMaster.java b/plugins/org.eclipse.emf.cdo.examples/src/org/eclipse/emf/cdo/examples/server/offline/OfflineExampleMaster.java
index 64ec8b94a5..544fd83576 100644
--- a/plugins/org.eclipse.emf.cdo.examples/src/org/eclipse/emf/cdo/examples/server/offline/OfflineExampleMaster.java
+++ b/plugins/org.eclipse.emf.cdo.examples/src/org/eclipse/emf/cdo/examples/server/offline/OfflineExampleMaster.java
@@ -1,51 +1,47 @@
-/*
- * 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.server.CDOServerUtil;
-import org.eclipse.emf.cdo.server.IRepository;
-import org.eclipse.emf.cdo.server.IStore;
-
-import java.util.Map;
-
-/**
- * @author Eike Stepper
- * @author Martin Fluegge
- * @since 4.0
- */
-public class OfflineExampleMaster extends AbstractOfflineExampleServer
-{
- public static final String NAME = "master";
-
- public static final int PORT = 2036;
-
- private static final int BROWSER_PORT = 7777;
-
- public OfflineExampleMaster()
- {
- super(NAME, PORT, BROWSER_PORT);
- }
-
- @Override
- protected IRepository createRepository(IStore store, Map<String, String> props)
- {
- return CDOServerUtil.createRepository(name, store, props);
- }
-
- public static void main(String[] args) throws Exception
- {
- System.out.println("Master repository starting...");
- OfflineExampleMaster example = new OfflineExampleMaster();
- example.init();
- example.run();
- example.done();
- }
-}
+/*
+ * 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.server.CDOServerUtil;
+import org.eclipse.emf.cdo.server.IRepository;
+import org.eclipse.emf.cdo.server.IStore;
+
+import java.util.Map;
+
+/**
+ * @author Eike Stepper
+ * @author Martin Fluegge
+ * @since 4.0
+ */
+public class OfflineExampleMaster extends AbstractOfflineExampleServer
+{
+ private static final int BROWSER_PORT = 7777;
+
+ public OfflineExampleMaster()
+ {
+ super(OfflineExampleUtil.MASTER_NAME, OfflineExampleUtil.MASTER_PORT, BROWSER_PORT);
+ }
+
+ @Override
+ protected IRepository createRepository(IStore store, Map<String, String> props)
+ {
+ return CDOServerUtil.createRepository(name, store, props);
+ }
+
+ public static void main(String[] args) throws Exception
+ {
+ System.out.println("Master repository starting...");
+ OfflineExampleMaster example = new OfflineExampleMaster();
+ example.init();
+ example.run();
+ example.done();
+ }
+}
diff --git a/plugins/org.eclipse.emf.cdo.examples/src/org/eclipse/emf/cdo/examples/server/offline/OfflineExampleUtil.java b/plugins/org.eclipse.emf.cdo.examples/src/org/eclipse/emf/cdo/examples/server/offline/OfflineExampleUtil.java
index 5aaaaa917f..4405d542c8 100644
--- a/plugins/org.eclipse.emf.cdo.examples/src/org/eclipse/emf/cdo/examples/server/offline/OfflineExampleUtil.java
+++ b/plugins/org.eclipse.emf.cdo.examples/src/org/eclipse/emf/cdo/examples/server/offline/OfflineExampleUtil.java
@@ -1,45 +1,53 @@
-/*
- * 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.net4j.CDONet4jUtil;
-import org.eclipse.emf.cdo.server.CDOServerBrowser;
-import org.eclipse.emf.cdo.server.db.CDODBUtil;
-import org.eclipse.emf.cdo.server.net4j.CDONet4jServerUtil;
-
-import org.eclipse.net4j.Net4jUtil;
-import org.eclipse.net4j.tcp.TCPUtil;
-import org.eclipse.net4j.util.container.ContainerUtil;
-import org.eclipse.net4j.util.container.IManagedContainer;
-
-/**
- * @author Martin Fluegge
- * @since 4.0
- */
-public class OfflineExampleUtil
-{
- public static IManagedContainer createContainer()
- {
- IManagedContainer container = ContainerUtil.createContainer();
-
- Net4jUtil.prepareContainer(container); // Register Net4j factories
- TCPUtil.prepareContainer(container); // Register TCP factories
-
- CDONet4jUtil.prepareContainer(container); // Register CDO client factories
- CDONet4jServerUtil.prepareContainer(container); // Register CDO server factories
-
- container.registerFactory(new CDOServerBrowser.ContainerBased.Factory(container));
- CDODBUtil.prepareContainer(container); // Register DBBrowserPage.Factory
-
- container.activate();
- return container;
- }
-}
+/*
+ * 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.net4j.CDONet4jUtil;
+import org.eclipse.emf.cdo.server.CDOServerBrowser;
+import org.eclipse.emf.cdo.server.db.CDODBUtil;
+import org.eclipse.emf.cdo.server.net4j.CDONet4jServerUtil;
+
+import org.eclipse.net4j.Net4jUtil;
+import org.eclipse.net4j.tcp.TCPUtil;
+import org.eclipse.net4j.util.container.ContainerUtil;
+import org.eclipse.net4j.util.container.IManagedContainer;
+
+/**
+ * @author Martin Fluegge
+ * @since 4.0
+ */
+public class OfflineExampleUtil
+{
+ public static final String MASTER_NAME = "master";
+
+ public static final int MASTER_PORT = 2036;
+
+ public final static String CLONE_NAME = "clone";
+
+ public static final int CLONE_PORT = 2037;
+
+ public static IManagedContainer createContainer()
+ {
+ IManagedContainer container = ContainerUtil.createContainer();
+
+ Net4jUtil.prepareContainer(container); // Register Net4j factories
+ TCPUtil.prepareContainer(container); // Register TCP factories
+
+ CDONet4jUtil.prepareContainer(container); // Register CDO client factories
+ CDONet4jServerUtil.prepareContainer(container); // Register CDO server factories
+
+ container.registerFactory(new CDOServerBrowser.ContainerBased.Factory(container));
+ CDODBUtil.prepareContainer(container); // Register DBBrowserPage.Factory
+
+ container.activate();
+ return container;
+ }
+}

Back to the top