You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shindig.apache.org by jo...@apache.org on 2009/11/04 20:09:14 UTC

svn commit: r832844 - in /incubator/shindig/trunk/java/gadgets/src: main/java/org/apache/shindig/gadgets/features/FeatureRegistry.java test/java/org/apache/shindig/gadgets/features/FeatureRegistryTest.java

Author: johnh
Date: Wed Nov  4 19:09:14 2009
New Revision: 832844

URL: http://svn.apache.org/viewvc?rev=832844&view=rev
Log:
Adding optional flag to ignore transitive deps when retrieving FeatureResources. Useful for debug functionality (status servlets) and one-offs, but typically ignored.


Modified:
    incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/features/FeatureRegistry.java
    incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/features/FeatureRegistryTest.java

Modified: incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/features/FeatureRegistry.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/features/FeatureRegistry.java?rev=832844&r1=832843&r2=832844&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/features/FeatureRegistry.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/features/FeatureRegistry.java Wed Nov  4 19:09:14 2009
@@ -160,7 +160,7 @@
   
   /**
    * For the given list of needed features, retrieves all the FeatureResource objects that
-   * contain their content and that of their transitive dependencies.
+   * contain their content and, if requested, that of their transitive dependencies.
    * 
    * Resources are returned in order of their place in the dependency tree, with "bottom"/
    * depended-on resources returned before those that depend on them. Resource objects
@@ -177,22 +177,29 @@
    * @throws GadgetException
    */
   public List<FeatureResource> getFeatureResources(
-      GadgetContext ctx, Collection<String> needed, List<String> unsupported) {
-    Map<Collection<String>, List<FeatureResource>> useCache =
-      (unsupported != null) ? cache : cacheIgnoreUnsupported;
+      GadgetContext ctx, Collection<String> needed, List<String> unsupported, boolean transitive) {
+    Map<Collection<String>, List<FeatureResource>> useCache = null;
+    if (transitive) {
+      useCache = (unsupported != null) ? cache : cacheIgnoreUnsupported;
+    }
     
     List<FeatureResource> resources = Lists.newLinkedList();
     
-    if (useCache.containsKey(needed)) {
+    if (useCache != null && useCache.containsKey(needed)) {
       return useCache.get(needed);
     }
     
-    List<FeatureNode> fullTree = getTransitiveDeps(needed, unsupported);
+    List<FeatureNode> featureNodes = null;
+    if (transitive) {
+      featureNodes = getTransitiveDeps(needed, unsupported);
+    } else {
+      featureNodes = getRequestedNodes(needed, unsupported);
+    }
 
     String targetBundleType =
         ctx.getRenderingContext() == RenderingContext.CONTAINER ? "container" : "gadget";
     
-    for (FeatureNode entry : fullTree) {
+    for (FeatureNode entry : featureNodes) {
       for (FeatureBundle bundle : entry.getBundles()) {
         if (bundle.getType().equals(targetBundleType)) {
           if (containerMatch(bundle.getAttribs().get("container"), ctx.getContainer())) {
@@ -202,7 +209,7 @@
       }
     }
     
-    if (unsupported == null || unsupported.isEmpty()) {
+    if (useCache != null && (unsupported == null || unsupported.isEmpty())) {
       useCache.put(needed, resources);
     }
       
@@ -210,6 +217,18 @@
   }
   
   /**
+   * Helper method retrieving feature resources, including transitive dependencies.
+   * @param ctx Context for the request.
+   * @param needed List of all needed features.
+   * @param unsupported If non-null, a List populated with unknown features from the needed list.
+   * @return List of FeatureResources that may be used to render the needed features.
+   */
+  public List<FeatureResource> getFeatureResources(
+      GadgetContext ctx, Collection<String> needed, List<String> unsupported) {
+    return getFeatureResources(ctx, needed, unsupported, true);
+  }
+  
+  /**
    * Returns all known FeatureResources in dependency order, as described in getFeatureResources.
    * Returns only GADGET-context resources. This is a convenience method largely for calculating
    * JS checksum.
@@ -255,14 +274,7 @@
   }
   
   private List<FeatureNode> getTransitiveDeps(Collection<String> needed, List<String> unsupported) {
-    final List<FeatureNode> requested = Lists.newArrayList();
-    for (String featureName : needed) {
-      if (featureMap.containsKey(featureName)) {
-        requested.add(featureMap.get(featureName));
-      } else {
-        if (unsupported != null) unsupported.add(featureName);
-      }
-    }
+    final List<FeatureNode> requested = getRequestedNodes(needed, unsupported);
     
     Comparator<FeatureNode> nodeDepthComparator = new Comparator<FeatureNode>() {
       public int compare(FeatureNode one, FeatureNode two) {
@@ -298,6 +310,18 @@
     return fullDeps;
   }
   
+  private List<FeatureNode> getRequestedNodes(Collection<String> needed, List<String> unsupported) {
+    List<FeatureNode> requested = Lists.newArrayList();
+    for (String featureName : needed) {
+      if (featureMap.containsKey(featureName)) {
+        requested.add(featureMap.get(featureName));
+      } else {
+        if (unsupported != null) unsupported.add(featureName);
+      }
+    }
+    return requested;
+  }
+  
   private boolean containerMatch(String containerAttrib, String container) {
     if (containerAttrib == null || containerAttrib.length() == 0) {
       // Nothing specified = all match.

Modified: incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/features/FeatureRegistryTest.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/features/FeatureRegistryTest.java?rev=832844&r1=832843&r2=832844&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/features/FeatureRegistryTest.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/features/FeatureRegistryTest.java Wed Nov  4 19:09:14 2009
@@ -319,6 +319,19 @@
     assertEquals(0, unsupported.size());
   }
   
+  @Test
+  public void getFeatureResourcesNoTransitiveSingle() throws Exception {
+    setupFullRegistry("gadget", null);
+    GadgetContext ctx = getCtx(RenderingContext.GADGET, null);
+    List<String> needed = Lists.newArrayList("top", "bottom");
+    List<String> unsupported = Lists.<String>newLinkedList();
+    List<FeatureResource> resources = registry.getFeatureResources(ctx, needed, unsupported, false);
+    // Should return in order requested.
+    assertEquals(2, resources.size());
+    assertEquals("top", resources.get(0).getContent());
+    assertEquals("bottom", resources.get(1).getContent());
+    assertEquals(0, unsupported.size());
+  }
   
   @Test
   public void getAllFeatures() throws Exception {