Skip to content

Commit

Permalink
[MENFORCER-456] New Enforcer API - RuleConfigProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed Jan 8, 2023
1 parent ec44333 commit 5c65dd7
Show file tree
Hide file tree
Showing 32 changed files with 666 additions and 366 deletions.
5 changes: 5 additions & 0 deletions enforcer-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,13 @@
* @author Slawomir Jaranowski
* @since 3.2.0
*/
public abstract class AbstractEnforcerRule implements EnforcerRuleBase {

/**
* EnforcerLogger instance
*/
private EnforcerLogger log;
public abstract class AbstractEnforcerRule extends AbstractEnforcerRuleBase {

/**
* Enforcer Rule execution level
*/
private EnforcerLevel level = EnforcerLevel.ERROR;

/**
* Used by {@code EnforcerMojo} to inject logger instance
*
* @param log an {@link EnforcerLogger} instance
*/
public void setLog(EnforcerLogger log) {
this.log = log;
}

/**
* Provide an {@link EnforcerLogger} instance for Rule
*
* @return an {@link EnforcerLogger} instance
*/
public EnforcerLogger getLog() {
return log;
}

/**
* Current Enforcer execution level
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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.maven.enforcer.rule.api;

/**
* Base rule implementation for new API.
* <p>
* Used for internal purpose.
*
* @author Slawomir Jaranowski
* @since 3.2.0
*/
public abstract class AbstractEnforcerRuleBase implements EnforcerRuleBase {

/**
* EnforcerLogger instance
*/
private EnforcerLogger log;

/**
* Used by {@code EnforcerMojo} to inject logger instance
*
* @param log an {@link EnforcerLogger} instance
*/
public void setLog(EnforcerLogger log) {
this.log = log;
}

/**
* Provide an {@link EnforcerLogger} instance for Rule
*
* @return an {@link EnforcerLogger} instance
*/
public EnforcerLogger getLog() {
return log;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.maven.enforcer.rule.api;

import org.codehaus.plexus.util.xml.Xpp3Dom;

/**
* Entry point for custom {@code Enforcer Rule} which provide additional rules configuration.
* <p>
* Provided configuration will be added to current rules list by {@code Enforcer Mojo}
*
* @author Slawomir Jaranowski
* @since 3.2.0
*/
public abstract class AbstractEnforcerRuleConfigProvider extends AbstractEnforcerRuleBase {

/**
* Produce rule configuration.
* <p>
* Returned configuration must contain rules configuration as in example:
* <pre>
* &lt;rules&gt;
* &lt;ruleName/&gt;
* &lt;ruleName&gt;
* &lt;ruleConfig&gt;config value&lt;/ruleConfig&gt;
* &lt;/ruleName&gt;
* &lt;/rules&gt;
* </pre>
*
* @return a rules configuration
* @throws EnforcerRuleError the error during executing
*/
public abstract Xpp3Dom getRulesConfig() throws EnforcerRuleError;
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ public EnforcerRuleError(String message, Throwable cause) {
public EnforcerRuleError(String message) {
super(message);
}

public EnforcerRuleError(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public EnforcerRuleException(Object source, String shortMessage, String longMess
* <code>message</code>.
*
* @param message the message
* @param cause the cause
* @param cause the cause
*/
public EnforcerRuleException(String message, Exception cause) {
super(message, cause);
Expand All @@ -108,4 +108,14 @@ public EnforcerRuleException(String message, Throwable cause) {
public EnforcerRuleException(String message) {
super(message);
}

/**
* Construct a new <code>EnforcerRuleException</code> exception wrapping
* an underlying <code>Throwable</code>.
*
* @param cause the cause
*/
public EnforcerRuleException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,25 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.plugins.enforcer;
package org.apache.maven.enforcer.rules;

import org.apache.maven.enforcer.rule.api.EnforcerRule;
import org.apache.maven.enforcer.rule.api.AbstractEnforcerRule;

/**
* An enforcer rules descriptor used by {@link ExternalRules}
* Abstract help rule.
*
* @author <a href="mailto:gastaldi@apache.org">George Gastaldi</a>
* @author Slawomir Jaranowski
* @since 3.2.0
*/
public class EnforcerDescriptor {
EnforcerRule[] rules;
abstract class AbstractStandardEnforcerRule extends AbstractEnforcerRule {

public EnforcerRule[] getRules() {
return rules;
private String message;

public String getMessage() {
return message;
}

public void setRules(EnforcerRule[] rules) {
this.rules = rules;
public void setMessage(String message) {
this.message = message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import javax.inject.Named;

import org.apache.maven.enforcer.rule.api.AbstractEnforcerRule;
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;

/**
Expand All @@ -29,23 +28,21 @@
* @author Ben Lidgey
*/
@Named("alwaysFail")
public final class AlwaysFail extends AbstractEnforcerRule {

private String message;
public final class AlwaysFail extends AbstractStandardEnforcerRule {

@Override
public void execute() throws EnforcerRuleException {

StringBuilder buf = new StringBuilder();
if (message != null) {
buf.append(message).append(System.lineSeparator());
if (getMessage() != null) {
buf.append(getMessage()).append(System.lineSeparator());
}
buf.append("Always fails!");
throw new EnforcerRuleException(buf.toString());
}

@Override
public String toString() {
return String.format("AlwaysFail[level=%s, message=%s]", getLevel(), message);
return String.format("AlwaysFail[level=%s, message=%s]", getLevel(), getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,30 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.plugins.enforcer;
package org.apache.maven.enforcer.rules;

import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
import org.apache.maven.plugin.logging.Log;
import javax.inject.Named;

/**
* Always pass. This rule is useful for testing the Enforcer configuration.
* @author Ben Lidgey
*/
public class AlwaysPass extends AbstractNonCacheableEnforcerRule {
@Named("alwaysPass")
public final class AlwaysPass extends AbstractStandardEnforcerRule {

@Override
public void execute(EnforcerRuleHelper helper) {
final Log log = helper.getLog();

String message = getMessage();
public void execute() {

StringBuilder buf = new StringBuilder();
if (message != null) {
buf.append(message).append(System.lineSeparator());
if (getMessage() != null) {
buf.append(getMessage()).append(System.lineSeparator());
}
buf.append("Always pass!");
log.info(buf.toString());
getLog().info(buf.toString());
}

@Override
public String toString() {
return String.format("AlwaysPass[level=%s, message=%s]", getLevel(), getMessage());
}
}
Loading

0 comments on commit 5c65dd7

Please sign in to comment.