Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "CDO/Embedded"

< CDO
(Libraries)
(Code)
Line 40: Line 40:
 
import org.eclipse.emf.cdo.session.CDOSession;
 
import org.eclipse.emf.cdo.session.CDOSession;
 
import org.eclipse.emf.cdo.transaction.CDOTransaction;
 
import org.eclipse.emf.cdo.transaction.CDOTransaction;
 +
import org.eclipse.emf.cdo.util.CommitException;
 
import org.eclipse.emf.ecore.EClass;
 
import org.eclipse.emf.ecore.EClass;
 
import org.eclipse.emf.ecore.EObject;
 
import org.eclipse.emf.ecore.EObject;
Line 82: Line 83:
  
 
// TODO update URL
 
// TODO update URL
JdbcDataSource dataSource = getJdbcDataSource("jdbc:h2:" + DEFAULT_REPOSITORY_NAME);
+
JdbcDataSource dataSource = getJdbcDataSource("jdbc:h2:/Users/steve/Temporary/" + DEFAULT_REPOSITORY_NAME);
  
 
IStore cdoStore = getStore(dataSource);
 
IStore cdoStore = getStore(dataSource);
Line 107: Line 108:
 
CDOResource cdoResource = transaction.getOrCreateResource("/resource"); //$NON-NLS-1$
 
CDOResource cdoResource = transaction.getOrCreateResource("/resource"); //$NON-NLS-1$
  
EObject eObject = EcoreUtil.create(eClass); // DO NOT DO THIS BEFORE THE DYNAMIC PACKAGE IS REGISTERED WITH THE SESSION PACKAGE REGISTRY!!!
+
EObject eObject = EcoreUtil.create(eClass);
 
cdoResource.getContents().add(eObject);
 
cdoResource.getContents().add(eObject);
transaction.commit();
+
try {
 +
transaction.commit();
 +
} catch (CommitException e) {
 +
// TODO Auto-generated catch block
 +
e.printStackTrace();
 +
}
 
 
 
// Cleanup
 
// Cleanup
Line 137: Line 143:
 
Map<String, String> props = new HashMap<String, String>();
 
Map<String, String> props = new HashMap<String, String>();
 
props.put(IRepository.Props.OVERRIDE_UUID, DEFAULT_REPOSITORY_NAME);
 
props.put(IRepository.Props.OVERRIDE_UUID, DEFAULT_REPOSITORY_NAME);
props.put(IRepository.Props.SUPPORTING_AUDITS, Boolean.toString(true));
+
    props.put(IRepository.Props.SUPPORTING_AUDITS, "true");
props.put(IRepository.Props.SUPPORTING_BRANCHES, Boolean.toString(true));
+
    props.put(IRepository.Props.SUPPORTING_BRANCHES, "false");
props.put(IRepository.Props.CURRENT_LRU_CAPACITY, "100000");
+
// props.put(IRepository.Props.CURRENT_LRU_CAPACITY, "100000");
props.put(IRepository.Props.REVISED_LRU_CAPACITY, "10000");
+
// props.put(IRepository.Props.REVISED_LRU_CAPACITY, "10000");
 
return CDOServerUtil.createRepository(DEFAULT_REPOSITORY_NAME, store, props);
 
return CDOServerUtil.createRepository(DEFAULT_REPOSITORY_NAME, store, props);
 
}
 
}
Line 149: Line 155:
 
JVMUtil.prepareContainer(container);
 
JVMUtil.prepareContainer(container);
 
CDONet4jUtil.prepareContainer(container);
 
CDONet4jUtil.prepareContainer(container);
                CDONet4jServerUtil.prepareContainer(container);
+
CDONet4jServerUtil.prepareContainer(container);
 
container.activate();
 
container.activate();
 
return container;
 
return container;
 
}
 
}
 +
 
 
 
