You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by kw...@apache.org on 2023/03/22 10:57:16 UTC

[jackrabbit-filevault] branch bugfix/JCRVLT-699-throw-away-old-state-for-snapshots created (now e4bf5139)

This is an automated email from the ASF dual-hosted git repository.

kwin pushed a change to branch bugfix/JCRVLT-699-throw-away-old-state-for-snapshots
in repository https://gitbox.apache.org/repos/asf/jackrabbit-filevault.git


      at e4bf5139 Refactor JcrPackageRegistry to reduce code duplication for upload

This branch includes the following new commits:

     new e4bf5139 Refactor JcrPackageRegistry to reduce code duplication for upload

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[jackrabbit-filevault] 01/01: Refactor JcrPackageRegistry to reduce code duplication for upload

Posted by kw...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

kwin pushed a commit to branch bugfix/JCRVLT-699-throw-away-old-state-for-snapshots
in repository https://gitbox.apache.org/repos/asf/jackrabbit-filevault.git

commit e4bf513918875ad245e0196aac75e6b87d854c77
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Wed Mar 22 11:57:08 2023 +0100

    Refactor JcrPackageRegistry to reduce code duplication for upload
    
    Both upload methods now use the same private method internally
---
 .../registry/impl/JcrPackageRegistry.java          | 139 +++++++++------------
 1 file changed, 56 insertions(+), 83 deletions(-)

diff --git a/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/registry/impl/JcrPackageRegistry.java b/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/registry/impl/JcrPackageRegistry.java
index 8d02e6b5..6f69d570 100644
--- a/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/registry/impl/JcrPackageRegistry.java
+++ b/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/registry/impl/JcrPackageRegistry.java
@@ -361,83 +361,6 @@ public class JcrPackageRegistry extends AbstractPackageRegistry {
         }
     }
 
