SLING-6151 Allow HApi core to be disabled

git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1764850 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/sling/hapi/HApiUtil.java b/src/main/java/org/apache/sling/hapi/HApiUtil.java
index c2b336a..184cd10 100644
--- a/src/main/java/org/apache/sling/hapi/HApiUtil.java
+++ b/src/main/java/org/apache/sling/hapi/HApiUtil.java
@@ -29,14 +29,23 @@
 
     String DEFAULT_RESOURCE_TYPE = "sling/hapi/components/type";
     String RESOURCE_TYPE = "org.apache.sling.hapi.tools.resourcetype";
+    String RESOURCE_TYPE_DESC = "The resource used for hapi types";
 
     String DEFAULT_COLLECTION_RESOURCE_TYPE = "sling/hapi/components/typescollection";
     String COLLECTION_RESOURCE_TYPE = "org.apache.sling.hapi.tools.collectionresourcetype";
+    String COLLECTION_RESOURCE_TYPE_DESC = "The resource used for hapi type collections";
 
+    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 DEFAULT_SERVER_URL = "http://localhost:8080";
     String EXTERNAL_URL = "org.apache.sling.hapi.tools.externalurl";
+    String EXTERNAL_URL_DESC = "The external URL of the instance. This will prefix hapi absolute URLs";
+
+    boolean DEFAULT_ENABLED = true;
+    String ENABLED = "org.apache.sling.hapi.tools.enabled";
+    String ENABLED_DESC = "Whether hapi is enabled or completely disabled (no output rendered in components)";
 
     /**
      * <p>Get a HApi type jcr node from a type identifier.</p>
diff --git a/src/main/java/org/apache/sling/hapi/impl/EmptyAttributeHelperImpl.java b/src/main/java/org/apache/sling/hapi/impl/EmptyAttributeHelperImpl.java
new file mode 100644
index 0000000..c18dc33
--- /dev/null
+++ b/src/main/java/org/apache/sling/hapi/impl/EmptyAttributeHelperImpl.java
@@ -0,0 +1,61 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+package org.apache.sling.hapi.impl;
+
+import org.apache.sling.hapi.MicrodataAttributeHelper;
+
+import java.util.Collections;
+import java.util.Map;
+
+public class EmptyAttributeHelperImpl implements MicrodataAttributeHelper {
+    @Override
+    public String itemtype() {
+        return "";
+    }
+
+    @Override
+    public Map<String, String> itemtypeMap() {
+        return Collections.emptyMap();
+    }
+
+    @Override
+    public String itemprop(String propName) {
+        return "";
+    }
+
+    @Override
+    public String itemprop(String propName, boolean withType) {
+        return "";
+    }
+
+    @Override
+    public Map<String, String> itempropMap(String propName, boolean withType) {
+        return Collections.emptyMap();
+    }
+
+    @Override
+    public Map<String, Map<String, String>> allItemPropMap() {
+        return Collections.emptyMap();
+    }
+
+    @Override
+    public Map<String, String> allPropTypesMap() {
+        return Collections.emptyMap();
+    }
+}
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 f3d5e33..a140713 100644
--- a/src/main/java/org/apache/sling/hapi/impl/HApiUtilImpl.java
+++ b/src/main/java/org/apache/sling/hapi/impl/HApiUtilImpl.java
@@ -53,26 +53,39 @@
 
     private final Logger LOG = LoggerFactory.getLogger(HApiUtil.class);
 
-    @Property(label = "HApi Resource Type", cardinality = 0, value = DEFAULT_RESOURCE_TYPE)
+    @Property(label = "HApi Resource Type", cardinality = 0, value = DEFAULT_RESOURCE_TYPE,
+            description = RESOURCE_TYPE_DESC)
     public static final String HAPI_RESOURCE_TYPE = RESOURCE_TYPE;
 
-    @Property(label = "HApi Collection Resource Type", cardinality = 0, value = DEFAULT_COLLECTION_RESOURCE_TYPE)
+    @Property(label = "HApi Collection Resource Type", cardinality = 0, value = DEFAULT_COLLECTION_RESOURCE_TYPE,
+            description = COLLECTION_RESOURCE_TYPE_DESC)
     private static final String HAPI_COLLECTION_RESOURCE_TYPE = COLLECTION_RESOURCE_TYPE;
 
-    @Property(label = "HApi Types Search Paths", cardinality=50, value = {"/libs/sling/hapi/types"})
+    @Property(label = "HApi Types Search Paths", cardinality=50, value = {DEFAULT_SEARCH_PATH},
+            description = SEARCH_PATHS_DESC)
     public static final String HAPI_PATHS = SEARCH_PATHS;
 
-    @Property(label = "External server URL", cardinality = 0, value = DEFAULT_SERVER_URL)
+    @Property(label = "External server URL", cardinality = 0, value = DEFAULT_SERVER_URL,
+            description = EXTERNAL_URL_DESC)
     public static final String HAPI_EXTERNAL_URL = EXTERNAL_URL;
 
-    public static String resourceType;
+    @Property(label = "Enabled", boolValue = DEFAULT_ENABLED,
+            description = ENABLED_DESC)
+    public static final String HAPI_ENABLED = ENABLED;
+
+
+    private static String resourceType;
     private String collectionResourceType;
-    public static String[] hApiPaths;
-    public static String serverContextPath;
+    private static String[] hApiPaths;
+    private static String serverContextPath;
+    private boolean enabled;
 
 
     @Activate
     private void activate(Map<String, Object> configuration) {
+        enabled = PropertiesUtil.toBoolean(configuration.get(HAPI_ENABLED), false);
+        if (!enabled) return;
+
         resourceType = PropertiesUtil.toString(configuration.get(HAPI_RESOURCE_TYPE), DEFAULT_RESOURCE_TYPE);
         collectionResourceType = PropertiesUtil.toString(configuration.get(HAPI_COLLECTION_RESOURCE_TYPE),
                 DEFAULT_COLLECTION_RESOURCE_TYPE);
@@ -86,6 +99,9 @@
     @Override
     @Deprecated
     public Node getTypeNode(ResourceResolver resolver, String type) throws RepositoryException {
+        if (!enabled) {
+            return null;
+        }
         return getTypeResource(resolver, type).adaptTo(Node.class);
     }
 
@@ -94,6 +110,9 @@
      */
     @Override
     public Resource getTypeResource(ResourceResolver resolver, String type) throws RepositoryException {
+        if (!enabled) {
+            return null;
+        }
         return getFqdnResource(resolver, type, resourceType);
     }
 
