-
Notifications
You must be signed in to change notification settings - Fork 2
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
cf2df95
commit b027c8f
Showing
15 changed files
with
463 additions
and
140 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
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
72 changes: 72 additions & 0 deletions
72
vertx-gaia/vertx-ams/src/main/jib/io/horizon/uca/aop/AspectComponent.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,72 @@ | ||
package io.horizon.uca.aop; | ||
|
||
import io.horizon.util.HUt; | ||
import io.modello.eon.configure.VPC; | ||
import io.vertx.core.json.JsonArray; | ||
import io.vertx.core.json.JsonObject; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.function.Consumer; | ||
import java.util.function.Predicate; | ||
|
||
/** | ||
* <pre><code> | ||
* { | ||
* "plugin.component.before": [], | ||
* "plugin.component.job": [], | ||
* "plugin.component.after": [] | ||
* } | ||
* </code></pre> | ||
* | ||
* @author lang : 2023-06-03 | ||
*/ | ||
class AspectComponent { | ||
|
||
private final List<Class<?>> nameBefore = new ArrayList<>(); | ||
private final List<Class<?>> nameAfter = new ArrayList<>(); | ||
private final List<Class<?>> nameJob = new ArrayList<>(); | ||
|
||
AspectComponent(final JsonObject configuration) { | ||
// Plugin Before | ||
this.initialize(configuration, VPC.aop.PLUGIN_COMPONENT_BEFORE, | ||
this.nameBefore::add, | ||
clazz -> HUt.isImplement(clazz, Before.class) || HUt.isImplement(clazz, Around.class)); | ||
|
||
// Plugin After | ||
this.initialize(configuration, VPC.aop.PLUGIN_COMPONENT_AFTER, | ||
this.nameAfter::add, | ||
clazz -> HUt.isImplement(clazz, After.class) || HUt.isImplement(clazz, Around.class)); | ||
|
||
// Plugin Job | ||
this.initialize(configuration, VPC.aop.PLUGIN_COMPONENT_JOB, | ||
this.nameJob::add, | ||
clazz -> HUt.isImplement(clazz, After.class) || HUt.isImplement(clazz, Around.class)); | ||
} | ||
|
||
private void initialize( | ||
final JsonObject configuration, final String field, | ||
final Consumer<Class<?>> fnConsumer, | ||
final Predicate<Class<?>> fnCheck) { | ||
final JsonArray source = HUt.valueJArray(configuration, field); | ||
HUt.itJArray(source, String.class, (item, index) -> { | ||
final Class<?> clazz = HUt.clazz(item, null); | ||
if (Objects.nonNull(clazz) && fnCheck.test(clazz)) { | ||
fnConsumer.accept(clazz); | ||
} | ||
}); | ||
} | ||
|
||
public List<Class<?>> nameBefore() { | ||
return this.nameBefore; | ||
} | ||
|
||
public List<Class<?>> nameAfter() { | ||
return this.nameAfter; | ||
} | ||
|
||
public List<Class<?>> nameJob() { | ||
return this.nameJob; | ||
} | ||
} |
149 changes: 63 additions & 86 deletions
149
vertx-gaia/vertx-ams/src/main/jib/io/horizon/uca/aop/AspectConfig.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,113 +1,90 @@ | ||
package io.horizon.uca.aop; | ||
|
||
import io.horizon.exception.web._501NotSupportException; | ||
import io.horizon.specification.uca.HRobin; | ||
import io.horizon.util.HUt; | ||
import io.modello.eon.configure.VPC; | ||
import io.vertx.core.json.JsonArray; | ||
import io.vertx.core.json.JsonObject; | ||
|
||
import java.io.Serializable; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Collection; | ||
import java.util.Objects; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.Predicate; | ||
|
||
/** | ||
* The configuration data structure: | ||
* | ||
* @author <a href="http://www.origin-x.cn">Lang</a> | ||
* @author lang : 2023-06-03 | ||
*/ | ||
public class AspectConfig implements Serializable { | ||
/* | ||
* { | ||
* "components": { | ||
* "plugin.component.before": [], | ||
* "plugin.component.job": [], | ||
* "plugin.component.after": [], | ||
* "plugin.config": { | ||
* } | ||
* } | ||
* } | ||
*/ | ||
class AspectConfig { | ||
private final JsonObject forkJ = new JsonObject(); | ||
private final ConcurrentMap<Class<?>, JsonObject> configMap = new ConcurrentHashMap<>(); | ||
|
||
private final List<Class<?>> nameBefore = new ArrayList<>(); | ||
private final List<Class<?>> nameAfter = new ArrayList<>(); | ||
private final List<Class<?>> nameJob = new ArrayList<>(); | ||
|
||
private AspectConfig(final JsonObject components) { | ||
// Plugin Before | ||
this.initialize(components, VPC.aop.PLUGIN_COMPONENT_BEFORE, (clazz, config) -> { | ||
this.nameBefore.add(clazz); | ||
this.configMap.put(clazz, config); | ||
}, clazz -> HUt.isImplement(clazz, Before.class) || HUt.isImplement(clazz, io.horizon.uca.aop.Around.class)); | ||
|
||
// Plugin After | ||
this.initialize(components, VPC.aop.PLUGIN_COMPONENT_AFTER, (clazz, config) -> { | ||
this.nameAfter.add(clazz); | ||
this.configMap.put(clazz, config); | ||
}, clazz -> HUt.isImplement(clazz, After.class) || HUt.isImplement(clazz, io.horizon.uca.aop.Around.class)); | ||
|
||
// Plugin Job | ||
this.initialize(components, VPC.aop.PLUGIN_COMPONENT_JOB, (clazz, config) -> { | ||
this.nameJob.add(clazz); | ||
this.configMap.put(clazz, config); | ||
}, clazz -> HUt.isImplement(clazz, After.class) || HUt.isImplement(clazz, io.horizon.uca.aop.Around.class)); | ||
} | ||
|
||
public static AspectConfig create(final JsonObject components) { | ||
return new AspectConfig(components); | ||
} | ||
|
||
public static AspectConfig create() { | ||
return new AspectConfig(new JsonObject()); | ||
} | ||
|
||
private void initialize( | ||
final JsonObject configuration, final String field, | ||
final BiConsumer<Class<?>, JsonObject> fnConsumer, | ||
final Predicate<Class<?>> fnCheck) { | ||
final JsonArray source = HUt.valueJArray(configuration, field); | ||
HUt.itJArray(source, String.class, (item, index) -> { | ||
final Class<?> clazz = HUt.clazz(item, null); | ||
if (Objects.nonNull(clazz) && fnCheck.test(clazz)) { | ||
final JsonObject config = HUt.valueJObject(configuration, VPC.aop.PLUGIN_CONFIG); | ||
final JsonObject valueJ = HUt.valueJObject(config, clazz.getName()); | ||
fnConsumer.accept(clazz, valueJ); | ||
} | ||
}); | ||
JsonObject config(final Class<?> clazz) { | ||
Objects.requireNonNull(clazz); | ||
return this.configMap.getOrDefault(clazz, new JsonObject()); | ||
} | ||
|
||
public List<Class<?>> nameBefore() { | ||
return this.nameBefore; | ||
void config(final Class<?> clazz, final JsonObject config) { | ||
Objects.requireNonNull(clazz); | ||
this.configMap.put(clazz, HUt.isNil(config) ? new JsonObject() : config); | ||
} | ||
|
||
public List<Class<?>> nameAfter() { | ||
return this.nameAfter; | ||
void bind(final AspectComponent component, final JsonObject configJ) { | ||
this.bind(component.nameBefore(), configJ); | ||
this.bind(component.nameAfter(), configJ); | ||
this.bind(component.nameJob(), configJ); | ||
} | ||
|
||
public List<Class<?>> nameJob() { | ||
return this.nameJob; | ||
void bind(final JsonObject forkJ) { | ||
this.forkJ.mergeIn(forkJ, true); | ||
} | ||
|
||
public JsonObject config(final Class<?> clazz) { | ||
Objects.requireNonNull(clazz); | ||
return this.configMap.getOrDefault(clazz, new JsonObject()); | ||
} | ||
|
||
public AspectConfig config(final Class<?> clazz, final JsonObject external) { | ||
this.configMap.put(clazz, external); | ||
return this; | ||
/** | ||
* 值节点选择器,主要针对 | ||
* <pre><code> | ||
* { | ||
* "type": "FIELD", | ||
* "robin": "", | ||
* "config": { | ||
* "by": "identifier" | ||
* } | ||
* } | ||
* </code></pre> | ||
* | ||
* @param input 输入参数(JsonObject、JsonArray、T) | ||
* @param <T> 输入类型 | ||
* | ||
* @return 选择计算的值 | ||
*/ | ||
<T> String forkKey(final T input) { | ||
final Class<?> clazz = HUt.valueC(this.forkJ, VPC.aop.plugin_fork.ROBIN); | ||
final JsonObject config = HUt.valueJObject(this.forkJ, VPC.aop.plugin_fork.CONFIG); | ||
if (Objects.nonNull(clazz)) { | ||
// 配置了 robin | ||
final HRobin<T> robin = HUt.singleton(clazz); | ||
return robin.execute(input, config); | ||
} else { | ||
// 未配置组件,暂时只考虑 type = FIELD 的情况 | ||
final String by = HUt.valueString(config, VPC.aop.plugin_fork.config.BY); | ||
Objects.requireNonNull(by); | ||
if (input instanceof JsonObject) { | ||
final JsonObject inputJ = (JsonObject) input; | ||
return HUt.valueString(inputJ, by); | ||
} else if (input instanceof JsonArray) { | ||
final JsonArray inputA = (JsonArray) input; | ||
return HUt.valueString(inputA, by); | ||
} else { | ||
throw new _501NotSupportException(this.getClass()); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "AspectConfig{" + | ||
"nameBefore=" + this.nameBefore + | ||
", nameAfter=" + this.nameAfter + | ||
", nameJob=" + this.nameJob + | ||
'}'; | ||
private void bind(final Collection<Class<?>> clazzSet, final JsonObject configJ) { | ||
clazzSet.forEach(clazz -> { | ||
if (configJ.containsKey(clazz.getName())) { | ||
final JsonObject componentConfig = HUt.valueJObject(configJ, clazz.getName()); | ||
this.config(clazz, componentConfig); | ||
} | ||
}); | ||
} | ||
} |
Oops, something went wrong.