This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-maven-launchpad-plugin.git
commit c816f452dd2ac4560a1b09307c362e7f14744074 Author: Bertrand Delacretaz <[email protected]> AuthorDate: Fri Oct 25 12:21:41 2013 +0000 SLING-3205 - provide bundle resources under resources/install to support run modes git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1535709 13f79535-47bb-0310-9956-ffa450edef68 --- .../projectsupport/BundleListContentProvider.java | 153 ++++++++++++++++--- .../BundleListContentProviderTest.java | 164 +++++++++++++++++---- src/test/resources/test-bundle-list.xml | 10 ++ 3 files changed, 276 insertions(+), 51 deletions(-) diff --git a/src/main/java/org/apache/sling/maven/projectsupport/BundleListContentProvider.java b/src/main/java/org/apache/sling/maven/projectsupport/BundleListContentProvider.java index 472ec6c..eb34f54 100644 --- a/src/main/java/org/apache/sling/maven/projectsupport/BundleListContentProvider.java +++ b/src/main/java/org/apache/sling/maven/projectsupport/BundleListContentProvider.java @@ -26,6 +26,7 @@ import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.Iterator; @@ -45,6 +46,10 @@ import org.apache.sling.maven.projectsupport.bundlelist.v1_0_0.StartLevel; */ abstract class BundleListContentProvider implements LaunchpadContentProvider { + public static final String INSTALL_PATH_PREFIX = "resources/install"; + public static final int BOOTSTRAP_DEF_START_LEVEL = -1; + public static final int ACTUAL_BOOTSTRAP_START_LEVEL = 1; + private final File resourceProviderRoot; private final static List<String> EMPTY_STRING_LIST = Collections.emptyList(); @@ -55,11 +60,10 @@ abstract class BundleListContentProvider implements LaunchpadContentProvider { private Iterator<String> handleBundlePathRoot(String path) { final Set<String> levels = new HashSet<String>(); for (final StartLevel level : getInitializedBundleList().getStartLevels()) { - // we treat the boot level as level 1 - if ( level.getStartLevel() == -1 ) { - levels.add(BUNDLE_PATH_PREFIX + "/1/"); - } else { - levels.add(BUNDLE_PATH_PREFIX + "/" + level.getLevel() + "/"); + // Include only bootstrap bundles here, with start level 1. + // Other bundles go under the install folder, to support run modes + if( level.getStartLevel() == BOOTSTRAP_DEF_START_LEVEL) { + levels.add(BUNDLE_PATH_PREFIX + "/" + ACTUAL_BOOTSTRAP_START_LEVEL + "/"); } } return levels.iterator(); @@ -88,31 +92,53 @@ abstract class BundleListContentProvider implements LaunchpadContentProvider { } } - private Iterator<String> handleBundlePathFolder(String path) { + private Iterator<String> handleBundlesSubfolder(String path) { + Iterator<String> result = null; final String startLevelInfo = path.substring(BUNDLE_PATH_PREFIX.length() + 1); try { final int startLevel = Integer.parseInt(startLevelInfo); + + // To be consistent with handleBundlePathRoot, consider only level 1 which + // is assigned to bootstrap bundles + if(startLevel == ACTUAL_BOOTSTRAP_START_LEVEL) { + final List<String> bundles = new ArrayList<String>(); + addBundles(bundles, ACTUAL_BOOTSTRAP_START_LEVEL, null); + addBundles(bundles, BOOTSTRAP_DEF_START_LEVEL, null); + result = bundles.iterator(); + } - final List<String> bundles = new ArrayList<String>(); - for (final StartLevel level : getInitializedBundleList().getStartLevels()) { - if (level.getStartLevel() == startLevel || (startLevel == 1 && level.getStartLevel() == -1)) { - for (final Bundle bundle : level.getBundles()) { - final ArtifactDefinition d = new ArtifactDefinition(bundle, startLevel); - try { - final Artifact artifact = getArtifact(d); - bundles.add(artifact.getFile().toURI().toURL().toExternalForm()); - } catch (Exception e) { - getLog().error("Unable to resolve artifact ", e); - } + } catch (NumberFormatException e) { + getLog().warn("Invalid start level " + startLevelInfo + " in path " + path); + } + + return result; + } + + private void addBundles(Collection<String> bundles, int startLevel, String runMode) { + for (final StartLevel level : getInitializedBundleList().getStartLevels()) { + if(level.getStartLevel() == startLevel) { + for (final Bundle bundle : level.getBundles()) { + if(!runModeMatches(bundle, runMode)) { + continue; + } + final ArtifactDefinition d = new ArtifactDefinition(bundle, startLevel); + try { + final Artifact artifact = getArtifact(d); + bundles.add(artifact.getFile().toURI().toURL().toExternalForm()); + } catch (Exception e) { + getLog().error("Unable to resolve artifact ", e); } } } - return bundles.iterator(); - - } catch (NumberFormatException e) { - // we ignore this } - return null; + } + + private boolean runModeMatches(Bundle b, String runMode) { + if(runMode == null || runMode.length() == 0) { + return b.getRunModes() == null || b.getRunModes().length() == 0; + } else { + return b.getRunModes() != null && b.getRunModes().contains(runMode); + } } private Iterator<String> handleResourcesRoot() { @@ -120,8 +146,81 @@ abstract class BundleListContentProvider implements LaunchpadContentProvider { subDirs.add(BUNDLE_PATH_PREFIX); subDirs.add(CONFIG_PATH_PREFIX); subDirs.add("resources/corebundles"); + subDirs.add(INSTALL_PATH_PREFIX); + + // Compute the set of run modes in our bundles + final Set<String> runModes = new HashSet<String>(); + for (final StartLevel level : getInitializedBundleList().getStartLevels()) { + for(Bundle bundle : level.getBundles()) { + final String modes = bundle.getRunModes(); + if(modes != null && modes.length() > 0) { + for(String m : modes.split(",")) { + runModes.add("." + m); + } + } + } + } + + // Add one install subdir per run mode + for(String m : runModes) { + subDirs.add(INSTALL_PATH_PREFIX + m); + } return subDirs.iterator(); } + + /** Add one folder per child, using given path as prefix, for start + * levels which actually provide bundles for the given run mode. + */ + private void addStartLevelSubdirs(Collection<String> children, String path, String runMode) { + for (final StartLevel level : getInitializedBundleList().getStartLevels()) { + final List<String> bundles = new ArrayList<String>(); + addBundles(bundles, level.getStartLevel(), runMode); + if(!bundles.isEmpty()) { + int folderLevel = level.getStartLevel(); + if(folderLevel== BOOTSTRAP_DEF_START_LEVEL) { + folderLevel = ACTUAL_BOOTSTRAP_START_LEVEL; + } + children.add(path + "/" + folderLevel); + } + } + } + + private Iterator<String> handleInstallPath(String path) { + // Path is like + // bundles/install.runMode/12 + // or a subset of that. + // Extract optional run mode and start level from that + if(path.endsWith("/")) { + path = path.substring(0, path.length() - 1); + } + final String [] parts = path.substring(INSTALL_PATH_PREFIX.length()).split("/"); + if (parts.length > 2){ + throw new IllegalStateException("Cannot parse path " + path); + } + final String runMode = parts[0].length() == 0 ? null : parts[0].substring(1); + final String startLevelInfo = parts.length > 1 ? parts[1] : null; + Set<String> result = new HashSet<String>(); + + if(runMode == null && startLevelInfo == null) { + // Root folder: add one subdir per start level that provides bundles + addStartLevelSubdirs(result, INSTALL_PATH_PREFIX, null); + + } else if(startLevelInfo == null) { + // The root of a run mode folder - one subdir per start + // level which actually provides bundles + addStartLevelSubdirs(result, path, runMode); + + } else { + // A folder that contains bundles + try { + addBundles(result, Integer.parseInt(startLevelInfo), runMode); + } catch (NumberFormatException e) { + getLog().warn("Invalid start level info " + startLevelInfo + " in path " + path); + } + } + + return result.iterator(); + } public Iterator<String> getChildren(String path) { Iterator<String> result = null; @@ -132,11 +231,17 @@ abstract class BundleListContentProvider implements LaunchpadContentProvider { } else if (path.equals(CONFIG_PATH_PREFIX)) { result = handleConfigPath(); } else if (path.startsWith(BUNDLE_PATH_PREFIX)) { - result = handleBundlePathFolder(path); + result = handleBundlesSubfolder(path); + } else if (path.startsWith(INSTALL_PATH_PREFIX)) { + result = handleInstallPath(path); } else if (path.equals("resources") ) { result = handleResourcesRoot(); + } else if (path.startsWith("file:") ) { + // Client looks for files under a file - we have none, + // as our file URLs point to Maven artifacts + result = EMPTY_STRING_LIST.iterator(); } else { - getLog().warn("un-handlable " + getClass().getSimpleName() + " path: " + path); + getLog().warn("BundleListContentProvider cannot handle path: " + path); } return result; diff --git a/src/test/java/org/apache/sling/maven/projectsupport/BundleListContentProviderTest.java b/src/test/java/org/apache/sling/maven/projectsupport/BundleListContentProviderTest.java index b4c3905..21cbe53 100644 --- a/src/test/java/org/apache/sling/maven/projectsupport/BundleListContentProviderTest.java +++ b/src/test/java/org/apache/sling/maven/projectsupport/BundleListContentProviderTest.java @@ -27,6 +27,7 @@ import java.io.InputStream; import java.net.URL; import java.util.ArrayList; import java.util.Iterator; +import java.util.LinkedList; import java.util.List; import org.apache.maven.artifact.Artifact; @@ -48,7 +49,7 @@ public class BundleListContentProviderTest { private static BundleList bundleList; public static final String TEST_BUNDLE_LIST = "test-bundle-list.xml"; - public static final int BUNDLES_IN_TEST_BUNDLE_LIST = 11; + public static final int BUNDLES_IN_TEST_BUNDLE_LIST = 13; private LaunchpadContentProvider provider; private File resourceProviderRoot; @@ -114,7 +115,7 @@ public class BundleListContentProviderTest { Artifact getArtifact(ArtifactDefinition def) throws MojoExecutionException { final Artifact a = Mockito.mock(Artifact.class); final String fakeName = new StringBuilder() - .append("/") + .append("/FAKE_BUNDLE/") .append(def.getArtifactId()) .append("/") .append(def.getStartLevel()) @@ -138,14 +139,17 @@ public class BundleListContentProviderTest { if(expected.length == 0) { assertTrue("Expecting no children for " + path, it == null || !it.hasNext()); } else { + assertNotNull("Expecting non-null iterator for " + path, it); while(it.hasNext()) { kids.add(it.next()); } for(String exp : expected) { - assertTrue("Expecting " + exp + " in children of " + path + " (result=" + kids + ")", kids.contains(exp)); + assertTrue("Expecting " + exp + " in children of " + path + " (result=" + kids + ")", + kids.contains(exp)); } } - assertEquals("Expecting the correct number of children for " + path, expected.length, kids.size()); + assertEquals("Expecting the correct number of children for " + path + " (result=" + kids + ")", + expected.length, kids.size()); } @Test @@ -162,16 +166,53 @@ public class BundleListContentProviderTest { assertChildren("resources", "resources/bundles", "resources/corebundles", - "resources/config"); + "resources/config", + "resources/install", + "resources/install.dev", + "resources/install.test", + "resources/install.oak", + "resources/install.jackrabbit"); } @Test public void testBundles() { assertChildren("resources/bundles", - "resources/bundles/0/", - "resources/bundles/1/", - "resources/bundles/5/", - "resources/bundles/15/"); + "resources/bundles/1/"); + } + + @Test + public void testInstall() { + assertChildren("resources/install", + "resources/install/0", + "resources/install/1", + "resources/install/5", + "resources/install/15"); + } + + @Test + public void testInstallDev() { + assertChildren("resources/install.dev", + "resources/install.dev/0", + "resources/install.dev/5"); + } + + @Test + public void testInstallTest() { + assertChildren("resources/install.test", + "resources/install.test/0", + "resources/install.test/5"); + } + + @Test + public void testInstallOak() { + assertChildren("resources/install.oak", + "resources/install.oak/15"); + } + + @Test + public void testInstallJackrabbit() { + assertChildren("resources/install.jackrabbit", + "resources/install.jackrabbit/15"); } @Test @@ -194,34 +235,68 @@ public class BundleListContentProviderTest { } @Test - public void testBundles0() { - assertChildren("resources/bundles/0", - "file:/commons-io/0/null", - "file:/commons-fileupload/0/null", - "file:/commons-collections/0/null", - "file:/org.apache.sling.installer.provider.jcr/0/test,dev"); + public void testIgnoredNonBootstrapBundles() { + // All these start levels do not provide resources/bundles anymore - moved to resources/install + for(int i=0; i <= 30; i++) { + if(i == 1) { + continue; + } + final String path ="resources/bundles/" + i; + assertNull("Expecting no resources under " + path, provider.getChildren(path)); + } + } + + @Test + public void testInstall0() { + assertChildren("resources/install/0", + "file:/FAKE_BUNDLE/commons-io/0/null", + "file:/FAKE_BUNDLE/commons-fileupload/0/null", + "file:/FAKE_BUNDLE/commons-collections/0/null"); } @Test - public void testBundles1() { + public void testBootstrapBundles() { assertChildren("resources/bundles/1", - "file:/slf4j-api/1/null", - "file:/org.apache.sling.commons.log/1/null"); + "file:/FAKE_BUNDLE/slf4j-api/-1/null", + "file:/FAKE_BUNDLE/org.apache.sling.commons.log/-1/null"); + } + + @Test + public void testInstall5() { + assertChildren("resources/install/5", + "file:/FAKE_BUNDLE/five.norunmode/5/null"); } @Test - public void testBundles5() { - assertChildren("resources/bundles/5", - "file:/org.apache.sling.extensions.webconsolebranding/5/dev", - "file:/org.apache.sling.extensions.webconsolesecurityprovider/5/test"); + public void testInstall5Dev() { + assertChildren("resources/install.dev/5", + "file:/FAKE_BUNDLE/org.apache.sling.extensions.webconsolebranding/5/dev"); } @Test - public void testBundles15() { - assertChildren("resources/bundles/15", - "file:/org.apache.sling.jcr.oak.server/15/oak", - "file:/guava/15/jackrabbit", - "file:/jsr305/15/oak,jackrabbit"); + public void testInstall5Test() { + assertChildren("resources/install.test/5", + "file:/FAKE_BUNDLE/org.apache.sling.extensions.webconsolesecurityprovider/5/test"); + } + + @Test + public void testInstall15() { + assertChildren("resources/install/15", + "file:/FAKE_BUNDLE/fifteen.norunmode/15/null"); + } + + @Test + public void testInstall15Oak() { + assertChildren("resources/install.oak/15", + "file:/FAKE_BUNDLE/org.apache.sling.jcr.oak.server/15/oak", + "file:/FAKE_BUNDLE/jsr305/15/oak,jackrabbit"); + } + + @Test + public void testInstall15Jackrabbit() { + assertChildren("resources/install.jackrabbit/15", + "file:/FAKE_BUNDLE/guava/15/jackrabbit", + "file:/FAKE_BUNDLE/jsr305/15/oak,jackrabbit"); } @Test @@ -271,4 +346,39 @@ public class BundleListContentProviderTest { assertNull(provider.getChildren("/FOO/bar")); } + @Test + public void testAllBundlesFound() { + final List<String> allResources = new LinkedList<String>(); + addRecursively(allResources, "resources"); + final List<String> bundles = new LinkedList<String>(); + for(String r : allResources) { + if(r.contains("FAKE_BUNDLE")) { + bundles.add(r); + } + } + + // Bundles that have two run modes appear in two folders, we have two of those + // with two run modes each + final int expected = BUNDLES_IN_TEST_BUNDLE_LIST + 2; + assertEquals("Expecting the exact number of test bundles to be found", expected, bundles.size()); + } + + @Test + public void testFile() { + assertChildren("file:/something"); + } + + private void addRecursively(List<String> resources, String path) { + if(path.endsWith("/")) { + path = path.substring(0, path.length() - 1); + } + resources.add(path); + final Iterator<String> it = provider.getChildren(path); + if(it != null) { + while(it.hasNext()) { + addRecursively(resources, it.next()); + } + } + } + } diff --git a/src/test/resources/test-bundle-list.xml b/src/test/resources/test-bundle-list.xml index 8c3b87c..43b7da5 100644 --- a/src/test/resources/test-bundle-list.xml +++ b/src/test/resources/test-bundle-list.xml @@ -51,6 +51,11 @@ <version>1.0.0</version> <runModes>test</runModes> </bundle> + <bundle> + <groupId>org.apache.sling</groupId> + <artifactId>five.norunmode</artifactId> + <version>1.0.0</version> + </bundle> </startLevel> <startLevel level="15"> @@ -72,5 +77,10 @@ <version>2.0.0</version> <runModes>oak,jackrabbit</runModes> </bundle> + <bundle> + <groupId>org.apache.sling</groupId> + <artifactId>fifteen.norunmode</artifactId> + <version>1.0.0</version> + </bundle> </startLevel> </bundles> -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