-    public JcrPackage upload(InputStream in, boolean replace)
-            throws RepositoryException, IOException, PackageExistsException {
-
-        MemoryArchive archive = new MemoryArchive(true);
-        Binary bin;
-        try (InputStreamPump pump = new InputStreamPump(in , archive)) {
-            // this will cause the input stream to be consumed and the memory archive being initialized.
-            bin = session.getValueFactory().createBinary(pump);
-        }
-
-        if (archive.getJcrRoot() == null) {
-            String msg = "Stream is not a content package. Missing 'jcr_root'.";
-            bin.dispose();
-            archive.close();
-            throw new IOException(msg);
-        }
-
-        final MetaInf inf = archive.getMetaInf();
-        PackageId pid = inf.getPackageProperties().getId();
-
-        // invalidate pid if path is unknown
-        if (pid == null) {
-            pid = createRandomPid();
-        }
-        if (!pid.isValid()) {
-            bin.dispose();
-            archive.close();
-            throw new RepositoryException("Unable to create package. Illegal package name.");
-        }
-
-        // create parent node
-        String path = getInstallationPath(pid) + ".zip";
-        String parentPath = Text.getRelativeParent(path, 1);
-        String name = Text.getName(path);
-        Node parent = mkdir(parentPath, false);
-
-        // remember installation state properties (GRANITE-2018)
-        JcrPackageDefinitionImpl.State state = null;
-        Calendar oldCreatedDate = null;
-
-        if (parent.hasNode(name)) {
-            try (JcrPackage oldPackage = new JcrPackageImpl(this, parent.getNode(name))) {
-                JcrPackageDefinitionImpl oldDef = (JcrPackageDefinitionImpl) oldPackage.getDefinition();
-                if (oldDef != null) {
-                    state = oldDef.getState();
-                    oldCreatedDate = oldDef.getCreated();
-                }
-            }
-
-            if (replace) {
-                parent.getNode(name).remove();
-            } else {
-                archive.close();
-                throw new PackageExistsException("Package already exists: " + pid).setId(pid);
-            }
-        }
-        JcrPackage jcrPack = null;
-        try {
-            jcrPack = createNew(parent, pid, bin, archive);
-            JcrPackageDefinitionImpl def = (JcrPackageDefinitionImpl) jcrPack.getDefinition();
-            Calendar newCreateDate = def == null ? null : def.getCreated();
-            // only transfer the old package state to the new state in case both packages have the same create date
-            if (state != null && newCreateDate != null && oldCreatedDate != null && oldCreatedDate.compareTo(newCreateDate) == 0) {
-                def.setState(state);
-            }
-            dispatch(PackageEvent.Type.UPLOAD, pid, null);
-            return jcrPack;
-        } finally {
-            bin.dispose();
-            if (jcrPack == null) {
-                session.refresh(false);
-            } else {
-                session.save();
-            }
-        }
-    }
-
     @NotNull
     @Override
     public PackageId register(@NotNull File file, boolean replace) throws IOException, PackageExistsException {
@@ -455,6 +378,23 @@ public class JcrPackageRegistry extends AbstractPackageRegistry {
         throw new UnsupportedOperationException("linking files to repository persistence is not supported.");
     }
 
+    public JcrPackage upload(InputStream in, boolean replace)
+            throws RepositoryException, IOException, PackageExistsException {
+    
+        MemoryArchive archive = new MemoryArchive(true);
+        final Binary bin;
+        try (InputStreamPump pump = new InputStreamPump(in , archive)) {
+            // this will cause the input stream to be consumed and the memory archive being initialized.
+            bin = session.getValueFactory().createBinary(pump);
+        }
+        try {
+            return upload(archive, bin, replace);
+        } finally {
+            archive.close();
+            bin.dispose();
+        }
+    }
+
     /**
      * Uploads the given package to the JCR package manager,
      * @param pkg the package
@@ -466,16 +406,45 @@ public class JcrPackageRegistry extends AbstractPackageRegistry {
      */
     public JcrPackage upload(@NotNull ZipVaultPackage pkg, boolean replace) throws RepositoryException, IOException, PackageExistsException {
 
+        final Binary binary;
+        File file = pkg.getFile();
+        if (file != null) {
+            try (InputStream input = new FileInputStream(file)) {
+                binary = session.getValueFactory().createBinary(input);
+            }
+        } else {
+            try (InputStream input = new NullInputStream(0)) {
+                binary = session.getValueFactory().createBinary(input);
+            }
+        }
+        try {
+            // the archive is implicitly closed with the given ZipVaultPackage
+            return upload(pkg.getArchive(), binary, replace);
+        } finally {
+            binary.dispose();
+        }
+    }
+
+    /**
+     * 
+     * @param archive the source archive to upload
+     * @param binary the binary value holding the actual package, is not disposed
+     * @param replace whether to replace an existing package with the same id
+     * @return the newly uploaded package
+     * @throws IOException 
+     * @throws RepositoryException 
+     * @throws PackageExistsException 
+     */
+    private JcrPackage upload(@NotNull Archive archive, Binary binary, boolean replace) throws RepositoryException, IOException, PackageExistsException {
         // open zip packages
-        if (pkg.getArchive().getJcrRoot() == null) {
-            String msg = "Zip File is not a content package. Missing 'jcr_root'.";
+        if (archive.getJcrRoot() == null) {
+            String msg = "Given archive is not a content package. Missing 'jcr_root'.";
             log.error(msg);
-            pkg.close();
             throw new IOException(msg);
         }
 
         // invalidate pid if path is unknown
-        PackageId pid = pkg.getId();
+        PackageId pid = archive.getMetaInf().getPackageProperties().getId();
         if (pid == null) {
             pid = createRandomPid();
         }
@@ -491,12 +460,14 @@ public class JcrPackageRegistry extends AbstractPackageRegistry {
 
         // remember installation state properties (GRANITE-2018)
         JcrPackageDefinitionImpl.State state = null;
+        Calendar oldCreatedDate = null;
 
         if (parent.hasNode(name)) {
             try (JcrPackage oldPackage = new JcrPackageImpl(this, parent.getNode(name))) {
                 JcrPackageDefinitionImpl oldDef = (JcrPackageDefinitionImpl) oldPackage.getDefinition();
                 if (oldDef != null) {
                     state = oldDef.getState();
+                    oldCreatedDate = oldDef.getCreated();
                 }
             }
 
@@ -508,9 +479,11 @@ public class JcrPackageRegistry extends AbstractPackageRegistry {
         }
         JcrPackage jcrPack = null;
         try {
-            jcrPack = createNew(parent, pid, pkg, false);
+            jcrPack = createNew(parent, pid, binary, archive);
             JcrPackageDefinitionImpl def = (JcrPackageDefinitionImpl) jcrPack.getDefinition();
-            if (state != null && def != null) {
+            Calendar newCreateDate = def == null ? null : def.getCreated();
+            // only transfer the old package state to the new state in case both packages have the same create date
+            if (state != null && newCreateDate != null && oldCreatedDate != null && oldCreatedDate.compareTo(newCreateDate) == 0) {
                 def.setState(state);
             }
             dispatch(PackageEvent.Type.UPLOAD, pid, null);