SLING-6723 : Make dependency to javax.jcr, jcr.contentloader and jcr.api optional

git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1792883 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/sling/servlets/post/impl/PostResponseWithErrorHandling.java b/src/main/java/org/apache/sling/servlets/post/impl/PostResponseWithErrorHandling.java
index 7377105..a9af086 100644
--- a/src/main/java/org/apache/sling/servlets/post/impl/PostResponseWithErrorHandling.java
+++ b/src/main/java/org/apache/sling/servlets/post/impl/PostResponseWithErrorHandling.java
@@ -29,7 +29,10 @@
 import org.apache.sling.servlets.post.SlingPostConstants;
 import org.osgi.service.component.annotations.Component;
 
-@Component(service = PostResponseCreator.class)
+@Component(service = PostResponseCreator.class,
+    property = {
+            "service.vendor=The Apache Software Foundation"
+    })
 public class PostResponseWithErrorHandling implements PostResponseCreator {
 
 	@Override
diff --git a/src/main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java b/src/main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java
index 2e02f08..f8cfcf3 100644
--- a/src/main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java
+++ b/src/main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java
@@ -49,6 +49,7 @@
 import org.apache.sling.servlets.post.VersioningConfiguration;
 import org.apache.sling.servlets.post.impl.helper.DateParser;
 import org.apache.sling.servlets.post.impl.helper.DefaultNodeNameGenerator;
+import org.apache.sling.servlets.post.impl.helper.JCRSupport;
 import org.apache.sling.servlets.post.impl.helper.MediaRangeList;
 import org.apache.sling.servlets.post.impl.operations.CheckinOperation;
 import org.apache.sling.servlets.post.impl.operations.CheckoutOperation;
@@ -70,6 +71,7 @@
 import org.osgi.service.component.annotations.Reference;
 import org.osgi.service.component.annotations.ReferenceCardinality;
 import org.osgi.service.component.annotations.ReferencePolicy;
+import org.osgi.service.component.annotations.ReferencePolicyOption;
 import org.osgi.service.metatype.annotations.AttributeDefinition;
 import org.osgi.service.metatype.annotations.Designate;
 import org.osgi.service.metatype.annotations.ObjectClassDefinition;
@@ -191,11 +193,6 @@
 
     private final ImportOperation importOperation = new ImportOperation();
 
-    /**
-     * The content importer reference.
-     */
-	private ContentImporter contentImporter;
-
     private VersioningConfiguration baseVersioningConfiguration;
 
     @Override
@@ -434,19 +431,17 @@
             SlingPostConstants.OPERATION_DELETE, new DeleteOperation()));
         providedServices.add(registerOperation(bundleContext,
             SlingPostConstants.OPERATION_NOP, new NopOperation()));
-        providedServices.add(registerOperation(bundleContext,
-            SlingPostConstants.OPERATION_IMPORT, importOperation));
 
         // the following operations require JCR:
-        try {
+        if ( JCRSupport.INSTANCE.jcrEnabled()) {
+            providedServices.add(registerOperation(bundleContext,
+                SlingPostConstants.OPERATION_IMPORT, importOperation));
             providedServices.add(registerOperation(bundleContext,
                     SlingPostConstants.OPERATION_CHECKIN, new CheckinOperation()));
             providedServices.add(registerOperation(bundleContext,
                     SlingPostConstants.OPERATION_CHECKOUT, new CheckoutOperation()));
             providedServices.add(registerOperation(bundleContext,
                     SlingPostConstants.OPERATION_RESTORE, new RestoreOperation()));
-        } catch ( final Throwable t) {
-            // ignore as JCR is optional
         }
         internalOperations = providedServices.toArray(new ServiceRegistration[providedServices.size()]);
     }
@@ -722,17 +717,16 @@
         this.cachedPostResponseCreators = localCache;
     }
 
-    @Reference(cardinality = ReferenceCardinality.OPTIONAL, policy = ReferencePolicy.DYNAMIC)
-    protected void bindContentImporter(final ContentImporter importer) {
-        this.contentImporter = importer;
+    @Reference(service = ContentImporter.class,
+            cardinality = ReferenceCardinality.OPTIONAL,
+            policy = ReferencePolicy.DYNAMIC,
+            policyOption = ReferencePolicyOption.GREEDY)
+    protected void bindContentImporter(final Object importer) {
         importOperation.setContentImporter(importer);
     }
 
-    protected void unbindContentImporter(final ContentImporter importer) {
-        if ( this.contentImporter == importer ) {
-            this.contentImporter = null;
-            importOperation.setContentImporter(null);
-        }
+    protected void unbindContentImporter(final Object importer) {
+        importOperation.unsetContentImporter(importer);
     }
 
     private VersioningConfiguration createBaseVersioningConfiguration(Config config) {
diff --git a/src/main/java/org/apache/sling/servlets/post/impl/helper/JCRSupport.java b/src/main/java/org/apache/sling/servlets/post/impl/helper/JCRSupport.java
index 45330e1..c09cf04 100644
--- a/src/main/java/org/apache/sling/servlets/post/impl/helper/JCRSupport.java
+++ b/src/main/java/org/apache/sling/servlets/post/impl/helper/JCRSupport.java
@@ -43,7 +43,7 @@
         try {
             impl = new JCRSupportImpl();
         } catch ( final Throwable t) {
-            logger.warn("Support for JCR operations like checkin, checkout, ordering etc. is currently disabled " +
+            logger.warn("Support for JCR operations like checkin, checkout, import, ordering etc. is currently disabled " +
                         "in the servlets post module. Check whether the JCR API is available.");
         }
         this.supportImpl = impl;
@@ -215,4 +215,8 @@
         // the caller already got an item and a node, so supportImpl is available
         ((JCRSupportImpl)supportImpl).move(src, dstParent, name);
     }
+
+    public boolean jcrEnabled() {
+        return this.supportImpl != null;
+    }
 }
diff --git a/src/main/java/org/apache/sling/servlets/post/impl/operations/ImportOperation.java b/src/main/java/org/apache/sling/servlets/post/impl/operations/ImportOperation.java
index 0d6910b..9d934d1 100644
--- a/src/main/java/org/apache/sling/servlets/post/impl/operations/ImportOperation.java
+++ b/src/main/java/org/apache/sling/servlets/post/impl/operations/ImportOperation.java
@@ -49,12 +49,18 @@
     /**
      * Reference to the content importer service
      */
-    private ContentImporter contentImporter;
+    private Object contentImporter;
 
-    public void setContentImporter(ContentImporter importer) {
+    public void setContentImporter(Object importer) {
         this.contentImporter = importer;
     }
 
+    public void unsetContentImporter(Object importer) {
+        if ( this.contentImporter == importer ) {
+            this.contentImporter = null;
+        }
+    }
+
     private String getRequestParamAsString(SlingHttpServletRequest request, String key) {
     	RequestParameter requestParameter = request.getRequestParameter(key);
     	if (requestParameter == null) {
@@ -67,7 +73,7 @@
     protected void doRun(SlingHttpServletRequest request, PostResponse response, final List<Modification> changes)
             throws PersistenceException {
         try {
-            ContentImporter importer = contentImporter;
+            Object importer = contentImporter;
             if (importer == null) {
                 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                         "Missing content importer for import");
@@ -166,7 +172,7 @@
                             "Missing content for import");
                     return;
                 } else {
-                    importer.importContent(node, contentRootName, contentStream,
+                    ((ContentImporter)importer).importContent(node, contentRootName, contentStream,
                             new ImportOptions() {
 
                                 @Override