Skip to content

Commit

Permalink
minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcGiffing committed Nov 13, 2024
1 parent 697e685 commit f27f23b
Show file tree
Hide file tree
Showing 177 changed files with 2,795 additions and 3,836 deletions.
5 changes: 5 additions & 0 deletions wicket-spring-boot-context/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
<artifactId>spring-context-support</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,54 +1,48 @@
package com.giffing.wicket.spring.boot.context.condition;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.context.annotation.Conditional;

import java.lang.annotation.*;

/**
* Conditional annotation to pre-check if an extension should be picked for autoconfiguration.
*
* Conditional annotation to pre-check if an extension should be picked for autoconfiguration.
* <p>
* You can define the major Wicket version on which the extension
* should or should not be executed.
*
*
* @author Marc Giffing
*/
@Target({ ElementType.TYPE, ElementType.METHOD })
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(WicketSettingsCondition.class)
public @interface ConditionalOnWicket {

/**
* @return The major java version to check with the current value
*/
int value();

/**
* @return Defines how the given major version should be checked with the current version
*/
Range range() default Range.EQUALS_OR_HIGHER;




enum Range {
/**
* The Wicket major version equals the
*/
EQUALS,
/**
* The Wicket major version equals or is newer
*/
EQUALS_OR_HIGHER,

/**
* The Wicket major version equals or is lower
*/
EQUALS_OR_LOWER,

}

/**
* @return The major java version to check with the current value
*/
int value();

/**
* @return Defines how the given major version should be checked with the current version
*/
Range range() default Range.EQUALS_OR_HIGHER;


enum Range {
/**
* The Wicket major version equals the
*/
EQUALS,
/**
* The Wicket major version equals or is newer
*/
EQUALS_OR_HIGHER,

/**
* The Wicket major version equals or is lower
*/
EQUALS_OR_LOWER,

}
}
Original file line number Diff line number Diff line change
@@ -1,83 +1,68 @@
package com.giffing.wicket.spring.boot.context.condition;

import java.util.Map;

import com.giffing.wicket.spring.boot.context.condition.ConditionalOnWicket.Range;
import org.apache.wicket.settings.FrameworkSettings;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.StringUtils;

import com.giffing.wicket.spring.boot.context.condition.ConditionalOnWicket.Range;
import java.util.Map;

/**
* Retrieves the current major Wicket Version from the classpath and matches it against
* the given conditional value in the {@link ConditionalOnWicket} annotation.
*
* @author Marc Giffing
*
* @author Marc Giffing
*/
public class WicketSettingsCondition extends SpringBootCondition {

@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
String implVersion = null;
String wicketVersion = retrieveWicketVersion(implVersion);

Map<String, Object> attributes = metadata
.getAnnotationAttributes(ConditionalOnWicket.class.getName());
Range range = (Range) attributes.get("range");
int expectedVersion = (int) attributes.get("value");
String[] splittedWicketVersion = wicketVersion.split("\\.");
int majorWicketVersion = Integer.valueOf(splittedWicketVersion[0]);
return getMatchOutcome(range, majorWicketVersion, expectedVersion);
}
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
String wicketVersion = retrieveWicketVersion();

Map<String, Object> attributes = metadata
.getAnnotationAttributes(ConditionalOnWicket.class.getName());
Range range = (Range) attributes.get("range");
int expectedVersion = (int) attributes.get("value");
String[] splittedWicketVersion = wicketVersion.split("\\.");
int majorWicketVersion = Integer.parseInt(splittedWicketVersion[0]);
return getMatchOutcome(range, majorWicketVersion, expectedVersion);
}

protected ConditionOutcome getMatchOutcome(Range range, int runningVersion,
int expectedVersion) {
boolean match = matches(range, expectedVersion, runningVersion);
return new ConditionOutcome(match, getMessage(match, range, runningVersion, expectedVersion));
}

private boolean matches(Range range, int expectedVersion, int runningVersion) {
return switch (range) {
case EQUALS -> runningVersion == expectedVersion;
case EQUALS_OR_LOWER -> runningVersion <= expectedVersion;
case EQUALS_OR_HIGHER -> runningVersion >= expectedVersion;
};
}

private String getMessage(boolean matches, Range range, int runningVersion,
int expectedVersion) {
if (matches) {
return "Wicket version matches current: " + runningVersion + " " + range + " expected: " + expectedVersion;
} else {
return "Wicket version does not match current: " + runningVersion + " " + range + " expected: " + expectedVersion;
}
}

