Skip to content

Commit

Permalink
extension: init
Browse files Browse the repository at this point in the history
  • Loading branch information
tanghanzheng committed Sep 30, 2022
1 parent fd1c0d9 commit e52df8c
Show file tree
Hide file tree
Showing 34 changed files with 1,097 additions and 0 deletions.
17 changes: 17 additions & 0 deletions concept-extension/build.gradle
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 {

}
26 changes: 26 additions & 0 deletions concept-extension/concept-extension-core/build.gradle
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'
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);
}
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);
}
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);
}
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;
}
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);
}
}
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();
}

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

0 comments on commit e52df8c

Please sign in to comment.