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

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

commit 3581dbbe4fb3d339e734fc7432d8d774a722ec51
Author: Bertrand Delacretaz <bdelacre...@apache.org>
AuthorDate: Tue Aug 2 13:45:35 2016 +0000

    SLING-5943 - support explicit format in RepositoryInitializer configuration
    
    git-svn-id: 
https://svn.apache.org/repos/asf/sling/trunk/bundles/jcr/repoinit@1754918 
13f79535-47bb-0310-9956-ffa450edef68
---
 .../jcr/repoinit/impl/RepositoryInitializer.java   | 58 ++++++++++++++++++----
 .../jcr/repoinit/RepositoryInitializerTest.java    | 47 ++++++++++++++----
 2 files changed, 86 insertions(+), 19 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/jcr/repoinit/impl/RepositoryInitializer.java 
b/src/main/java/org/apache/sling/jcr/repoinit/impl/RepositoryInitializer.java
index 5c83c90..0c6475f 100644
--- 
a/src/main/java/org/apache/sling/jcr/repoinit/impl/RepositoryInitializer.java
+++ 
b/src/main/java/org/apache/sling/jcr/repoinit/impl/RepositoryInitializer.java
@@ -22,6 +22,7 @@ import java.io.StringReader;
 import java.io.StringWriter;
 import java.net.URL;
 import java.net.URLConnection;
+import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
 
