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/14 15:52:25 UTC

svn commit: r161283 - in lenya/trunk/src/java/org/apache/lenya/cms: cocoon/source/RepositorySource.java repository/Node.java repository/SourceNode.java

Author: andreas
Date: Thu Apr 14 06:52:24 2005
New Revision: 161283

URL: http://svn.apache.org/viewcvs?view=rev&rev=161283
Log:
added support for non-XML repository sources

Modified:
    lenya/trunk/src/java/org/apache/lenya/cms/cocoon/source/RepositorySource.java
    lenya/trunk/src/java/org/apache/lenya/cms/repository/Node.java
    lenya/trunk/src/java/org/apache/lenya/cms/repository/SourceNode.java

Modified: lenya/trunk/src/java/org/apache/lenya/cms/cocoon/source/RepositorySource.java
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/cocoon/source/RepositorySource.java?view=diff&r1=161282&r2=161283
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/cocoon/source/RepositorySource.java (original)
+++ lenya/trunk/src/java/org/apache/lenya/cms/cocoon/source/RepositorySource.java Thu Apr 14 06:52:24 2005
@@ -31,7 +31,6 @@
 import org.apache.lenya.cms.repository.SourceNodeFactory;
 import org.apache.lenya.transaction.IdentityMap;
 import org.apache.lenya.transaction.TransactionException;
-import org.apache.lenya.xml.DocumentHelper;
 import org.w3c.dom.Document;
 
 /**
@@ -108,7 +107,7 @@
                 throw new RuntimeException("Cannot write to source [" + getURI() + "]: not locked!");
             }
             this.identityMap.getUnitOfWork().registerDirty(this.node);
-            return new DOMOutputStream();
+            return this.node.getOutputStream();
         } catch (TransactionException e) {
             throw new RuntimeException(e);
         }
@@ -219,27 +218,6 @@
 
         transformer.transform(new DOMSource(edoc), new StreamResult(pos));
 
-    }
-
-    /**
-     * DOM output stream.
-     */
-    private class DOMOutputStream extends ByteArrayOutputStream {
-        /**
-         * @see java.io.OutputStream#close()
-         */
-        public void close() throws IOException {
-            byte[] content = super.toByteArray();
-            InputStream in = new ByteArrayInputStream(content);
-            Document document;
-            try {
-                document = DocumentHelper.readDocument(in);
-            } catch (Exception e) {
-                throw new RuntimeException(e);
-            }
-            RepositorySource.this.node.setDocument(document);
-            super.close();
-        }
     }
 
     /**

Modified: lenya/trunk/src/java/org/apache/lenya/cms/repository/Node.java
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/repository/Node.java?view=diff&r1=161282&r2=161283
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/repository/Node.java (original)
+++ lenya/trunk/src/java/org/apache/lenya/cms/repository/Node.java Thu Apr 14 06:52:24 2005
@@ -16,13 +16,16 @@
  */
 package org.apache.lenya.cms.repository;
 
+import java.io.InputStream;
+import java.io.OutputStream;
+
 import org.apache.lenya.transaction.TransactionException;
 import org.apache.lenya.transaction.Transactionable;
 import org.w3c.dom.Document;
 
 /**
  * Repository node.
- *
+ * 
  * @version $Id:$
  */
 public interface Node extends Transactionable {
@@ -31,21 +34,33 @@
      * @return The DOM that holds the information.
      */
     Document getDocument();
-    
+
     /**
      * @param document The DOM that holds the information.
      */
     void setDocument(Document document);
-    
+
     /**
      * The identifiable type.
      */
     String IDENTIFIABLE_TYPE = "node";
-    
+
     /**
      * @return if the node exists.
      * @throws TransactionException if an error occurs.
      */
     boolean exists() throws TransactionException;
-    
-}
+
+    /**
+     * @return The input stream.
+     * @throws TransactionException if the node does not exist.
+     */
+    InputStream getInputStream() throws TransactionException;
+
+    /**
+     * @return The output stream.
+     * @throws TransactionException if the node does not exist.
+     */
+    OutputStream getOutputStream() throws TransactionException;
+
+}
\ No newline at end of file

Modified: lenya/trunk/src/java/org/apache/lenya/cms/repository/SourceNode.java
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/repository/SourceNode.java?view=diff&r1=161282&r2=161283
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/repository/SourceNode.java (original)
+++ lenya/trunk/src/java/org/apache/lenya/cms/repository/SourceNode.java Thu Apr 14 06:52:24 2005
@@ -16,12 +16,19 @@
  */
 package org.apache.lenya.cms.repository;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
 
 import org.apache.avalon.framework.logger.AbstractLogEnabled;
 import org.apache.avalon.framework.logger.Logger;
 import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.excalibur.source.ModifiableSource;
 import org.apache.excalibur.source.Source;
 import org.apache.excalibur.source.SourceResolver;
 import org.apache.lenya.ac.Identity;
@@ -34,6 +41,7 @@
 import org.apache.lenya.transaction.IdentityMap;
 import org.apache.lenya.transaction.Lock;
 import org.apache.lenya.transaction.TransactionException;