@@ -102,6 +121,9 @@
      */
     @Override
     public Resource getTypeCollectionResource(ResourceResolver resolver, String collection) throws RepositoryException {
+        if (!enabled) {
+            return null;
+        }
         return getFqdnResource(resolver, collection, collectionResourceType);
     }
 
@@ -152,6 +174,9 @@
      */
     @Override
     public HApiType fromPath(ResourceResolver resolver, String type) throws RepositoryException {
+        if (!enabled) {
+            return null;
+        }
         Resource typeResource = this.getTypeResource(resolver, type);
         LOG.debug("typeResource=" + typeResource);
         if (null == typeResource) {
@@ -167,6 +192,9 @@
     @Override
     @Deprecated
     public HApiType fromNode(ResourceResolver resolver, Node typeNode) throws RepositoryException {
+        if (!enabled) {
+            return null;
+        }
         if (null == typeNode) return null;
         Resource resource = resolver.getResource(typeNode.getPath());
         return fromResource(resolver, resource);
@@ -176,6 +204,9 @@
      * {@inheritDoc}
      */
     public HApiType fromResource(ResourceResolver resolver, Resource typeResource) throws RepositoryException {
+        if (!enabled) {
+            return null;
+        }
         if (null == typeResource) return null;
 
         ValueMap resProps = typeResource.adaptTo(ValueMap.class);
@@ -244,6 +275,9 @@
      */
     @Override
     public HApiTypesCollection collectionFromResource(ResourceResolver resolver, Resource collectionResource) throws RepositoryException {
+        if (!enabled) {
+            return null;
+        }
         if (null == collectionResource) return null;
         ValueMap resProps = collectionResource.adaptTo(ValueMap.class);
         String name = resProps.get("name", (String) null);
@@ -271,6 +305,9 @@
      */
     @Override
     public HApiTypesCollection collectionFromPath(ResourceResolver resolver, String collectionPath) throws RepositoryException {
+        if (!enabled) {
+            return null;
+        }
         return collectionFromResource(resolver, this.getTypeCollectionResource(resolver, collectionPath));
     }
 
@@ -279,6 +316,9 @@
      */
     @Override
     public MicrodataAttributeHelper getHelper(ResourceResolver resolver, String type) throws RepositoryException {
+        if (!enabled) {
+            return new EmptyAttributeHelperImpl();
+        }
         return new MicrodataAttributeHelperImpl(resolver, TypesCache.getInstance(this).getType(resolver, getTypeResource(resolver, type)));
     }
 }
diff --git a/src/main/java/org/apache/sling/hapi/sightly/TypeView.java b/src/main/java/org/apache/sling/hapi/sightly/TypeView.java
index 927deb5..cb3650b 100644
--- a/src/main/java/org/apache/sling/hapi/sightly/TypeView.java
+++ b/src/main/java/org/apache/sling/hapi/sightly/TypeView.java
@@ -62,20 +62,34 @@
     public void activate() throws Exception {
         hapi = sling.getService(HApiUtil.class);
         me = hapi.fromPath(resourceResolver, resource.getPath());
+
+        if (null == me) {
+            return;
+        }
+
         LOG.debug("me: {}  resource: {}", me, resource.getPath());
         description = me.getDescription();
         parent = me.getParent();
     }
 
     public String getTitle() {
+        if (null == me) {
+            return "";
+        }
         return me.getFqdn();
     }
 
     public String getDescription() {
+        if (null == me) {
+            return "";
+        }
         return description;
     }
 
     public String getParentUrl() {
+        if (null == me) {
+            return "";
+        }
         if (null != parent) {
             return parent.getUrl();
         } else {
@@ -84,6 +98,9 @@
     }
 
     public String getParentFqdn() {
+        if (null == me) {
+            return "";
+        }
         if (null != parent) {
             return parent.getFqdn();
         } else {
@@ -92,16 +109,25 @@
     }
 
     public List<String> getParameters() {
+        if (null == me) {
+            return Collections.emptyList();
+        }
         return me.getParameters();
     }
 
     public List<HApiProperty> getProps() {
+        if (null == me) {
+            return Collections.emptyList();
+        }
         List<HApiProperty> props = new ArrayList<HApiProperty>(me.getAllProperties().values());
         LOG.debug("props: ", props);
         return props;
     }
 
     public boolean getHasProps() {
+        if (null == me) {
+            return false;
+        }
         return getProps().size() > 0;
     }
 }