You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lenya.apache.org by an...@apache.org on 2005/04/09 00:35:42 UTC

svn commit: r160633 - in lenya/trunk/src/java/org/apache/lenya/cms: publication/DocumentManager.java publication/DocumentManagerImpl.java site/usecases/ChangeNodeID.java site/usecases/CreateDocument.java site/usecases/LinkRewriterImpl.java

Author: andreas
Date: Fri Apr  8 15:35:41 2005
New Revision: 160633

URL: http://svn.apache.org/viewcvs?view=rev&rev=160633
Log:
proper locking when a document is moved or links are rewritten

Modified:
    lenya/trunk/src/java/org/apache/lenya/cms/publication/DocumentManager.java
    lenya/trunk/src/java/org/apache/lenya/cms/publication/DocumentManagerImpl.java
    lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/ChangeNodeID.java
    lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/CreateDocument.java
    lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/LinkRewriterImpl.java

Modified: lenya/trunk/src/java/org/apache/lenya/cms/publication/DocumentManager.java
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/publication/DocumentManager.java?view=diff&r1=160632&r2=160633
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/publication/DocumentManager.java (original)
+++ lenya/trunk/src/java/org/apache/lenya/cms/publication/DocumentManager.java Fri Apr  8 15:35:41 2005
@@ -24,7 +24,7 @@
  * @version $Id:$
  */
 public interface DocumentManager {
-    
+
     /**
      * The Avalon component role.
      */
@@ -34,31 +34,28 @@
      * Copies a document from one location to another location.
      * @param sourceDocument The document to copy.
      * @param destinationDocument The destination document.
-     * @throws PublicationException if a document which destinationDocument
-     *             depends on does not exist.
+     * @throws PublicationException if a document which destinationDocument depends on does not
+     *             exist.
      */
-    void copy(Document sourceDocument, Document destinationDocument)
-            throws PublicationException;
+    void copy(Document sourceDocument, Document destinationDocument) throws PublicationException;
 
     /**
      * Copies a document to another area.
      * @param sourceDocument The document to copy.
      * @param destinationArea The destination area.
-     * @throws PublicationException if a document which the destination document
-     *             depends on does not exist.
+     * @throws PublicationException if a document which the destination document depends on does not
+     *             exist.
      */
-    void copyToArea(Document sourceDocument, String destinationArea)
-            throws PublicationException;
+    void copyToArea(Document sourceDocument, String destinationArea) throws PublicationException;
 
     /**
      * Copies a document set to another area.
      * @param documentSet The document set to copy.
      * @param destinationArea The destination area.
-     * @throws PublicationException if a document which one of the destination
-     *             documents depends on does not exist.
+     * @throws PublicationException if a document which one of the destination documents depends on
+     *             does not exist.
      */
-    void copyToArea(DocumentSet documentSet, String destinationArea)
-            throws PublicationException;
+    void copyToArea(DocumentSet documentSet, String destinationArea) throws PublicationException;
 
     /**
      * Adds a document to the publication.
@@ -78,33 +75,32 @@
      * Moves a document from one location to another.
      * @param sourceDocument The source document.
      * @param destinationDocument The destination document.
-     * @throws PublicationException if a document which the destination document
-     *             depends on does not exist.
+     * @throws PublicationException if a document which the destination document depends on does not
+     *             exist.
      */
-    void move(Document sourceDocument, Document destinationDocument)
-            throws PublicationException;
+    void move(Document sourceDocument, Document destinationDocument) throws PublicationException;
 
     /**
-     * Checks if a document can be created. This is the case if the document ID
-     * is valid and the document does not yet exist.
+     * Checks if a document can be created. This is the case if the document ID is valid and the
+     * document does not yet exist.
      * @param identityMap The identity map to use.
+     * @param publication The publication.
      * @param area The area.
-     * @param parent The parent of the document or <code>null</code> if the
-     *            document has no parent.
+     * @param parent The parent of the document or <code>null</code> if the document has no
+     *            parent.
      * @param nodeId The node ID.
      * @param language The language.
-     * @return An array of error messages. The array is empty if the document
-     *         can be created.
+     * @return An array of error messages. The array is empty if the document can be created.
      * @throws DocumentBuildException if an error occurs.
      * @throws DocumentException if an error occurs.
      */
-    String[] canCreate(DocumentIdentityMap identityMap, String area, Document parent,
-            String nodeId, String language) throws DocumentBuildException, DocumentException;
+    String[] canCreate(DocumentIdentityMap identityMap, Publication publication, String area,
+            Document parent, String nodeId, String language) throws DocumentBuildException,
+            DocumentException;
 
     /**
-     * Checks if the document does already exist. If it does, returns a
-     * non-existing document with a similar document ID. If it does not, the
-     * original document is returned.
+     * Checks if the document does already exist. If it does, returns a non-existing document with a
+     * similar document ID. If it does not, the original document is returned.
      * @param document The document.
      * @return A document.
      * @throws DocumentBuildException if the new document could not be built.
@@ -112,8 +108,8 @@
     Document getAvailableDocument(Document document) throws DocumentBuildException;
 
     /**
-     * Moves a document to another location, incl. all requiring documents. If a
-     * sitetree is used, this means that the whole subtree is moved.
+     * Moves a document to another location, incl. all requiring documents. If a sitetree is used,
+     * this means that the whole subtree is moved.
      * @param source The source document.
      * @param target The target document.
      * @throws PublicationException if an error occurs.
@@ -126,12 +122,11 @@
      * @param target The target.
      * @throws PublicationException if the documents could not be moved.
      */
-    void moveAllLanguageVersions(Document source, Document target)
-            throws PublicationException;
+    void moveAllLanguageVersions(Document source, Document target) throws PublicationException;
 
     /**
-     * Copies a document to another location, incl. all requiring documents. If
-     * a sitetree is used, this means that the whole subtree is copied.
+     * Copies a document to another location, incl. all requiring documents. If a sitetree is used,
+     * this means that the whole subtree is copied.
      * @param source The source document.
      * @param target The target document.
      * @throws PublicationException if an error occurs.
@@ -144,24 +139,23 @@
      * @param target The target.
      * @throws PublicationException if the documents could not be copied.
      */
-    void copyAllLanguageVersions(Document source, Document target)
-            throws PublicationException;
-    
+    void copyAllLanguageVersions(Document source, Document target) throws PublicationException;
+
     /**
-     * Deletes a document, incl. all requiring documents. If
-     * a sitetree is used, this means that the whole subtree is deleted.
+     * Deletes a document, incl. all requiring documents. If a sitetree is used, this means that the
+     * whole subtree is deleted.
      * @param document The document.
      * @throws PublicationException if an error occurs.
      */
     void deleteAll(Document document) throws PublicationException;
-    
+
     /**
      * Deletes all language versions of a document.
      * @param document The document.
      * @throws PublicationException if the documents could not be copied.
      */
     void deleteAllLanguageVersions(Document document) throws PublicationException;
-    
+
     /**
      * Deletes a set of documents.
      * @param documents The documents.

Modified: lenya/trunk/src/java/org/apache/lenya/cms/publication/DocumentManagerImpl.java
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/publication/DocumentManagerImpl.java?view=diff&r1=160632&r2=160633
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/publication/DocumentManagerImpl.java (original)
+++ lenya/trunk/src/java/org/apache/lenya/cms/publication/DocumentManagerImpl.java Fri Apr  8 15:35:41 2005
@@ -38,7 +38,6 @@
 import org.apache.lenya.cms.site.tree.SiteTreeNode;
 import org.apache.lenya.cms.site.tree.TreeSiteManager;
 import org.apache.lenya.cms.workflow.WorkflowManager;
-import org.apache.lenya.transaction.UnitOfWork;
 
 /**
  * Abstract DocumentManager implementation.
@@ -139,7 +138,7 @@
             selector = (ServiceSelector) this.manager.lookup(SiteManager.ROLE + "Selector");
             siteManager = (SiteManager) selector.select(publication.getSiteManagerHint());
             siteManager.delete(document);
-            
+
             document.delete();
         } catch (Exception e) {
             throw new PublicationException(e);
@@ -233,11 +232,12 @@
 
     /**
      * @see org.apache.lenya.cms.publication.DocumentManager#canCreate(org.apache.lenya.cms.publication.DocumentIdentityMap,
-     *      java.lang.String, org.apache.lenya.cms.publication.Document, java.lang.String,
-     *      java.lang.String)
+     *      org.apache.lenya.cms.publication.Publication, java.lang.String,
+     *      org.apache.lenya.cms.publication.Document, java.lang.String, java.lang.String)
      */
-    public String[] canCreate(DocumentIdentityMap identityMap, String area, Document parent,
-            String nodeId, String language) throws DocumentBuildException, DocumentException {
+    public String[] canCreate(DocumentIdentityMap identityMap, Publication publication,
+            String area, Document parent, String nodeId, String language)
+            throws DocumentBuildException, DocumentException {
 
         List errorMessages = new ArrayList();
 
@@ -253,10 +253,7 @@
         } else if (nodeId.indexOf("/") > -1) {
             errorMessages.add("The document ID may not contain a slash ('/').");
         } else if (identityMap.isValidDocumentId(newDocumentId)) {
-            Document newDocument = identityMap.get(parent.getPublication(),
-                    area,
-                    newDocumentId,
-                    language);
+            Document newDocument = identityMap.get(publication, area, newDocumentId, language);
 
             if (newDocument.exists()) {
                 errorMessages.add("A document with this ID already exists.");

Modified: lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/ChangeNodeID.java
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/ChangeNodeID.java?view=diff&r1=160632&r2=160633
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/ChangeNodeID.java (original)
+++ lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/ChangeNodeID.java Fri Apr  8 15:35:41 2005
@@ -17,6 +17,7 @@
 package org.apache.lenya.cms.site.usecases;
 
 import org.apache.avalon.framework.service.ServiceException;
+import org.apache.avalon.framework.service.ServiceSelector;
 import org.apache.lenya.cms.publication.Document;
 import org.apache.lenya.cms.publication.DocumentBuildException;
 import org.apache.lenya.cms.publication.DocumentException;
@@ -24,7 +25,10 @@
 import org.apache.lenya.cms.publication.DocumentManager;
 import org.apache.lenya.cms.publication.Publication;
 import org.apache.lenya.cms.publication.PublicationException;
+import org.apache.lenya.cms.site.SiteManager;
 import org.apache.lenya.cms.usecase.DocumentUsecase;
+import org.apache.lenya.cms.usecase.UsecaseException;
+import org.apache.lenya.transaction.TransactionException;
 
 /**
  * Change the node ID of a document.
@@ -47,6 +51,41 @@
     }
 
     /**
+     * @see org.apache.lenya.cms.usecase.Usecase#lockInvolvedObjects()
+     */
+    public void lockInvolvedObjects() throws UsecaseException {
+        super.lockInvolvedObjects();
+
+        SiteManager siteManager = null;
+        ServiceSelector selector = null;
+        try {
+            Document doc = getSourceDocument();
+            selector = (ServiceSelector) this.manager.lookup(SiteManager.ROLE + "Selector");
+            siteManager = (SiteManager) selector.select(doc.getPublication().getSiteManagerHint());
+            siteManager.getSiteStructure(doc.getIdentityMap(), doc.getPublication(), doc.getArea())
+                    .lock();
+            
+            lockAllLanguageVersions(doc);
+        } catch (Exception e) {
+            throw new UsecaseException(e);
+        } finally {
+            if (selector != null) {
+                if (siteManager != null) {
+                    selector.release(siteManager);
+                }
+                this.manager.release(selector);
+            }
+        }
+    }
+
+    protected void lockAllLanguageVersions(Document doc) throws DocumentException, TransactionException, DocumentBuildException {
+        String[] languages = doc.getLanguages();
+        for (int i = 0; i < languages.length; i++) {
+            doc.getIdentityMap().getLanguageVersion(doc, languages[i]).lock();
+        }
+    }
+
+    /**
      * @see org.apache.lenya.cms.usecase.AbstractUsecase#doCheckPreconditions()
      */
     protected void doCheckPreconditions() throws Exception {
@@ -79,8 +118,12 @@
         DocumentManager documentManager = null;
         try {
             documentManager = (DocumentManager) this.manager.lookup(DocumentManager.ROLE);
-            String[] messages = documentManager.canCreate(identityMap, getSourceDocument()
-                    .getArea(), parent, nodeId, getSourceDocument().getLanguage());
+            String[] messages = documentManager.canCreate(identityMap,
+                    getSourceDocument().getPublication(),
+                    getSourceDocument().getArea(),
+                    parent,
+                    nodeId,
+                    getSourceDocument().getLanguage());
             addErrorMessages(messages);
         } finally {
             if (documentManager != null) {
@@ -139,12 +182,15 @@
                 Document newLanguageVersion = identityMap.get(document.getPublication(), document
                         .getArea(), newDocumentId, availableLanguages[i]);
 
+                newLanguageVersion.lock();
                 documentManager.move(languageVersion, newLanguageVersion);
 
                 if (availableLanguages[i].equals(document.getLanguage())) {
                     newDocument = newLanguageVersion;
                 }
             }
+        } catch (TransactionException e) {
+            throw new PublicationException(e);
         } finally {
             if (documentManager != null) {
                 this.manager.release(documentManager);

Modified: lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/CreateDocument.java
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/CreateDocument.java?view=diff&r1=160632&r2=160633
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/CreateDocument.java (original)
+++ lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/CreateDocument.java Fri Apr  8 15:35:41 2005
@@ -67,6 +67,7 @@
         try {
             documentManager = (DocumentManager) this.manager.lookup(DocumentManager.ROLE);
             String[] messages = documentManager.canCreate(getDocumentIdentityMap(),
+                    getPublication(),
                     getArea(),
                     parent,
                     nodeId,

Modified: lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/LinkRewriterImpl.java
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/LinkRewriterImpl.java?view=diff&r1=160632&r2=160633
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/LinkRewriterImpl.java (original)
+++ lenya/trunk/src/java/org/apache/lenya/cms/site/usecases/LinkRewriterImpl.java Fri Apr  8 15:35:41 2005
@@ -16,9 +16,6 @@
  */
 package org.apache.lenya.cms.site.usecases;
 
-import java.io.OutputStream;
-import java.io.OutputStreamWriter;
-import java.io.Writer;
 import java.util.Map;
 
 import org.apache.avalon.framework.context.Context;
@@ -32,8 +29,7 @@
 import org.apache.cocoon.components.ContextHelper;
 import org.apache.cocoon.environment.ObjectModelHelper;
 import org.apache.cocoon.environment.Request;
-import org.apache.excalibur.source.ModifiableSource;
-import org.apache.excalibur.source.SourceResolver;
+import org.apache.lenya.cms.cocoon.source.SourceUtil;
 import org.apache.lenya.cms.publication.Document;
 import org.apache.lenya.cms.publication.DocumentBuilder;
 import org.apache.lenya.cms.publication.DocumentIdentityMap;
@@ -41,7 +37,6 @@
 import org.apache.lenya.cms.publication.DocumentTypeResolver;
 import org.apache.lenya.cms.publication.Publication;
 import org.apache.lenya.cms.site.SiteManager;
-import org.apache.lenya.xml.DocumentHelper;
 import org.apache.xpath.XPathAPI;
 import org.w3c.dom.Attr;
 import org.w3c.dom.Node;
@@ -91,7 +86,6 @@
         }
 
         DocumentTypeResolver doctypeResolver = null;
-        SourceResolver sourceResolver = null;
 
         Request request = ObjectModelHelper.getRequest(this.objectModel);
         String contextPath = request.getContextPath();
@@ -100,7 +94,6 @@
             for (int documentIndex = 0; documentIndex < documents.length; documentIndex++) {
 
                 doctypeResolver = (DocumentTypeResolver) manager.lookup(DocumentTypeResolver.ROLE);
-                sourceResolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
 
                 Document examinedDocument = documents[documentIndex];
                 if (examinedDocument.exists()) {
@@ -113,14 +106,10 @@
 
                     DocumentType doctype = doctypeResolver.resolve(examinedDocument);
                     String[] xPaths = doctype.getLinkAttributeXPaths();
-                    ModifiableSource source = null;
 
                     try {
 
-                        source = (ModifiableSource) sourceResolver.resolveURI(examinedDocument
-                                .getSourceURI());
-                        org.w3c.dom.Document xmlDocument = DocumentHelper.readDocument(source
-                                .getInputStream());
+                        org.w3c.dom.Document xmlDocument = SourceUtil.readDOM(examinedDocument.getSourceURI(), this.manager);
 
                         for (int xPathIndex = 0; xPathIndex < xPaths.length; xPathIndex++) {
                             NodeList nodes = XPathAPI.selectNodeList(xmlDocument,
@@ -153,26 +142,11 @@
                         }
 
                         if (linksRewritten) {
-                            OutputStream oStream = source.getOutputStream();
-                            Writer writer = new OutputStreamWriter(oStream);
-                            DocumentHelper.writeDocument(xmlDocument, writer);
-                            if (oStream != null) {
-                                oStream.flush();
-                                try {
-                                    oStream.close();
-                                } catch (Throwable t) {
-                                    if (getLogger().isDebugEnabled()) {
-                                        getLogger().debug("Exception closing output stream: ", t);
-                                    }
-                                    throw new RuntimeException("Could not write document: ", t);
-                                }
-                            }
+                            examinedDocument.lock();
+                            SourceUtil.writeDOM(xmlDocument, examinedDocument.getSourceURI(), this.manager);
                         }
 
                     } finally {
-                        if (source != null) {
-                            sourceResolver.release(source);
-                        }
                     }
 
                 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@lenya.apache.org
For additional commands, e-mail: commits-help@lenya.apache.org