Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEike Stepper2017-02-10 11:06:01 +0000
committerEike Stepper2017-02-10 11:06:01 +0000
commitdbf0eccacd79b51aa9fde303136a439bd2e6115f (patch)
tree5a62c604dc560125a3f208b9c9fe197a28498b83 /plugins
parent26bd1f4d8c5b39735a29676f04bddc49bc2d193b (diff)
downloadcdo-dbf0eccacd79b51aa9fde303136a439bd2e6115f.tar.gz
cdo-dbf0eccacd79b51aa9fde303136a439bd2e6115f.tar.xz
cdo-dbf0eccacd79b51aa9fde303136a439bd2e6115f.zip
[509149] Provide an EmbeddedRepository
https://bugs.eclipse.org/bugs/show_bug.cgi?id=509149
Diffstat (limited to 'plugins')
-rw-r--r--plugins/org.eclipse.emf.cdo.examples/META-INF/MANIFEST.MF2
-rw-r--r--plugins/org.eclipse.emf.cdo.examples/src/org/eclipse/emf/cdo/examples/embedded/CDOFacade.java170
-rw-r--r--plugins/org.eclipse.emf.cdo.examples/src/org/eclipse/emf/cdo/examples/embedded/EmbeddedRepositoryExample.java53
3 files changed, 225 insertions, 0 deletions
diff --git a/plugins/org.eclipse.emf.cdo.examples/META-INF/MANIFEST.MF b/plugins/org.eclipse.emf.cdo.examples/META-INF/MANIFEST.MF
index d039b7e2cd..2bc5f8d74d 100644
--- a/plugins/org.eclipse.emf.cdo.examples/META-INF/MANIFEST.MF
+++ b/plugins/org.eclipse.emf.cdo.examples/META-INF/MANIFEST.MF
@@ -16,11 +16,13 @@ Require-Bundle: org.eclipse.core.runtime;bundle-version="[3.5.0,4.0.0)";resoluti
org.eclipse.emf.cdo.server;bundle-version="[4.0.0,5.0.0)",
org.eclipse.emf.cdo.server.db;bundle-version="[4.0.0,5.0.0)",
org.eclipse.emf.cdo.server.net4j;bundle-version="[4.0.0,5.0.0)",
+ org.eclipse.emf.cdo.server.embedded;bundle-version="[4.0.0,5.0.0)",
org.eclipse.net4j.jvm;bundle-version="[4.0.0,5.0.0)",
org.eclipse.net4j.tcp;bundle-version="[4.0.0,5.0.0)",
org.eclipse.net4j.db.h2;bundle-version="[4.0.0,5.0.0)"
Import-Package: org.h2.jdbcx;version="[1.0.0,2.0.0)"
Export-Package: org.eclipse.emf.cdo.examples;version="4.0.600";x-internal:=true,
+ org.eclipse.emf.cdo.examples.embedded;version="4.0.600",
org.eclipse.emf.cdo.examples.server;version="4.0.600";x-internal:=true,
org.eclipse.emf.cdo.examples.server.offline;version="4.0.600";x-internal:=true,
org.eclipse.emf.cdo.internal.examples.bundle;version="4.0.600";x-internal:=true
diff --git a/plugins/org.eclipse.emf.cdo.examples/src/org/eclipse/emf/cdo/examples/embedded/CDOFacade.java b/plugins/org.eclipse.emf.cdo.examples/src/org/eclipse/emf/cdo/examples/embedded/CDOFacade.java
new file mode 100644
index 0000000000..c1d6dc2268
--- /dev/null
+++ b/plugins/org.eclipse.emf.cdo.examples/src/org/eclipse/emf/cdo/examples/embedded/CDOFacade.java
@@ -0,0 +1,170 @@
+package org.eclipse.emf.cdo.examples.embedded;
+
+import org.eclipse.emf.cdo.net4j.CDONet4jSession;
+import org.eclipse.emf.cdo.server.CDOServerBrowser;
+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.IDBStore;
+import org.eclipse.emf.cdo.server.db.mapping.IMappingStrategy;
+import org.eclipse.emf.cdo.server.embedded.CDOEmbeddedRepositoryConfig;
+import org.eclipse.emf.cdo.transaction.CDOTransaction;
+
+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.IManagedContainer;
+import org.eclipse.net4j.util.lifecycle.ILifecycle;
+import org.eclipse.net4j.util.lifecycle.LifecycleEventAdapter;
+import org.eclipse.net4j.util.lifecycle.LifecycleUtil;
+
+import org.eclipse.emf.ecore.resource.ResourceSet;
+import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
+
+import org.h2.jdbcx.JdbcDataSource;
+
+import java.io.File;
+import java.sql.Connection;
+import java.util.Map;
+
+/**
+ * @author Eike Stepper
+ */
+public final class CDOFacade extends CDOEmbeddedRepositoryConfig
+{
+ public static final CDOFacade INSTANCE = new CDOFacade();
+
+ private static final String NAME = "repo";
+
+ private static final boolean AUDITING = false;
+
+ private static final boolean BRANCHING = false;
+
+ private static final File DB_FOLDER = new File("./database");
+
+ private static final String DS_URL = "jdbc:h2:" + DB_FOLDER + "/" + NAME + ";MVCC=FALSE";
+
+ private CDOServerBrowser serverBrowser;
+
+ private CDONet4jSession session;
+
+ private CDOTransaction transaction;
+
+ private ResourceSet resourceSet;
+
+ private CDOFacade()
+ {
+ super(NAME);
+ }
+
+ public Connection getJDBCConnection()
+ {
+ IDBStore store = (IDBStore)getRepository().getStore();
+ return store.getConnection();
+ }
+
+ public synchronized CDONet4jSession getSession(boolean openOnDemand)
+ {
+ checkActive();
+
+ if (session == null && openOnDemand)
+ {
+ session = openClientSession();
+ session.addListener(new LifecycleEventAdapter()
+ {
+ @Override
+ protected void onDeactivated(ILifecycle lifecycle)
+ {
+ if (lifecycle == session)
+ {
+ session = null;
+ }
+ }
+ });
+ }
+
+ return session;
+ }
+
+ public synchronized CDOTransaction getTransaction()
+ {
+ checkActive();
+
+ if (transaction == null)
+ {
+ resourceSet = new ResourceSetImpl();
+
+ CDONet4jSession session = getSession(true);
+ transaction = session.openTransaction(resourceSet);
+
+ transaction.addListener(new LifecycleEventAdapter()
+ {
+ @Override
+ protected void onDeactivated(ILifecycle lifecycle)
+ {
+ if (lifecycle == transaction)
+ {
+ transaction = null;
+ resourceSet = null;
+ }
+ }
+ });
+ }
+
+ return transaction;
+ }
+
+ public ResourceSet getResourceSet()
+ {
+ return resourceSet;
+ }
+
+ @Override
+ public IStore createStore(IManagedContainer container)
+ {
+ DB_FOLDER.mkdirs();
+
+ IMappingStrategy mappingStrategy = CDODBUtil.createHorizontalMappingStrategy(AUDITING, BRANCHING);
+ mappingStrategy.getProperties().put(IMappingStrategy.Props.FORCE_NAMES_WITH_ID, "true");
+
+ JdbcDataSource dataSource = new JdbcDataSource();
+ dataSource.setURL(DS_URL);
+
+ IDBAdapter dbAdapter = new H2Adapter();
+ IDBConnectionProvider dbConnectionProvider = DBUtil.createConnectionProvider(dataSource);
+
+ return CDODBUtil.createStore(mappingStrategy, dbAdapter, dbConnectionProvider);
+ }
+
+ @Override
+ public void initProperties(IManagedContainer container, Map<String, String> properties)
+ {
+ properties.put(IRepository.Props.SUPPORTING_AUDITS, Boolean.toString(AUDITING));
+ properties.put(IRepository.Props.SUPPORTING_BRANCHES, Boolean.toString(BRANCHING));
+ }
+
+ @Override
+ public boolean isInitialPackage(IRepository repository, String nsURI)
+ {
+ return nsURI.equals("http://www.eclipse.org/emf/CDO/examples/company/1.0.0");
+ }
+
+ @Override
+ protected void doActivate() throws Exception
+ {
+ super.doActivate();
+
+ serverBrowser = new CDOServerBrowser.ContainerBased(getContainer());
+ LifecycleUtil.activate(serverBrowser);
+ }
+
+ @Override
+ protected void doDeactivate() throws Exception
+ {
+ LifecycleUtil.deactivate(serverBrowser);
+ serverBrowser = null;
+
+ super.doDeactivate();
+ }
+}
diff --git a/plugins/org.eclipse.emf.cdo.examples/src/org/eclipse/emf/cdo/examples/embedded/EmbeddedRepositoryExample.java b/plugins/org.eclipse.emf.cdo.examples/src/org/eclipse/emf/cdo/examples/embedded/EmbeddedRepositoryExample.java
new file mode 100644
index 0000000000..1f662612a3
--- /dev/null
+++ b/plugins/org.eclipse.emf.cdo.examples/src/org/eclipse/emf/cdo/examples/embedded/EmbeddedRepositoryExample.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2004-2016 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.embedded;
+
+import org.eclipse.emf.cdo.eresource.CDOResource;
+import org.eclipse.emf.cdo.examples.company.Company;
+import org.eclipse.emf.cdo.examples.company.CompanyFactory;
+import org.eclipse.emf.cdo.transaction.CDOTransaction;
+
+import org.eclipse.emf.ecore.EObject;
+
+/**
+ * @author Eike Stepper
+ */
+public class EmbeddedRepositoryExample
+{
+ public static void main(String[] args) throws Exception
+ {
+ CDOFacade.INSTANCE.activate();
+
+ try
+ {
+ CDOTransaction transaction = CDOFacade.INSTANCE.getTransaction();
+ CDOResource resource = transaction.getOrCreateResource("test1");
+
+ Company company = CompanyFactory.eINSTANCE.createCompany();
+ company.setName("Company-" + System.currentTimeMillis());
+
+ resource.getContents().add(company);
+ transaction.commit();
+
+ for (EObject object : resource.getContents())
+ {
+ if (object instanceof Company)
+ {
+ System.out.println(((Company)object).getName());
+ }
+ }
+ }
+ finally
+ {
+ CDOFacade.INSTANCE.deactivate();
+ }
+ }
+}

Back to the top