Return-Path: Delivered-To: apmail-jackrabbit-commits-archive@www.apache.org Received: (qmail 89101 invoked from network); 23 Nov 2006 19:11:00 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 23 Nov 2006 19:11:00 -0000 Received: (qmail 41631 invoked by uid 500); 23 Nov 2006 19:11:10 -0000 Delivered-To: apmail-jackrabbit-commits-archive@jackrabbit.apache.org Received: (qmail 41595 invoked by uid 500); 23 Nov 2006 19:11:10 -0000 Mailing-List: contact commits-help@jackrabbit.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@jackrabbit.apache.org Delivered-To: mailing list commits@jackrabbit.apache.org Received: (qmail 41586 invoked by uid 99); 23 Nov 2006 19:11:09 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 23 Nov 2006 11:11:09 -0800 X-ASF-Spam-Status: No, hits=-8.6 required=10.0 tests=ALL_TRUSTED,INFO_TLD,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 23 Nov 2006 11:10:59 -0800 Received: by eris.apache.org (Postfix, from userid 65534) id 5B65E1A9846; Thu, 23 Nov 2006 11:10:24 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r478644 - /jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/version/AbstractVersionManager.java Date: Thu, 23 Nov 2006 19:10:24 -0000 To: commits@jackrabbit.apache.org From: jukka@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20061123191024.5B65E1A9846@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: jukka Date: Thu Nov 23 11:10:23 2006 New Revision: 478644 URL: http://svn.apache.org/viewvc?view=rev&rev=478644 Log: JCR-546: Introduced the WriteOperation helper class to hide the handling of the StateManager and the write lock. Modified: jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/version/AbstractVersionManager.java Modified: jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/version/AbstractVersionManager.java URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/version/AbstractVersionManager.java?view=diff&rev=478644&r1=478643&r2=478644 ============================================================================== --- jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/version/AbstractVersionManager.java (original) +++ jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/version/AbstractVersionManager.java Thu Nov 23 11:10:23 2006 @@ -158,6 +158,82 @@ } /** + * Helper for managing write operations. + */ + private class WriteOperation { + + /** + * Flag for successful completion of the write operation. + */ + private boolean success = false; + + /** + * Saves the pending operations in the {@link StateManager}. + * + * @throws ItemStateException if the pending state is invalid + * @throws RepositoryException if the pending state could not be persisted + */ + public void save() throws ItemStateException, RepositoryException { + stateMgr.update(); + success = true; + } + + /** + * Closes the write operation. The pending operations are cancelled + * if they could not be properly saved. Finally the write lock is + * released. + */ + public void close() { + try { + if (!success) { + // update operation failed, cancel all modifications + stateMgr.cancel(); + } + } finally { + releaseWriteLock(); + } + } + } + + /** + * Starts a write operation by acquiring the write lock and setting the + * item state manager to the "edit" state. If something goes wrong, the + * write lock is released and an exception is thrown. + *

+ * The pattern for using this method and the returned helper instance is: + *

+     *     WriteOperation operation = startWriteOperation();
+     *     try {
+     *         ...
+     *         operation.save(); // if everything is OK
+     *         ...
+     *     } catch (...) {
+     *         ...
+     *     } finally {
+     *         operation.close();
+     *     }
+     * 
+ * + * @return write operation helper + * @throws RepositoryException if the write operation could not be started + */ + private WriteOperation startWriteOperation() throws RepositoryException { + boolean success = false; + acquireWriteLock(); + try { + stateMgr.edit(); + success = true; + return new WriteOperation(); + } catch (IllegalStateException e) { + throw new RepositoryException("Unable to start edit operation.", e); + } finally { + if (!success) { + releaseWriteLock(); + } + } + } + + /** * {@inheritDoc} */ public VersionHistory getVersionHistory(Session session, NodeState node) @@ -217,17 +293,7 @@ */ InternalVersionHistory createVersionHistory(NodeState node) throws RepositoryException { - - acquireWriteLock(); - try { - stateMgr.edit(); - } catch (IllegalStateException e) { - releaseWriteLock(); - throw new RepositoryException("Unable to start edit operation", e); - } - - boolean succeeded = false; - + WriteOperation operation = startWriteOperation(); try { // create deep path String uuid = node.getNodeId().getUUID().toString(); @@ -251,8 +317,7 @@ this, root, new NodeId(UUID.randomUUID()), historyNodeName, node); // end update - stateMgr.update(); - succeeded = true; + operation.save(); log.info("Created new version history " + hist.getId() + " for " + node + "."); return hist; @@ -260,11 +325,7 @@ } catch (ItemStateException e) { throw new RepositoryException(e); } finally { - if (!succeeded) { - // update operation failed, cancel all modifications - stateMgr.cancel(); - } - releaseWriteLock(); + operation.close(); } } @@ -375,27 +436,14 @@ */ protected void removeVersion(InternalVersionHistoryImpl history, QName name) throws VersionException, RepositoryException { - - acquireWriteLock(); - try { - stateMgr.edit(); - } catch (IllegalStateException e) { - releaseWriteLock(); - throw new VersionException("Unable to start edit operation", e); - } - boolean succeeded = false; + WriteOperation operation = startWriteOperation(); try { history.removeVersion(name); - stateMgr.update(); - succeeded = true; + operation.save(); } catch (ItemStateException e) { log.error("Error while storing: " + e.toString()); } finally { - if (!succeeded) { - // update operation failed, cancel all modifications - stateMgr.cancel(); - } - releaseWriteLock(); + operation.close(); } } @@ -412,30 +460,17 @@ QName version, QName label, boolean move) throws RepositoryException { - - acquireWriteLock(); - try { - stateMgr.edit(); - } catch (IllegalStateException e) { - releaseWriteLock(); - throw new VersionException("Unable to start edit operation", e); - } - InternalVersion v = null; - boolean success = false; + WriteOperation operation = startWriteOperation(); try { - v = history.setVersionLabel(version, label, move); - stateMgr.update(); - success = true; + InternalVersion v = history.setVersionLabel(version, label, move); + operation.save(); + return v; } catch (ItemStateException e) { log.error("Error while storing: " + e.toString()); + return null; } finally { - if (!success) { - // update operation failed, cancel all modifications - stateMgr.cancel(); - } - releaseWriteLock(); + operation.close(); } - return v; } /**