Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
added TransactionService/-Tests in kernel and changed FedoraTransacti…
…ons Endpoint to use the service
- Loading branch information
Showing
6 changed files
with
253 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 0 additions & 107 deletions
107
fcrepo-http-api/src/test/java/org/fcrepo/api/FedoraTransactionsTest.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
fcrepo-kernel/src/main/java/org/fcrepo/services/TransactionService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/** | ||
* | ||
*/ | ||
|
||
package org.fcrepo.services; | ||
|
||
import java.util.Iterator; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import javax.jcr.PathNotFoundException; | ||
import javax.jcr.RepositoryException; | ||
import javax.jcr.Session; | ||
|
||
import org.fcrepo.Transaction; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* This is part of the Strawman implementation for Fedora transactions | ||
* This service implements a simple {@link Transaction} service which is able to | ||
* create/commit/rollback {@link Transaction} objects | ||
* | ||
* A {@link Scheduled} annotation is used for removing timed out Transactions | ||
* @author frank asseg | ||
* | ||
*/ | ||
@Component | ||
public class TransactionService { | ||
|
||
/* | ||
* TODO: since transactions have to be available on all nodes, they have to | ||
* be either persisted or written to a distributed map or sth, not just | ||
* this plain hashmap that follows | ||
*/ | ||
private static Map<String, Transaction> TRANSACTIONS = | ||
new ConcurrentHashMap<String, Transaction>(); | ||
|
||
public static final long REAP_INTERVAL = 1000; | ||
|
||
@Scheduled(fixedRate = REAP_INTERVAL) | ||
public void removeAndRollbackExpired() { | ||
synchronized (TRANSACTIONS) { | ||
Iterator<Entry<String, Transaction>> txs = | ||
TRANSACTIONS.entrySet().iterator(); | ||
while (txs.hasNext()) { | ||
Transaction tx = txs.next().getValue(); | ||
if (tx.getExpires().getTime() <= System.currentTimeMillis()) { | ||
try { | ||
tx.rollback(); | ||
} catch (RepositoryException e) { | ||
// TODO Not clear how to respond here | ||
e.printStackTrace(); | ||
} | ||
txs.remove(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Create a new Transaction and add it to the currently open ones | ||
* @param sess The session to use for this Transaction | ||
* @return the {@link Transaction} | ||
*/ | ||
public Transaction beginTransaction(final Session sess) { | ||
final Transaction tx = new Transaction(sess); | ||
TRANSACTIONS.put(tx.getId(), tx); | ||
return tx; | ||
} | ||
|
||
/** | ||
* Retrieve an open {@link Transaction} | ||
* @param txid the Id of the {@link Transaction} | ||
* @return the {@link Transaction} | ||
* @throws PathNotFoundException if the {@link Transaction} with the given id has not be found | ||
*/ | ||
public Transaction getTransaction(final String txid) | ||
throws PathNotFoundException { | ||
final Transaction tx = TRANSACTIONS.get(txid); | ||
if (tx == null) { | ||
throw new PathNotFoundException("Transaction is not available"); | ||
} | ||
return tx; | ||
} | ||
|
||
/** | ||
* Check if a Transaction exists | ||
* @param txid the Id of the {@link Transaction} | ||
* @return the {@link Transaction} | ||
* @throws PathNotFoundException if the {@link Transaction} with the given id has not be found | ||
*/ | ||
public boolean exists(final String txid) { | ||
final Transaction tx = TRANSACTIONS.get(txid); | ||
return tx != null; | ||
} | ||
|
||
/** | ||
* Commit a {@link Transaction} with the given id | ||
* @param txid the id of the {@link Transaction} | ||
* @throws RepositoryException | ||
*/ | ||
public Transaction commit(final String txid) throws RepositoryException { | ||
final Transaction tx = TRANSACTIONS.remove(txid); | ||
if (tx == null) { | ||
throw new RepositoryException("Transaction with id " + txid + | ||
" is not available"); | ||
} | ||
tx.commit(); | ||
return tx; | ||
} | ||
|
||
/** | ||
* Roll a {@link Transaction} back | ||
* @param txid the id of the {@link Transaction} | ||
* @return the {@link Transaction} object | ||
* @throws RepositoryException if the {@link Transaction} could not be found | ||
*/ | ||
public Transaction rollback(final String txid) throws RepositoryException { | ||
final Transaction tx = TRANSACTIONS.remove(txid); | ||
if (tx == null) { | ||
throw new RepositoryException("Transaction with id " + txid + | ||
" is not available"); | ||
} | ||
tx.rollback(); | ||
return tx; | ||
} | ||
|
||
} |
Oops, something went wrong.