You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by jo...@apache.org on 2006/02/07 17:23:28 UTC
svn commit: r375638 - in /jakarta/commons/proper/transaction/trunk:
RELEASE-NOTES.txt
src/java/org/apache/commons/transaction/file/FileResourceManager.java
Author: joerg
Date: Tue Feb 7 08:23:26 2006
New Revision: 375638
URL: http://svn.apache.org/viewcvs?rev=375638&view=rev
Log:
Added functions to FileResourceManager for copying and moving resources. Added possibility to append to (instead of overwriting) an existing resource with writeResource in FileResourceManager. Testcases will follow ...
Modified:
jakarta/commons/proper/transaction/trunk/RELEASE-NOTES.txt
jakarta/commons/proper/transaction/trunk/src/java/org/apache/commons/transaction/file/FileResourceManager.java
Modified: jakarta/commons/proper/transaction/trunk/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/transaction/trunk/RELEASE-NOTES.txt?rev=375638&r1=375637&r2=375638&view=diff
==============================================================================
--- jakarta/commons/proper/transaction/trunk/RELEASE-NOTES.txt (original)
+++ jakarta/commons/proper/transaction/trunk/RELEASE-NOTES.txt Tue Feb 7 08:23:26 2006
@@ -24,6 +24,8 @@
ENHANCEMENTS FROM 1.1
---------------------
- Better error reporting in FileHelper when destination directories (moveRec) or files (copyRec) could not be created
+- Added functions to FileResourceManager for copying and moving resources.
+- Added possibility to append to (instead of overwriting) an existing resource with writeResource in FileResourceManager.
BUGFIXES FROM 1.1
-----------------
@@ -39,4 +41,4 @@
KNOWN ISSUES
------------
-- Deadlock detection sometimes determines more than one thread as a deadlock victim
\ No newline at end of file
+- Deadlock detection sometimes determines more than one thread as a deadlock victim
Modified: jakarta/commons/proper/transaction/trunk/src/java/org/apache/commons/transaction/file/FileResourceManager.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/transaction/trunk/src/java/org/apache/commons/transaction/file/FileResourceManager.java?rev=375638&r1=375637&r2=375638&view=diff
==============================================================================
--- jakarta/commons/proper/transaction/trunk/src/java/org/apache/commons/transaction/file/FileResourceManager.java (original)
+++ jakarta/commons/proper/transaction/trunk/src/java/org/apache/commons/transaction/file/FileResourceManager.java Tue Feb 7 08:23:26 2006
@@ -197,11 +197,11 @@
protected Map globalTransactions;
protected List globalOpenResources;
protected LockManager2 lockManager;
-
+
protected ResourceIdToPathMapper idMapper = null;
protected int idCnt = 0;
-
+
/*
* --- ctor and general getter / setter methods ---
*
@@ -780,6 +780,44 @@
}
}
+ public void copyResource(Object txId, Object fromResourceId, Object toResourceId, boolean overwrite) throws ResourceManagerException {
+ if (logger.isFineEnabled()) logger.logFine(txId + " copying " + fromResourceId + " to " + toResourceId);
+
+ lockResource(fromResourceId, txId, true);
+ lockResource(toResourceId, txId, false);
+
+ if (resourceExists(txId, toResourceId) && !overwrite) {
+ throw new ResourceManagerException(
+ "Resource at '" + toResourceId + "' already exists",
+ ERR_RESOURCE_EXISTS,
+ txId);
+ }
+
+ InputStream fromResourceStream = null;
+ OutputStream toResourceStream = null;
+ try {
+ fromResourceStream = readResource(txId, fromResourceId);
+ toResourceStream = writeResource(txId, toResourceId);
+ FileHelper.copy(fromResourceStream, toResourceStream);
+ } catch (IOException e) {
+ throw new ResourceManagerException(ERR_SYSTEM, txId, e);
+ } finally {
+ closeOpenResource(fromResourceStream);
+ closeOpenResource(toResourceStream);
+ }
+ }
+
+ public void moveResource(Object txId, Object fromResourceId, Object toResourceId, boolean overwrite) throws ResourceManagerException {
+ if (logger.isFineEnabled()) logger.logFine(txId + " moving " + fromResourceId + " to " + toResourceId);
+
+ lockResource(fromResourceId, txId, false);
+ lockResource(toResourceId, txId, false);
+
+ copyResource(txId, fromResourceId, toResourceId, overwrite);
+
+ deleteResource(txId, fromResourceId, false);
+ }
+
public InputStream readResource(Object resourceId) throws ResourceManagerException {
// create temporary light weight tx
Object txId;
@@ -821,16 +859,35 @@
}
public OutputStream writeResource(Object txId, Object resourceId) throws ResourceManagerException {
+ return writeResource(txId, resourceId, false);
+ }
+
+ public OutputStream writeResource(Object txId, Object resourceId, boolean append) throws ResourceManagerException {
if (logger.isFineEnabled()) logger.logFine(txId + " writing " + resourceId);
lockResource(resourceId, txId, false);
+ if (append) {
+ String mainPath = getMainPath(resourceId);
+ String txChangePath = getChangePath(txId, resourceId);
+ String txDeletePath = getDeletePath(txId, resourceId);
+
+ boolean changeExists = FileHelper.fileExists(txChangePath);
+ boolean deleteExists = FileHelper.fileExists(txDeletePath);
+ boolean mainExists = FileHelper.fileExists(mainPath);
+
+ if (mainExists && !changeExists && !deleteExists) {
+ // the read and the write path for resourceId will be different!
+ copyResource(txId, resourceId, resourceId, true);
+ }
+ }
+
String resourcePath = getPathForWrite(txId, resourceId);
File file = new File(resourcePath);
try {
- FileOutputStream stream = new FileOutputStream(file);
+ FileOutputStream stream = new FileOutputStream(file, append);
TransactionContext context = getContext(txId);
context.registerResource(stream);
context.readOnly = false;
@@ -1207,7 +1264,6 @@
} catch (ResourceManagerException e) {
logger.logWarning("Rolling back of " + context.txId + " failed", e);
}
- //
}
}
@@ -1513,12 +1569,12 @@
buf.append(Long.toString(startTime)).append('\n');
if (debug) {
buf.append("----- Lock Debug Info -----\n");
-
+
for (Iterator it = lockManager.getAll(txId).iterator(); it.hasNext();) {
GenericLock lock = (GenericLock) it.next();
buf.append(lock.toString()+"\n");
}
-
+
}
return buf.toString();
}
@@ -1573,11 +1629,7 @@
// release access lock in order to allow other transactions to commit
if (lockManager.getLevel(txId, resourceId) == LOCK_ACCESS) {
if (logger.isFinerEnabled()) {
- logger.logFiner(
- "Upon close of resource releasing access lock for tx "
- + txId
- + " on resource at "
- + resourceId);
+ logger.logFiner("Upon close of resource releasing access lock for tx " + txId + " on resource at " + resourceId);
}
lockManager.release(txId, resourceId);
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org