forked from Linyuzai/concept
-
Notifications
You must be signed in to change notification settings - Fork 0
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
tanghanzheng
committed
Sep 30, 2022
1 parent
fd1c0d9
commit e52df8c
Showing
34 changed files
with
1,097 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
plugins { | ||
id 'java' | ||
} | ||
|
||
apply from: 'version.gradle' | ||
|
||
group 'com.github.linyuzai' | ||
version "${conceptExtensionVersion}" | ||
sourceCompatibility = '1.8' | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
|
||
} |
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,26 @@ | ||
plugins { | ||
id 'java' | ||
} | ||
|
||
apply from: '../version.gradle' | ||
|
||
group 'com.github.linyuzai' | ||
version "${conceptExtensionVersion}" | ||
sourceCompatibility = '1.8' | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
compileOnly "org.projectlombok:lombok:${lombokVersion}" | ||
annotationProcessor "org.projectlombok:lombok:${lombokVersion}" | ||
testImplementation "org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}" | ||
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}" | ||
} | ||
|
||
test { | ||
useJUnitPlatform() | ||
} | ||
|
||
apply from: '../publish.gradle' |
11 changes: 11 additions & 0 deletions
11
...nsion-core/src/main/java/com/github/linyuzai/extension/core/adapter/ExtensionAdapter.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,11 @@ | ||
package com.github.linyuzai.extension.core.adapter; | ||
|
||
import com.github.linyuzai.extension.core.concept.Extension; | ||
import com.github.linyuzai.extension.core.repository.ExtensionRepository; | ||
|
||
import java.util.Collection; | ||
|
||
public interface ExtensionAdapter { | ||
|
||
Collection<Extension> adapt(Extension.Argument argument, ExtensionRepository repository); | ||
} |
8 changes: 8 additions & 0 deletions
8
...extension-core/src/main/java/com/github/linyuzai/extension/core/batch/BatchExtension.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,8 @@ | ||
package com.github.linyuzai.extension.core.batch; | ||
|
||
import com.github.linyuzai.extension.core.concept.Extension; | ||
|
||
public interface BatchExtension extends Extension { | ||
|
||
boolean batchSupport(Argument argument); | ||
} |
31 changes: 31 additions & 0 deletions
31
...on-core/src/main/java/com/github/linyuzai/extension/core/batch/BatchExtensionHandler.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,31 @@ | ||
package com.github.linyuzai.extension.core.batch; | ||
|
||
import com.github.linyuzai.extension.core.concept.Extension; | ||
import com.github.linyuzai.extension.core.handler.ExtensionHandler; | ||
import com.github.linyuzai.extension.core.handler.ExtensionHandlerChain; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
|
||
public abstract class BatchExtensionHandler implements ExtensionHandler { | ||
|
||
@Override | ||
public Collection<Extension.ArgumentAndResult> onExtend(Extension extension, Extension.Argument argument, ExtensionHandlerChain chain) { | ||
if (isBatchArgument(argument) && | ||
extension instanceof BatchExtension && | ||
((BatchExtension) extension).batchSupport(argument)) { | ||
Collection<Extension.Argument> arguments = splitArgument(argument); | ||
if (arguments.isEmpty()) { | ||
return Collections.emptyList(); | ||
} else { | ||
return nestingExtend(arguments, extension, chain); | ||
} | ||
} else { | ||
return chain.next(extension, argument); | ||
} | ||
} | ||
|
||
public abstract boolean isBatchArgument(Extension.Argument argument); | ||
|
||
public abstract Collection<Extension.Argument> splitArgument(Extension.Argument argument); | ||
} |
25 changes: 25 additions & 0 deletions
25
...sion-core/src/main/java/com/github/linyuzai/extension/core/concept/AbstractExtension.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,25 @@ | ||
package com.github.linyuzai.extension.core.concept; | ||
|
||
import com.github.linyuzai.extension.core.dependence.DependenceProvider; | ||
import com.github.linyuzai.extension.core.lifecycle.AbstractLifecycle; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import java.util.Map; | ||
|
||
@Getter | ||
@Setter | ||
public abstract class AbstractExtension extends AbstractLifecycle implements Extension { | ||
|
||
private String id; | ||
|
||
private String name; | ||
|
||
private String type; | ||
|
||
private Map<Object, Object> metadata; | ||
|
||
private DependenceProvider dependenceProvider; | ||
|
||
private ExtensionConcept concept; | ||
} |
120 changes: 120 additions & 0 deletions
120
...re/src/main/java/com/github/linyuzai/extension/core/concept/AbstractExtensionConcept.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,120 @@ | ||
package com.github.linyuzai.extension.core.concept; | ||
|
||
import com.github.linyuzai.extension.core.adapter.ExtensionAdapter; | ||
import com.github.linyuzai.extension.core.dependence.DependenceProvider; | ||
import com.github.linyuzai.extension.core.exception.ExtensionException; | ||
import com.github.linyuzai.extension.core.factory.ExtensionFactory; | ||
import com.github.linyuzai.extension.core.invoker.ExtensionInvoker; | ||
import com.github.linyuzai.extension.core.invoker.ExtensionInvokerFactory; | ||
import com.github.linyuzai.extension.core.lifecycle.AbstractLifecycle; | ||
import com.github.linyuzai.extension.core.lifecycle.Lifecycle; | ||
import com.github.linyuzai.extension.core.repository.ExtensionRepository; | ||
import com.github.linyuzai.extension.core.strategy.ExtensionStrategy; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
import java.util.stream.Collectors; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public abstract class AbstractExtensionConcept extends AbstractLifecycle | ||
implements ExtensionConcept, DependenceProvider { | ||
|
||
private final ExtensionRepository extensionRepository; | ||
|
||
private final ExtensionAdapter extensionAdapter; | ||
|
||
private final ExtensionInvokerFactory extensionInvokerFactory; | ||
|
||
private final ExtensionStrategy extensionStrategy; | ||
|
||
private final Collection<ExtensionFactory> extensionFactories = new CopyOnWriteArrayList<>(); | ||
|
||
@Override | ||
public void register(Extension extension) { | ||
extension.setConcept(this); | ||
extension.setDependenceProvider(this); | ||
initializeExtensionIfConceptInitialized(extension); | ||
extensionRepository.add(extension); | ||
} | ||
|
||
@Override | ||
public void register(ExtensionFactory factory) { | ||
extensionFactories.add(factory); | ||
} | ||
|
||
private void initializeExtensionIfConceptInitialized(Extension extension) { | ||
if (initialized() && !extension.initialized()) { | ||
extension.initialize(); | ||
} | ||
} | ||
|
||
@Override | ||
public Extension getExtension(String extensionId) { | ||
return extensionRepository.get(extensionId); | ||
} | ||
|
||
@Override | ||
public Collection<Extension.ArgumentAndResult> extend(Collection<? extends Extension.Argument> arguments) { | ||
List<ExtensionInvoker> invokers = new ArrayList<>(); | ||
for (Extension.Argument argument : arguments) { | ||
ExtensionAdapter adapter = argument.getConfig(ExtensionAdapter.class, | ||
extensionAdapter); | ||
Collection<Extension> extensions = adapter.adapt(argument, extensionRepository); | ||
if (extensions == null) { | ||
continue; | ||
} | ||
|
||
ExtensionInvokerFactory invokerFactory = | ||
argument.getConfig(ExtensionInvokerFactory.class, | ||
extensionInvokerFactory); | ||
for (Extension extension : extensions) { | ||
ExtensionInvoker invoker = invokerFactory.create(extension, argument); | ||
invokers.add(invoker); | ||
} | ||
} | ||
|
||
if (invokers.isEmpty()) { | ||
return Collections.emptyList(); | ||
} | ||
|
||
return extensionStrategy.extend(invokers); | ||
} | ||
|
||
@Override | ||
public Extension getDependenceExtension(String extensionId) { | ||
Extension extension = extensionRepository.get(extensionId); | ||
initializeExtensionIfConceptInitialized(extension); | ||
return extension; | ||
} | ||
|
||
@Override | ||
public Extension getDependenceExtension(Class<? extends Extension> clazz) { | ||
Collection<Extension> extensions = extensionRepository.stream() | ||
.filter(it -> clazz.isAssignableFrom(it.getClass())) | ||
.collect(Collectors.toList()); | ||
if (extensions.isEmpty()) { | ||
return null; | ||
} else if (extensions.size() == 1) { | ||
return extensions.iterator().next(); | ||
} else { | ||
throw new ExtensionException(extensions.size() + " extensions found"); | ||
} | ||
} | ||
|
||
@Override | ||
public void onInitialize() { | ||
extensionRepository.stream().sorted().forEach(this::initializeExtensionIfConceptInitialized); | ||
extensionFactories.stream().map(ExtensionFactory::create).forEach(this::register); | ||
} | ||
|
||
@Override | ||
public void onDestroy() { | ||
extensionRepository.stream().filter(Lifecycle::initialized).forEach(Lifecycle::destroy); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...pt-extension-core/src/main/java/com/github/linyuzai/extension/core/concept/Extension.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,57 @@ | ||
package com.github.linyuzai.extension.core.concept; | ||
|
||
import com.github.linyuzai.extension.core.dependence.DependenceProvider; | ||
import com.github.linyuzai.extension.core.lifecycle.Lifecycle; | ||
|
||
import java.util.Map; | ||
|
||
public interface Extension extends Lifecycle { | ||
|
||
String getId(); | ||
|
||
String getName(); | ||
|
||
String getType(); | ||
|
||
Map<Object, Object> getMetadata(); | ||
|
||
DependenceProvider getDependenceProvider(); | ||
|
||
void setDependenceProvider(DependenceProvider resolver); | ||
|
||
ExtensionConcept getConcept(); | ||
|
||
void setConcept(ExtensionConcept concept); | ||
|
||
Result extend(Argument argument); | ||
|
||
interface Argument { | ||
|
||
Object getTarget(); | ||
|
||
String getKey(); | ||
|
||
Object getValue(); | ||
|
||
<T> T getConfig(Object o); | ||
|
||
<T> T getConfig(Object o, T defaultValue); | ||
} | ||
|
||
interface Result { | ||
|
||
String getCode(); | ||
|
||
String getMessage(); | ||
|
||
Object getObject(); | ||
} | ||
|
||
interface ArgumentAndResult { | ||
|
||
Argument getArgument(); | ||
|
||
Result getResult(); | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
...nsion-core/src/main/java/com/github/linyuzai/extension/core/concept/ExtensionConcept.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,16 @@ | ||
package com.github.linyuzai.extension.core.concept; | ||
|
||
import com.github.linyuzai.extension.core.factory.ExtensionFactory; | ||
|
||
import java.util.Collection; | ||
|
||
public interface ExtensionConcept { | ||
|
||
void register(Extension extension); | ||
|
||
void register(ExtensionFactory factory); | ||
|
||
Extension getExtension(String extensionId); | ||
|
||
Collection<Extension.ArgumentAndResult> extend(Collection<? extends Extension.Argument> arguments); | ||
} |
Oops, something went wrong.