jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tri...@apache.org
Subject svn commit: r1569161 - in /jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault: fs/io/ packaging/ packaging/impl/ util/
Date Tue, 18 Feb 2014 01:32:27 GMT
Author: tripod
Date: Tue Feb 18 01:32:26 2014
New Revision: 1569161

URL: http://svn.apache.org/r1569161
Log:
JCRVLT-31 Packages defined by a stream are copied to a file first

- create memory archive and is fed when the package is uploaded
- also use memory archive if package size is < 1mb.

Added:
    jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/io/MemoryArchive.java
    jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/util/InputStreamPump.java
Modified:
    jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/io/FileArchive.java
    jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/io/JcrArchive.java
    jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/io/ZipArchive.java
    jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/JcrPackageDefinition.java
    jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/impl/JcrPackageDefinitionImpl.java
    jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/impl/JcrPackageImpl.java
    jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/impl/JcrPackageManagerImpl.java
    jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/impl/ZipVaultPackage.java

Modified: jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/io/FileArchive.java
URL: http://svn.apache.org/viewvc/jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/io/FileArchive.java?rev=1569161&r1=1569160&r2=1569161&view=diff
==============================================================================
--- jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/io/FileArchive.java
(original)
+++ jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/io/FileArchive.java
Tue Feb 18 01:32:26 2014
@@ -47,6 +47,9 @@ public class FileArchive extends Abstrac
     }
 
     public void open(boolean strict) throws IOException {
+        if (jcrRoot != null) {
+            return;
+        }
         eRoot = ExportRoot.findRoot(file);
         if (!eRoot.isValid()) {
             throw new IOException("No " + Constants.ROOT_DIR + " found.");

Modified: jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/io/JcrArchive.java
URL: http://svn.apache.org/viewvc/jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/io/JcrArchive.java?rev=1569161&r1=1569160&r2=1569161&view=diff
==============================================================================
--- jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/io/JcrArchive.java
(original)
+++ jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/io/JcrArchive.java
Tue Feb 18 01:32:26 2014
@@ -74,6 +74,9 @@ public class JcrArchive extends Abstract
     }
 
     public void open(boolean strict) throws IOException {
+        if (jcrRoot != null) {
+            return;
+        }
         try {
             if (archiveRoot.hasNode(Constants.ROOT_DIR)) {
                 jcrRoot = new JcrEntry(archiveRoot.getNode(Constants.ROOT_DIR), Constants.ROOT_DIR,
true);

Added: jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/io/MemoryArchive.java
URL: http://svn.apache.org/viewvc/jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/io/MemoryArchive.java?rev=1569161&view=auto
==============================================================================
--- jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/io/MemoryArchive.java
(added)
+++ jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/io/MemoryArchive.java
Tue Feb 18 01:32:26 2014
@@ -0,0 +1,238 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.vault.fs.io;
+
+import java.io.BufferedInputStream;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.jackrabbit.vault.fs.api.VaultInputSource;
+import org.apache.jackrabbit.vault.fs.config.DefaultMetaInf;
+import org.apache.jackrabbit.vault.fs.config.MetaInf;
+import org.apache.jackrabbit.vault.fs.config.VaultSettings;
+import org.apache.jackrabbit.vault.fs.spi.CNDReader;
+import org.apache.jackrabbit.vault.fs.spi.ServiceProviderFactory;
+import org.apache.jackrabbit.vault.util.Constants;
+import org.apache.jackrabbit.vault.util.InputStreamPump;
+import org.apache.jackrabbit.vault.util.Text;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * {@code MemoryArchive}...
+ */
+public class MemoryArchive extends AbstractArchive implements InputStreamPump.Pump {
+
+    /**
+     * default logger
+     */
+    private static final Logger log = LoggerFactory.getLogger(MemoryArchive.class);
+
+    private final VirtualEntry root;
+
+    private final DefaultMetaInf inf;
+
+    private boolean cacheMetaOnly = false;
+
+    public MemoryArchive(boolean metaOnly) throws IOException {
+        this.cacheMetaOnly = metaOnly;
+        root = new VirtualEntry("", 0, null);
+        inf = new DefaultMetaInf();
+    }
+
+    public void run(InputStream in) throws Exception {
+        // scan the zip and copy data to temporary file
+        ZipInputStream zin = new ZipInputStream(
+                new BufferedInputStream(in)
+        );
+        ZipEntry entry;
+        boolean hasRoot = false;
+        while ((entry = zin.getNextEntry()) != null) {
+            String name = entry.getName();
+            boolean isMeta = name.startsWith(Constants.META_DIR + "/");
+            if (!hasRoot && name.startsWith(Constants.ROOT_DIR + "/")) {
+                hasRoot = true;
+            }
+            if (isMeta || !cacheMetaOnly) {
+                String[] names = Text.explode(name, '/');
+                byte[] data = entry.isDirectory() ? null : IOUtils.toByteArray(zin);
+                if (names.length > 0) {
+                    VirtualEntry je = root;
+                    for (int i=0; i<names.length; i++) {
+                        if (i == names.length -1 && !entry.isDirectory()) {
+                            je = je.add(names[i], entry.getTime(), data);
+                        } else {
+                            je = je.add(names[i], 0, null);
+                        }
+                    }
+                    if (log.isDebugEnabled()) {
+                        log.debug("scanning jar: {}", name);
+                    }
+                }
+                if (isMeta) {
+                    String path = "InputStream:" + name;
+                    name = name.substring((Constants.META_DIR + "/").length());
+                    if (name.equals(Constants.FILTER_XML)) {
+                        // load filter
+                        inf.loadFilter(new ByteArrayInputStream(data), path);
+                    } else if (name.equals(Constants.CONFIG_XML)) {
+                        // load config
+                        inf.loadConfig(new ByteArrayInputStream(data), path);
+                    } else if (name.equals(Constants.SETTINGS_XML)) {
+                        // load settings
+                        inf.loadSettings(new ByteArrayInputStream(data), path);
+                    } else if (name.equals(Constants.PROPERTIES_XML)) {
+                        // load properties
+                        inf.loadProperties(new ByteArrayInputStream(data), path);
+                    } else if (name.equals(Constants.PRIVILEGES_XML)) {
+                        // load privileges
+                        inf.loadPrivileges(new ByteArrayInputStream(data), path);
+                    } else if (name.equals(Constants.PACKAGE_DEFINITION_XML)) {
+                        inf.setHasDefinition(true);
+                        log.debug("Contains package definition {}.", path);
+                    } else if (name.endsWith(".cnd")) {
+                        try {
+                            Reader r = new InputStreamReader(new ByteArrayInputStream(data),
"utf8");
+                            CNDReader reader = ServiceProviderFactory.getProvider().getCNDReader();
+                            reader.read(r, entry.getName(), null);
+                            inf.getNodeTypes().add(reader);
+                            log.debug("Loaded nodetypes from {}.", path);
+                        } catch (IOException e1) {
+                            log.error("Error while reading CND: {}", e1.toString());
+                        }
+                    }
+                }
+            }
+        }
+        // ensure that root directory is present, even if we are not caching non-meta-inf
stuff
+        if (hasRoot && !root.children.containsKey(Constants.ROOT_DIR)) {
+            root.add(Constants.ROOT_DIR, 0, null);
+        }
+        if (inf.getSettings() == null) {
+            VaultSettings settings = new VaultSettings();
+            settings.getIgnoredNames().add(".svn");
+            inf.setSettings(settings);
+        }
+    }
+
+    public void open(boolean strict) throws IOException {
+    }
+
+    public InputStream openInputStream(Entry entry) throws IOException {
+        VirtualEntry ve = (VirtualEntry) entry;
+        if (ve == null || ve.data == null) {
+            return null;
+        }
+        return new ByteArrayInputStream(ve.data);
+    }
+
+    public VaultInputSource getInputSource(Entry entry) throws IOException {
+        final VirtualEntry ve = (VirtualEntry) entry;
+        if (ve == null) {
+            return null;
+        }
+        return new VaultInputSource() {
+
+            public InputStream getByteStream() {
+                return ve.data == null ? null : new ByteArrayInputStream(ve.data);
+            }
+
+            /**
+             * {@inheritDoc}
+             */
+            public long getContentLength() {
+                return ve.data == null ? -1 : ve.data.length;
+            }
+
+            /**
+             * {@inheritDoc}
+             */
+            public long getLastModified() {
+                return ve.time;
+            }
+
+        };
+    }
+
+    public Entry getRoot() throws IOException {
+        return root;
+    }
+
+    public MetaInf getMetaInf() {
+        return inf;
+    }
+
+    public void close() {
+    }
+
+    private static class VirtualEntry implements Entry {
+
+        private final String name;
+
+        private final long time;
+
+        private final byte[] data;
+
+        private Map<String, VirtualEntry> children;
+
+        private VirtualEntry(String name, long time, byte[] data) {
+            this.name = name;
+            this.time = time;
+            this.data = data;
+        }
+
+        public String getName() {
+            return name;
+        }
+
+        public boolean isDirectory() {
+            return data == null;
+        }
+
+        public Collection<? extends Entry> getChildren() {
+            return children.values();
+        }
+
+        public Entry getChild(String name) {
+            return children.get(name);
+        }
+
+        public VirtualEntry add(String name, long time, byte[] data) {
+            if (children != null) {
+                VirtualEntry ret = children.get(name);
+                if (ret != null) {
+                    return ret;
+                }
+            }
+            VirtualEntry ve = new VirtualEntry(name, time, data);
+            if (children == null) {
+                children = new LinkedHashMap<String, VirtualEntry>();
+            }
+            children.put(name, ve);
+            return ve;
+        }
+    }
+}
\ No newline at end of file

Modified: jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/io/ZipArchive.java
URL: http://svn.apache.org/viewvc/jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/io/ZipArchive.java?rev=1569161&r1=1569160&r2=1569161&view=diff
==============================================================================
--- jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/io/ZipArchive.java
(original)
+++ jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/fs/io/ZipArchive.java
Tue Feb 18 01:32:26 2014
@@ -28,6 +28,7 @@ import java.util.zip.ZipEntry;
 import java.util.zip.ZipFile;
 import java.util.zip.ZipInputStream;
 
+import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.io.input.CloseShieldInputStream;
 import org.apache.jackrabbit.vault.fs.api.VaultInputSource;
@@ -59,16 +60,22 @@ public class ZipArchive extends Abstract
 
     private Archive base;
 
-    private final File zipFile;
+    private File zipFile;
+
+    private final boolean isTempFile;
 
     public ZipArchive(File zipFile) {
-        this.zipFile = zipFile;
+        this(zipFile, false);
     }
 
+    public ZipArchive(File zipFile, boolean isTempFile) {
+        this.zipFile = zipFile;
+        this.isTempFile = isTempFile;
+    }
 
     public void open(boolean strict) throws IOException {
         if (inf != null) {
-            throw new IllegalStateException("already open.");
+            return;
         }
         // first load the meta info and count the entries
         ZipInputStream zin = new ZipInputStream(
@@ -213,6 +220,19 @@ public class ZipArchive extends Abstract
             base = null;
         }
         inf = null;
+        if (zipFile != null && isTempFile) {
+            FileUtils.deleteQuietly(zipFile);
+        }
+        zipFile = null;
+
+    }
+
+    public File getFile() {
+        return zipFile;
+    }
+
+    public long getFileSize() {
+        return zipFile == null ? -1 : zipFile.length();
     }
 
     @Override

Modified: jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/JcrPackageDefinition.java
URL: http://svn.apache.org/viewvc/jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/JcrPackageDefinition.java?rev=1569161&r1=1569160&r2=1569161&view=diff
==============================================================================
--- jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/JcrPackageDefinition.java
(original)
+++ jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/JcrPackageDefinition.java
Tue Feb 18 01:32:26 2014
@@ -27,6 +27,7 @@ import org.apache.jackrabbit.vault.fs.ap
 import org.apache.jackrabbit.vault.fs.api.WorkspaceFilter;
 import org.apache.jackrabbit.vault.fs.config.MetaInf;
 import org.apache.jackrabbit.vault.fs.io.AccessControlHandling;
+import org.apache.jackrabbit.vault.fs.io.Archive;
 
 /**
  * Specifies the interface of a package definition stored in the repository.
@@ -212,7 +213,7 @@ public interface JcrPackageDefinition {
     boolean isModified();
 
     /**
-     * Unwrapps the package definition to the underlying node.
+     * Unwraps the package definition to the underlying node.
      * @param pack the package
      * @param force if <code>true</code> unwrapping is forced
      * @param autoSave if <code>true</code> modifications are saved automatically

Modified: jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/impl/JcrPackageDefinitionImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/impl/JcrPackageDefinitionImpl.java?rev=1569161&r1=1569160&r2=1569161&view=diff
==============================================================================
--- jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/impl/JcrPackageDefinitionImpl.java
(original)
+++ jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/impl/JcrPackageDefinitionImpl.java
Tue Feb 18 01:32:26 2014
@@ -206,11 +206,22 @@ public class JcrPackageDefinitionImpl im
         }
         log.info("unwrapping package {}", pack == null ? "(unknown)" : pack.getId());
         long now = System.currentTimeMillis();
-        if (pack != null && pack.getFile() != null) {
-            MetaInf inf = pack.getMetaInf();
+        unwrap(pack == null ? null : pack.getArchive(), autoSave);
+        if (log.isDebugEnabled()) {
+            log.debug("unwrapping package {} completed in {}ms", getId(), System.currentTimeMillis()
- now);
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void unwrap(Archive archive, boolean autoSave)
+            throws RepositoryException, IOException {
+        if (archive != null) {
+            MetaInf inf = archive.getMetaInf();
             // explode definition if present
             if (inf.hasDefinition()) {
-                extractDefinition(pack.getArchive(), false);
+                extractDefinition(archive, false);
             }
             if (inf.getFilter() != null) {
                 writeFilter(inf.getFilter(), false);
@@ -224,9 +235,6 @@ public class JcrPackageDefinitionImpl im
         if (autoSave) {
             defNode.save();
         }
-        if (log.isDebugEnabled()) {
-            log.debug("unwrapping package {} completed in {}ms", getId(), System.currentTimeMillis()
- now);
-        }
     }
 
     /**

Modified: jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/impl/JcrPackageImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/impl/JcrPackageImpl.java?rev=1569161&r1=1569160&r2=1569161&view=diff
==============================================================================
--- jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/impl/JcrPackageImpl.java
(original)
+++ jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/impl/JcrPackageImpl.java
Tue Feb 18 01:32:26 2014
@@ -27,6 +27,7 @@ import java.util.Calendar;
 import java.util.LinkedList;
 import java.util.List;
 
+import javax.jcr.Binary;
 import javax.jcr.Node;
 import javax.jcr.Property;
 import javax.jcr.RepositoryException;
@@ -39,6 +40,7 @@ import org.apache.jackrabbit.vault.fs.ap
 import org.apache.jackrabbit.vault.fs.api.ProgressTrackerListener;
 import org.apache.jackrabbit.vault.fs.io.AccessControlHandling;
 import org.apache.jackrabbit.vault.fs.io.ImportOptions;
+import org.apache.jackrabbit.vault.fs.io.MemoryArchive;
 import org.apache.jackrabbit.vault.packaging.CyclicDependencyException;
 import org.apache.jackrabbit.vault.packaging.DependencyUtil;
 import org.apache.jackrabbit.vault.packaging.ExportOptions;
@@ -222,6 +224,36 @@ public class JcrPackageImpl implements J
         }
         return new JcrPackageImpl(node, (ZipVaultPackage) pack);
     }
+    /**
+     * Creates a new jcr vault package.
+     *
+     * @param parent the parent node
+     * @param pid the package id of the new package.
+     * @param bin the binary containing the zip
+     * @param archive the archive with the meta data
+     * @return the created jcr vault package.
+     * @throws RepositoryException if an repository error occurs
+     * @throws IOException if an I/O error occurs
+     *
+     * @since 3.1
+     */
+    public static JcrPackage createNew(Node parent, PackageId pid, Binary bin, MemoryArchive
archive)
+            throws RepositoryException, IOException {
+        Node node = parent.addNode(Text.getName(pid.getInstallationPath() + ".zip"), JcrConstants.NT_FILE);
+        Node content = node.addNode(JcrConstants.JCR_CONTENT, JcrConstants.NT_RESOURCE);
+        content.addMixin(NT_VLT_PACKAGE);
+        Node defNode = content.addNode(NN_VLT_DEFINITION);
+        JcrPackageDefinitionImpl def = new JcrPackageDefinitionImpl(defNode);
+        def.set(JcrPackageDefinition.PN_NAME, pid.getName(), false);
+        def.set(JcrPackageDefinition.PN_GROUP, pid.getGroup(), false);
+        def.set(JcrPackageDefinition.PN_VERSION, pid.getVersionString(), false);
+        def.touch(null, false);
+        content.setProperty(JcrConstants.JCR_LASTMODIFIED, Calendar.getInstance());
+        content.setProperty(JcrConstants.JCR_MIMETYPE, MIME_TYPE);
+        content.setProperty(JcrConstants.JCR_DATA, bin);
+        def.unwrap(archive, false);
+        return new JcrPackageImpl(node);
+    }
 
     /**
      * {@inheritDoc}
@@ -286,15 +318,33 @@ public class JcrPackageImpl implements J
      */
     public VaultPackage getPackage() throws RepositoryException, IOException {
         if (pack == null) {
-            File tmpFile = File.createTempFile("vaultpack", ".zip");
-            FileOutputStream out = FileUtils.openOutputStream(tmpFile);
-            // stay jcr 1.0 compatible
-            //noinspection deprecation
-            InputStream in = getData().getStream();
-            IOUtils.copy(in, out);
-            in.close();
-            out.close();
-            pack = new ZipVaultPackage(tmpFile, true);
+            long size = getSize();
+            if (size >= 0 && size < 1024*1024) {
+                MemoryArchive archive = new MemoryArchive(false);
+                InputStream in = getData().getStream();
+                try {
+                    archive.run(in);
+                } catch (Exception e) {
+                    throw new IOException("Error while reading stream", e);
+                } finally {
+                    in.close();
+                }
+                pack = new ZipVaultPackage(archive, true);
+            } else {
+                File tmpFile = File.createTempFile("vaultpack", ".zip");
+                FileOutputStream out = FileUtils.openOutputStream(tmpFile);
+                Binary bin = getData().getBinary();
+                InputStream in = null;
+                try {
+                    in = bin.getStream();
+                    IOUtils.copy(in, out);
+                } finally {
+                    IOUtils.closeQuietly(in);
+                    IOUtils.closeQuietly(out);
+                    bin.dispose();
+                }
+                pack = new ZipVaultPackage(tmpFile, true);
+            }
         }
         return pack;
     }

Modified: jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/impl/JcrPackageManagerImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/impl/JcrPackageManagerImpl.java?rev=1569161&r1=1569160&r2=1569161&view=diff
==============================================================================
--- jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/impl/JcrPackageManagerImpl.java
(original)
+++ jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/impl/JcrPackageManagerImpl.java
Tue Feb 18 01:32:26 2014
@@ -26,7 +26,9 @@ import java.util.Calendar;
 import java.util.Collections;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.Properties;
 
+import javax.jcr.Binary;
 import javax.jcr.ItemExistsException;
 import javax.jcr.Node;
 import javax.jcr.NodeIterator;
@@ -37,6 +39,8 @@ import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
 import org.apache.jackrabbit.vault.fs.api.ProgressTrackerListener;
 import org.apache.jackrabbit.vault.fs.api.WorkspaceFilter;
+import org.apache.jackrabbit.vault.fs.config.MetaInf;
+import org.apache.jackrabbit.vault.fs.io.MemoryArchive;
 import org.apache.jackrabbit.vault.fs.spi.CNDReader;
 import org.apache.jackrabbit.vault.fs.spi.NodeTypeInstaller;
 import org.apache.jackrabbit.vault.fs.spi.ServiceProviderFactory;
@@ -49,6 +53,7 @@ import org.apache.jackrabbit.vault.packa
 import org.apache.jackrabbit.vault.packaging.PackageId;
 import org.apache.jackrabbit.vault.packaging.VaultPackage;
 import org.apache.jackrabbit.vault.packaging.Version;
+import org.apache.jackrabbit.vault.util.InputStreamPump;
 import org.apache.jackrabbit.vault.util.JcrConstants;
 import org.apache.jackrabbit.vault.util.Text;
 import org.slf4j.Logger;
@@ -169,15 +174,81 @@ public class JcrPackageManagerImpl exten
      */
     public JcrPackage upload(InputStream in, boolean replace, boolean strict)
             throws RepositoryException, IOException {
-        File file = File.createTempFile("vault", ".tmp");
-        OutputStream out = FileUtils.openOutputStream(file);
+
+        MemoryArchive archive = new MemoryArchive(true);
+        InputStreamPump pump = new InputStreamPump(in , archive);
+
+        // this will cause the input stream to be consumed and the memory archive being initialized.
+        Binary bin = session.getValueFactory().createBinary(pump);
+        if (pump.getError() != null) {
+            Exception error = pump.getError();
+            log.error("Error while reading from input stream.", error);
+            bin.dispose();
+            throw new IOException("Error while reading from input stream", error);
+        }
+
+        if (archive.getJcrRoot() == null) {
+            String msg = "Stream is not a content package. Missing 'jcr_root'.";
+            log.error(msg);
+            bin.dispose();
+            throw new IOException(msg);
+        }
+
+        final MetaInf inf = archive.getMetaInf();
+        PackagePropertiesImpl props = new PackagePropertiesImpl() {
+            @Override
+            protected Properties getPropertiesMap() {
+                return inf.getProperties();
+            }
+        };
+        PackageId pid = props.getId();
+
+        // invalidate pid if path is unknown
+        if (pid == null || pid.getInstallationPath().equals(ZipVaultPackage.UNKNOWN_PATH))
{
+            bin.dispose();
+            throw new IOException("Package does not contain a path specification or valid
package id.");
+        }
+
+        // create parent node
+        String path = pid.getInstallationPath() + ".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;
+
+        if (parent.hasNode(name)) {
+            JcrPackage oldPackage = new JcrPackageImpl(parent.getNode(name));
+            JcrPackageDefinitionImpl oldDef = (JcrPackageDefinitionImpl) oldPackage.getDefinition();
+            if (oldDef != null) {
+                state = oldDef.getState();
+            }
+
+            if (replace) {
+                parent.getNode(name).remove();
+            } else {
+                throw new ItemExistsException("Package already exists: " + path);
+            }
+        }
+        JcrPackage jcrPack = null;
         try {
-            IOUtils.copy(in, out);
+            jcrPack = JcrPackageImpl.createNew(parent, pid, bin, archive);
+            if (jcrPack != null) {
+                JcrPackageDefinitionImpl def = (JcrPackageDefinitionImpl) jcrPack.getDefinition();
+                if (state != null) {
+                    def.setState(state);
+                }
+            }
+            return jcrPack;
         } finally {
-            IOUtils.closeQuietly(in);
-            IOUtils.closeQuietly(out);
+            bin.dispose();
+            if (jcrPack == null) {
+                session.refresh(false);
+            } else {
+                session.save();
+            }
         }
-        return upload(file, true, replace, null, strict);
     }
 
     /**

Modified: jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/impl/ZipVaultPackage.java
URL: http://svn.apache.org/viewvc/jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/impl/ZipVaultPackage.java?rev=1569161&r1=1569160&r2=1569161&view=diff
==============================================================================
--- jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/impl/ZipVaultPackage.java
(original)
+++ jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/packaging/impl/ZipVaultPackage.java
Tue Feb 18 01:32:26 2014
@@ -26,7 +26,6 @@ import java.util.regex.PatternSyntaxExce
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 
-import org.apache.commons.io.FileUtils;
 import org.apache.jackrabbit.vault.fs.config.MetaInf;
 import org.apache.jackrabbit.vault.fs.io.AccessControlHandling;
 import org.apache.jackrabbit.vault.fs.io.Archive;
@@ -52,26 +51,25 @@ public class ZipVaultPackage extends Pac
 
     public static final String UNKNOWN_PATH = "/etc/packages/unknown";
 
-    private File file;
-
     private Archive archive;
 
-    private boolean isTmpFile;
-
     protected ZipVaultPackage(File file, boolean isTmpFile) throws IOException {
         this(file, isTmpFile, false);
     }
 
     protected ZipVaultPackage(File file, boolean isTmpFile, boolean strict)
             throws IOException {
-        this.file = file;
-        this.isTmpFile = isTmpFile;
+        this(new ZipArchive(file, isTmpFile), strict);
+    }
+
+    protected ZipVaultPackage(Archive archive, boolean strict)
+            throws IOException {
+        this.archive = archive;
         if (strict) {
             try {
-                archive = new ZipArchive(file);
-                archive.open(strict);
+                archive.open(true);
             } catch (IOException e) {
-                log.error("Error while loading package {}.", file.getPath());
+                log.error("Error while loading package {}.", archive);
                 throw e;
             }
         }
@@ -85,10 +83,6 @@ public class ZipVaultPackage extends Pac
             archive.close();
             archive = null;
         }
-        if (file != null && isTmpFile) {
-            FileUtils.deleteQuietly(file);
-        }
-        file = null;
     }
 
     /**
@@ -96,17 +90,14 @@ public class ZipVaultPackage extends Pac
      */
     public Archive getArchive() {
         if (archive == null) {
-            if (file == null) {
-                log.error("Package already closed: {}", getId());
-                throw new IllegalStateException("Package already closed: " + getId());
-            }
-            archive = new ZipArchive(file);
-            try {
-                archive.open(false);
-            } catch (IOException e) {
-                log.error("Archive not valid.", e);
-                throw new IllegalStateException("Archive not valid for file " + file, e);
-            }
+            log.error("Package already closed: {}", getId());
+            throw new IllegalStateException("Package already closed: " + getId());
+        }
+        try {
+            archive.open(false);
+        } catch (IOException e) {
+            log.error("Archive not valid.", e);
+            throw new IllegalStateException("Archive not valid.", e);
         }
         return archive;
     }
@@ -126,7 +117,7 @@ public class ZipVaultPackage extends Pac
      * {@inheritDoc}
      */
     public boolean isClosed() {
-        return file == null;
+        return archive == null;
     }
 
     /**
@@ -134,7 +125,7 @@ public class ZipVaultPackage extends Pac
      * @return the file of this package or <code>null</code>.
      */
     public File getFile() {
-        return file;
+        return (archive instanceof ZipArchive) ? ((ZipArchive) archive).getFile() : null;
     }
 
     /**
@@ -152,9 +143,7 @@ public class ZipVaultPackage extends Pac
      * {@inheritDoc}
      */
     public long getSize() {
-        return file == null
-                ? -1
-                : file.length();
+        return (archive instanceof ZipArchive) ? ((ZipArchive) archive).getFileSize() : -1;
     }
 
     /**
@@ -177,8 +166,8 @@ public class ZipVaultPackage extends Pac
      * @param session repository session
      * @param opts import options
      *
-     * @throws RepositoryException if a repository error during installation occurs.
-     * @throws PackageException if an error during packaging occurs
+     * @throws javax.jcr.RepositoryException if a repository error during installation occurs.
+     * @throws org.apache.jackrabbit.vault.packaging.PackageException if an error during
packaging occurs
      * @throws IllegalStateException if the package is not valid.
      * @return installation context
      */
@@ -225,8 +214,8 @@ public class ZipVaultPackage extends Pac
      * @param ctx install context
      * @param subPackages receives the list of potential sub packages
      *
-     * @throws RepositoryException if a repository error during installation occurs.
-     * @throws PackageException if an error during packaging occurs
+     * @throws javax.jcr.RepositoryException if a repository error during installation occurs.
+     * @throws org.apache.jackrabbit.vault.packaging.PackageException if an error during
packaging occurs
      * @throws IllegalStateException if the package is not valid.
      */
     protected void extract(InstallContextImpl ctx,

Added: jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/util/InputStreamPump.java
URL: http://svn.apache.org/viewvc/jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/util/InputStreamPump.java?rev=1569161&view=auto
==============================================================================
--- jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/util/InputStreamPump.java
(added)
+++ jackrabbit/commons/filevault/trunk/vault-core/src/main/java/org/apache/jackrabbit/vault/util/InputStreamPump.java
Tue Feb 18 01:32:26 2014
@@ -0,0 +1,140 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.vault.util;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PipedInputStream;
+import java.io.PipedOutputStream;
+
+import org.apache.commons.io.input.CloseShieldInputStream;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * {@code InputStreamPump}...
+ */
+public class InputStreamPump extends InputStream {
+
+    /**
+     * default logger
+     */
+    private static final Logger log = LoggerFactory.getLogger(InputStreamPump.class);
+
+    private final InputStream source;
+
+    private final PipedOutputStream out;
+
+    private final PipedInputStream in;
+
+    private Thread pumpThread;
+
+    private Exception error;
+
+    public InputStreamPump(InputStream source, final Pump pump) throws IOException {
+        this.source = source;
+
+        out = new PipedOutputStream();
+        in = new PipedInputStream(out, 8192);
+
+        pumpThread = new Thread(new Runnable() {
+            public void run() {
+                try {
+                    pump.run(new CloseShieldInputStream(in));
+                    // ensure that input stream is pumping in case it didn't read to the
end
+                    byte[] buffer = new byte[8192];
+                    while (in.read(buffer) >= 0);
+                } catch (Exception e) {
+                    error = e;
+                    log.error("Error while processing input stream", e);
+                }
+            }
+        });
+        pumpThread.start();
+    }
+
+    public interface Pump {
+        void run(InputStream in) throws Exception;
+    }
+
+    public Exception getError() {
+        return error;
+    }
+
+    @Override
+    public int read() throws IOException {
+        int b = source.read();
+        if (b >= 0) {
+            out.write(b);
+        }
+        return b;
+    }
+
+    @Override
+    public int read(byte[] b) throws IOException {
+        int len = source.read(b);
+        if (len > 0) {
+            out.write(b, 0, len);
+        }
+        return len;
+    }
+
+    @Override
+    public int read(byte[] b, int off, int len) throws IOException {
+        int read = source.read(b, off, len);
+        if (read > 0) {
+            out.write(b, off, read);
+        }
+        return read;
+    }
+
+    @Override
+    public long skip(long n) throws IOException {
+        return source.skip(n);
+    }
+
+    @Override
+    public int available() throws IOException {
+        return source.available();
+    }
+
+    @Override
+    public void close() throws IOException {
+        source.close();
+        out.flush();
+        try {
+            out.close();
+            pumpThread.join();
+            in.close();
+        } catch (InterruptedException e) {
+            throw new IOException(e);
+        }
+    }
+
+    @Override
+    public void mark(int readlimit) {
+    }
+
+    @Override
+    public void reset() throws IOException {
+    }
+
+    @Override
+    public boolean markSupported() {
+        return false;
+    }
+}
\ No newline at end of file



Mime
View raw message