-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
697e685
commit f27f23b
Showing
177 changed files
with
2,795 additions
and
3,836 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 34 additions & 40 deletions
74
...t/src/main/java/com/giffing/wicket/spring/boot/context/condition/ConditionalOnWicket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
|
||
} | ||
} |
109 changes: 47 additions & 62 deletions
109
...c/main/java/com/giffing/wicket/spring/boot/context/condition/WicketSettingsCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.