Author: bdelacretaz
Date: Tue Aug 2 13:45:35 2016
New Revision: 1754918
URL: http://svn.apache.org/viewvc?rev=1754918&view=rev
Log:
SLING-5943 - support explicit format in RepositoryInitializer configuration
Modified:
sling/trunk/bundles/jcr/repoinit/src/main/java/org/apache/sling/jcr/repoinit/impl/RepositoryInitializer.java
sling/trunk/bundles/jcr/repoinit/src/test/java/org/apache/sling/jcr/repoinit/RepositoryInitializerTest.java
Modified:
sling/trunk/bundles/jcr/repoinit/src/main/java/org/apache/sling/jcr/repoinit/impl/RepositoryInitializer.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/repoinit/src/main/java/org/apache/sling/jcr/repoinit/impl/RepositoryInitializer.java?rev=1754918&r1=1754917&r2=1754918&view=diff
==============================================================================
---
sling/trunk/bundles/jcr/repoinit/src/main/java/org/apache/sling/jcr/repoinit/impl/RepositoryInitializer.java
(original)
+++
sling/trunk/bundles/jcr/repoinit/src/main/java/org/apache/sling/jcr/repoinit/impl/RepositoryInitializer.java
Tue Aug 2 13:45:35 2016
@@ -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 imple
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 imple
@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 imple
@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 imple
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 imple
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");
Modified:
sling/trunk/bundles/jcr/repoinit/src/test/java/org/apache/sling/jcr/repoinit/RepositoryInitializerTest.java
URL:
http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/repoinit/src/test/java/org/apache/sling/jcr/repoinit/RepositoryInitializerTest.java?rev=1754918&r1=1754917&r2=1754918&view=diff
==============================================================================
---
sling/trunk/bundles/jcr/repoinit/src/test/java/org/apache/sling/jcr/repoinit/RepositoryInitializerTest.java
(original)
+++
sling/trunk/bundles/jcr/repoinit/src/test/java/org/apache/sling/jcr/repoinit/RepositoryInitializerTest.java
Tue Aug 2 13:45:35 2016
@@ -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