@@ -66,10 +67,6 @@ public class RepositoryInitializer implements 
SlingRepositoryInitializer {
 
     public static final String DEFAULT_TEXT_URL = 
"context:/resources/provisioning/model.txt";
     
-    /** Special value for model section name config parameter, which indicates 
that
-     *  the configured URL provides raw repoinit statements */ 
-    public static final String RAW_SECTION_MARKER = "<RAW>";
-    
     @Property(
             label="Text URL", 
             description="URL of the source text that provides repoinit 
statements."
@@ -83,13 +80,23 @@ public class RepositoryInitializer implements 
SlingRepositoryInitializer {
     @Property(
             label="Model section name", 
             description=
-                "Optional provisioning model additional section name (without 
leading colon) used to extract"
-                + " repoinit statements from the raw text provided by the 
configured source text URL. Leave empty or set to <RAW> to consider the content"
-                + " provided by that URL to already be in repoinit format", 
+                "If using the provisioning model format, this specifies the 
additional section name (without leading colon) used to extract"
+                + " repoinit statements from the raw text provided by the 
configured source text URL.",
             value=DEFAULT_MODEL_SECTION_NAME)
     public static final String PROP_MODEL_SECTION_NAME = "model.section.name";
     private String modelSectionName;
     
+    @Property(
+            label="Text format", 
+            description=
+                "The format to use to interpret the text provided by the 
configured source text URL. "
+                + "That text can be either a Sling provisioning model with 
repoinit statements embedded in additional sections,"
+                + " or raw repoinit statements",
+            value=DEFAULT_MODEL_SECTION_NAME)
+    public static final String PROP_TEXT_FORMAT = "text.format";
+    public static enum TextFormat { RAW, MODEL };
+    private TextFormat textFormat;
+    
     @Reference
     private RepoInitParser parser;
     
@@ -99,13 +106,44 @@ public class RepositoryInitializer implements 
SlingRepositoryInitializer {
     @Activate
     public void activate(Map<String, Object> config) {
         textURL = PropertiesUtil.toString(config.get(PROP_TEXT_URL), 
DEFAULT_TEXT_URL);
+        
+        final String fmt = 
PropertiesUtil.toString(config.get(PROP_TEXT_FORMAT), 
TextFormat.MODEL.toString());
+        try {
+            textFormat = TextFormat.valueOf(fmt);
+        } catch(Exception e) {
+            throw new IllegalArgumentException("Invalid text format '" + fmt + 
"',"
+                    + " valid values are " + 
Arrays.asList(TextFormat.values()));
+        }
+        
         modelSectionName = 
PropertiesUtil.toString(config.get(PROP_MODEL_SECTION_NAME), 
DEFAULT_MODEL_SECTION_NAME);
+        
+        log.debug("Activated: {}", this.toString());
+    }
+    
+    @Override
+    public String toString() {
+        final StringBuilder sb = new StringBuilder();
+        sb.append(getClass().getSimpleName()).append(": ");
+        sb.append(PROP_TEXT_URL).append("=").append(textURL).append(", ");
+        sb.append(PROP_TEXT_FORMAT).append("=").append(textFormat).append(", 
");
+        
sb.append(PROP_MODEL_SECTION_NAME).append("=").append(modelSectionName);
+        return sb.toString();
     }
     
     @Override
     public void processRepository(SlingRepository repo) throws Exception {
         final String repoinit = getRepoInitText();
         
+        if(TextFormat.MODEL.equals(textFormat)) {
+            if(modelSectionName == null) {
+                throw new IllegalStateException("Section name is null, cannot 
read model");
+            }
+            if(modelSectionName.trim().length() == 0) {
+                throw new IllegalStateException("Empty " + 
PROP_MODEL_SECTION_NAME + " is not supported anymore, please use " 
+                        + PROP_TEXT_FORMAT + " to specify the input text 
format");
+            }
+        }
+        
         // loginAdministrative is ok here, definitely an admin operation
         final Session s = repo.loginAdministrative(null);
         try {
@@ -142,8 +180,7 @@ public class RepositoryInitializer implements 
SlingRepositoryInitializer {
         final String rawText = getRawRepoInitText();
         log.debug("Raw text from {}: \n{}", textURL, rawText);
         log.info("Got {} characters from {}", rawText.length(), textURL);
-        final boolean parseRawText = modelSectionName.trim().length() == 0 || 
RAW_SECTION_MARKER.equals(modelSectionName);
-        if (parseRawText) {
+        if(TextFormat.RAW.equals(textFormat)) {
             log.info("Parsing raw repoinit statements from {}", textURL);
             return rawText;
         } else {
@@ -152,6 +189,9 @@ public class RepositoryInitializer implements 
SlingRepositoryInitializer {
             try {
                 final Model model = ModelReader.read(reader, textURL);
                 final StringBuilder sb = new StringBuilder();
+                if(modelSectionName == null) {
+                    throw new IllegalStateException("Model section name is 
null, cannot read model");
+                }
                 for (final Feature feature : model.getFeatures()) {
                     for (final Section section : 
feature.getAdditionalSections(modelSectionName)) {
                         sb.append("# ").append(modelSectionName).append(" from 
").append(feature.getName()).append("\n");
diff --git 
a/src/test/java/org/apache/sling/jcr/repoinit/RepositoryInitializerTest.java 
b/src/test/java/org/apache/sling/jcr/repoinit/RepositoryInitializerTest.java
index 4c977a3..4081050 100644
--- a/src/test/java/org/apache/sling/jcr/repoinit/RepositoryInitializerTest.java
+++ b/src/test/java/org/apache/sling/jcr/repoinit/RepositoryInitializerTest.java
@@ -16,6 +16,7 @@
  */
 package org.apache.sling.jcr.repoinit;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.fail;
 
 import java.io.File;
@@ -31,6 +32,7 @@ import java.util.UUID;
 import org.apache.sling.jcr.api.SlingRepository;
 import org.apache.sling.jcr.repoinit.impl.JcrRepoInitOpsProcessorImpl;
 import org.apache.sling.jcr.repoinit.impl.RepositoryInitializer;
+import org.apache.sling.jcr.repoinit.impl.RepositoryInitializer.TextFormat;
 import org.apache.sling.jcr.repoinit.impl.TestUtil;
 import org.apache.sling.repoinit.parser.impl.RepoInitParserService;
 import org.apache.sling.testing.mock.sling.ResourceResolverType;
@@ -58,22 +60,33 @@ public class RepositoryInitializerTest {
     private final String repoInitText;
     private final String url;
     private final String modelSection;
+    private final String textFormat;
     private final boolean testLogin;
     private final String serviceUser;
+    private final Class<?> expectedActivateException;
     
     @Parameters(name="{0}")
     public static Collection<Object[]> data() {
         final List<Object []> result = new ArrayList<Object[]>();
         
-        result.add(new Object[] { "All empty, just setup + parsing", "", 
false, false });
-        result.add(new Object[] { "Using provisioning model", "SECTION_" + 
UUID.randomUUID(), true, true }); 
-        result.add(new Object[] { "Raw repoinit/empty section", "", false, 
true}); 
-        result.add(new Object[] { "Raw repoinit/special section name", 
"<RAW>", false, true}); 
-        result.add(new Object[] { "Default value of model section config", 
null, true, true}); 
+        // Realistic cases
+        result.add(new Object[] { "Using provisioning model", "SECTION_" + 
UUID.randomUUID(), TextFormat.MODEL.toString(), true, true, null }); 
+        result.add(new Object[] { "Default value of model section config", 
null, TextFormat.MODEL.toString(), true, true, null });
+        result.add(new Object[] { "Raw repoinit/empty section", "", 
TextFormat.RAW.toString(), false, true, null }); 
+        result.add(new Object[] { "Raw repoinit/ignored section name", 
"IGNORED_SectionName", TextFormat.RAW.toString(), false, true, null }); 
+        
+        // Edge and failure cases 
+        result.add(new Object[] { "All empty, just setup + parsing", "", 
TextFormat.RAW.toString(), false, false, null });
+        result.add(new Object[] { "Raw repoinit/null format", null, null, 
true, false, RuntimeException.class });
+        result.add(new Object[] { "With model/null format", null, null, false, 
false, RuntimeException.class });
+        result.add(new Object[] { "Invalid format", null, "invalidFormat", 
false, false, RuntimeException.class }); 
+        result.add(new Object[] { "Empty model section", "", 
TextFormat.MODEL.toString(), false, false, IllegalStateException.class }); 
+        result.add(new Object[] { "Null model section", null, 
TextFormat.MODEL.toString(), false, false, IllegalStateException.class }); 
         return result;
     }
     
-    public RepositoryInitializerTest(String description, String modelSection, 
boolean useProvisioningModel, boolean testLogin) throws IOException {
+    public RepositoryInitializerTest(String description, String modelSection, 
String textFormat, 
+            boolean useProvisioningModel, boolean testLogin, Class<?> 
expectedException) throws IOException {
         serviceUser = getClass().getSimpleName() + "-" + UUID.randomUUID();
         
         String txt = "create service user " + serviceUser; 
@@ -86,6 +99,8 @@ public class RepositoryInitializerTest {
         this.url = getTestUrl(repoInitText);
         this.modelSection = modelSection;
         this.testLogin = testLogin;
+        this.textFormat = textFormat;
+        this.expectedActivateException = expectedException;
     }
     
     @Before
@@ -98,14 +113,26 @@ public class RepositoryInitializerTest {
         if(modelSection != null) {
             config.put(RepositoryInitializer.PROP_MODEL_SECTION_NAME, 
modelSection);
         }
-        initializer.activate(config);
+        if(textFormat != null) {
+            config.put(RepositoryInitializer.PROP_TEXT_FORMAT, textFormat);
+        }
         
         context.registerInjectActivateService(new RepoInitParserService());
         context.registerInjectActivateService(new 
JcrRepoInitOpsProcessorImpl());
-        context.registerInjectActivateService(initializer, config);
         
-        // Mock environment doesn't cause this to be called
-        
initializer.processRepository(context.getService(SlingRepository.class));
+        try {
+            context.registerInjectActivateService(initializer, config);
+            
+            // Mock environment doesn't cause this to be called
+            
initializer.processRepository(context.getService(SlingRepository.class));
+        } catch(Exception e) {
+            if(expectedActivateException != null) {
+                assertEquals(expectedActivateException, e.getClass());
+            } else {
+                fail("Got unexpected " + e.getClass().getName() + " in 
activation");
+            }
+        }
+        
     }
     
     @Test

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

Reply via email to