You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:58:25 UTC

[sling-org-apache-sling-resourcebuilder] 16/36: SLING-5356 - get rid of ResourceBuilderProvider, adding two for* methods to ResourceBuilder

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.resourcebuilder-1.0.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-resourcebuilder.git

commit 8ebf1b65f37b8bca1be0f25493a6ceb5043bfb8c
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Mon Dec 14 12:43:30 2015 +0000

    SLING-5356 - get rid of ResourceBuilderProvider, adding two for* methods to ResourceBuilder
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/resourcebuilder@1719891 13f79535-47bb-0310-9956-ffa450edef68
---
 .../sling/resourcebuilder/api/ResourceBuilder.java |  14 ++-
 .../api/ResourceBuilderProvider.java               |  30 ------
 .../resourcebuilder/impl/ResourceBuilderImpl.java  |  16 ++-
 .../impl/ResourceBuilderProviderImpl.java          |  41 --------
 .../impl/ResourceBuilderService.java               | 110 +++++++++++++++++++++
 .../customizers/RBIT_TeleporterCustomizer.java     |   4 +-
 .../resourcebuilder/it/ResourceBuilderIT.java      |  28 ++++++
 .../sling/resourcebuilder/it/TestEnvironment.java  |   5 +-
 8 files changed, 171 insertions(+), 77 deletions(-)

diff --git a/src/main/java/org/apache/sling/resourcebuilder/api/ResourceBuilder.java b/src/main/java/org/apache/sling/resourcebuilder/api/ResourceBuilder.java
index 0d73fcc..1a1f965 100644
--- a/src/main/java/org/apache/sling/resourcebuilder/api/ResourceBuilder.java
+++ b/src/main/java/org/apache/sling/resourcebuilder/api/ResourceBuilder.java
@@ -21,16 +21,28 @@ package org.apache.sling.resourcebuilder.api;
 import java.io.InputStream;
 
 import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
 
 import aQute.bnd.annotation.ProviderType;
 
 /** Builds Sling Resources using a simple fluent API */
