diff options
author | Simon McDuff | 2008-09-11 06:38:38 +0000 |
---|---|---|
committer | Simon McDuff | 2008-09-11 06:38:38 +0000 |
commit | 65087f87d7d13b3d1b79f5aa3a0086e23a137d0b (patch) | |
tree | 6cbd5ba0450b6ed791c407207033ed8fe0e18afd /plugins/org.eclipse.emf.cdo.server/src/org/eclipse/emf/cdo/internal/server/CommitManager.java | |
parent | 128d02f77c213ee2aca3ecd23ef6bba7a16dfa09 (diff) | |
download | cdo-65087f87d7d13b3d1b79f5aa3a0086e23a137d0b.tar.gz cdo-65087f87d7d13b3d1b79f5aa3a0086e23a137d0b.tar.xz cdo-65087f87d7d13b3d1b79f5aa3a0086e23a137d0b.zip |
[213402] Support external references
https://bugs.eclipse.org/bugs/show_bug.cgi?id=213402
Diffstat (limited to 'plugins/org.eclipse.emf.cdo.server/src/org/eclipse/emf/cdo/internal/server/CommitManager.java')
-rw-r--r-- | plugins/org.eclipse.emf.cdo.server/src/org/eclipse/emf/cdo/internal/server/CommitManager.java | 163 |
1 files changed, 163 insertions, 0 deletions
diff --git a/plugins/org.eclipse.emf.cdo.server/src/org/eclipse/emf/cdo/internal/server/CommitManager.java b/plugins/org.eclipse.emf.cdo.server/src/org/eclipse/emf/cdo/internal/server/CommitManager.java new file mode 100644 index 0000000000..dc2cc04632 --- /dev/null +++ b/plugins/org.eclipse.emf.cdo.server/src/org/eclipse/emf/cdo/internal/server/CommitManager.java @@ -0,0 +1,163 @@ +/*************************************************************************** + * Copyright (c) 2004 - 2008 Eike Stepper, Germany. + * 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: + * Simon McDuff - initial API and implementation + **************************************************************************/ +package org.eclipse.emf.cdo.internal.server; + +import org.eclipse.emf.cdo.internal.server.Transaction.InternalCommitContext; +import org.eclipse.emf.cdo.server.IRepositoryElement; + +import org.eclipse.net4j.util.lifecycle.Lifecycle; + +import java.util.Map; +import java.util.concurrent.Callable; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +/** + * @author Simon McDuff + * @since 2.0 + */ +public class CommitManager extends Lifecycle implements IRepositoryElement +{ + private Repository repository; + + private ExecutorService executors; + + private Map<Transaction, TransactionCommitContextEntry> commitContextMap = new ConcurrentHashMap<Transaction, TransactionCommitContextEntry>(); + + public CommitManager(Repository repository) + { + this.repository = repository; + } + + public Repository getRepository() + { + return repository; + } + + public synchronized ExecutorService getExecutors() + { + if (executors == null) + { + executors = Executors.newFixedThreadPool(10); + } + + return executors; + } + + public void setExecutors(ExecutorService executors) + { + this.executors = executors; + } + + /** + * Create a future to execute commitContext in a different thread. + */ + public void preCommit(InternalCommitContext commitContext) + { + TransactionCommitContextEntry contextEntry = new TransactionCommitContextEntry(); + contextEntry.setContext(commitContext); + + Future<Object> future = getExecutors().submit(contextEntry.createCallable()); + + contextEntry.setFuture(future); + + commitContextMap.put(commitContext.getTransaction(), contextEntry); + } + + /** + * Called after a commitContext is done successfully or not. + */ + public void remove(InternalCommitContext commitContext) + { + commitContextMap.remove(commitContext.getTransaction()); + } + + public void rollback(InternalCommitContext commitContext) + { + TransactionCommitContextEntry contextEntry = commitContextMap.get(commitContext.getTransaction()); + if (contextEntry != null) + { + contextEntry.getFuture().cancel(true); + commitContext.postCommit(false); + } + } + + /** + * Waiting for a commit to be done. + */ + public void waitForTermination(Transaction transaction) throws InterruptedException, ExecutionException + { + TransactionCommitContextEntry contextEntry = commitContextMap.get(transaction); + contextEntry.getFuture().get(); + } + + public InternalCommitContext get(Transaction transaction) + { + TransactionCommitContextEntry contextEntry = commitContextMap.get(transaction); + if (contextEntry != null) + { + return contextEntry.getContext(); + } + return null; + + } + + /** + * @author Simon McDuff + */ + private static final class TransactionCommitContextEntry + { + private InternalCommitContext context; + + private Future<Object> future; + + public TransactionCommitContextEntry() + { + } + + public Callable<Object> createCallable() + { + return new Callable<Object>() + { + + public Object call() throws Exception + { + context.write(); + return null; + } + }; + } + + public InternalCommitContext getContext() + { + return context; + } + + public void setContext(InternalCommitContext context) + { + this.context = context; + } + + public Future<Object> getFuture() + { + return future; + } + + public void setFuture(Future<Object> future) + { + this.future = future; + } + + } +} |