Author: ozeigermann Date: Sat Jul 28 14:51:02 2007 New Revision: 560629 URL: http://svn.apache.org/viewvc?view=rev&rev=560629 Log: Completed first version of tx file resource manager
Modified: jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/TxFileResourceManager.java Modified: jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/TxFileResourceManager.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/TxFileResourceManager.java?view=diff&rev=560629&r1=560628&r2=560629 ============================================================================== --- jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/TxFileResourceManager.java (original) +++ jakarta/commons/proper/transaction/branches/TRANSACTION_2/src/java/org/apache/commons/transaction/file/TxFileResourceManager.java Sat Jul 28 14:51:02 2007 @@ -18,6 +18,7 @@ import java.io.Closeable; import java.io.File; +import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; @@ -30,7 +31,9 @@ import org.apache.commons.transaction.ManageableResourceManager; import org.apache.commons.transaction.AbstractTransactionalResourceManager.AbstractTxContext; import org.apache.commons.transaction.file.FileResourceManager.FileResource; -import org.apache.commons.transaction.locking.LockException; +import org.apache.commons.transaction.locking.HierarchicalLockManager; +import org.apache.commons.transaction.locking.HierarchicalRWLockManager; +import org.apache.commons.transaction.locking.LockManager; import org.apache.commons.transaction.resource.ResourceException; import org.apache.commons.transaction.resource.ResourceManager; import org.apache.commons.transaction.resource.StreamableResource; @@ -49,7 +52,10 @@ protected FileResourceUndoManager undoManager; - public TxFileResourceManager(String rootPath) { + protected HierarchicalLockManager hlm; + + public TxFileResourceManager(String name, String rootPath) { + super(name); this.rootPath = rootPath; wrapped = new FileResourceManager(rootPath); } @@ -63,149 +69,190 @@ return new FileTxContext(); } - // TODO resource manager needs to forward requests to this context, locking - // will happen here - // FIXME - // needs - // - custom commit / rollback - // - proper resource tracking + protected HierarchicalLockManager getHLM() { + return hlm; + } + + @Override + public void setLm(LockManager<Object, Object> lm) { + super.setLm(lm); + hlm = new HierarchicalRWLockManager(rootPath, lm); + } + public class FileTxContext extends AbstractTxContext implements ResourceManager<StreamableResource> { // list of streams participating in this tx private Collection<Closeable> openStreams = new ArrayList<Closeable>(); - public FileTxContext() { - } - protected void registerStream(Closeable stream) { openStreams.add(stream); } - public StreamableResource getResource(String path) throws ResourceException { + protected void closeAllStreams() { + for (Closeable stream : openStreams) { + try { + stream.close(); + } catch (IOException e) { + logger.warn("Could not close stream during cleaning", e); + } + } + } + + @Override + public TxFileResource getResource(String path) throws ResourceException { return new TxFileResource(path); } + @Override public String getRootPath() { return TxFileResourceManager.this.getRootPath(); } - // FIXME needs custom implementations - // Details: - // - Hierarchical locking - // - Calls to configured undo manager + @Override + public void dispose() { + closeAllStreams(); + getUndoManager().forgetRecord(); + super.dispose(); + } + + @Override + public void commit() { + super.commit(); + } + + @Override + public void rollback() { + getUndoManager().undoRecord(); + super.rollback(); + } + protected class TxFileResource extends FileResource { - public TxFileResource(File file) { + public TxFileResource(File file) throws ResourceException { super(file); } - public TxFileResource(String path) { + public TxFileResource(String path) throws ResourceException { super(path); } - public void copy(String destinationpath) throws ResourceException { - super.copy(destinationpath); - } - public void createAsDirectory() throws ResourceException { + writeLock(); + getUndoManager().recordCreateAsDirectory(getFile()); super.createAsDirectory(); } public void createAsFile() throws ResourceException { + writeLock(); + getUndoManager().recordCreateAsFile(getFile()); super.createAsFile(); } public void delete() throws ResourceException { + writeLock(); + getUndoManager().recordDelete(getFile()); super.delete(); } public boolean exists() { + readLock(); return super.exists(); } - public List<StreamableResource> getChildren() throws ResourceException { - return super.getChildren(); + public List<? extends TxFileResource> getChildren() throws ResourceException { + readLock(); + + List<FileResource> children = (List<FileResource>) super.getChildren(); + List<TxFileResource> txChildren = new ArrayList<TxFileResource>(); + // if we have a list of chidren we do not want them to suddenly + // vanish + for (FileResource resource : children) { + TxFileResource txFileResource = new TxFileResource(resource.getFile()); + txFileResource.readLock(); + txChildren.add(txFileResource); + } + return txChildren; } - public StreamableResource getParent() throws ResourceException { - return super.getParent(); + public TxFileResource getParent() throws ResourceException { + readLock(); + FileResource parent = super.getParent(); + TxFileResource txFileParent = new TxFileResource(parent.getFile()); + // if we acquire the parent, we want to be sure it really exist + txFileParent.readLock(); + return txFileParent; } - public String getPath() throws ResourceException { + public String getPath() { return super.getPath(); } public Object getProperty(String name) { + readLock(); return super.getProperty(name); } public boolean isDirectory() { + readLock(); return super.isDirectory(); } public boolean isFile() { + readLock(); return super.isFile(); } - public void move(String destinationpath) throws ResourceException { - super.move(destinationpath); + public void copy(FileResource destination) throws ResourceException { + super.moveorCopySaneCheck(destination); + readLock(); + new TxFileResource(destination.getFile()).writeLock(); + getUndoManager().recordCopy(getFile(), getFileForResource(destination)); + super.copy(destination); + } + + public void move(FileResource destination) throws ResourceException { + super.moveorCopySaneCheck(destination); + writeLock(); + new TxFileResource(destination.getFile()).writeLock(); + getUndoManager().recordMove(getFile(), getFileForResource(destination)); + super.move(destination); } public InputStream readStream() throws ResourceException { - return super.readStream(); + readLock(); + InputStream is = super.readStream(); + registerStream(is); + return is; + } + + public OutputStream writeStream(boolean append) throws ResourceException { + writeLock(); + getUndoManager().recordChangeContent(getFile()); + OutputStream os = super.writeStream(append); + registerStream(os); + return os; } public void removeProperty(String name) { + // no need to handle, as we do not support this super.removeProperty(name); } public void setProperty(String name, Object newValue) { + // no need to handle, as we do not support this super.setProperty(name, newValue); } - public boolean tryReadLock() { - try { - return getLm().tryLock(getName(), getPath(), false); - } catch (ResourceException e) { - // FIXME: ouch! - throw new LockException(e); - } - } - - public boolean tryWriteLock() { - try { - return getLm().tryLock(getName(), getPath(), true); - } catch (ResourceException e) { - // FIXME: ouch! - throw new LockException(e); - } - } - public void readLock() { - try { - getLm().lock(getName(), getPath(), false); - } catch (ResourceException e) { - // FIXME: ouch! - throw new LockException(e); - } + getHLM().lockInHierarchy(getName(), getPath(), false); super.readLock(); } public void writeLock() { - try { - getLm().lock(getName(), getPath(), true); - } catch (ResourceException e) { - // FIXME: ouch! - throw new LockException(e); - } + getHLM().lockInHierarchy(getName(), getPath(), true); super.writeLock(); } - - public OutputStream writeStream(boolean append) throws ResourceException { - return super.writeStream(append); - } - } } @@ -214,7 +261,7 @@ return false; } - public StreamableResource getResource(String path) throws ResourceException { + public FileResource getResource(String path) throws ResourceException { FileTxContext context = getActiveTx(); if (context != null) { return context.getResource(path); --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]