+import org.apache.lenya.xml.DocumentHelper;
 import org.w3c.dom.Document;
 
 /**
@@ -43,7 +51,6 @@
  */
 public class SourceNode extends AbstractLogEnabled implements Node {
 
-    private Document document;
     private String sourceUri;
     private ServiceManager manager;
     private IdentityMap identityMap;
@@ -66,14 +73,15 @@
      * @see org.apache.lenya.cms.repository.Node#getDocument()
      */
     public Document getDocument() {
+        Document document = null;
         try {
-            if (this.document == null && SourceUtil.exists(getRealSourceURI(), this.manager)) {
-                this.document = SourceUtil.readDOM(getRealSourceURI(), this.manager);
+            if (exists()) {
+                document = DocumentHelper.readDocument(getInputStream());
             }
         } catch (Exception e) {
             throw new RuntimeException(e);
         }
-        return this.document;
+        return document;
     }
 
     protected String getUserId() {
@@ -143,7 +151,7 @@
     protected String getRCPath() throws IOException {
         String publicationsPath = this.sourceUri.substring("lenya://lenya/pubs/".length());
         String publicationId = publicationsPath.split("/")[0];
-        String path = "lenya://lenya/pubs/" + publicationId + "/content/";
+        String path = "lenya://lenya/pubs/" + publicationId + "/";
         return this.sourceUri.substring(path.length());
     }
 
@@ -229,14 +237,54 @@
             throw new TransactionException("Cannot save node [" + this.sourceUri
                     + "]: not checked out!");
         }
-        try {
-            if (getLogger().isDebugEnabled()) {
-                getLogger().debug("Saving document [" + document.getDocumentElement().getNodeName()
-                        + "] to source [" + getRealSourceURI() + "]");
+        if (getLogger().isDebugEnabled()) {
+            getLogger().debug("Saving [" + this + "] to source [" + getRealSourceURI() + "]");
+        }
+
+        if (this.data != null) {
+            SourceResolver resolver = null;
+            ModifiableSource source = null;
+            InputStream in = null;
+            OutputStream out = null;
+            try {
+
+                resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
+                source = (ModifiableSource) resolver.resolveURI(getRealSourceURI());
+
+                out = source.getOutputStream();
+
+                byte[] buf = new byte[4096];
+                in = new ByteArrayInputStream(this.data);
+                int read = in.read(buf);
+
+                while (read > 0) {
+                    out.write(buf, 0, read);
+                    read = in.read(buf);
+                }
+
+            } catch (Exception e) {
+                throw new TransactionException(e);
+            } finally {
+
+                try {
+                    if (in != null) {
+                        in.close();
+                    }
+                    if (out != null) {
+                        out.flush();
+                        out.close();
+                    }
+                } catch (Throwable t) {
+                    throw new RuntimeException("Could not close streams: ", t);
+                }
+
+                if (resolver != null) {
+                    if (source != null) {
+                        resolver.release(source);
+                    }
+                    manager.release(resolver);
+                }
             }
-            SourceUtil.writeDOM(this.document, getRealSourceURI(), this.manager);
-        } catch (Exception e) {
-            throw new TransactionException(e);
         }
     }
 
@@ -303,14 +351,30 @@
             getLogger().debug("Setting document [" + document.getDocumentElement().getNodeName()
                     + "]");
         }
-        this.document = document;
+
+        Writer writer = null;
+        try {
+            writer = new OutputStreamWriter(getOutputStream());
+            DocumentHelper.writeDocument(document, writer);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        } finally {
+            if (writer != null) {
+                try {
+                    writer.close();
+                } catch (IOException e1) {
+                    throw new RuntimeException(e1);
+                }
+            }
+        }
     }
 
     /**
      * @see org.apache.lenya.cms.repository.Node#exists()
      */
     public boolean exists() throws TransactionException {
-        return getDocument() != null;
+        loadData();
+        return this.data != null;
     }
 
     /**
@@ -318,6 +382,90 @@
      */
     public String toString() {
         return "node " + this.sourceUri;
+    }
+
+    private byte[] data = null;
+
+    /**
+     * @see org.apache.lenya.cms.repository.Node#getInputStream()
+     */
+    public InputStream getInputStream() throws TransactionException {
+        if (!exists()) {
+            throw new RuntimeException(this + " does not exist!");
+        }
+        return new ByteArrayInputStream(this.data);
+    }
+
+    /**
+     * Loads the data from the real source.
+     * @throws TransactionException if an error occurs.
+     */
+    protected void loadData() throws TransactionException {
+
+        if (this.data != null) {
+            return;
+        }
+
+        ByteArrayOutputStream out = null;
+        InputStream in = null;
+        SourceResolver resolver = null;
+        Source source = null;
+        try {
+            resolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE);
+            source = resolver.resolveURI(getRealSourceURI());
+
+            if (source.exists()) {
+                byte[] buf = new byte[4096];
+                out = new ByteArrayOutputStream();
+                in = source.getInputStream();
+                int read = in.read(buf);
+
+                while (read > 0) {
+                    out.write(buf, 0, read);
+                    read = in.read(buf);
+                }
+
+                this.data = out.toByteArray();
+            }
+        } catch (Exception e) {
+            throw new TransactionException(e);
+        } finally {
+            try {
+                if (in != null)
+                    in.close();
+                if (out != null)
+                    out.close();
+            } catch (Exception e) {
+                throw new TransactionException(e);
+            }
+
+            if (resolver != null) {
+                if (source != null) {
+                    resolver.release(source);
+                }
+                this.manager.release(resolver);
+            }
+        }
+    }
+
+    /**
+     * @see org.apache.lenya.cms.repository.Node#getOutputStream()
+     */
+    public OutputStream getOutputStream() throws TransactionException {
+        return new NodeOutputStream();
+    }
+
+    /**
+     * Output stream.
+     */
+    private class NodeOutputStream extends ByteArrayOutputStream {
+        /**
+         * @see java.io.OutputStream#close()
+         */
+        public void close() throws IOException {
+            SourceNode.this.data = super.toByteArray();
+            super.close();
+        }
     }
 
 }



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