r1123 - in xwiki/trunk/src/main/java/com/xpn/xwiki: api doc plugin/packaging store

Ludovic Dubost ludovic at users.forge.objectweb.org
Tue Aug 8 22:13:34 CEST 2006


Author: ludovic
Date: 2006-08-08 22:13:33 +0200 (Tue, 08 Aug 2006)
New Revision: 1123

Modified:
   xwiki/trunk/src/main/java/com/xpn/xwiki/api/Document.java
   xwiki/trunk/src/main/java/com/xpn/xwiki/doc/XWikiDocument.java
   xwiki/trunk/src/main/java/com/xpn/xwiki/doc/XWikiDocumentArchive.java
   xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/packaging/Package.java
   xwiki/trunk/src/main/java/com/xpn/xwiki/store/XWikiHibernateStore.java
   xwiki/trunk/src/main/java/com/xpn/xwiki/store/XWikiHibernateVersioningStore.java
   xwiki/trunk/src/main/java/com/xpn/xwiki/store/XWikiVersioningStoreInterface.java
Log:
Changed way to handle Archives as it would break reading and saving an XWikiDocument using fromXML. It would loose the version archive file.
Now archives are handled using SoftReferences which allow the garbage collector to discards them but still have them for saving. The reference to the archive is saved in the XWikiContext object to make sure it is not discarded during a request.

Modified: xwiki/trunk/src/main/java/com/xpn/xwiki/api/Document.java
===================================================================
--- xwiki/trunk/src/main/java/com/xpn/xwiki/api/Document.java	2006-08-08 09:12:45 UTC (rev 1122)
+++ xwiki/trunk/src/main/java/com/xpn/xwiki/api/Document.java	2006-08-08 20:13:33 UTC (rev 1123)
@@ -37,7 +37,6 @@
 import java.util.Vector;
 
 import org.suigeneris.jrcs.diff.DifferentiationFailedException;
-import org.suigeneris.jrcs.rcs.Archive;
 import org.suigeneris.jrcs.rcs.Version;
 
 import com.xpn.xwiki.XWikiContext;
@@ -45,6 +44,7 @@
 import com.xpn.xwiki.doc.XWikiAttachment;
 import com.xpn.xwiki.doc.XWikiDocument;
 import com.xpn.xwiki.doc.XWikiLock;
+import com.xpn.xwiki.doc.XWikiDocumentArchive;
 import com.xpn.xwiki.objects.BaseObject;
 import com.xpn.xwiki.objects.classes.BaseClass;
 import com.xpn.xwiki.stats.impl.DocumentStats;
@@ -197,9 +197,13 @@
     }
 
     public String getArchive() throws XWikiException {
-        return doc.getArchive(context);
+        return doc.getDocumentArchive(context).getArchiveAsString();
     }
 
+    public XWikiDocumentArchive getDocumentArchive() throws XWikiException {
+        return doc.getDocumentArchive(context);
+    }
+
     public boolean isNew() {
         return doc.isNew();
     }

Modified: xwiki/trunk/src/main/java/com/xpn/xwiki/doc/XWikiDocument.java
===================================================================
--- xwiki/trunk/src/main/java/com/xpn/xwiki/doc/XWikiDocument.java	2006-08-08 09:12:45 UTC (rev 1122)
+++ xwiki/trunk/src/main/java/com/xpn/xwiki/doc/XWikiDocument.java	2006-08-08 20:13:33 UTC (rev 1123)
@@ -48,13 +48,11 @@
 import org.suigeneris.jrcs.diff.DifferentiationFailedException;
 import org.suigeneris.jrcs.diff.Revision;
 import org.suigeneris.jrcs.rcs.Version;
-import org.suigeneris.jrcs.rcs.Archive;
 import org.suigeneris.jrcs.util.ToString;
 import org.apache.commons.lang.StringUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.ecs.filter.CharacterFilter;
-import org.apache.tools.ant.filters.StringInputStream;
 import org.apache.velocity.VelocityContext;
 import org.apache.velocity.app.tools.VelocityFormatter;
 import org.dom4j.Document;
@@ -74,6 +72,7 @@
 import java.util.*;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipOutputStream;