protected CDOSession getSession(IConnector connector) {
 
protected CDOSession getSession(IConnector connector) {

Revision as of 15:59, 11 November 2010

This example demonstrates the following techniques:

  • Embedding a CDOSession with a Net4j IJVMConnector and IJVMAcceptor
  • Using native dynamic models with CDO
  • Setting up a standalone IRepository with an IManagedContainer
  • Setting up a standalone CDOSession with an IManagedContainer

This example is in development ... please support :)

Based on this discussion.

This sample will eventually use "EmbeddedClientSessionProtocol" as soon as it is available in CDO 3.0.

Code

/*******************************************************************************
 * Copyright (c) 2010 Stephan Zehrer 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:
 *    Stephan Zehrer - initial API and implementation
 *******************************************************************************/
 
import java.util.HashMap;
import java.util.Map;
 
import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.net4j.CDONet4jUtil;
import org.eclipse.emf.cdo.net4j.CDOSessionConfiguration;
import org.eclipse.emf.cdo.server.CDOServerUtil;
import org.eclipse.emf.cdo.server.IRepository;
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.emf.cdo.server.net4j.CDONet4jServerUtil;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CommitException;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EcoreFactory;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.net4j.Net4jUtil;
import org.eclipse.net4j.acceptor.IAcceptor;
import org.eclipse.net4j.connector.IConnector;
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.jvm.JVMUtil;
import org.eclipse.net4j.util.container.ContainerUtil;
import org.eclipse.net4j.util.container.IManagedContainer;
import org.eclipse.net4j.util.om.OMPlatform;
import org.eclipse.net4j.util.om.log.PrintLogHandler;
import org.eclipse.net4j.util.om.trace.PrintTraceHandler;
import org.h2.jdbcx.JdbcDataSource;
 
public class StandaloneCDODynamic {
 
	EcoreFactory ecoreFactory = EcoreFactory.eINSTANCE;
 
	private static final String DEFAULT_REPOSITORY_NAME = "test";
 
	public StandaloneCDODynamic() {
		// Enable logging and tracing
		OMPlatform.INSTANCE.setDebugging(true);
		OMPlatform.INSTANCE.addLogHandler(PrintLogHandler.CONSOLE);
		OMPlatform.INSTANCE.addTraceHandler(PrintTraceHandler.CONSOLE);
	}
 
	public static void main(String[] args) {
		StandaloneCDODynamic sample = new StandaloneCDODynamic();
 
		sample.run();
	}
 
	protected void run() {
 
		// TODO update URL
		JdbcDataSource dataSource = getJdbcDataSource("jdbc:h2:/Users/steve/Temporary/" + DEFAULT_REPOSITORY_NAME);
 
		IStore cdoStore = getStore(dataSource);
		IRepository cdoRepository = getRepository(cdoStore);
 
		IManagedContainer container = getContainer();
		CDOServerUtil.addRepository(container, cdoRepository);
 
		IAcceptor acceptor = JVMUtil.getAcceptor(container, "default"); //$NON-NLS-1$
		IConnector connector = JVMUtil.getConnector(container, "default"); //$NON-NLS-1$
 
		// Open session
		CDOSession cdoSession = getSession(connector);
 
		// Create dynamic package and class
		EPackage rootPackage = createRootPackage();
		EClass eClass = createEmptyClass(rootPackage);
		cdoSession.getPackageRegistry().putEPackage(rootPackage);
 
		// Open transaction
		CDOTransaction transaction = cdoSession.openTransaction();
 
		// Get or create resource
		CDOResource cdoResource = transaction.getOrCreateResource("/resource"); //$NON-NLS-1$
 
		EObject eObject = EcoreUtil.create(eClass);
		cdoResource.getContents().add(eObject);
		try {
			transaction.commit();
		} catch (CommitException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 
		// Cleanup
		cdoSession.close();
		connector.close();
		container.deactivate();
	}
 
	protected JdbcDataSource getJdbcDataSource(String url) {
		// Setup JdbcDataSource
		JdbcDataSource dataSource = new JdbcDataSource();
		dataSource.setURL(url);
		return dataSource;
	}
 
	protected IStore getStore(JdbcDataSource dataSource) {
		// Setup Store
		IMappingStrategy mappingStrategy = CDODBUtil.createHorizontalMappingStrategy(true);
		// TODO : change Adapter if an other DB is used.
		IDBAdapter dbAdapter = new H2Adapter();
		IDBConnectionProvider dbConnectionProvider = DBUtil.createConnectionProvider(dataSource);
		return CDODBUtil.createStore(mappingStrategy, dbAdapter, dbConnectionProvider);
	}
 
	protected IRepository getRepository(IStore store) {
		// Setup Repository
		Map<String, String> props = new HashMap<String, String>();
		props.put(IRepository.Props.OVERRIDE_UUID, DEFAULT_REPOSITORY_NAME);
	    props.put(IRepository.Props.SUPPORTING_AUDITS, "true");
	    props.put(IRepository.Props.SUPPORTING_BRANCHES, "false");	
//		props.put(IRepository.Props.CURRENT_LRU_CAPACITY, "100000");
//		props.put(IRepository.Props.REVISED_LRU_CAPACITY, "10000");
		return CDOServerUtil.createRepository(DEFAULT_REPOSITORY_NAME, store, props);
	}
 
	protected IManagedContainer getContainer() {
		IManagedContainer container = ContainerUtil.createContainer();
		Net4jUtil.prepareContainer(container);
		JVMUtil.prepareContainer(container);
		CDONet4jUtil.prepareContainer(container);
		CDONet4jServerUtil.prepareContainer(container);
		container.activate();
		return container;
	}
 
 
	protected CDOSession getSession(IConnector connector) {
		CDOSessionConfiguration config = CDONet4jUtil.createSessionConfiguration();
		config.setConnector(connector);
		config.setRepositoryName(DEFAULT_REPOSITORY_NAME);
 
		return config.openSession();		
	}
 
 
	protected EPackage createRootPackage() {
 
		// create root package
		EPackage rootPackage = ecoreFactory.createEPackage();
		rootPackage.setName("root");
		rootPackage.setNsPrefix("test");
		rootPackage.setNsURI("http://no2.zehrer.net/root");
 
		return rootPackage;
	}
 
	protected EClass createEmptyClass(EPackage aPackage) {
 
		EClass eClass = ecoreFactory.createEClass();
		eClass.setName("Foo");
 
		if (aPackage != null)
			aPackage.getEClassifiers().add(eClass);
 
		return eClass;
	}
}

Libraries

The following libraries were used:

<eclipse-userlibraries version="2">
    <library name="Eclipse" systemlibrary="false">
        <archive path="/eclipse_scala/plugins/org.eclipse.equinox.common_3.5.1.R35x_v20090807-1100.jar"/>
        <archive path="/eclipse_scala/plugins/org.eclipse.core.runtime_3.5.0.v20090525.jar"/>
        <archive path="/eclipse_scala/plugins/org.eclipse.core.commands_3.5.0.I20090525-2000.jar"/>
    </library>
    <library name="EMF CDO" systemlibrary="false">
        <archive path="/eclipse_scala/plugins/org.eclipse.emf.ecore_2.5.0.v200906151043.jar"/>
        <archive path="/eclipse_scala/plugins/org.eclipse.emf.common_2.5.0.v200906151043.jar"/>
        <archive path="/eclipse_scala/plugins/org.eclipse.emf.cdo_3.0.0.v20100316-1831.jar"/>
        <archive path="/eclipse_scala/plugins/org.eclipse.emf.cdo.common_3.0.0.v20100315-2154.jar"/>
        <archive path="/eclipse_scala/plugins/org.eclipse.emf.cdo.net4j_3.0.0.v20100311-1446.jar"/>
        <archive path="/eclipse_scala/plugins/org.eclipse.emf.cdo.server_3.0.0.v20100319-1215.jar"/>
        <archive path="/eclipse_scala/plugins/org.eclipse.emf.cdo.server.db_3.0.0.v20100310-1521.jar"/>
        <archive path="/eclipse_scala/plugins/org.eclipse.emf.cdo.server.net4j_3.0.0.v20100311-1446.jar"/>
        <archive path="/eclipse_scala/plugins/org.eclipse.net4j_3.0.0.v20100310-0527.jar"/>
        <archive path="/eclipse_scala/plugins/org.eclipse.net4j.db_3.0.0.v20100309-0917.jar"/>
        <archive path="/eclipse_scala/plugins/org.eclipse.net4j.defs_3.0.0.v20100206-0802.jar"/>
        <archive path="/eclipse_scala/plugins/org.eclipse.net4j.jvm_3.0.0.v20100228-2259.jar"/>
        <archive path="/eclipse_scala/plugins/org.eclipse.net4j.util_3.0.0.v20100311-1446.jar"/>
    </library>
    <library name="H2" systemlibrary="false">
        <archive path="/eclipse_scala/plugins/org.eclipse.net4j.db.h2_3.0.0.v20100206-0802.jar"/>
        <archive path="/eclipse_scala/plugins/org.h2.jdbc_1.1.114.200912171633.jar"/>
    </library>
    <library name="SWT" systemlibrary="false">
        <archive path="/eclipse_scala/plugins/org.eclipse.swt_3.5.1.v3555a.jar"/>
        <archive path="/eclipse_scala/plugins/org.eclipse.swt.cocoa.macosx.x86_64_3.5.1.v3555a.jar"/>
        <archive path="/eclipse_scala/plugins/org.eclipse.jface_3.5.1.M20090826-0800.jar"/>
    </library>
</eclipse-userlibraries>

Back to the top