Skip to content

Commit

Permalink
added repoinit support: checklist, plan, and init stage.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcin committed May 5, 2020
1 parent 469d5a7 commit 51ef769
Show file tree
Hide file tree
Showing 46 changed files with 1,202 additions and 84 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ target/
*.iml
.mvn/wrapper/maven-wrapper.jar
.oakpal-cache/
pom.xml.versionsBackup
dependency-reduced-pom.xml
2 changes: 1 addition & 1 deletion api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>net.adamcin.oakpal</groupId>
<artifactId>oakpal</artifactId>
<version>2.0.1-SNAPSHOT</version>
<version>2.1.0-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>net.adamcin.oakpal</groupId>
<artifactId>oakpal</artifactId>
<version>2.0.1-SNAPSHOT</version>
<version>2.1.0-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>

Expand Down
14 changes: 13 additions & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>net.adamcin.oakpal</groupId>
<artifactId>oakpal</artifactId>
<version>2.0.1-SNAPSHOT</version>
<version>2.1.0-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>

Expand Down Expand Up @@ -191,6 +191,14 @@
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.commons.johnzon</artifactId>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.repoinit.parser</artifactId>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.jcr.repoinit</artifactId>
</dependency>

<dependency>
<groupId>javax.jcr</groupId>
Expand All @@ -209,6 +217,10 @@
<groupId>org.apache.jackrabbit</groupId>
<artifactId>oak-jcr</artifactId>
</dependency>
<dependency>
<groupId>org.apache.jackrabbit</groupId>
<artifactId>oak-authorization-principalbased</artifactId>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
Expand Down
28 changes: 26 additions & 2 deletions core/src/main/java/net/adamcin/oakpal/core/Checklist.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public interface JsonKeys {

String forcedRoots();

String repoInits();

String checks();

List<String> orderedKeys();
Expand All @@ -84,6 +86,7 @@ public interface JsonKeys {
private final List<String> allKeys = Arrays.asList(
name(),
checks(),
repoInits(),
forcedRoots(),
cndNames(),
cndUrls(),
Expand Down Expand Up @@ -126,6 +129,11 @@ public String forcedRoots() {
return "forcedRoots";
}

@Override
public String repoInits() {
return "repoInits";
}

@Override
public String checks() {
return "checks";
Expand Down Expand Up @@ -170,6 +178,7 @@ public static <T> Comparator<T> comparingJsonKeys(final @NotNull Function<T, Str
private final List<PrivilegeDefinition> jcrPrivileges;
private final List<ForcedRoot> forcedRoots;
private final List<CheckSpec.ImmutableSpec> checks;
private final List<String> repoInits;

Checklist(final @Nullable JsonObject originalJson,
final @Nullable String moduleName,
Expand All @@ -179,7 +188,8 @@ public static <T> Comparator<T> comparingJsonKeys(final @NotNull Function<T, Str
final @NotNull List<QNodeTypeDefinition> jcrNodetypes,
final @NotNull List<PrivilegeDefinition> jcrPrivileges,
final @NotNull List<ForcedRoot> forcedRoots,
final @NotNull List<CheckSpec.ImmutableSpec> checks) {
final @NotNull List<CheckSpec.ImmutableSpec> checks,
final @NotNull List<String> repoInits) {
this.originalJson = originalJson;
this.moduleName = moduleName;
this.name = name;
Expand All @@ -189,6 +199,7 @@ public static <T> Comparator<T> comparingJsonKeys(final @NotNull Function<T, Str
this.jcrPrivileges = jcrPrivileges;
this.forcedRoots = forcedRoots;
this.checks = checks;
this.repoInits = repoInits;
}


Expand Down Expand Up @@ -234,8 +245,13 @@ public List<CheckSpec> getChecks() {
.collect(Collectors.toList());
}

public List<String> getRepoInits() {
return repoInits;
}

public InitStage asInitStage() {
InitStage.Builder builder = new InitStage.Builder()
.withRepoInits(getRepoInits())
.withOrderedCndUrls(getCndUrls())
.withForcedRoots(getForcedRoots())
.withPrivileges(getJcrPrivilegeNames())
Expand All @@ -254,6 +270,7 @@ static final class Builder {
private List<PrivilegeDefinition> jcrPrivileges = new ArrayList<>();
private List<ForcedRoot> forcedRoots = new ArrayList<>();
private List<CheckSpec> checks = new ArrayList<>();
private List<String> repoInits = new ArrayList<>();

Builder(final @NotNull String moduleName) {
this.moduleName = moduleName;
Expand Down Expand Up @@ -296,6 +313,11 @@ Builder withChecks(final @NotNull List<CheckSpec> checks) {
return this;
}

Builder withRepoInits(final @NotNull List<String> repoInits) {
this.repoInits.addAll(repoInits);
return this;
}

boolean isValidCheckspec(final @NotNull CheckSpec check) {
return !check.isAbstract()
&& check.getName() != null
Expand All @@ -318,7 +340,8 @@ Checklist build(final @Nullable JsonObject originalJson) {
Collections.unmodifiableList(forcedRoots),
Collections.unmodifiableList(checks.stream()
.map(CheckSpec::immutableCopyOf)
.collect(Collectors.toList())));
.collect(Collectors.toList())),
Collections.unmodifiableList(repoInits));
}
}

Expand Down Expand Up @@ -363,6 +386,7 @@ public JsonObject toJson() {
return obj()
.key(jsonKeys.name()).opt(getName())
.key(jsonKeys.checks()).opt(checks)
.key(jsonKeys.repoInits()).opt(getRepoInits())
.key(jsonKeys.forcedRoots()).opt(getForcedRoots())
.key(jsonKeys.cndUrls()).opt(getCndUrls())
.key(jsonKeys.jcrNodetypes()).opt(JsonCnd.toJson(getJcrNodetypes(), mapping))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.net.URL;
import java.text.MessageFormat;
import java.util.Collection;
import java.util.List;
import java.util.MissingResourceException;
import java.util.Optional;
import java.util.ResourceBundle;
Expand Down Expand Up @@ -193,4 +194,21 @@ public void onProhibitedInstallHookRegistration(final PackageId packageId) {
new SimpleViolation(Severity.MAJOR,
getString("Policy prohibits the use of InstallHooks in packages"), packageId));
}

@Override
public void onRepoInitUrlError(final Throwable e, final URL repoinitUrl) {
final String message = MessageFormat.format(getString("repoinit url error ({0}): {1} \"{2}\""),
String.valueOf(repoinitUrl), e.getClass().getName(), e.getMessage());
LOGGER.trace("[onRepoInitUrlError] stack trace for: " + message, e);
reportViolation(new SimpleViolation(Severity.MAJOR, message));
}

@Override
public void onRepoInitInlineError(final Throwable e, final List<String> repoinits) {
final String firstLine = repoinits == null || repoinits.isEmpty() ? "<empty>" : repoinits.get(0) + "...";
final String message = MessageFormat.format(getString("repoinit inline error ({0}): {1} \"{2}\""),
firstLine, e.getClass().getName(), e.getMessage());
LOGGER.trace("[onRepoInitInlineError] stack trace for: " + message, e);
reportViolation(new SimpleViolation(Severity.MAJOR, message));
}
}
23 changes: 23 additions & 0 deletions core/src/main/java/net/adamcin/oakpal/core/ErrorListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.osgi.annotation.versioning.ConsumerType;

import java.net.URL;
import java.util.List;

/**
* A single error handler is used during an OakPAL scan.
Expand Down Expand Up @@ -130,4 +131,26 @@ default void onInstallHookError(final Throwable e, final PackageId packageId) {
default void onProhibitedInstallHookRegistration(final PackageId packageId) {

}

/**
* Called for an IOException or RepoInitParsingException when parsing a repoinit url during
* {@code InitStage.initSession()}.
*
* @param e the error thrown
* @param repoinitUrl the repoinit url
*/
default void onRepoInitUrlError(final Throwable e, final URL repoinitUrl) {

}

/**
* Called for an IOException or RepoInitParsingException when parsing a list of repoinit scripts during
* {@code InitStage.initSession()}.
*
* @param e the error thrown
* @param repoinits the repoinit scripts
*/
default void onRepoInitInlineError(final Throwable e, final List<String> repoinits) {

}
}
69 changes: 66 additions & 3 deletions core/src/main/java/net/adamcin/oakpal/core/InitStage.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.nodetype.NodeTypeDefinition;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -56,6 +61,10 @@
* Encapsulation of JCR initialization parameters for multistage inits.
*/
public final class InitStage {
private final List<String> repoInits;

private final List<URL> repoInitUrls;

private final List<URL> unorderedCndUrls;

private final List<URL> orderedCndUrls;
Expand All @@ -71,13 +80,17 @@ public final class InitStage {

private final Map<String, ForcedRoot> forcedRoots;

private InitStage(final List<URL> unorderedCndUrls,
private InitStage(final List<String> repoInits,
final List<URL> repoInitUrls,
final List<URL> unorderedCndUrls,
final List<URL> orderedCndUrls,
final List<QNodeTypeDefinition> qNodeTypes,
final Map<String, String> namespaces,
final Set<PrivilegeDefinition> privileges,
final Set<String> privilegeNames,
final Map<String, ForcedRoot> forcedRoots) {
this.repoInits = repoInits;
this.repoInitUrls = repoInitUrls;
this.unorderedCndUrls = unorderedCndUrls;
this.orderedCndUrls = orderedCndUrls;
this.qNodeTypes = qNodeTypes;
Expand All @@ -104,6 +117,10 @@ public static class Builder {

private List<URL> orderedCndUrls = new ArrayList<>();

private List<URL> repoInitUrls = new ArrayList<>();

private List<String> repoInits = new ArrayList<>();

private List<QNodeTypeDefinition> qNodeTypes = new ArrayList<>();

/**
Expand Down Expand Up @@ -292,18 +309,47 @@ public Builder withQNodeTypes(final @Nullable List<QNodeTypeDefinition> qNodeTyp
return this;
}

/**
* Provide a list of repoinit URLs to apply, in order. The scripts at these urls will be evaluated individually,
* after the structured JCR definitions and prior to evaluation
*
* @param repoInitUrls the list of repoinit URLs.
* @return my builder self
*/
public Builder withRepoInitUrls(final @NotNull List<URL> repoInitUrls) {
this.repoInitUrls = new ArrayList<>(repoInitUrls);
return this;
}

/**
* Provide a list of inline repoinit scripts, replacing any that have already been added. All inline repoinit
* scripts provided to an InitStage are joined together with newlines and executed together as a single repoinit
* script.
*
* @param repoInits the repoinit scripts
* @return my builder self
*/
public Builder withRepoInits(final @NotNull List<String> repoInits) {
this.repoInits = new ArrayList<>(repoInits);
return this;
}

/**
* Construct an {@link InitStage} from the {@link Builder} state.
*
* @return an {@link InitStage}
*/
public InitStage build() {
return new InitStage(unorderedCndUrls, orderedCndUrls, qNodeTypes,
return new InitStage(repoInits, repoInitUrls, unorderedCndUrls, orderedCndUrls, qNodeTypes,
namespaces, privileges, privilegeNames, forcedRoots);
}
}

void initSession(final Session admin, final ErrorListener errorListener) throws RepositoryException {
void initSession(final Session admin, final ErrorListener errorListener,
final OakMachine.RepoInitProcessor repoInitProcessor)
throws RepositoryException {


final CNDURLInstaller cndInstaller = new CNDURLInstaller(errorListener,
this.unorderedCndUrls, this.orderedCndUrls);

Expand Down Expand Up @@ -392,5 +438,22 @@ void initSession(final Session admin, final ErrorListener errorListener) throws
admin.refresh(false);
}
}));

repoInitUrls.stream().forEachOrdered(repoinitUrl -> {
try (final InputStream repoinitInput = repoinitUrl.openStream();
final Reader repoinitReader = new InputStreamReader(repoinitInput, StandardCharsets.UTF_8)) {
repoInitProcessor.apply(admin, repoinitReader);
} catch (Exception e) {
errorListener.onRepoInitUrlError(e, repoinitUrl);
}
});

if (!repoInits.isEmpty()) {
try (Reader repoinitReader = new StringReader(String.join("\n", repoInits))) {
repoInitProcessor.apply(admin, repoinitReader);
} catch (Exception e) {
errorListener.onRepoInitInlineError(e, repoInits);
}
}
}
}
Loading

0 comments on commit 51ef769

Please sign in to comment.