SLING-6868 Initial content should be migrated to bundle resources

git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1811219 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/pom.xml b/pom.xml
index 5f30755..5586331 100644
--- a/pom.xml
+++ b/pom.xml
@@ -61,7 +61,10 @@
                 <configuration>
                     <instructions>
                         <Bundle-Category>sling</Bundle-Category>
-                        <Sling-Initial-Content>SLING-INF/libs/sling/hapi/; path:=/libs/sling/hapi; overwrite:=true; uninstall:=false</Sling-Initial-Content>
+                        <Sling-Bundle-Resources>
+                            /libs/sling/hapi/;path:=SLING-INF/libs/sling/hapi;propsJSON:=json
+                        </Sling-Bundle-Resources>
+                        <Require-Capability>osgi.extender;filter:="(&amp;(osgi.extender=org.apache.sling.bundleresource)(version&gt;=1.1.0)(!(version&gt;=2.0.0)))"</Require-Capability>
                     </instructions>
                 </configuration>
             </plugin>
diff --git a/src/main/java/org/apache/sling/hapi/HApiUtil.java b/src/main/java/org/apache/sling/hapi/HApiUtil.java
index 83bcf63..143c1b5 100644
--- a/src/main/java/org/apache/sling/hapi/HApiUtil.java
+++ b/src/main/java/org/apache/sling/hapi/HApiUtil.java
@@ -39,7 +39,7 @@
 
     String DEFAULT_SEARCH_PATH = "/libs/sling/hapi/types";
     String SEARCH_PATHS = "org.apache.sling.hapi.tools.searchpaths";
-    String SEARCH_PATHS_DESC = "The path under each hapi types can be identified by their FQDN value in addition to the type path";
+    String SEARCH_PATHS_DESC = "The path under which children resources can be identified as hapi types by their FQDN value in addition to the type path";
 
     String DEFAULT_SERVER_URL = "http://localhost:8080";
     String EXTERNAL_URL = "org.apache.sling.hapi.tools.externalurl";
diff --git a/src/main/java/org/apache/sling/hapi/impl/HApiUtilImpl.java b/src/main/java/org/apache/sling/hapi/impl/HApiUtilImpl.java
index f1ae6e9..5fed3f0 100644
--- a/src/main/java/org/apache/sling/hapi/impl/HApiUtilImpl.java
+++ b/src/main/java/org/apache/sling/hapi/impl/HApiUtilImpl.java
@@ -26,13 +26,8 @@
 import java.util.Map;
 
 import javax.jcr.Node;
-import javax.jcr.NodeIterator;
 import javax.jcr.RepositoryException;
-import javax.jcr.Session;
 import javax.jcr.Value;
-import javax.jcr.query.Query;
-import javax.jcr.query.QueryManager;
-import javax.jcr.query.QueryResult;
 
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
@@ -136,7 +131,7 @@
      * {@inheritDoc}
      */
     @Override
-    public Resource getTypeResource(ResourceResolver resolver, String type) throws RepositoryException {
+    public Resource getTypeResource(ResourceResolver resolver, String type) {
         if (!enabled) {
             return null;
         }
@@ -147,7 +142,7 @@
      * {@inheritDoc}
      */
     @Override
-    public Resource getTypeCollectionResource(ResourceResolver resolver, String collection) throws RepositoryException {
+    public Resource getTypeCollectionResource(ResourceResolver resolver, String collection) {
         if (!enabled) {
             return null;
         }
@@ -155,10 +150,10 @@
     }
 
 
-
-    private Resource getFqdnResource(ResourceResolver resolver, String fqdn, String resType) throws RepositoryException {
-        Session session = resolver.adaptTo(Session.class);
-
+    /**
+     * Get the type resource
+     */
+    private Resource getFqdnResource(ResourceResolver resolver, String fqdn, String resType) {
         // Try to resolve the resource as a path
         Resource res = resolver.getResource(fqdn);
         if (null != res) {
@@ -167,32 +162,29 @@
         } else {
             for (String path : new HashSet<String>(Arrays.asList(hApiPaths))) {
                 // Remove trailing slash from path
-                path = (path.endsWith("/")) ? path.substring(0,path.length() - 1) : path;
+                path = (path.endsWith("/")) ? path.substring(0, path.length() - 1) : path;
+                return getTypeResourceFromFqdn(path, resolver, fqdn, resType);
+            }
+        }
 
-                // Get the query manager for the session
-                QueryManager queryManager = session.getWorkspace().getQueryManager();
+        return null;
+    }
 
-                // Build query for the search paths
-                StringBuilder queryString = new StringBuilder("SELECT * FROM [nt:unstructured] WHERE ");
-                queryString.append(String.format("ISDESCENDANTNODE([%s]) ", path));
-                queryString.append(String.format("AND [sling:resourceType]='%s' AND fqdn = '%s'", resType, fqdn));
-
-                // Execute query
-                Query query = queryManager.createQuery(queryString.toString(), Query.JCR_SQL2);
-                LOG.debug("Querying HApi: {}", queryString.toString());
-                QueryResult result = query.execute();
-
-                NodeIterator nodeIter = result.getNodes();
-                if (nodeIter.hasNext()) {
-                    return resolver.getResource(nodeIter.nextNode().getPath());
-                } else {
-                    // continue
+    /**
+     * Get a type resource as direct _child_ of {{parentPath}} if the fqdn prop matches
+     */
+    private Resource getTypeResourceFromFqdn(String parentPath, ResourceResolver resolver, String fqdn, String resType) {
+        if (null == fqdn) return null;
+        Resource searchParent = resolver.getResource(parentPath);
+        for (Resource tr: searchParent.getChildren()) {
+            if (tr.isResourceType(resType)) {
+                ValueMap resProps = tr.adaptTo(ValueMap.class);
+                if (fqdn.equals(resProps.get("fqdn", (String) null))) {
+                    return tr;
                 }
             }
-
-            // Type has to be abstract
-            return null;
         }
+        return null;
     }
 
 
@@ -346,6 +338,11 @@
     private HApiUtil hApiUtil;
     Map<String, HApiType> types;
 
+    /**
+     * Get the singleton instance
+     * @param hApiUtil
+     * @return
+     */
     public static TypesCache getInstance(HApiUtil hApiUtil) {
         if (null == singleton) {
             singleton = new TypesCache(hApiUtil);
@@ -363,6 +360,13 @@
         this.hApiUtil = hApiUtil;
     }
 
+    /**
+     * Get a type from the cache
+     * @param resolver
+     * @param typeResource
+     * @return
+     * @throws RepositoryException
+     */
     public HApiType getType(ResourceResolver resolver, Resource typeResource) throws RepositoryException {
         if (null == typeResource) return new AbstractHapiTypeImpl("Abstract");
 
@@ -379,10 +383,18 @@
         }
     }
 
+    /**
+     * Add a type to the cache
+     * @param type
+     */
     public void addType(HApiType type) {
         this.types.put(type.getPath(), type);
     }
 
+    /**
+     * Remove a type from the cache
+     * @param path
+     */
     public void removeType(String path) {
         this.types.remove(path);
     }