Skip to content

Commit

Permalink
plugin metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
Linyuzai committed Jul 2, 2024
1 parent ab1aa1c commit 18dc4b6
Show file tree
Hide file tree
Showing 28 changed files with 380 additions and 216 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.linyuzai.plugin.core.context.DefaultPluginContext;
import com.github.linyuzai.plugin.core.context.PluginContext;
import com.github.linyuzai.plugin.core.metadata.PluginMetadata;
import com.github.linyuzai.plugin.core.read.PluginReader;
import com.github.linyuzai.plugin.core.tree.PluginTree;
import lombok.Getter;
Expand All @@ -21,7 +22,7 @@ public abstract class AbstractPlugin implements Plugin {

private final Collection<DestroyListener> destroyListeners = new CopyOnWriteArrayList<>();

private Metadata metadata;
private PluginMetadata metadata;

private PluginConcept concept;

Expand Down Expand Up @@ -152,7 +153,7 @@ public void onRelease(PluginContext context) {

@Override
public String toString() {
String name = metadata.get(Metadata.PropertyKey.NAME, getId().toString());
String name = metadata.property(MetadataProperties.NAME, getId().toString());
return getClass().getSimpleName() + "(" + name + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@ public Plugin load(@NonNull Object o) {
plugin.prepare(context);
eventPublisher.publish(new PluginPreparedEvent(context));

String handlerEnabled = plugin.getMetadata()
.get(PluginHandler.PropertyKey.ENABLED, Boolean.TRUE.toString());
if (Boolean.parseBoolean(handlerEnabled)) {
Boolean handlerEnabled = plugin.getMetadata()
.property(PluginHandler.PropertyKeys.ENABLED, Boolean.TRUE.toString());
if (handlerEnabled) {
//解析插件
obtainHandlerChain(context).next(context);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.github.linyuzai.plugin.core.concept;

import com.github.linyuzai.plugin.core.context.PluginContext;
import com.github.linyuzai.plugin.core.metadata.PluginMetadata;
import com.github.linyuzai.plugin.core.metadata.property.MetadataProperty;
import com.github.linyuzai.plugin.core.metadata.property.PrefixMetadataProperty;
import com.github.linyuzai.plugin.core.metadata.property.StringArrayValueMetadataProperty;
import com.github.linyuzai.plugin.core.metadata.property.StringValueMetadataProperty;
import com.github.linyuzai.plugin.core.read.PluginReader;

import java.io.IOException;
import java.io.InputStream;
import java.util.Set;

/**
* 插件抽象
Expand All @@ -14,9 +18,9 @@ public interface Plugin {

Object getId();

Metadata getMetadata();
PluginMetadata getMetadata();

void setMetadata(Metadata metadata);
void setMetadata(PluginMetadata metadata);

PluginConcept getConcept();

Expand Down Expand Up @@ -46,26 +50,19 @@ public interface Plugin {
*/
void release(PluginContext context);

interface Metadata {
interface MetadataProperties {

interface PropertyKey {
MetadataProperty<String> NAME = new StringValueMetadataProperty("name");

String PREFIX = "concept.plugin.";
MetadataProperty<?> DEPENDENCY = new PrefixMetadataProperty("dependency");

String NAME = PREFIX + "name";
MetadataProperty<String[]> DEPENDENCY_NAMES = new StringArrayValueMetadataProperty("names", DEPENDENCY);

String DEPENDENCY_NAMES = PREFIX + "dependency.names";
}
MetadataProperty<?> FILTER = new PrefixMetadataProperty("filter");

String get(String key);
MetadataProperty<?> FILTER_ENTRY = new PrefixMetadataProperty("entry", FILTER);

String get(String key, String defaultValue);

Set<String> keys();

<T> T bind(String key, Class<T> type);

boolean isEmpty();
MetadataProperty<String[]> FILTER_ENTRY_PATTERNS = new StringArrayValueMetadataProperty("patterns", FILTER_ENTRY);
}

interface Entry {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.linyuzai.plugin.core.factory;

import com.github.linyuzai.plugin.core.concept.Plugin;
import com.github.linyuzai.plugin.core.metadata.PluginMetadata;
import com.github.linyuzai.plugin.core.context.PluginContext;

public abstract class MetadataPluginFactory<T> implements PluginFactory {
Expand All @@ -11,7 +12,7 @@ public Plugin create(Object o, PluginContext context) {
if (source == null) {
return null;
}
Plugin.Metadata metadata = createMetadata(source, context);
PluginMetadata metadata = createMetadata(source, context);
if (metadata == null) {
return null;
}
Expand All @@ -20,9 +21,9 @@ public Plugin create(Object o, PluginContext context) {
return plugin;
}

public abstract Plugin doCreate(T o, Plugin.Metadata metadata, PluginContext context);
public abstract Plugin doCreate(T o, PluginMetadata metadata, PluginContext context);

protected abstract T getSource(Object o, PluginContext context);

protected abstract Plugin.Metadata createMetadata(T o, PluginContext context);
protected abstract PluginMetadata createMetadata(T o, PluginContext context);
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package com.github.linyuzai.plugin.core.factory;

import com.github.linyuzai.plugin.core.concept.Plugin;
import com.github.linyuzai.plugin.core.metadata.PluginMetadata;
import com.github.linyuzai.plugin.core.context.PluginContext;

import java.util.Collections;
import java.util.Set;

public abstract class SubPluginFactory implements PluginFactory {

private final SubPluginMetadata metadata = new SubPluginMetadata();

@Override
public Plugin create(Object o, PluginContext context) {
if (o instanceof Plugin.Entry) {
Plugin plugin = doCreate((Plugin.Entry) o, context);
if (plugin != null) {
plugin.setMetadata(new SubPluginMetadata());
plugin.setMetadata(metadata);
}
return plugin;
}
Expand All @@ -22,25 +25,25 @@ public Plugin create(Object o, PluginContext context) {

public abstract Plugin doCreate(Plugin.Entry entry, PluginContext context);

public static class SubPluginMetadata implements Plugin.Metadata {
public static class SubPluginMetadata implements PluginMetadata {

@Override
public String get(String key) {
public String get(String name) {
return null;
}

@Override
public String get(String key, String defaultValue) {
public String get(String name, String defaultValue) {
return defaultValue;
}

@Override
public Set<String> keys() {
public Set<String> names() {
return Collections.emptySet();
}

@Override
public <T> T bind(String key, Class<T> type) {
public <T> T bind(String name, Class<T> type) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package com.github.linyuzai.plugin.core.handle;

import com.github.linyuzai.plugin.core.concept.Plugin;
import com.github.linyuzai.plugin.core.context.PluginContext;
import com.github.linyuzai.plugin.core.metadata.property.BooleanValueMetadataProperty;
import com.github.linyuzai.plugin.core.metadata.property.MetadataProperty;
import com.github.linyuzai.plugin.core.util.ReflectionUtils;

public interface PluginHandler {

void handle(PluginContext context);

interface PropertyKey {
interface PropertyKeys {

String PREFIX = Plugin.Metadata.PropertyKey.PREFIX + "handler.";

String ENABLED = PREFIX + "enabled";
MetadataProperty<Boolean> ENABLED = new BooleanValueMetadataProperty("handler.enabled");
}

interface Dependency {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.github.linyuzai.plugin.core.handle;

import com.github.linyuzai.plugin.core.metadata.PluginMetadata;
import com.github.linyuzai.plugin.core.context.PluginContext;
import com.github.linyuzai.plugin.core.metadata.property.MetadataProperty;

public abstract class StringArrayPropertyPluginHandlerFactory implements PluginHandlerFactory {

@Override
public PluginHandler create(PluginContext context) {
PluginMetadata metadata = context.getPlugin().getMetadata();
String[] array = metadata.property(getProperty());
if (array.length == 0) {
return null;
}
return doCreate(array);
}

public abstract MetadataProperty<String[]> getProperty();

public abstract PluginHandler doCreate(String[] array);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.github.linyuzai.plugin.core.handle.filter;

import com.github.linyuzai.plugin.core.concept.Plugin;
import com.github.linyuzai.plugin.core.handle.PluginHandler;
import com.github.linyuzai.plugin.core.handle.PropertyPluginHandlerFactory;
import com.github.linyuzai.plugin.core.handle.StringArrayPropertyPluginHandlerFactory;
import com.github.linyuzai.plugin.core.metadata.property.MetadataProperty;

public class EntryFilterFactory extends PropertyPluginHandlerFactory {
public class EntryFilterFactory extends StringArrayPropertyPluginHandlerFactory {

@Override
public String getPropertyName() {
return PluginFilter.PropertyKey.PREFIX + "entry";
public MetadataProperty<String[]> getProperty() {
return Plugin.MetadataProperties.FILTER_ENTRY_PATTERNS;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.linyuzai.plugin.core.handle.filter;

import com.github.linyuzai.plugin.core.concept.Plugin;
import com.github.linyuzai.plugin.core.context.PluginContext;
import com.github.linyuzai.plugin.core.handle.PluginHandler;

Expand All @@ -9,11 +8,6 @@
*/
public interface PluginFilter extends PluginHandler, PluginHandler.Dependency {

interface PropertyKey {

String PREFIX = Plugin.Metadata.PropertyKey.PREFIX + "filter.";
}

@Override
default void handle(PluginContext context) {
filter(context);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.github.linyuzai.plugin.core.metadata;

import com.github.linyuzai.plugin.core.metadata.property.MetadataProperty;

import java.util.Set;

public interface PluginMetadata {

String NAME = "plugin.properties";

default <T> T property(MetadataProperty<T> mp) {
String value = get(mp.getName());
return mp.getValue(value);
}

default <T> T property(MetadataProperty<T> mp, String defaultValue) {
String value = get(mp.getName(), defaultValue);
return mp.getValue(value);
}

String get(String name);

String get(String name, String defaultValue);

Set<String> names();

<T> T bind(String name, Class<T> type);

boolean isEmpty();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.github.linyuzai.plugin.core.metadata;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.Properties;
import java.util.Set;

@Getter
@RequiredArgsConstructor
public class PropertiesPluginMetadata implements PluginMetadata {

private final Properties properties;

@Override
public String get(String name) {
return properties.getProperty(name);
}

@Override
public String get(String name, String defaultValue) {
return properties.getProperty(name, defaultValue);
}

@Override
public Set<String> names() {
return properties.stringPropertyNames();
}

@Override
public <T> T bind(String name, Class<T> type) {
throw new UnsupportedOperationException();
}

@Override
public boolean isEmpty() {
return properties.isEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.github.linyuzai.plugin.core.metadata.property;

import lombok.Getter;

@Getter
public abstract class AbstractMetadataProperty<T> implements MetadataProperty<T> {

private final String name;

public AbstractMetadataProperty(String name, MetadataProperty<?> prefix) {
if (prefix == null) {
this.name = name;
} else {
this.name = prefix.getName() + "." + name;
}
}
}
Loading

0 comments on commit 18dc4b6

Please sign in to comment.