Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[SHRINKWRAP-353] Added shallow copy

  • Loading branch information...
commit 47dbd4dae43f452fa88211311fbaa2fcb0199276 1 parent 78ea66e
@elefevre elefevre authored committed
View
1  .gitignore
@@ -8,3 +8,4 @@ bin
*.ipr
*.iws
test-output/
+.metadata
View
10 api/src/main/java/org/jboss/shrinkwrap/api/Archive.java
@@ -353,7 +353,7 @@
Node delete(String archivePath) throws IllegalArgumentException;
/**
- * Obtains all assets in this archive, along with its respective Path. The returned Map will be an immutable view.
+ * Obtains all assets in this archive, along with their respective paths. The returned Map will be an immutable view.
*
* @return
*/
@@ -554,4 +554,12 @@ T add(Archive<?> archive, ArchivePath path, Class<? extends StreamExporter> expo
*/
void writeTo(OutputStream outputStream, Formatter formatter) throws IllegalArgumentException;
+ /**
+ * Creates a shallow copy of this {@link Archive}. Assets from this archive are made available under the same paths.
+ * However, removing old assets or adding new assets on this archive affects does not affect the new archive.
+ *
+ * @return a new archive with a copy of the pointers to the assets
+ */
+ Archive<T> shallowCopy();
+
}
View
2  impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/ArchiveBase.java
@@ -50,8 +50,6 @@
import org.jboss.shrinkwrap.spi.Configurable;
/**
- * ArchiveBase
- *
* Base implementation of {@link Archive}. Contains support for operations (typically overloaded) that are not specific
* to any particular storage implementation, and may be delegated to other forms.
*
View
12 impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/GenericArchiveImpl.java
@@ -51,6 +51,18 @@ public GenericArchiveImpl(final Archive<?> delegate) {
/**
* {@inheritDoc}
*
+ * @see Archive#shallowCopy()
+ */
+ @Override
+ public GenericArchiveImpl shallowCopy() {
+ GenericArchiveImpl newInstance = new GenericArchiveImpl(getArchive().shallowCopy());
+ ShallowCopy.shallowCopyContentTo(this, newInstance);
+ return newInstance;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
* @see org.jboss.shrinkwrap.impl.base.container.ContainerBase#getClassesPath()
*/
@Override
View
12 impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/MemoryMapArchiveImpl.java
@@ -67,6 +67,18 @@ public MemoryMapArchiveImpl(final String archiveName, final Configuration config
/**
* {@inheritDoc}
*
+ * @see Archive#shallowCopy()
+ */
+ @Override
+ public MemoryMapArchiveImpl shallowCopy() {
+ MemoryMapArchiveImpl newInstance = new MemoryMapArchiveImpl(getConfiguration());
+ ShallowCopy.shallowCopyContentTo(this, newInstance);
+ return newInstance;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
* @see org.jboss.shrinkwrap.impl.base.ArchiveBase#getActualClass()
*/
@Override
View
20 impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/ShallowCopy.java
@@ -0,0 +1,20 @@
+package org.jboss.shrinkwrap.impl.base;
+
+import org.jboss.shrinkwrap.api.Archive;
+import org.jboss.shrinkwrap.api.ArchivePath;
+
+public class ShallowCopy {
+
+ /**
+ * Copies pointers to assets from an archive to another archive. Paths are recreated in the target archive.
+ *
+ * @param from origin archive
+ * @param to target archive
+ */
+ public static <T extends Archive<T>> void shallowCopyContentTo(Archive<T> from, Archive<T> to) {
+ for (ArchivePath path : from.getContent().keySet()) {
+ to.add(from.get(path).getAsset(), path);
+ }
+ }
+
+}
View
13 impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/spec/EnterpriseArchiveImpl.java
@@ -21,6 +21,7 @@
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ArchivePath;
import org.jboss.shrinkwrap.api.spec.EnterpriseArchive;
+import org.jboss.shrinkwrap.impl.base.ShallowCopy;
import org.jboss.shrinkwrap.impl.base.container.EnterpriseContainerBase;
import org.jboss.shrinkwrap.impl.base.path.BasicPath;
@@ -77,6 +78,18 @@ public EnterpriseArchiveImpl(final Archive<?> delegate) {
super(EnterpriseArchive.class, delegate);
}
+ /**
+ * {@inheritDoc}
+ *
+ * @see Archive#shallowCopy()
+ */
+ @Override
+ public EnterpriseArchiveImpl shallowCopy() {
+ EnterpriseArchiveImpl newInstance = new EnterpriseArchiveImpl(getArchive().shallowCopy());
+ ShallowCopy.shallowCopyContentTo(this, newInstance);
+ return newInstance;
+ }
+
// -------------------------------------------------------------------------------------||
// Required Implementations -----------------------------------------------------------||
// -------------------------------------------------------------------------------------||
View
16 impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/spec/JavaArchiveImpl.java
@@ -21,12 +21,11 @@
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ArchivePath;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.jboss.shrinkwrap.impl.base.ShallowCopy;
import org.jboss.shrinkwrap.impl.base.container.ContainerBase;
import org.jboss.shrinkwrap.impl.base.path.BasicPath;
/**
- * JavaArchiveImpl
- *
* Implementation of an archive with JAR-specific support.
*
* @author <a href="mailto:andrew.rubinger@jboss.org">ALR</a>
@@ -75,6 +74,19 @@ public JavaArchiveImpl(final Archive<?> delegate) {
super(JavaArchive.class, delegate);
}
+ /**
+ * {@inheritDoc}
+ *
+ * @see Archive#shallowCopy()
+ */
+ @Override
+ public JavaArchiveImpl shallowCopy() {
+ Archive<?> underlyingArchive = getArchive();
+ JavaArchiveImpl newInstance = new JavaArchiveImpl(underlyingArchive.shallowCopy());
+ ShallowCopy.shallowCopyContentTo(this, newInstance);
+ return newInstance;
+ }
+
// -------------------------------------------------------------------------------------||
// Required Implementations -----------------------------------------------------------||
// -------------------------------------------------------------------------------------||
View
13 impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/spec/ResourceAdapterArchiveImpl.java
@@ -21,6 +21,7 @@
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ArchivePath;
import org.jboss.shrinkwrap.api.spec.ResourceAdapterArchive;
+import org.jboss.shrinkwrap.impl.base.ShallowCopy;
import org.jboss.shrinkwrap.impl.base.container.ResourceAdapterContainerBase;
import org.jboss.shrinkwrap.impl.base.path.BasicPath;
@@ -68,6 +69,18 @@ public ResourceAdapterArchiveImpl(final Archive<?> delegate) {
super(ResourceAdapterArchive.class, delegate);
}
+ /**
+ * {@inheritDoc}
+ *
+ * @see Archive#shallowCopy()
+ */
+ @Override
+ public ResourceAdapterArchiveImpl shallowCopy() {
+ ResourceAdapterArchiveImpl newInstance = new ResourceAdapterArchiveImpl(getArchive().shallowCopy());
+ ShallowCopy.shallowCopyContentTo(this, newInstance);
+ return newInstance;
+ }
+
// -------------------------------------------------------------------------------------||
// Required Implementations -----------------------------------------------------------||
// -------------------------------------------------------------------------------------||
View
13 impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/spec/WebArchiveImpl.java
@@ -22,6 +22,7 @@
import org.jboss.shrinkwrap.api.ArchivePath;
import org.jboss.shrinkwrap.api.ArchivePaths;
import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.jboss.shrinkwrap.impl.base.ShallowCopy;
import org.jboss.shrinkwrap.impl.base.container.WebContainerBase;
/**
@@ -164,4 +165,16 @@ protected ArchivePath getWebInfPath() {
protected ArchivePath getServiceProvidersPath() {
return PATH_SERVICE_PROVIDERS;
}
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see Archive#shallowCopy()
+ */
+ @Override
+ public WebArchiveImpl shallowCopy() {
+ WebArchiveImpl newInstance = new WebArchiveImpl(getArchive().shallowCopy());
+ ShallowCopy.shallowCopyContentTo(this, newInstance);
+ return newInstance;
+ }
}
View
12 impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/MockArchiveImpl.java
@@ -64,6 +64,18 @@ public MockArchiveImpl(final Archive<?> delegate) {
/**
* {@inheritDoc}
*
+ * @see Archive#shallowCopy()
+ */
+ @Override
+ public MockArchiveImpl shallowCopy() {
+ MockArchiveImpl newInstance = new MockArchiveImpl(getArchive().shallowCopy());
+ ShallowCopy.shallowCopyContentTo(this, newInstance);
+ return newInstance;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
* @see org.jboss.shrinkwrap.impl.base.container.ContainerBase#getManifestPath()
*/
@Override
View
5 impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/ShrinkWrapTestCase.java
@@ -457,6 +457,11 @@ public MockJavaArchiveImpl(Archive<?> archive) {
}
@Override
+ public Archive<JavaArchive> shallowCopy() {
+ return this;
+ }
+
+ @Override
protected ArchivePath getClassesPath() {
return ArchivePaths.root();
}
View
24 impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/test/ArchiveTestBase.java
@@ -1214,6 +1214,30 @@ public void shouldNotMoveAssetBecauseOfInexistentPath() {
archive.move(sourcePath, targetPath);
}
+ @Test
+ public void ensureShallowCopyPreservesPointers() {
+ Archive<T> archive = getArchive();
+ Asset asset = new ClassLoaderAsset(NAME_TEST_PROPERTIES);
+ archive.add(asset, "location");
+
+ Archive<T> copyArchive = archive.shallowCopy();
+
+ Assert.assertTrue(copyArchive.contains("location"));
+ Assert.assertSame(copyArchive.get("location").getAsset(), archive.get("location").getAsset());
+ }
+
+ @Test
+ public void ensureShallowCopyHasASeparateCollectionOfTheSamePointers() {
+ Archive<T> archive = getArchive();
+ Asset asset = new ClassLoaderAsset(NAME_TEST_PROPERTIES);
+ archive.add(asset, "location");
+
+ Archive<T> copyArchive = archive.shallowCopy();
+ archive.delete("location");
+
+ Assert.assertTrue(copyArchive.contains("location"));
+ }
+
// -------------------------------------------------------------------------------------||
// Internal Helper Methods ------------------------------------------------------------||
// -------------------------------------------------------------------------------------||
Please sign in to comment.
Something went wrong with that request. Please try again.