You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by tm...@apache.org on 2018/03/14 10:12:18 UTC
[sling-org-apache-sling-distribution-core] branch master updated:
SLING-7541 - Allow to build Vault packages in memory
This is an automated email from the ASF dual-hosted git repository.
tmaret pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-distribution-core.git
The following commit(s) were added to refs/heads/master by this push:
new 8add861 SLING-7541 - Allow to build Vault packages in memory
8add861 is described below
commit 8add861c8a112d45edafbb74886126010d0bdf5f
Author: tmaret <tm...@adobe.com>
AuthorDate: Wed Mar 14 11:12:03 2018 +0100
SLING-7541 - Allow to build Vault packages in memory
---
.../impl/InMemoryDistributionPackage.java | 87 +++++++++++++
.../impl/InMemoryDistributionPackageBuilder.java | 138 +++++++++++++++++++++
.../VaultDistributionPackageBuilderFactory.java | 6 +
.../InMemoryDistributionPackageBuilderTest.java | 89 +++++++++++++
.../impl/InMemoryDistributionPackageTest.java | 47 +++++++
5 files changed, 367 insertions(+)
diff --git a/src/main/java/org/apache/sling/distribution/packaging/impl/InMemoryDistributionPackage.java b/src/main/java/org/apache/sling/distribution/packaging/impl/InMemoryDistributionPackage.java
new file mode 100644
index 0000000..4f2920e
--- /dev/null
+++ b/src/main/java/org/apache/sling/distribution/packaging/impl/InMemoryDistributionPackage.java
@@ -0,0 +1,87 @@
+/*
+ * 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.sling.distribution.packaging.impl;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import javax.annotation.Nonnull;
+
+import org.apache.sling.distribution.packaging.DistributionPackage;
+import org.apache.sling.distribution.packaging.DistributionPackageInfo;
+
+public class InMemoryDistributionPackage implements DistributionPackage {
+
+ private final String id;
+
+ private final String type;
+
+ private final long size;
+
+ private final byte[] data;
+
+ private final DistributionPackageInfo info;
+
+ public InMemoryDistributionPackage(String id, String type, byte[] data) {
+ this.id = id;
+ this.type = type;
+ this.data = data;
+ this.size = data.length;
+ this.info = new DistributionPackageInfo(type);
+ }
+
+ @Nonnull
+ @Override
+ public String getId() {
+ return id;
+ }
+
+ @Nonnull
+ @Override
+ public String getType() {
+ return type;
+ }
+
+ @Nonnull
+ @Override
+ public InputStream createInputStream() throws IOException {
+ return new ByteArrayInputStream(data);
+ }
+
+ @Override
+ public long getSize() {
+ return size;
+ }
+
+ @Override
+ public void close() {
+ }
+
+ @Override
+ public void delete() {
+ // nothing to do
+ }
+
+ @Nonnull
+ @Override
+ public DistributionPackageInfo getInfo() {
+ return info;
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/org/apache/sling/distribution/packaging/impl/InMemoryDistributionPackageBuilder.java b/src/main/java/org/apache/sling/distribution/packaging/impl/InMemoryDistributionPackageBuilder.java
new file mode 100644
index 0000000..72b733c
--- /dev/null
+++ b/src/main/java/org/apache/sling/distribution/packaging/impl/InMemoryDistributionPackageBuilder.java
@@ -0,0 +1,138 @@
+/*
+ * 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.sling.distribution.packaging.impl;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.NavigableMap;
+import java.util.UUID;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.distribution.DistributionRequest;
+import org.apache.sling.distribution.common.DistributionException;
+import org.apache.sling.distribution.packaging.DistributionPackage;
+import org.apache.sling.distribution.serialization.DistributionContentSerializer;
+import org.apache.sling.distribution.serialization.DistributionExportFilter;
+import org.apache.sling.distribution.serialization.DistributionExportOptions;
+import org.apache.sling.distribution.serialization.impl.vlt.VltUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Note: This Package Builder does not keep track of the package created.
+ */
+public class InMemoryDistributionPackageBuilder extends AbstractDistributionPackageBuilder {
+
+ private final Logger log = LoggerFactory.getLogger(getClass());
+
+ private final DistributionContentSerializer serializer;
+
+ private final NavigableMap<String, List<String>> nodeFilters;
+
+ private final NavigableMap<String, List<String>> propertyFilters;
+
+ public InMemoryDistributionPackageBuilder(@Nonnull String type,
+ @Nonnull DistributionContentSerializer serializer,
+ @Nullable String[] nodeFilters,
+ @Nullable String[] propertyFilters) {
+ super(type, serializer.getContentType(), serializer.isDeletionSupported());
+ this.serializer = serializer;
+ this.nodeFilters = VltUtils.parseFilters(nodeFilters);
+ this.propertyFilters = VltUtils.parseFilters(propertyFilters);
+ }
+
+ @Override
+ protected DistributionPackage createPackageForAdd(@Nonnull ResourceResolver resourceResolver,
+ @Nonnull DistributionRequest request)
+ throws DistributionException {
+
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ export(resourceResolver, request, baos);
+
+ String packageId = "dstrpck-" + System.currentTimeMillis() + "-" + UUID.randomUUID().toString();
+
+ return new InMemoryDistributionPackage(packageId, getType(), baos.toByteArray());
+ }
+
+ @Override
+ protected DistributionPackage readPackageInternal(@Nonnull ResourceResolver resourceResolver,
+ @Nonnull InputStream stream)
+ throws DistributionException {
+
+ Map<String, Object> info = new HashMap<String, Object>();
+ DistributionPackageUtils.readInfo(stream, info);
+
+ final String packageId;
+ Object remoteId = info.get(DistributionPackageUtils.PROPERTY_REMOTE_PACKAGE_ID);
+ if (remoteId != null) {
+ packageId = remoteId.toString();
+ log.debug("preserving remote id {}", packageId);
+ } else {
+ packageId = "distrpck-read-" + System.nanoTime();
+ log.debug("generating a new id {}", packageId);
+ }
+
+ try {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ IOUtils.copy(stream, baos);
+ baos.flush();
+
+ byte[] data = baos.toByteArray();
+ return new InMemoryDistributionPackage(packageId, getType(), data);
+ } catch (IOException e) {
+ throw new DistributionException(e);
+ }
+ }
+
+ @Override
+ protected boolean installPackageInternal(@Nonnull ResourceResolver resourceResolver,
+ @Nonnull InputStream inputStream)
+ throws DistributionException {
+ try {
+ serializer.importFromStream(resourceResolver, inputStream);
+ return true;
+ } finally {
+ IOUtils.closeQuietly(inputStream);
+ }
+ }
+
+ @Override
+ protected DistributionPackage getPackageInternal(@Nonnull ResourceResolver resourceResolver,
+ @Nonnull String id) {
+ return null;
+ }
+
+ private void export(@Nonnull ResourceResolver resourceResolver,
+ @Nonnull final DistributionRequest request,
+ @Nonnull OutputStream outputStream)
+ throws DistributionException {
+ final DistributionExportFilter filter = serializer.isRequestFiltering() ? null : DistributionExportFilter.createFilter(request, nodeFilters, propertyFilters);
+ DistributionExportOptions distributionExportOptions = new DistributionExportOptions(request, filter);
+ serializer.exportToStream(resourceResolver, distributionExportOptions, outputStream);
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/org/apache/sling/distribution/serialization/impl/vlt/VaultDistributionPackageBuilderFactory.java b/src/main/java/org/apache/sling/distribution/serialization/impl/vlt/VaultDistributionPackageBuilderFactory.java
index 409589c..f6dc523 100644
--- a/src/main/java/org/apache/sling/distribution/serialization/impl/vlt/VaultDistributionPackageBuilderFactory.java
+++ b/src/main/java/org/apache/sling/distribution/serialization/impl/vlt/VaultDistributionPackageBuilderFactory.java
@@ -35,6 +35,7 @@ import org.apache.sling.distribution.packaging.DistributionPackage;
import org.apache.sling.distribution.packaging.DistributionPackageBuilder;
import org.apache.sling.distribution.packaging.DistributionPackageInfo;
import org.apache.sling.distribution.packaging.impl.FileDistributionPackageBuilder;
+import org.apache.sling.distribution.packaging.impl.InMemoryDistributionPackageBuilder;
import org.apache.sling.distribution.packaging.impl.ResourceDistributionPackageBuilder;
import org.apache.sling.distribution.packaging.impl.ResourceDistributionPackageCleanup;
import org.apache.sling.distribution.serialization.DistributionContentSerializer;
@@ -79,6 +80,9 @@ public class VaultDistributionPackageBuilderFactory implements DistributionPacka
),
@PropertyOption(name = "filevlt",
value = "file packages"
+ ),
+ @PropertyOption(name = "inmemory",
+ value = "in memory packages"
)},
value = "jcrvlt", label = "type", description = "The type of this package builder")
private static final String TYPE = DistributionComponentConstants.PN_TYPE;
@@ -258,6 +262,8 @@ public class VaultDistributionPackageBuilderFactory implements DistributionPacka
DistributionPackageBuilder wrapped;
if ("filevlt".equals(type)) {
wrapped = new FileDistributionPackageBuilder(name, contentSerializer, tempFsFolder, digestAlgorithm, packageNodeFilters, packagePropertyFilters);
+ } else if ("inmemory".equals(type)) {
+ wrapped = new InMemoryDistributionPackageBuilder(name, contentSerializer, packageNodeFilters, packagePropertyFilters);
} else {
final int fileThreshold = PropertiesUtil.toInteger(config.get(FILE_THRESHOLD), DEFAULT_FILE_THRESHOLD_VALUE);
String memoryUnitName = PropertiesUtil.toString(config.get(MEMORY_UNIT), DEFAULT_MEMORY_UNIT);
diff --git a/src/test/java/org/apache/sling/distribution/packaging/impl/InMemoryDistributionPackageBuilderTest.java b/src/test/java/org/apache/sling/distribution/packaging/impl/InMemoryDistributionPackageBuilderTest.java
new file mode 100644
index 0000000..8d385f0
--- /dev/null
+++ b/src/test/java/org/apache/sling/distribution/packaging/impl/InMemoryDistributionPackageBuilderTest.java
@@ -0,0 +1,89 @@
+/*
+ * 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.sling.distribution.packaging.impl;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.distribution.DistributionRequestType;
+import org.apache.sling.distribution.SimpleDistributionRequest;
+import org.apache.sling.distribution.common.DistributionException;
+import org.apache.sling.distribution.packaging.DistributionPackage;
+import org.apache.sling.distribution.serialization.DistributionContentSerializer;
+import org.apache.sling.distribution.serialization.DistributionExportOptions;
+import org.junit.Test;
+
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Mockito.mock;
+
+public class InMemoryDistributionPackageBuilderTest {
+
+ @Test
+ public void testCreatePackage() throws Exception {
+ InMemoryDistributionPackageBuilder builder = new InMemoryDistributionPackageBuilder("name", new InMemDistributionContentSerializer(), new String[0], new String[0]);
+ DistributionPackage pkg = builder.createPackageForAdd(mock(ResourceResolver.class), new SimpleDistributionRequest(DistributionRequestType.ADD, false, "/test"));
+ assertNotNull(pkg.createInputStream());
+ }
+
+ private final class InMemDistributionContentSerializer implements DistributionContentSerializer {
+
+ @Override
+ public void exportToStream(ResourceResolver resourceResolver,
+ DistributionExportOptions exportOptions,
+ OutputStream outputStream)
+ throws DistributionException {
+ try {
+ IOUtils.write("test", outputStream, "UTF8");
+ } catch (IOException e) {
+ throw new DistributionException(e);
+ }
+ }
+
+ @Override
+ public void importFromStream(ResourceResolver resourceResolver,
+ InputStream inputStream)
+ throws DistributionException {
+
+ }
+
+ @Override
+ public String getName() {
+ return "serialiserName";
+ }
+
+ @Override
+ public String getContentType() {
+ return "text/plain";
+ }
+
+ @Override
+ public boolean isRequestFiltering() {
+ return false;
+ }
+
+ @Override
+ public boolean isDeletionSupported() {
+ return false;
+ }
+ }
+
+}
\ No newline at end of file
diff --git a/src/test/java/org/apache/sling/distribution/packaging/impl/InMemoryDistributionPackageTest.java b/src/test/java/org/apache/sling/distribution/packaging/impl/InMemoryDistributionPackageTest.java
new file mode 100644
index 0000000..1737d63
--- /dev/null
+++ b/src/test/java/org/apache/sling/distribution/packaging/impl/InMemoryDistributionPackageTest.java
@@ -0,0 +1,47 @@
+/*
+ * 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.sling.distribution.packaging.impl;
+
+import java.util.Random;
+
+import org.apache.sling.distribution.packaging.DistributionPackage;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class InMemoryDistributionPackageTest {
+
+ @Test
+ public void testGetInfo() throws Exception {
+ int size = 1000;
+ byte[] data = new byte[size];
+ new Random().nextBytes(data);
+ DistributionPackage pkg = new InMemoryDistributionPackage("id", "type", data);
+ Assert.assertEquals("type", pkg.getType());
+ Assert.assertEquals("id", pkg.getId());
+ Assert.assertEquals(size, pkg.getSize());
+ }
+
+ @Test
+ public void testCreateInputStream() throws Exception {
+ byte[] data = new byte[1000];
+ new Random().nextBytes(data);
+ DistributionPackage pkg = new InMemoryDistributionPackage("id", "type", data);
+ Assert.assertNotEquals(pkg.createInputStream(), pkg.createInputStream());
+ }
+}
\ No newline at end of file
--
To stop receiving notification emails like this one, please contact
tmaret@apache.org.