Permalink
Browse files

[SHRINKWRAP-353] Added shallow copy

  • Loading branch information...
1 parent 78ea66e commit 47dbd4dae43f452fa88211311fbaa2fcb0199276 @elefevre elefevre committed with Nov 14, 2011
View
@@ -8,3 +8,4 @@ bin
*.ipr
*.iws
test-output/
+.metadata
@@ -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();
+
}
@@ -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.
*
@@ -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
@@ -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
@@ -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);
+ }
+ }
+
+}
@@ -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 -----------------------------------------------------------||
// -------------------------------------------------------------------------------------||
@@ -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 -----------------------------------------------------------||
// -------------------------------------------------------------------------------------||
@@ -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 -----------------------------------------------------------||
// -------------------------------------------------------------------------------------||
@@ -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;
+ }
}
@@ -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
@@ -457,6 +457,11 @@ public MockJavaArchiveImpl(Archive<?> archive) {
}
@Override
+ public Archive<JavaArchive> shallowCopy() {
+ return this;
+ }
+
+ @Override
protected ArchivePath getClassesPath() {
return ArchivePaths.root();
}
@@ -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 ------------------------------------------------------------||
// -------------------------------------------------------------------------------------||

0 comments on commit 47dbd4d

Please sign in to comment.