-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add multi ability instance's support
- Loading branch information
Rocky Yu
committed
Jan 28, 2023
1 parent
071c4c3
commit 4035050
Showing
26 changed files
with
372 additions
and
20 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
17 changes: 17 additions & 0 deletions
17
lattice-model/src/main/java/org/hiforce/lattice/annotation/Priority.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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.hiforce.lattice.annotation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* @author Rocky Yu | ||
* @since 2023/1/28 | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.TYPE, ElementType.METHOD}) | ||
public @interface Priority { | ||
|
||
int value() default 500; | ||
} |
15 changes: 15 additions & 0 deletions
15
lattice-model/src/main/java/org/hiforce/lattice/annotation/model/PriorityAnnotation.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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.hiforce.lattice.annotation.model; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
/** | ||
* @author Rocky Yu | ||
* @since 2023/1/28 | ||
*/ | ||
public class PriorityAnnotation { | ||
|
||
@Getter | ||
@Setter | ||
private int value; | ||
} |
22 changes: 22 additions & 0 deletions
22
.../src/main/java/org/hiforce/lattice/annotation/parser/DefaultPriorityAnnotationParser.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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.hiforce.lattice.annotation.parser; | ||
|
||
import com.google.auto.service.AutoService; | ||
import org.hiforce.lattice.annotation.Priority; | ||
import org.hiforce.lattice.spi.annotation.PriorityAnnotationParser; | ||
|
||
/** | ||
* @author Rocky Yu | ||
* @since 2023/1/28 | ||
*/ | ||
@AutoService(PriorityAnnotationParser.class) | ||
public class DefaultPriorityAnnotationParser extends PriorityAnnotationParser<Priority> { | ||
@Override | ||
public Class<Priority> getAnnotationClass() { | ||
return Priority.class; | ||
} | ||
|
||
@Override | ||
public int getValue(Priority annotation) { | ||
return annotation.value(); | ||
} | ||
} |
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
49 changes: 49 additions & 0 deletions
49
lattice-model/src/main/java/org/hiforce/lattice/spi/annotation/PriorityAnnotationParser.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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package org.hiforce.lattice.spi.annotation; | ||
|
||
import org.hiforce.lattice.annotation.model.PriorityAnnotation; | ||
import org.hiforce.lattice.spi.LatticeAnnotationSpiFactory; | ||
import org.hiforce.lattice.utils.LatticeAnnotationUtils; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Method; | ||
|
||
/** | ||
* @author Rocky Yu | ||
* @since 2023/1/28 | ||
*/ | ||
@SuppressWarnings("all") | ||
public abstract class PriorityAnnotationParser<T extends Annotation> extends LatticeAnnotationParser<T> { | ||
|
||
public abstract int getValue(T annotation); | ||
|
||
public PriorityAnnotation buildAnnotationInfo(T annotation) { | ||
if (null == annotation) { | ||
return null; | ||
} | ||
PriorityAnnotation info = new PriorityAnnotation(); | ||
info.setValue(getValue(annotation)); | ||
return info; | ||
} | ||
|
||
public static PriorityAnnotation getPriorityAnnotationInfo(Class<?> targetClass) { | ||
for (PriorityAnnotationParser parser : LatticeAnnotationSpiFactory.getInstance().getPriorityAnnotationParsers()) { | ||
Annotation annotation = targetClass.getDeclaredAnnotation(parser.getAnnotationClass()); | ||
if (null == annotation) { | ||
continue; | ||
} | ||
return parser.buildAnnotationInfo(annotation); | ||
} | ||
return null; | ||
} | ||
|
||
public static PriorityAnnotation getPriorityAnnotationInfo(Method method) { | ||
for (PriorityAnnotationParser parser : LatticeAnnotationSpiFactory.getInstance().getPriorityAnnotationParsers()) { | ||
Annotation annotation = LatticeAnnotationUtils.findAnnotation(method, parser.getAnnotationClass()); | ||
if (null == annotation) { | ||
continue; | ||
} | ||
return parser.buildAnnotationInfo(annotation); | ||
} | ||
return null; | ||
} | ||
} |
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
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
50 changes: 50 additions & 0 deletions
50
...ice-runtime/src/main/java/org/hiforce/lattice/runtime/ability/cache/AbilityInstCache.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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.hiforce.lattice.runtime.ability.cache; | ||
|
||
import org.hiforce.lattice.model.ability.IAbility; | ||
import org.hiforce.lattice.runtime.cache.LatticeCache; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
/** | ||
* @author Rocky Yu | ||
* @since 2023/1/28 | ||
*/ | ||
public class AbilityInstCache implements LatticeCache { | ||
|
||
private static AbilityInstCache instance; | ||
|
||
private static final Object lock = new Object(); | ||
|
||
private static final Map<String, List<Class<IAbility>>> ABILITY_INST_MAP = new ConcurrentHashMap<>(); | ||
|
||
public static AbilityInstCache getInstance() { | ||
if (null == instance) { | ||
synchronized (lock) { | ||
if (null == instance) { | ||
instance = new AbilityInstCache(); | ||
} | ||
} | ||
} | ||
return instance; | ||
} | ||
|
||
public List<Class<IAbility>> getAbilityInstCodes(String abilityCode){ | ||
return ABILITY_INST_MAP.get(abilityCode); | ||
} | ||
|
||
public void cacheAbilityInstanceRelation(String abilityCode, List<Class<IAbility>> instanceClasses){ | ||
ABILITY_INST_MAP.put(abilityCode, instanceClasses); | ||
} | ||
|
||
@Override | ||
public void init() { | ||
|
||
} | ||
|
||
@Override | ||
public void clear() { | ||
ABILITY_INST_MAP.clear(); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...-runtime/src/main/java/org/hiforce/lattice/runtime/ability/cache/AbilityInstCacheKey.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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package org.hiforce.lattice.runtime.ability.cache; | ||
|
||
|
||
import java.util.Objects; | ||
|
||
public final class AbilityInstCacheKey { | ||
private String bizCode; | ||
private String abilityCode; | ||
|
||
private int hash; | ||
|
||
private AbilityInstCacheKey() { | ||
} | ||
|
||
public AbilityInstCacheKey(String bizCode, String abilityCode) { | ||
this.bizCode = bizCode; | ||
this.abilityCode = abilityCode; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { return true; } | ||
if (o == null || getClass() != o.getClass()) { return false; } | ||
|
||
AbilityInstCacheKey that = (AbilityInstCacheKey)o; | ||
|
||
if (!Objects.equals(bizCode, that.bizCode)) { return false; } | ||
return Objects.equals(abilityCode, that.abilityCode); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
if (hash == 0) { | ||
int result = bizCode != null ? bizCode.hashCode() : 0; | ||
hash = 31 * result + (abilityCode != null ? abilityCode.hashCode() : 0); | ||
} | ||
return hash; | ||
} | ||
} |
Oops, something went wrong.