Skip to content

Commit

Permalink
More changes to installHookPolicy.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcin committed Jul 7, 2019
1 parent 225512d commit 1cee331
Show file tree
Hide file tree
Showing 14 changed files with 404 additions and 97 deletions.
7 changes: 7 additions & 0 deletions aem/net.adamcin.oakpal.interactive/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,17 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>oakpal-core</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
Expand Down
4 changes: 2 additions & 2 deletions cli/src/main/resources/net/adamcin/oakpal/cli/help.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
oakpal [ <options> ] <opearFile> [ <scanFile> ... ]
oakpal [ <options> ] <scanFile> ...

Options
-------
Expand All @@ -8,8 +8,8 @@ oakpal [ <options> ] <opearFile> [ <scanFile> ... ]
-j | --json : Write check reports in JSON format.
-o | --outfile <outFile> : Write check reports to the specified <outFile>
instead of writing to stdout.
-f | --file <opearFile> : Specify an OPEAR file to use (overrides $OAKPAL_OPEAR).
-c | --cache <directory> : Specify a cache directory for oakpal (default: ${CWD}/.oakpal-cache)
-f | --file <opearFile> : Specify an OPEAR file to use (overrides $OAKPAL_OPEAR).
-p | --plan <planName> : Specify a different plan name to lookup in the specified opear.
By default, the first plan exported by a specified opear will be used,
or if no opear is specified, the basic oakpal plan will be used.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@

import java.net.URL;
import java.util.Collection;
import java.util.Optional;
import javax.jcr.PathNotFoundException;

import org.apache.jackrabbit.vault.packaging.PackageId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Default implementation which reports all exceptions as violations.
*/
public class DefaultErrorListener implements ErrorListener {
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultErrorListener.class);

private final ReportCollector collector = new ReportCollector();

Expand All @@ -43,10 +47,10 @@ public void onNodeTypeRegistrationError(final Throwable e, final URL resource) {
if (e.getCause() != null) {
onNodeTypeRegistrationError(e.getCause(), resource);
} else {
reportViolation(
new SimpleViolation(Violation.Severity.MAJOR,
String.format("NodeType registration error (%s): %s \"%s\"",
String.valueOf(resource), e.getClass().getName(), e.getMessage())));
final String message = String.format("NodeType registration error (%s): %s \"%s\"",
String.valueOf(resource), e.getClass().getName(), e.getMessage());
LOGGER.trace("[onNodeTypeRegistrationError] stack trace for: " + message, e);
reportViolation(new SimpleViolation(Violation.Severity.MAJOR, message));
}
}

Expand All @@ -55,10 +59,10 @@ public void onJcrNamespaceRegistrationError(final Throwable e, final String pref
if (e.getCause() != null) {
onJcrNamespaceRegistrationError(e.getCause(), prefix, uri);
} else {
reportViolation(
new SimpleViolation(Violation.Severity.MAJOR,
String.format("JCR namespace registration error (%s=%s): %s \"%s\"",
prefix, uri, e.getClass().getName(), e.getMessage())));
final String message = String.format("JCR namespace registration error (%s=%s): %s \"%s\"",
prefix, uri, e.getClass().getName(), e.getMessage());
LOGGER.trace("[onJcrNamespaceRegistrationError] stack trace for: " + message, e);
reportViolation(new SimpleViolation(Violation.Severity.MAJOR, message));
}
}

Expand All @@ -67,10 +71,10 @@ public void onJcrPrivilegeRegistrationError(final Throwable e, final String jcrP
if (e.getCause() != null) {
onJcrPrivilegeRegistrationError(e.getCause(), jcrPrivilege);
} else {
reportViolation(
new SimpleViolation(Violation.Severity.MAJOR,
String.format("JCR privilege registration error (%s): %s \"%s\"",
jcrPrivilege, e.getClass().getName(), e.getMessage())));
final String message = String.format("JCR privilege registration error (%s): %s \"%s\"",
jcrPrivilege, e.getClass().getName(), e.getMessage());
LOGGER.trace("[onJcrPrivilegeRegistrationError] stack trace for: " + message, e);
reportViolation(new SimpleViolation(Violation.Severity.MAJOR, message));
}
}

Expand All @@ -79,46 +83,58 @@ public void onForcedRootCreationError(final Throwable e, final ForcedRoot forced
if (e.getCause() != null) {
onForcedRootCreationError(e.getCause(), forcedRoot);
} else {
reportViolation(
new SimpleViolation(Violation.Severity.MAJOR,
String.format("Forced root creation error (%s): %s \"%s\"",
forcedRoot.getPath(), e.getClass().getName(), e.getMessage())));
final String message = String.format("Forced root creation error (%s): %s \"%s\"",
forcedRoot.getPath(), e.getClass().getName(), e.getMessage());
LOGGER.trace("[onForcedRootCreationError] stack trace for: " + message, e);
reportViolation(new SimpleViolation(Violation.Severity.MAJOR, message));
}
}