-
 @ProviderType
 public interface ResourceBuilder {
     
+    /** Default primary type for resources created by this builder */
     public static final String DEFAULT_PRIMARY_TYPE = "nt:unstructured";
     
+    /** Start a ResourceBuilder using the supplied parent resource 
+     *  @return the new builder
+     * */
+    ResourceBuilder forParent(Resource parent);
+    
+    /** Start a ResourceBuilder using the supplied ResourceResolver,
+     *  starting with the root resource as the builder's parent resource. 
+     *  @return the new builder
+     * */
+    ResourceBuilder forResolver(ResourceResolver r);
+    
     /** Create a Resource, which optionally becomes the current 
      *  parent Resource. 
      * @param relativePath The path of the Resource to create, relative to 
diff --git a/src/main/java/org/apache/sling/resourcebuilder/api/ResourceBuilderProvider.java b/src/main/java/org/apache/sling/resourcebuilder/api/ResourceBuilderProvider.java
deleted file mode 100644
index 379547e..0000000
--- a/src/main/java/org/apache/sling/resourcebuilder/api/ResourceBuilderProvider.java
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- * 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
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * 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.resourcebuilder.api;
-
-import org.apache.sling.api.resource.Resource;
-
-/** A service that provides ResourceBuilders */
-public interface ResourceBuilderProvider {
-    
-    /** Provides a ResourceBuilder to create resources 
-     *  under the supplied parent Resource. 
-     */
-    ResourceBuilder getResourceBuilder(Resource parent);
-}
diff --git a/src/main/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImpl.java b/src/main/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImpl.java
index 5dac7fa..3c89010 100644
--- a/src/main/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImpl.java
+++ b/src/main/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImpl.java
@@ -46,6 +46,10 @@ public class ResourceBuilderImpl implements ResourceBuilder {
     public static final String NT_RESOURCE = "nt:resource";
     public static final String NT_FILE = "nt:file";
     
+    public static final String CANNOT_RESTART =
+            "Cannot reset the parent resource or resource resolver, please create a new "
+            + "builder using the ResourceBuilder service";
+    
     private final MimeTypeService mimeTypeService;
     
     public ResourceBuilderImpl(Resource parent, MimeTypeService mts) {
@@ -58,7 +62,17 @@ public class ResourceBuilderImpl implements ResourceBuilder {
         withIntermediatePrimaryType(null);
         atParent();
     }
-
+    
+    @Override
+    public ResourceBuilder forParent(Resource parent) {
+        throw new UnsupportedOperationException(CANNOT_RESTART);
+    }
+    
+    @Override
+    public ResourceBuilder forResolver(ResourceResolver v) {
+        throw new UnsupportedOperationException(CANNOT_RESTART);
+    }
+    
     @Override
     public Resource getCurrentParent() {
         return currentParent;
diff --git a/src/main/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderProviderImpl.java b/src/main/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderProviderImpl.java
deleted file mode 100644
index 4832e4f..0000000
--- a/src/main/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderProviderImpl.java
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * 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
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * 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.resourcebuilder.impl;
-
-import org.apache.felix.scr.annotations.Component;
-import org.apache.felix.scr.annotations.Reference;
-import org.apache.felix.scr.annotations.Service;
-import org.apache.sling.api.resource.Resource;
-import org.apache.sling.commons.mime.MimeTypeService;
-import org.apache.sling.resourcebuilder.api.ResourceBuilder;
-import org.apache.sling.resourcebuilder.api.ResourceBuilderProvider;
-
-/** ResourceBuilderProvider implementation */
-@Component
-@Service(value=ResourceBuilderProvider.class)
-public class ResourceBuilderProviderImpl implements ResourceBuilderProvider {
-    
-    @Reference
-    private MimeTypeService mimeTypeService;
-
-    @Override
-    public ResourceBuilder getResourceBuilder(Resource parent) {
-        return new ResourceBuilderImpl(parent, mimeTypeService);
-    }
-}
\ No newline at end of file
diff --git a/src/main/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderService.java b/src/main/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderService.java
new file mode 100644
index 0000000..87f68db
--- /dev/null
+++ b/src/main/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderService.java
@@ -0,0 +1,110 @@
+/*
+ * 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
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.resourcebuilder.impl;
+
+import java.io.InputStream;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.commons.mime.MimeTypeService;
+import org.apache.sling.resourcebuilder.api.ResourceBuilder;
+
+/** ResourceBuilder service, only implements the for* methods to
+ *  create new builders. This allows us to provide a single service
+ *  interface which transparently becomes stateful, by switching from
+ *  this object to the ResourceBuilderImpl.
+ */
+@Component
+@Service(value=ResourceBuilder.class)
+public class ResourceBuilderService implements ResourceBuilder {
+    
+    @Reference
+    private MimeTypeService mimeTypeService;
+    
+    
+    private ResourceBuilder notStarted() {
+        throw new IllegalStateException(
+                "This ResourceBuilder is not started, please use the"
+                + "forParent or forResolver methods to start it."
+         );
+    }
+
+    @Override
+    public ResourceBuilder forParent(Resource parent) {
+        return new ResourceBuilderImpl(parent, mimeTypeService);
+    }
+
+    @Override
+    public ResourceBuilder forResolver(ResourceResolver r) {
+        final Resource root = r.getResource("/");
+        if(root == null) {
+            throw new IllegalStateException("Cannot read root resource");
+        }
+        return forParent(root);
+    }
+
+    @Override
+    public ResourceBuilder resource(String relativePath, Object... properties) {
+        return notStarted();
+    }
+
+    @Override
+    public ResourceBuilder file(String filename, InputStream data, String mimeType, long lastModified) {
+        return notStarted();
+    }
+
+    @Override
+    public ResourceBuilder file(String filename, InputStream data) {
+        return notStarted();
+    }
+
+    @Override
+    public ResourceBuilder commit() {
+        return notStarted();
+    }
+
+    @Override
+    public ResourceBuilder withIntermediatePrimaryType(String primaryType) {
+        return notStarted();
+    }
+
+    @Override
+    public ResourceBuilder siblingsMode() {
+        return notStarted();
+    }
+
+    @Override
+    public ResourceBuilder hierarchyMode() {
+        return notStarted();
+    }
+
+    @Override
+    public Resource getCurrentParent() {
+        notStarted();
+        return null;
+    }
+
+    @Override
+    public ResourceBuilder atParent() {
+        return notStarted();
+    }
+}
\ No newline at end of file
diff --git a/src/test/java/org/apache/sling/junit/teleporter/customizers/RBIT_TeleporterCustomizer.java b/src/test/java/org/apache/sling/junit/teleporter/customizers/RBIT_TeleporterCustomizer.java
index a01f991..765923b 100644
--- a/src/test/java/org/apache/sling/junit/teleporter/customizers/RBIT_TeleporterCustomizer.java
+++ b/src/test/java/org/apache/sling/junit/teleporter/customizers/RBIT_TeleporterCustomizer.java
@@ -17,7 +17,7 @@
 package org.apache.sling.junit.teleporter.customizers;
 
 import org.apache.sling.junit.rules.TeleporterRule;
-import org.apache.sling.resourcebuilder.api.ResourceBuilderProvider;
+import org.apache.sling.resourcebuilder.api.ResourceBuilder;
 import org.apache.sling.testing.teleporter.client.ClientSideTeleporter;
 import org.apache.sling.testing.tools.sling.SlingTestBase;
 import org.apache.sling.testing.tools.sling.TimeoutsProvider;
@@ -38,7 +38,7 @@ public class RBIT_TeleporterCustomizer implements TeleporterRule.Customizer {
         cst.setTestReadyTimeoutSeconds(TimeoutsProvider.getInstance().getTimeout(5));
         
         // Make sure our bundle API is imported instead of embedded
-        final String apiPackage = ResourceBuilderProvider.class.getPackage().getName();
+        final String apiPackage = ResourceBuilder.class.getPackage().getName();
         cst.includeDependencyPrefix("org.apache.sling.resourcebuilder");
         cst.excludeDependencyPrefix(apiPackage);
         cst.getAdditionalBundleHeaders().put(Constants.IMPORT_PACKAGE, apiPackage);
diff --git a/src/test/java/org/apache/sling/resourcebuilder/it/ResourceBuilderIT.java b/src/test/java/org/apache/sling/resourcebuilder/it/ResourceBuilderIT.java
index 6c6d43a..b869e41 100644
--- a/src/test/java/org/apache/sling/resourcebuilder/it/ResourceBuilderIT.java
+++ b/src/test/java/org/apache/sling/resourcebuilder/it/ResourceBuilderIT.java
@@ -96,4 +96,32 @@ public class ResourceBuilderIT {
         
         A.assertFile("a/b/c/model2.js", "application/javascript", "yes, it worked", startTime, moreThanStartTime);
     }
+    
+    @Test
+    public void usingResolver() throws IOException {
+        E.builderService.forResolver(E.resolver).resource("foo/a/b").commit();
+        E.builderService.forResolver(E.resolver).resource("foo/c/d").commit();
+        A.assertResource("/foo/a/b");
+        A.assertResource("/foo/c/d");
+    }
+    
+    @Test(expected=UnsupportedOperationException.class)
+    public void restartFailsA() throws IOException {
+        E.builder.forParent(E.resolver.getResource("/"));
+    }
+    
+    @Test(expected=UnsupportedOperationException.class)
+    public void restartFailsB() throws IOException {
+        E.builder.forResolver(E.resolver);
+    }
+    
+    @Test(expected=IllegalStateException.class)
+    public void notStartedFailsA() throws IOException {
+        E.builderService.resource("foo");
+    }
+    
+    @Test(expected=IllegalStateException.class)
+    public void notStartedFailsB() throws IOException {
+        E.builderService.file("foo", null);
+    }
 }
\ No newline at end of file
diff --git a/src/test/java/org/apache/sling/resourcebuilder/it/TestEnvironment.java b/src/test/java/org/apache/sling/resourcebuilder/it/TestEnvironment.java
index be22432..4749587 100644
--- a/src/test/java/org/apache/sling/resourcebuilder/it/TestEnvironment.java
+++ b/src/test/java/org/apache/sling/resourcebuilder/it/TestEnvironment.java
@@ -27,12 +27,12 @@ import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.resource.ResourceResolverFactory;
 import org.apache.sling.junit.rules.TeleporterRule;
 import org.apache.sling.resourcebuilder.api.ResourceBuilder;
-import org.apache.sling.resourcebuilder.api.ResourceBuilderProvider;
 import org.apache.sling.resourcebuilder.test.ResourceAssertions;
 
 class TestEnvironment {
     
     final ResourceBuilder builder;
+    final ResourceBuilder builderService;
     final ResourceResolver resolver;
     final String testRootPath;
     final Resource parent;
@@ -43,7 +43,8 @@ class TestEnvironment {
         resolver = teleporter.getService(ResourceResolverFactory.class).getAdministrativeResourceResolver(null);
         final Resource root = resolver.getResource("/");
         parent = resolver.create(root, testRootPath, null);
-        builder = teleporter.getService(ResourceBuilderProvider.class).getResourceBuilder(parent);
+        builderService = teleporter.getService(ResourceBuilder.class); 
+        builder = builderService.forParent(parent);
         A = new ResourceAssertions(testRootPath, resolver);
     }
     

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.