[jboss-svn-commits] JBoss Common SVN: r3542 - in shrinkwrap/trunk/impl-base/src: test/java/org/jboss/shrinkwrap/impl/base/test and 1 other directory.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Sat Sep 26 20:08:03 EDT 2009
Author: johnbailey
Date: 2009-09-26 20:08:02 -0400 (Sat, 26 Sep 2009)
New Revision: 3542
Modified:
shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/MemoryMapArchiveBase.java
shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/test/ArchiveTestBase.java
Log:
[SHRINKWRAP-32] - Added support for MemoryMapArchive contains and get methods to accept paths that fully qualify assets in nested archives.
Modified: shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/MemoryMapArchiveBase.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/MemoryMapArchiveBase.java 2009-09-26 23:56:21 UTC (rev 3541)
+++ shrinkwrap/trunk/impl-base/src/main/java/org/jboss/shrinkwrap/impl/base/MemoryMapArchiveBase.java 2009-09-27 00:08:02 UTC (rev 3542)
@@ -21,12 +21,15 @@
import java.util.List;
import java.util.Map;
import java.util.UUID;
+import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.Asset;
import org.jboss.shrinkwrap.api.Path;
+import org.jboss.shrinkwrap.impl.base.asset.ArchiveAsset;
+import org.jboss.shrinkwrap.impl.base.path.BasicPath;
/**
* MemoryMapArchiveBase
@@ -49,12 +52,12 @@
* Logger
*/
private static final Logger log = Logger.getLogger(MemoryMapArchiveBase.class.getName());
-
+
/**
* Newline character
*/
private static final char NEWLINE = '\n';
-
+
/**
* Colon character
*/
@@ -69,6 +72,11 @@
*/
private final Map<Path, Asset> content = new ConcurrentHashMap<Path, Asset>();
+ /**
+ * Storage for the {@link ArchiveAsset}s. Used to help get access to nested archive content.
+ */
+ private final Map<Path, ArchiveAsset> nestedArchives = new ConcurrentHashMap<Path, ArchiveAsset>();
+
//-------------------------------------------------------------------------------------||
// Constructor ------------------------------------------------------------------------||
//-------------------------------------------------------------------------------------||
@@ -102,7 +110,7 @@
// Required Implementations - Archive -------------------------------------------------||
//-------------------------------------------------------------------------------------||
- /* (non-Javadoc)
+ /* {@inheritDoc}
* @see org.jboss.declarchive.api.Archive#add(org.jboss.declarchive.api.Path, org.jboss.declarchive.api.Asset[])
*/
@Override
@@ -115,17 +123,49 @@
return covariantReturn();
}
- /* (non-Javadoc)
+ /* {@inheritDoc}
+ * @see org.jboss.shrinkwrap.impl.base.ArchiveBase#add(org.jboss.shrinkwrap.api.Path, org.jboss.shrinkwrap.api.Archive)
+ */
+ @Override
+ public T add(Path path, Archive<?> archive)
+ {
+ // Add archive asset
+ super.add(path, archive);
+
+ // Expected Archive Path
+ Path archivePath = new BasicPath(path, archive.getName());
+
+ // Get the Asset that was just added
+ Asset asset = get(archivePath);
+
+ // Make sure it is an ArchiveAsset
+ if (asset instanceof ArchiveAsset)
+ {
+ ArchiveAsset archiveAsset = ArchiveAsset.class.cast(asset);
+ // Add asset to ArchiveAsset Map
+ nestedArchives.put(archivePath, archiveAsset);
+ }
+
+ return covariantReturn();
+ }
+
+ /* {@inheritDoc}
* @see org.jboss.declarchive.api.Archive#contains(org.jboss.declarchive.api.Path)
*/
@Override
public boolean contains(Path path)
{
Validate.notNull(path, "No path was specified");
- return content.containsKey(path);
+
+ boolean found = content.containsKey(path);
+ if (!found)
+ {
+ found = nestedContains(path);
+ }
+ return found;
}
- /* (non-Javadoc)
+ /* {@inheritDoc}
* @see org.jboss.declarchive.api.Archive#delete(org.jboss.declarchive.api.Path)
*/
@Override
@@ -135,17 +175,22 @@
return content.remove(path) != null;
}
- /* (non-Javadoc)
+ /* {@inheritDoc}
* @see org.jboss.declarchive.api.Archive#get(org.jboss.declarchive.api.Path)
*/
@Override
public Asset get(Path path)
{
Validate.notNull(path, "No path was specified");
- return content.get(path);
+ Asset asset = content.get(path);
+ if (asset == null && contains(path))
+ {
+ asset = getNestedAsset(path);
+ }
+ return asset;
}
- /* (non-Javadoc)
+ /* {@inheritDoc}
* @see org.jboss.declarchive.api.Archive#getContent()
*/
@Override
@@ -154,7 +199,7 @@
return Collections.unmodifiableMap(content);
}
- /* (non-Javadoc)
+ /* {@inheritDoc}
* @see org.jboss.declarchive.api.Archive#toString(boolean)
*/
@Override
@@ -165,10 +210,10 @@
{
// Make a builder
StringBuilder sb = new StringBuilder();
-
+
// Add the name
sb.append(this.getName()).append(COLON).append(NEWLINE);
-
+
// Sort all paths
final List<Path> paths = new ArrayList<Path>(content.keySet());
Collections.sort(paths);
@@ -183,4 +228,96 @@
return this.toString();
}
+ //-------------------------------------------------------------------------------------||
+ // Internal Helper Methods ------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Check to see if a path is found in a nested archive
+ */
+ private boolean nestedContains(Path path)
+ {
+ // Iterate through nested archives
+ for (Entry<Path, ArchiveAsset> nestedArchiveEntry : nestedArchives.entrySet())
+ {
+ Path archivePath = nestedArchiveEntry.getKey();
+ ArchiveAsset archiveAsset = nestedArchiveEntry.getValue();
+
+ // Check to see if the requested path starts with the nested archive path
+ if (startsWith(path, archivePath))
+ {
+ Archive<?> nestedArchive = archiveAsset.getArchive();
+
+ // Get the asset path from within the nested archive
+ Path nestedAssetPath = getNestedPath(path, archivePath);
+
+ // Recurse the call to the nested archive
+ return nestedArchive.contains(nestedAssetPath);
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Attempt to get the asset from a nested archive.
+ *
+ * @param path
+ * @return
+ */
+ private Asset getNestedAsset(Path path)
+ {
+ // Iterate through nested archives
+ for (Entry<Path, ArchiveAsset> nestedArchiveEntry : nestedArchives.entrySet())
+ {
+ Path archivePath = nestedArchiveEntry.getKey();
+ ArchiveAsset archiveAsset = nestedArchiveEntry.getValue();
+
+ // Check to see if the requested path starts with the nested archive path
+ if (startsWith(path, archivePath))
+ {
+ Archive<?> nestedArchive = archiveAsset.getArchive();
+
+ // Get the asset path from within the nested archive
+ Path nestedAssetPath = getNestedPath(path, archivePath);
+
+ // Recurse the call to the nested archive
+ return nestedArchive.get(nestedAssetPath);
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Check to see if one path starts with another
+ *
+ * @param fullPath
+ * @param startingPath
+ * @return
+ */
+ private boolean startsWith(Path fullPath, Path startingPath)
+ {
+ final String context = fullPath.get();
+ final String startingContext = startingPath.get();
+
+ return context.startsWith(startingContext);
+ }
+
+ /**
+ * Given a full path and a base path return a new path containing the full path with the
+ * base path removed from the beginning.
+ *
+ * @param fullPath
+ * @param basePath
+ * @return
+ */
+ private Path getNestedPath(Path fullPath, Path basePath)
+ {
+ final String context = fullPath.get();
+ final String baseContent = basePath.get();
+
+ // Remove the base path from the full path
+ String nestedArchiveContext = context.substring(baseContent.length());
+
+ return new BasicPath(nestedArchiveContext);
+ }
}
Modified: shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/test/ArchiveTestBase.java
===================================================================
--- shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/test/ArchiveTestBase.java 2009-09-26 23:56:21 UTC (rev 3541)
+++ shrinkwrap/trunk/impl-base/src/test/java/org/jboss/shrinkwrap/impl/base/test/ArchiveTestBase.java 2009-09-27 00:08:02 UTC (rev 3542)
@@ -146,7 +146,7 @@
{
}
}
-
+
/**
* Ensure adding an asset to a string path results in successful storage.
* @throws Exception
@@ -160,9 +160,10 @@
archive.add(location.get(), asset);
- Assert.assertTrue("Asset should be placed on " + new BasicPath("/", "test.properties"), archive.contains(location));
+ Assert.assertTrue("Asset should be placed on " + new BasicPath("/", "test.properties"), archive
+ .contains(location));
}
-
+
/**
* Ensure adding an asset to a string path requires path.
* @throws Exception
@@ -182,7 +183,7 @@
{
}
}
-
+
/**
* Ensure adding an asset to the path string requires an asset.
* @throws Exception
@@ -364,7 +365,7 @@
{
}
}
-
+
/**
* Ensure an asset can be retrieved by a string path
* @throws Exception
@@ -381,7 +382,7 @@
Assert.assertTrue("Asset should be returned from path: " + location.get(), compareAssets(asset, fetchedAsset));
}
-
+
/**
* Ensure get asset by string requires a path
* @throws Exception
@@ -580,6 +581,72 @@
}
+ /**
+ * Ensure an archive contains assets from nested archives.
+ * @throws Exception
+ */
+ @Test
+ public void testNestedArchiveContains() throws Exception
+ {
+ Archive<T> archive = getArchive();
+
+ Archive<T> sourceArchive = createNewArchive();
+
+ Asset asset = new ClassLoaderAsset(NAME_TEST_PROPERTIES);
+
+ Path nestedAssetPath = new BasicPath("/", "test.properties");
+
+ sourceArchive.add(nestedAssetPath, asset);
+
+ Path baseLocation = new BasicPath("somewhere");
+
+ archive.add(baseLocation, sourceArchive);
+
+ Path archivePath = new BasicPath(baseLocation, sourceArchive.getName());
+
+ Path expectedPath = new BasicPath(archivePath, "test.properties");
+
+ Assert.assertTrue("Nested archive assets should be verified through a fully qualified path", archive
+ .contains(expectedPath));
+ }
+
+ /**
+ * Ensure assets from a nested archive are accessible from parent archives.
+ * @throws Exception
+ */
+ @Test
+ public void testNestedArchiveGet() throws Exception
+ {
+ Archive<T> archive = getArchive();
+
+ Archive<T> nestedArchive = createNewArchive();
+
+ Path baseLocation = new BasicPath("somewhere");
+
+ archive.add(baseLocation, nestedArchive);
+
+ Archive<T> nestedNestedArchive = createNewArchive();
+
+ nestedArchive.add(new BasicPath("/"), nestedNestedArchive);
+
+ Asset asset = new ClassLoaderAsset(NAME_TEST_PROPERTIES);
+
+ Path nestedAssetPath = new BasicPath("/", "test.properties");
+
+ nestedNestedArchive.add(nestedAssetPath, asset);
+
+ Path nestedArchivePath = new BasicPath(baseLocation, nestedArchive.getName());
+
+ Path nestedNestedArchivePath = new BasicPath(nestedArchivePath, nestedNestedArchive.getName());
+
+ Path expectedPath = new BasicPath(nestedNestedArchivePath, "test.properties");
+
+ Asset nestedAsset = archive.get(expectedPath);
+
+ Assert.assertNotNull("Nested archive asset should be available through partent archive at " + expectedPath.get(),
+ nestedAsset);
+ }
+
//-------------------------------------------------------------------------------------||
// Internal Helper Methods ------------------------------------------------------------||
//-------------------------------------------------------------------------------------||
More information about the jboss-svn-commits
mailing list