@Override
public void onListenerException(Exception e, ProgressCheck listener, PackageId packageId) {
reportViolation(
new SimpleViolation(Violation.Severity.MAJOR,
String.format("Listener error (%s): %s \"%s\"",
listener.getClass().getName(), e.getClass().getName(), e.getMessage()), packageId));
public void onListenerException(final Exception e, final ProgressCheck listener, final PackageId packageId) {
final String message = String.format("Listener error (%s): %s \"%s\"",
listener.getClass().getName(), e.getClass().getName(), e.getMessage());
LOGGER.trace("[onListenerException] stack trace for: " + message, e);
reportViolation(new SimpleViolation(Violation.Severity.MAJOR, message, packageId));
}

@Override
public void onSubpackageException(Exception e, PackageId packageId) {
reportViolation(
new SimpleViolation(Violation.Severity.MAJOR,
String.format("Package error: %s \"%s\"",
e.getClass().getName(), e.getMessage()), packageId));
public void onSubpackageException(final Exception e, final PackageId packageId) {
final String message = String.format("Package error: %s \"%s\"", e.getClass().getName(), e.getMessage());
LOGGER.trace("[onSubpackageException] stack trace for: " + message, e);
reportViolation(new SimpleViolation(Violation.Severity.MAJOR, message, packageId));
}

@Override
public void onImporterException(Exception e, PackageId packageId, String path) {
public void onImporterException(final Exception e, final PackageId packageId, final String path) {
// Ignore PathNotFoundException, as it is thrown A LOT
if (!(e instanceof PathNotFoundException)) {
reportViolation(
new SimpleViolation(Violation.Severity.MAJOR,
String.format("%s - Importer error: %s \"%s\"", path, e.getClass().getName(), e.getMessage()),
packageId));
final String message = String.format("%s - Importer error: %s \"%s\"", path, e.getClass().getName(), e.getMessage());
LOGGER.trace("[onImporterException] stack trace for: " + message, e);
reportViolation(new SimpleViolation(Violation.Severity.MAJOR, message, packageId));
}
}

@Override
public void onListenerPathException(Exception e, ProgressCheck handler, PackageId packageId, String path) {
public void onListenerPathException(final Exception e, final ProgressCheck handler,
final PackageId packageId, final String path) {
final String message = String.format("%s - Listener error: %s \"%s\"", path, e.getClass().getName(), e.getMessage());
LOGGER.trace("[onListenerPathException] stack trace for: " + message, e);
reportViolation(new SimpleViolation(Violation.Severity.MAJOR, message, packageId));
}

@Override
public void onInstallHookError(final Throwable e, final PackageId packageId) {
final String message = String.format("InstallHook error: %s \"%s\"",
Optional.ofNullable(e.getCause()).orElse(e).getClass().getName(), e.getMessage());
LOGGER.trace("[onInstallHookError] stack trace for: " + message, e);
reportViolation(new SimpleViolation(Violation.Severity.MAJOR, message, packageId));
}

@Override
public void onProhibitedInstallHookRegistration(final PackageId packageId) {
reportViolation(
new SimpleViolation(Violation.Severity.MAJOR,
String.format("%s - Listener error: %s \"%s\"", path, e.getClass().getName(), e.getMessage()),
packageId));
"Policy prohibits the use of InstallHooks in packages", packageId));
}

}
9 changes: 9 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 @@ -18,6 +18,7 @@

import java.net.URL;

import org.apache.jackrabbit.vault.packaging.PackageException;
import org.apache.jackrabbit.vault.packaging.PackageId;

/**
Expand Down Expand Up @@ -105,4 +106,12 @@ default void onImporterException(final Exception e, final PackageId packageId, f
default void onSubpackageException(final Exception e, final PackageId packageId) {

}

default void onInstallHookError(final Throwable e, final PackageId packageId) {

}

default void onProhibitedInstallHookRegistration(final PackageId packageId) {

}
}
59 changes: 59 additions & 0 deletions core/src/main/java/net/adamcin/oakpal/core/InstallHookPolicy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2019 Mark Adamcin
*
* Licensed 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 net.adamcin.oakpal.core;

import org.jetbrains.annotations.Nullable;

/**
* Enumeration of policies for dealing with InstallHook processing during a scan.
*/
public enum InstallHookPolicy {
/**
* Report a violation if any hook is registered successfully. Implies {@link #REPORT}, and {@link #SKIP},
* insofar as the hook will not be executed after registration. Use this level if your policy is to disallow install
* hooks in your content packages.
*/
PROHIBIT,

/**
* Report a violation if any hook fails to register. This is likely a class loading issue, or an issue with Jar or
* Vault packaging. Similar to {@link #ABORT}, except scan will proceed.
*/
REPORT,

/**
* Abort the scan if any hook fails to register. This is likely a class loading issue, or an issue with Jar or
* Vault packaging.
*/
ABORT,

/**
* Disable install hook processing for scanned packages.
*/
SKIP;

public static final InstallHookPolicy DEFAULT = REPORT;

public static @Nullable InstallHookPolicy forName(final @Nullable String name) {
for (InstallHookPolicy value : values()) {
if (value.name().equalsIgnoreCase(name)) {
return value;
}
}
return null;
}
}
Loading

0 comments on commit 1cee331

Please sign in to comment.