Skip to content

Commit

Permalink
add multi ability instance's support
Browse files Browse the repository at this point in the history
  • Loading branch information
Rocky Yu committed Jan 28, 2023
1 parent 071c4c3 commit 4035050
Show file tree
Hide file tree
Showing 26 changed files with 372 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lattice-model/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>lattice</artifactId>
<groupId>org.hiforce.lattice</groupId>
<version>1.0.14.1</version>
<version>1.0.15</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>lattice-model</artifactId>
Expand Down
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;
}
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;
}
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class LatticeAnnotationSpiFactory {

private List<UseCaseAnnotationParser> useCaseAnnotationParsers;

private List<PriorityAnnotationParser> priorityAnnotationParsers;

private LatticeAnnotationSpiFactory() {

}
Expand All @@ -48,6 +50,13 @@ public static LatticeAnnotationSpiFactory getInstance() {
return instance;
}

public List<PriorityAnnotationParser> getPriorityAnnotationParsers() {
if (null == priorityAnnotationParsers) {
priorityAnnotationParsers = getCustomServiceProviders(PriorityAnnotationParser.class);
}
return priorityAnnotationParsers;
}

/**
* @return The Ability's Custom Annotation Parsers..
*/
Expand Down
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;
}
}
2 changes: 1 addition & 1 deletion lattice-remote/lattice-remote-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>lattice-remote</artifactId>
<groupId>org.hiforce.lattice</groupId>
<version>1.0.14.1</version>
<version>1.0.15</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion lattice-remote/lattice-remote-container/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>lattice-remote</artifactId>
<groupId>org.hiforce.lattice</groupId>
<version>1.0.14.1</version>
<version>1.0.15</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>lattice-remote-runner</artifactId>
<groupId>org.hiforce.lattice</groupId>
<version>1.0.14.1</version>
<version>1.0.15</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion lattice-remote/lattice-remote-runner/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>lattice-remote</artifactId>
<groupId>org.hiforce.lattice</groupId>
<version>1.0.14.1</version>
<version>1.0.15</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion lattice-remote/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>lattice</artifactId>
<groupId>org.hiforce.lattice</groupId>
<version>1.0.14.1</version>
<version>1.0.15</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion lattice-runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>lattice</artifactId>
<groupId>org.hiforce.lattice</groupId>
<version>1.0.14.1</version>
<version>1.0.15</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.hiforce.lattice.model.config.builder.BusinessConfigBuilder;
import org.hiforce.lattice.model.register.*;
import org.hiforce.lattice.model.scenario.ScenarioRequest;
import org.hiforce.lattice.runtime.ability.creator.DefaultAbilityCreator;
import org.hiforce.lattice.runtime.ability.register.AbilityBuildRequest;
import org.hiforce.lattice.runtime.ability.register.AbilityRegister;
import org.hiforce.lattice.runtime.ability.register.TemplateRegister;
Expand Down Expand Up @@ -81,6 +82,12 @@ private Lattice() {

}

public AbilitySpec getAbilitySpecByCode(String code) {
return registeredAbilities.stream()
.filter(p -> StringUtils.equals(code, p.getCode()))
.findFirst().orElse(null);
}

public static Lattice getInstance() {
if (null == instance) {
instance = new Lattice();
Expand Down Expand Up @@ -480,4 +487,55 @@ public BusinessTemplate getFirstMatchedBusiness(ScenarioRequest request) {
return TemplateRegister.getInstance().getFirstMatchedBusiness(request);
}

@SuppressWarnings("all")
public static <Ability extends IAbility> List<Ability> getAllAbilities(Class<?> abilityClass, IBizObject target) {
if( null == abilityClass ){
throw new LatticeRuntimeException("LATTICE-CORE-RT-0025");
}
return getAllAbilities(abilityClass.getName(), target);
}

@SuppressWarnings("all")
public static <Ability extends IAbility> List<Ability> getAllAbilities(String abilityCode, IBizObject target) {
DefaultAbilityCreator creator = new DefaultAbilityCreator(abilityCode, target);
List<Ability> abilityList = creator.getAllAbilityInstancesWithCache();

if (abilityList.isEmpty()) {
return Collections.emptyList();
}
List<Ability> abilities = new ArrayList<>(abilityList.size());
for (Ability ability : abilityList) {
boolean supportChecking = ability.supportChecking();
if (supportChecking) {
abilities.add(ability);
}
}
return abilities;
}

@SuppressWarnings("all")
public static <Ability extends IAbility> Ability getFirstMatchedAbility(Class<?> abilityClass, IBizObject target) {
if( null == abilityClass ){
throw new LatticeRuntimeException("LATTICE-CORE-RT-0025");
}
return getFirstMatchedAbility(abilityClass.getName(), target);
}
@SuppressWarnings("all")
public static <Ability extends IAbility> Ability getFirstMatchedAbility(String abilityCode, IBizObject target) {
DefaultAbilityCreator creator = new DefaultAbilityCreator(abilityCode, target);
List<Ability> domainAbilitySet = creator.getAllAbilityInstancesWithCache();
if (domainAbilitySet.isEmpty()) {
return null;
}

for (Ability ability : domainAbilitySet) {
boolean supportChecking = ability.supportChecking();
if (supportChecking) {
return ability;
}
}
return null;
}


}
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();
}
}
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;
}
}
Loading

0 comments on commit 4035050

Please sign in to comment.