Skip to content

Commit

Permalink
[SHRINKWRAP-353] Added shallow copy
Browse files Browse the repository at this point in the history
  • Loading branch information
elefevre committed Nov 15, 2011
1 parent 8ed66db commit 64ce970
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 0 deletions.
8 changes: 8 additions & 0 deletions api/src/main/java/org/jboss/shrinkwrap/api/Archive.java
Expand Up @@ -524,4 +524,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();

}
Expand Up @@ -48,6 +48,13 @@ public GenericArchiveImpl(final Archive<?> delegate) {
super(GenericArchive.class, delegate);
}

@Override
public GenericArchiveImpl shallowCopy() {
GenericArchiveImpl newInstance = new GenericArchiveImpl(getArchive().shallowCopy());
ShallowCopy.shallowCopyContentTo(this, newInstance);
return newInstance;
}

/**
* {@inheritDoc}
*
Expand Down
Expand Up @@ -64,6 +64,13 @@ public MemoryMapArchiveImpl(final String archiveName, final Configuration config
super(archiveName, configuration);
}

@Override
public MemoryMapArchiveImpl shallowCopy() {
MemoryMapArchiveImpl newInstance = new MemoryMapArchiveImpl(getConfiguration());
ShallowCopy.shallowCopyContentTo(this, newInstance);
return newInstance;
}

/**
* {@inheritDoc}
*
Expand Down
@@ -0,0 +1,14 @@
package org.jboss.shrinkwrap.impl.base;

import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ArchivePath;

public class ShallowCopy {

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);
}
}

}
Expand Up @@ -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;

Expand Down Expand Up @@ -77,6 +78,13 @@ public EnterpriseArchiveImpl(final Archive<?> delegate) {
super(EnterpriseArchive.class, delegate);
}

@Override
public EnterpriseArchiveImpl shallowCopy() {
EnterpriseArchiveImpl newInstance = new EnterpriseArchiveImpl(getArchive().shallowCopy());
ShallowCopy.shallowCopyContentTo(this, newInstance);
return newInstance;
}

// -------------------------------------------------------------------------------------||
// Required Implementations -----------------------------------------------------------||
// -------------------------------------------------------------------------------------||
Expand Down
Expand Up @@ -21,6 +21,7 @@
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;

Expand Down Expand Up @@ -73,6 +74,14 @@ public JavaArchiveImpl(final Archive<?> delegate) {
super(JavaArchive.class, delegate);
}

@Override
public JavaArchiveImpl shallowCopy() {
Archive<?> underlyingArchive = getArchive();
JavaArchiveImpl newInstance = new JavaArchiveImpl(underlyingArchive.shallowCopy());
ShallowCopy.shallowCopyContentTo(this, newInstance);
return newInstance;
}

// -------------------------------------------------------------------------------------||
// Required Implementations -----------------------------------------------------------||
// -------------------------------------------------------------------------------------||
Expand Down
Expand Up @@ -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;

Expand Down Expand Up @@ -68,6 +69,13 @@ public ResourceAdapterArchiveImpl(final Archive<?> delegate) {
super(ResourceAdapterArchive.class, delegate);
}

@Override
public ResourceAdapterArchiveImpl shallowCopy() {
ResourceAdapterArchiveImpl newInstance = new ResourceAdapterArchiveImpl(getArchive().shallowCopy());
ShallowCopy.shallowCopyContentTo(this, newInstance);
return newInstance;
}

// -------------------------------------------------------------------------------------||
// Required Implementations -----------------------------------------------------------||
// -------------------------------------------------------------------------------------||
Expand Down
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -91,6 +92,13 @@ public WebArchiveImpl(final Archive<?> delegate) {
super(WebArchive.class, delegate);
}

@Override
public WebArchiveImpl shallowCopy() {
WebArchiveImpl newInstance = new WebArchiveImpl(getArchive().shallowCopy());
ShallowCopy.shallowCopyContentTo(this, newInstance);
return newInstance;
}

// -------------------------------------------------------------------------------------||
// Required Implementations -----------------------------------------------------------||
// -------------------------------------------------------------------------------------||
Expand Down
Expand Up @@ -61,6 +61,18 @@ public MockArchiveImpl(final Archive<?> delegate) {
// Required Implementations -----------------------------------------------------------||
// -------------------------------------------------------------------------------------||

/**
* {@inheritDoc}
*
* @see Archive#shallowCopy()
*/
@Override
public MockArchiveImpl shallowCopy() {
MockArchiveImpl newInstance = new MockArchiveImpl(getArchive().shallowCopy());
ShallowCopy.shallowCopyContentTo(this, newInstance);
return newInstance;
}

/**
* {@inheritDoc}
*
Expand Down
Expand Up @@ -456,6 +456,11 @@ public MockJavaArchiveImpl(Archive<?> archive) {
super(JavaArchive.class, archive);
}

@Override
public Archive<JavaArchive> shallowCopy() {
return this;
}

@Override
protected ArchivePath getClassesPath() {
return ArchivePaths.root();
Expand Down
Expand Up @@ -1194,6 +1194,36 @@ public void testNestedArchiveGet() throws Exception {
nestedNode.getAsset());
}

/**
* Ensure adding an asset to the path results in successful storage.
*/
@Test
public void testShallowCopyPreservesPointers() {
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());
}

/**
* Ensure adding an asset to the path results in successful storage.
*/
@Test
public void testShallowCopyHasASeparateCollectionOfTheSamePointers() {
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 ------------------------------------------------------------||
// -------------------------------------------------------------------------------------||
Expand Down

0 comments on commit 64ce970

Please sign in to comment.