+import java.lang.ref.SoftReference;
 
 
 public class XWikiDocument {
@@ -130,6 +129,10 @@
 
     private Object wikiNode;
 
+    // We are using a SoftReference which will allow the archive to be
+    // discarded by the Garbage collector as long as the context is closed (usually during the request)
+    private SoftReference archive;
+
     private XWikiStoreInterface store;
 
     public XWikiStoreInterface getStore(XWikiContext context) {
@@ -557,15 +560,38 @@
         return context.getURLFactory().getURL(url, context);
     }
 
-    public Archive getRCSArchive(XWikiContext context) throws XWikiException {
-        return getVersioningStore(context).getXWikiDocRCSArchive(this, context);
+    public XWikiDocumentArchive getDocumentArchive(XWikiContext context) throws XWikiException {
+        loadArchive(context);
+        return getDocumentArchive();
     }
 
-    public String getArchive(XWikiContext context) throws XWikiException {
-        return getVersioningStore(context).getXWikiDocArchive(this, context);
+    public void loadArchive(XWikiContext context) throws XWikiException {
+        if (archive==null) {
+            XWikiDocumentArchive arch = getVersioningStore(context).getXWikiDocumentArchive(this, context);
+            // We are using a SoftReference which will allow the archive to be
+            // discarded by the Garbage collector as long as the context is closed (usually during the request)
+            archive = new SoftReference(arch);
+        }
     }
 
+    public XWikiDocumentArchive getDocumentArchive() {
+        // We are using a SoftReference which will allow the archive to be
+        // discarded by the Garbage collector as long as the context is closed (usually during the request)
+        return (XWikiDocumentArchive) archive.get();
+    }
 
+    public void setDocumentArchive(XWikiDocumentArchive arch) {
+        // We are using a SoftReference which will allow the archive to be
+        // discarded by the Garbage collector as long as the context is closed (usually during the request)
+        this.archive = new SoftReference(arch);
+    }
+
+    public void setDocumentArchive(String sarch) throws XWikiException {
+        XWikiDocumentArchive xda = new XWikiDocumentArchive(getId());
+        xda.setArchiveFromString(sarch);
+        setDocumentArchive(xda);
+    }
+
     public Version[] getRevisions(XWikiContext context) throws XWikiException {
         return getVersioningStore(context).getXWikiDocVersions(this, context);
     }
@@ -1174,6 +1200,7 @@
         }
 
         doc.setRCSVersion(getRCSVersion());
+        doc.setDocumentArchive(getDocumentArchive());
         doc.setAuthor(getAuthor());
         doc.setContentAuthor(getContentAuthor());
         doc.setContent(getContent());
@@ -1431,7 +1458,7 @@
         el = new DOMElement("version");
         el.addText(getVersion());
         docel.add(el);
-        
+
         el = new DOMElement("title");
         el.addText(getTitle());
         docel.add(el);
@@ -1452,7 +1479,7 @@
             if (bclass.getFieldList().size() > 0) {
                 docel.add(bclass.toXML(null));
             }
-            
+
             // Add Objects
             Iterator it = getxWikiObjects().values().iterator();
             while (it.hasNext()) {
@@ -1494,7 +1521,7 @@
         if (bWithVersions) {
             el = new DOMElement("versions");
             try {
-                el.addText(getArchive(context));
+                el.addText(getDocumentArchive(context).getArchiveAsString());
             } catch (XWikiException e) {
                 return null;
             }
@@ -1592,13 +1619,10 @@
         else
             setTranslation(Integer.parseInt(strans));
 
-/*
-  TODO: set archive from XML
         String archive = getElement(docel, "versions");
         if (withArchive && archive != null && archive.length() > 0) {
-            setArchive(archive);
+            setDocumentArchive(archive);
         }
-*/
 
         String sdate = getElement(docel, "date");
         if (!sdate.equals("")) {
@@ -1635,6 +1659,10 @@
             bobject.fromXML(objel);
             addObject(bobject.getClassName(), bobject);
         }
+
+        // We have been reading from XML so the document does not need a new version when saved
+        setMetaDataDirty(false);
+        setContentDirty(false);
     }
 
     public void setAttachmentList(List list) {
@@ -1695,7 +1723,7 @@
                 context.setDatabase(getDatabase());
             try{
                context.getWiki().getAttachmentStore().deleteXWikiAttachment(attachment, context, true);
-            }catch(java.lang.OutOfMemoryError e){                
+            }catch(java.lang.OutOfMemoryError e){
                 throw new XWikiException(XWikiException.MODULE_XWIKI_APP,
                     XWikiException.ERROR_XWIKI_APP_JAVA_HEAP_SPACE,
                     "Out Of Memory Exception");

Modified: xwiki/trunk/src/main/java/com/xpn/xwiki/doc/XWikiDocumentArchive.java
===================================================================
--- xwiki/trunk/src/main/java/com/xpn/xwiki/doc/XWikiDocumentArchive.java	2006-08-08 09:12:45 UTC (rev 1122)
+++ xwiki/trunk/src/main/java/com/xpn/xwiki/doc/XWikiDocumentArchive.java	2006-08-08 20:13:33 UTC (rev 1123)
@@ -37,7 +37,7 @@
         this.archive = archive;
     }
 
-    public String getArchive() throws XWikiException {
+    public String getArchiveAsString() throws XWikiException {
         if (archive == null)
             return "";
         else {
@@ -47,7 +47,7 @@
         }
     }
 
-    public void setArchive(String text) throws XWikiException {
+    public void setArchiveFromString(String text) throws XWikiException {
         try {
             if ((text!=null)&&(!text.trim().equals(""))) {
                 StringInputStream is = new StringInputStream(text);
@@ -99,7 +99,7 @@
             return false;
 
         try {
-            if (!getArchive().equals(doc.getArchive()))
+            if (!getArchiveAsString().equals(doc.getArchiveAsString()))
                 return false;
         } catch (XWikiException e) {
             return false;

Modified: xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/packaging/Package.java
===================================================================
--- xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/packaging/Package.java	2006-08-08 09:12:45 UTC (rev 1122)
+++ xwiki/trunk/src/main/java/com/xpn/xwiki/plugin/packaging/Package.java	2006-08-08 20:13:33 UTC (rev 1123)
@@ -56,7 +56,6 @@
 import com.xpn.xwiki.doc.XWikiDocument;
 
 
-
 public class Package {
     private static final Log log = LogFactory.getLog(Package.class);
 
@@ -194,7 +193,7 @@
     public boolean updateDoc(String docFullName, int action, XWikiContext context) throws XWikiException {
         XWikiDocument doc = new XWikiDocument();
         doc.setFullName(docFullName, context);
-        return add(doc, action, context);           
+        return add(doc, action, context);
     }
 
     public boolean add(String docFullName, int DefaultAction, XWikiContext context) throws XWikiException {
@@ -449,6 +448,7 @@
                 // So we need to cancel the dirty status
                 doc.getDoc().setContentDirty(false);
                 doc.getDoc().setMetaDataDirty(false);
+
                 context.getWiki().saveDocument(doc.getDoc(), context);
                 doc.getDoc().saveAllAttachments(context);
             } catch (XWikiException e) {

Modified: xwiki/trunk/src/main/java/com/xpn/xwiki/store/XWikiHibernateStore.java
===================================================================
--- xwiki/trunk/src/main/java/com/xpn/xwiki/store/XWikiHibernateStore.java	2006-08-08 09:12:45 UTC (rev 1122)
+++ xwiki/trunk/src/main/java/com/xpn/xwiki/store/XWikiHibernateStore.java	2006-08-08 20:13:33 UTC (rev 1123)
@@ -235,15 +235,20 @@
                     doc.setContentAuthor(doc.getAuthor());
                 }
                 doc.incrementVersion();
-                //  TODO: versioning change this
                 context.getWiki().getVersioningStore().updateXWikiDocArchive(doc, doc.toXML(context), false, context);
             } else {
-                // Make sure the getArchive call has been made once
-                // with a valid context
-                try {
-                    doc.getArchive(context);
-                } catch (XWikiException e) {
-                    // this is a non critical error
+                if (doc.getDocumentArchive()!=null) {
+                    // Let's make sure we save the archive if we have one
+                    // This is especially needed if we load a document from XML
+                    context.getWiki().getVersioningStore().saveXWikiDocArchive(doc.getDocumentArchive(),false, context);
+                } else {
+                    // Make sure the getArchive call has been made once
+                    // with a valid context
+                    try {
+                        doc.getDocumentArchive(context);
+                    } catch (XWikiException e) {
+                        // this is a non critical error
+                    }
                 }
             }
 

Modified: xwiki/trunk/src/main/java/com/xpn/xwiki/store/XWikiHibernateVersioningStore.java
===================================================================
--- xwiki/trunk/src/main/java/com/xpn/xwiki/store/XWikiHibernateVersioningStore.java	2006-08-08 09:12:45 UTC (rev 1122)
+++ xwiki/trunk/src/main/java/com/xpn/xwiki/store/XWikiHibernateVersioningStore.java	2006-08-08 20:13:33 UTC (rev 1123)
@@ -48,7 +48,7 @@
 
     public Version[] getXWikiDocVersions(XWikiDocument doc, XWikiContext context) throws XWikiException {
         try {
-            Archive archive = getXWikiDocRCSArchive(doc, context);
+            Archive archive = getXWikiDocumentArchive(doc, context).getRCSArchive();
             if (archive==null)
                 return new Version[0];
 
@@ -65,17 +65,7 @@
         }
     }
 
-    public String getXWikiDocArchive(XWikiDocument doc, XWikiContext context) throws XWikiException {
-        try {
-            XWikiDocumentArchive archivedoc = getXWikiDocumentArchive(doc, context);
-            return archivedoc.getArchive();
-        } catch (Exception e) {
-            Object[] args = { doc.getFullName() };
-            throw new XWikiException( XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_READING_REVISIONS,
-                    "Exception while reading document {0} revisions", e, args);
-        }
-    }
-
+    /*
     public Archive getXWikiDocRCSArchive(XWikiDocument doc, XWikiContext context) throws XWikiException {
         try {
             XWikiDocumentArchive archivedoc = getXWikiDocumentArchive(doc, context);
@@ -86,23 +76,26 @@
                     "Exception while reading document {0} revisions", e, args);
         }
     }
+    */
 
     public XWikiDocumentArchive getXWikiDocumentArchive(XWikiDocument doc, XWikiContext context) throws XWikiException {
         String key = ((doc.getDatabase()==null)?"xwiki":doc.getDatabase()) + ":" + doc.getFullName();
         synchronized (key) {
-         XWikiDocumentArchive archivedoc = (XWikiDocumentArchive) context.getDocumentArchive(key);
-         if (archivedoc==null) {
-             String db = context.getDatabase();
-             try {
-                 if (doc.getDatabase()!=null)
-                     context.setDatabase(doc.getDatabase());
-                 archivedoc = new XWikiDocumentArchive(doc.getId());
-                 loadXWikiDocArchive(archivedoc, true, context);
-             } finally {
-                 context.setDatabase(db);
-             }
-          context.addDocumentArchive(key, archivedoc);
-         }
+            XWikiDocumentArchive archivedoc = (XWikiDocumentArchive) context.getDocumentArchive(key);
+            if (archivedoc==null) {
+                String db = context.getDatabase();
+                try {
+                    if (doc.getDatabase()!=null)
+                        context.setDatabase(doc.getDatabase());
+                    archivedoc = new XWikiDocumentArchive(doc.getId());
+                    loadXWikiDocArchive(archivedoc, true, context);
+                } finally {
+                    context.setDatabase(db);
+                }
+                // This will also make sure that the Archive has a strong reference
+                // and will not be discarded as long as the context exists.
+                context.addDocumentArchive(key, archivedoc);
+            }
             return archivedoc;
         }
     }
@@ -168,7 +161,7 @@
                 monitor.startTimer("hibernate");
             doc.setStore(basedoc.getStore());
 
-            Archive archive = getXWikiDocRCSArchive(doc, context);
+            Archive archive = getXWikiDocumentArchive(doc, context).getRCSArchive();
             Version v = null;
             try {
                 v = archive.getRevisionVersion(version);

Modified: xwiki/trunk/src/main/java/com/xpn/xwiki/store/XWikiVersioningStoreInterface.java
===================================================================
--- xwiki/trunk/src/main/java/com/xpn/xwiki/store/XWikiVersioningStoreInterface.java	2006-08-08 09:12:45 UTC (rev 1122)
+++ xwiki/trunk/src/main/java/com/xpn/xwiki/store/XWikiVersioningStoreInterface.java	2006-08-08 20:13:33 UTC (rev 1123)
@@ -12,10 +12,8 @@
     public void saveXWikiDocArchive(XWikiDocumentArchive archivedoc, boolean bTransaction, XWikiContext context) throws XWikiException;
     public void updateXWikiDocArchive(XWikiDocument doc, String text, boolean bTransaction, XWikiContext context) throws XWikiException;
     public Version[] getXWikiDocVersions(XWikiDocument doc, XWikiContext context) throws XWikiException;
-    public Archive getXWikiDocRCSArchive(XWikiDocument doc, XWikiContext context) throws XWikiException;
-    public String getXWikiDocArchive(XWikiDocument doc, XWikiContext context) throws XWikiException;
+    // public Archive getXWikiDocRCSArchive(XWikiDocument doc, XWikiContext context) throws XWikiException;
     public XWikiDocument loadXWikiDoc(XWikiDocument doc, String version, XWikiContext context) throws XWikiException;
     public void resetRCSArchive(XWikiDocument doc, boolean bTransaction, XWikiContext context) throws XWikiException;
-
-    XWikiDocumentArchive getXWikiDocumentArchive(XWikiDocument doc, XWikiContext context) throws XWikiException;
+    public XWikiDocumentArchive getXWikiDocumentArchive(XWikiDocument doc, XWikiContext context) throws XWikiException;
 }





More information about the Xwiki-notifications mailing list