protected ConditionOutcome getMatchOutcome(Range range, int runningVersion,
int expectedVersion) {
boolean match = matches(range, expectedVersion, runningVersion);
return new ConditionOutcome(match, getMessage(match, range, runningVersion, expectedVersion));
}
private String retrieveWicketVersion() {

private boolean matches(Range range, int expectedVersion, int runningVersion) {
switch(range){
case EQUALS:
return runningVersion == expectedVersion;
case EQUALS_OR_LOWER:
return runningVersion <= expectedVersion;
case EQUALS_OR_HIGHER:
return runningVersion >= expectedVersion;
default:
return false;

}
}
String implVersion = null;
var pkg = FrameworkSettings.class.getPackage();
if (pkg != null) {
implVersion = pkg.getImplementationVersion();
}
return StringUtils.hasLength(implVersion) ? implVersion : "0";
}

private String getMessage(boolean matches, Range range, int runningVersion,
int expectedVersion) {
if(matches){
return "Wicket version matches current: " + runningVersion + " " + range + " expected: " + expectedVersion;
}else {
return "Wicket version does not match current: " + runningVersion + " " + range + " expected: " + expectedVersion;
}
}

private String retrieveWicketVersion(String implVersion) {

Package pkg = FrameworkSettings.class.getPackage();
if (pkg != null)
{
implVersion = pkg.getImplementationVersion();
}
String wicketVersion = isEmpty(implVersion) ? "0" : implVersion;
return wicketVersion;
}

private boolean isEmpty(String stringToCheck){
if(stringToCheck == null || stringToCheck.length() == 0){
return true;
}
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,32 @@

/**
* General extension from which all exception should extend
*
* @author Marc Giffing
*
* @author Marc Giffing
*/
public class WicketSpringBootException extends RuntimeException{
public class WicketSpringBootException extends RuntimeException {

public WicketSpringBootException() {
super();
}
public WicketSpringBootException() {
super();
}

public WicketSpringBootException(String message, Throwable cause, boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
public WicketSpringBootException(String message,
Throwable cause,
boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}

public WicketSpringBootException(String message, Throwable cause) {
super(message, cause);
}
public WicketSpringBootException(String message, Throwable cause) {
super(message, cause);
}

public WicketSpringBootException(String message) {
super(message);
}
public WicketSpringBootException(String message) {
super(message);
}

public WicketSpringBootException(Throwable cause) {
super(cause);
}
public WicketSpringBootException(Throwable cause) {
super(cause);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@

/**
* This exception should be thrown if a extension misconfiguration is detected.
*
* <p>
* It can e.g. thrown if you detect an extension which configures the same aspect
* like two serializers
*
* @author Marc Giffing
* as two serializers
*
* @author Marc Giffing
*/
public class ExtensionMisconfigurationException extends WicketSpringBootException {

public ExtensionMisconfigurationException(String message) {
super(message);
}
public ExtensionMisconfigurationException(String message) {
super(message);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,26 @@
import org.springframework.stereotype.Component;

/**
* To be independent from Springs annotation this annotation was introduced
* To be independent of Springs annotation this annotation was introduced
* which is a replacement for the {@link Component} annotation.
*
* In future you may introduce different configuration options.
*
* @author Marc Giffing
* <p>
* In the future, you may introduce different configuration options.
*
* @author Marc Giffing
*/
@Configuration
@Target({ ElementType.TYPE })
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Order(ApplicationInitExtension.DEFAULT_PRECEDENCE)
public @interface ApplicationInitExtension {

int HIGHEST_PRECEDENCE = Integer.MIN_VALUE;
int DEFAULT_PRECEDENCE = Integer.MAX_VALUE / 2;
int HIGHEST_PRECEDENCE = Integer.MIN_VALUE;

int DEFAULT_PRECEDENCE = Integer.MAX_VALUE / 2;

int LOWEST_PRECEDENCE = Integer.MAX_VALUE;
int LOWEST_PRECEDENCE = Integer.MAX_VALUE;

String value() default "";
String value() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,22 @@
* class. An extension class should implement this interface. All classes implementing
* this interface are injected in Wickets starter WicketBootWebApplication class as a list and on
* each implementation the init method is called with the current {@link WebApplication}.
*
* <p>
* Every Spring Bean regardless to the package location which implements this interface
* will be considered as an extension. This means that you can write your own extension
* in a different project. You only have to ensure that the class will picked up by Springs
* component scan (or other bean configuration possibilities.
*
* @author Marc Giffing
*
* @author Marc Giffing
*/
public interface WicketApplicationInitConfiguration {

/**
* With the given {@link WebApplication} the
* {@link WicketApplicationInitConfiguration}s can modify/extend the init()
* method of the {@link WebApplication}.
*
* @param webApplication
* the current {@link WebApplication}
*/
void init(WebApplication webApplication);
/**
* With the given {@link WebApplication} the
* {@link WicketApplicationInitConfiguration}s can modify/extend the init()
* method of the {@link WebApplication}.
*
* @param webApplication the current {@link WebApplication}
*/
void init(WebApplication webApplication);
}
Loading

0 comments on commit f27f23b

Please sign in to comment.