diff options
Diffstat (limited to 'plugins/org.eclipse.emf.cdo.examples.installer/examples/org.eclipse.emf.cdo.examples.clone/src/org/eclipse/emf/cdo/examples')
3 files changed, 423 insertions, 0 deletions
diff --git a/plugins/org.eclipse.emf.cdo.examples.installer/examples/org.eclipse.emf.cdo.examples.clone/src/org/eclipse/emf/cdo/examples/server/offline/AbstractOfflineExampleServer.java b/plugins/org.eclipse.emf.cdo.examples.installer/examples/org.eclipse.emf.cdo.examples.clone/src/org/eclipse/emf/cdo/examples/server/offline/AbstractOfflineExampleServer.java new file mode 100644 index 0000000000..7e7e314f1e --- /dev/null +++ b/plugins/org.eclipse.emf.cdo.examples.installer/examples/org.eclipse.emf.cdo.examples.clone/src/org/eclipse/emf/cdo/examples/server/offline/AbstractOfflineExampleServer.java @@ -0,0 +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.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.installer/examples/org.eclipse.emf.cdo.examples.clone/src/org/eclipse/emf/cdo/examples/server/offline/OfflineExampleClone.java b/plugins/org.eclipse.emf.cdo.examples.installer/examples/org.eclipse.emf.cdo.examples.clone/src/org/eclipse/emf/cdo/examples/server/offline/OfflineExampleClone.java new file mode 100644 index 0000000000..b5a58fb95a --- /dev/null +++ b/plugins/org.eclipse.emf.cdo.examples.installer/examples/org.eclipse.emf.cdo.examples.clone/src/org/eclipse/emf/cdo/examples/server/offline/OfflineExampleClone.java @@ -0,0 +1,131 @@ +/* + * 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.CDONet4jSessionConfiguration; +import org.eclipse.emf.cdo.net4j.CDONet4jUtil; +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 +{ + private static final int DB_BROWSER_PORT = 7778; + + public OfflineExampleClone() + { + super(OfflineExampleUtil.CLONE_NAME, OfflineExampleUtil.CLONE_PORT, DB_BROWSER_PORT); + } + + @Override + protected IRepository createRepository(IStore store, Map<String, String> props) + { + IRepositorySynchronizer synchronizer = createRepositorySynchronizer("localhost:" + OfflineExampleUtil.MASTER_PORT, + OfflineExampleUtil.MASTER_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.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:" + OfflineExampleUtil.MASTER_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, 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(); + } +} diff --git a/plugins/org.eclipse.emf.cdo.examples.installer/examples/org.eclipse.emf.cdo.examples.clone/src/org/eclipse/emf/cdo/examples/server/offline/OfflineExampleUtil.java b/plugins/org.eclipse.emf.cdo.examples.installer/examples/org.eclipse.emf.cdo.examples.clone/src/org/eclipse/emf/cdo/examples/server/offline/OfflineExampleUtil.java new file mode 100644 index 0000000000..4405d542c8 --- /dev/null +++ b/plugins/org.eclipse.emf.cdo.examples.installer/examples/org.eclipse.emf.cdo.examples.clone/src/org/eclipse/emf/cdo/examples/server/offline/OfflineExampleUtil.java @@ -0,0 +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 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; + } +} |