YamlLoader

This commit is contained in:
redkale
2024-08-14 23:29:30 +08:00
parent 09d710b928
commit 5168dd59ce
2 changed files with 27 additions and 15 deletions

View File

@@ -14,6 +14,13 @@ package org.redkale.util;
*/ */
public interface YamlProvider { public interface YamlProvider {
/**
* 创建 YamlLoader
*/
public YamlLoader createLoader();
public interface YamlLoader {
/** /**
* 将yml内容转换成AnyValue * 将yml内容转换成AnyValue
* *
@@ -21,4 +28,5 @@ public interface YamlProvider {
* @return AnyValue * @return AnyValue
*/ */
public AnyValue read(String content); public AnyValue read(String content);
}
} }

View File

@@ -10,6 +10,7 @@ import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.ServiceLoader; import java.util.ServiceLoader;
import org.redkale.annotation.Nonnull; import org.redkale.annotation.Nonnull;
import org.redkale.util.YamlProvider.YamlLoader;
/** /**
* 简单的yml读取器 * 简单的yml读取器
@@ -22,7 +23,7 @@ import org.redkale.annotation.Nonnull;
*/ */
public class YamlReader { public class YamlReader {
private static YamlProvider currentProvider; private static YamlLoader currentYamlLoader;
private final String text; private final String text;
@@ -31,7 +32,7 @@ public class YamlReader {
} }
public AnyValue read() { public AnyValue read() {
return loadProvider().read(text); return loadYamlLoader().read(text);
} }
/** /**
@@ -39,8 +40,8 @@ public class YamlReader {
* *
* @return YamlProvider * @return YamlProvider
*/ */
protected static @Nonnull YamlProvider loadProvider() { protected static @Nonnull YamlLoader loadYamlLoader() {
if (currentProvider == null) { if (currentYamlLoader == null) {
Iterator<YamlProvider> it = ServiceLoader.load(YamlProvider.class).iterator(); Iterator<YamlProvider> it = ServiceLoader.load(YamlProvider.class).iterator();
RedkaleClassLoader.putServiceLoader(YamlProvider.class); RedkaleClassLoader.putServiceLoader(YamlProvider.class);
List<YamlProvider> providers = new ArrayList<>(); List<YamlProvider> providers = new ArrayList<>();
@@ -53,19 +54,22 @@ public class YamlReader {
} }
} }
for (YamlProvider provider : Utility.sortPriority(providers)) { for (YamlProvider provider : Utility.sortPriority(providers)) {
currentProvider = provider; YamlLoader leader = provider.createLoader();
return provider; if (leader != null) {
currentYamlLoader = leader;
return leader;
} }
currentProvider = new DefaultYmlProvider();
} }
return currentProvider; currentYamlLoader = new DefaultYamlLoader();
}
return currentYamlLoader;
} }
protected static class DefaultYmlProvider implements YamlProvider { protected static class DefaultYamlLoader implements YamlLoader {
@Override @Override
public AnyValue read(String content) { public AnyValue read(String content) {
throw new UnsupportedOperationException("Not supported yml."); throw new UnsupportedOperationException("Not supported yaml.");
} }
} }
} }