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 authored and ALRubinger committed Apr 3, 2012
1 parent 78ea66e commit 47dbd4d
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ bin
*.ipr *.ipr
*.iws *.iws
test-output/ test-output/
.metadata
10 changes: 9 additions & 1 deletion api/src/main/java/org/jboss/shrinkwrap/api/Archive.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ <X extends Archive<X>> Collection<X> getAsType(Class<X> type, Filter<ArchivePath
Node delete(String archivePath) throws IllegalArgumentException; 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 * @return
*/ */
Expand Down Expand Up @@ -554,4 +554,12 @@ T add(Archive<?> archive, ArchivePath path, Class<? extends StreamExporter> expo
*/ */
void writeTo(OutputStream outputStream, Formatter formatter) throws IllegalArgumentException; 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();

} }
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@
import org.jboss.shrinkwrap.spi.Configurable; import org.jboss.shrinkwrap.spi.Configurable;


/** /**
* ArchiveBase
*
* Base implementation of {@link Archive}. Contains support for operations (typically overloaded) that are not specific * 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. * to any particular storage implementation, and may be delegated to other forms.
* *
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ public GenericArchiveImpl(final Archive<?> delegate) {
super(GenericArchive.class, delegate); super(GenericArchive.class, delegate);
} }


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

/** /**
* {@inheritDoc} * {@inheritDoc}
* *
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ public MemoryMapArchiveImpl(final String archiveName, final Configuration config
super(archiveName, configuration); super(archiveName, configuration);
} }


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

/** /**
* {@inheritDoc} * {@inheritDoc}
* *
Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -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);
}
}

}
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.jboss.shrinkwrap.api.Archive; import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ArchivePath; import org.jboss.shrinkwrap.api.ArchivePath;
import org.jboss.shrinkwrap.api.spec.EnterpriseArchive; 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.container.EnterpriseContainerBase;
import org.jboss.shrinkwrap.impl.base.path.BasicPath; import org.jboss.shrinkwrap.impl.base.path.BasicPath;


Expand Down Expand Up @@ -77,6 +78,18 @@ public EnterpriseArchiveImpl(final Archive<?> delegate) {
super(EnterpriseArchive.class, 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 -----------------------------------------------------------|| // Required Implementations -----------------------------------------------------------||
// -------------------------------------------------------------------------------------|| // -------------------------------------------------------------------------------------||
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@
import org.jboss.shrinkwrap.api.Archive; import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ArchivePath; import org.jboss.shrinkwrap.api.ArchivePath;
import org.jboss.shrinkwrap.api.spec.JavaArchive; 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.container.ContainerBase;
import org.jboss.shrinkwrap.impl.base.path.BasicPath; import org.jboss.shrinkwrap.impl.base.path.BasicPath;


/** /**
* JavaArchiveImpl
*
* Implementation of an archive with JAR-specific support. * Implementation of an archive with JAR-specific support.
* *
* @author <a href="mailto:andrew.rubinger@jboss.org">ALR</a> * @author <a href="mailto:andrew.rubinger@jboss.org">ALR</a>
Expand Down Expand Up @@ -75,6 +74,19 @@ public JavaArchiveImpl(final Archive<?> delegate) {
super(JavaArchive.class, 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 -----------------------------------------------------------|| // Required Implementations -----------------------------------------------------------||
// -------------------------------------------------------------------------------------|| // -------------------------------------------------------------------------------------||
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.jboss.shrinkwrap.api.Archive; import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ArchivePath; import org.jboss.shrinkwrap.api.ArchivePath;
import org.jboss.shrinkwrap.api.spec.ResourceAdapterArchive; 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.container.ResourceAdapterContainerBase;
import org.jboss.shrinkwrap.impl.base.path.BasicPath; import org.jboss.shrinkwrap.impl.base.path.BasicPath;


Expand Down Expand Up @@ -68,6 +69,18 @@ public ResourceAdapterArchiveImpl(final Archive<?> delegate) {
super(ResourceAdapterArchive.class, 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 -----------------------------------------------------------|| // Required Implementations -----------------------------------------------------------||
// -------------------------------------------------------------------------------------|| // -------------------------------------------------------------------------------------||
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.jboss.shrinkwrap.api.ArchivePath; import org.jboss.shrinkwrap.api.ArchivePath;
import org.jboss.shrinkwrap.api.ArchivePaths; import org.jboss.shrinkwrap.api.ArchivePaths;
import org.jboss.shrinkwrap.api.spec.WebArchive; import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.impl.base.ShallowCopy;
import org.jboss.shrinkwrap.impl.base.container.WebContainerBase; import org.jboss.shrinkwrap.impl.base.container.WebContainerBase;


/** /**
Expand Down Expand Up @@ -164,4 +165,16 @@ protected ArchivePath getWebInfPath() {
protected ArchivePath getServiceProvidersPath() { protected ArchivePath getServiceProvidersPath() {
return PATH_SERVICE_PROVIDERS; return PATH_SERVICE_PROVIDERS;
} }

/**
* {@inheritDoc}
*
* @see Archive#shallowCopy()
*/
@Override
public WebArchiveImpl shallowCopy() {
WebArchiveImpl newInstance = new WebArchiveImpl(getArchive().shallowCopy());
ShallowCopy.shallowCopyContentTo(this, newInstance);
return newInstance;
}
} }
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ public MockArchiveImpl(final Archive<?> delegate) {
// Required Implementations -----------------------------------------------------------|| // Required Implementations -----------------------------------------------------------||
// -------------------------------------------------------------------------------------|| // -------------------------------------------------------------------------------------||


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

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


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

@Override @Override
protected ArchivePath getClassesPath() { protected ArchivePath getClassesPath() {
return ArchivePaths.root(); return ArchivePaths.root();
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1214,6 +1214,30 @@ public void shouldNotMoveAssetBecauseOfInexistentPath() {
archive.move(sourcePath, targetPath); 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 ------------------------------------------------------------|| // Internal Helper Methods ------------------------------------------------------------||
// -------------------------------------------------------------------------------------|| // -------------------------------------------------------------------------------------||
Expand Down

0 comments on commit 47dbd4d

Please sign in to comment.