From 509524144d46d0cfa7cd3f6ee7c9048f364ce6bb Mon Sep 17 00:00:00 2001 From: Redkale <8730487+redkale@users.noreply.github.com> Date: Tue, 8 Sep 2020 09:51:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20ConvertLoader?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/org/redkale/convert/ConvertFactory.java | 27 ++++++++++++++++++++- src/org/redkale/convert/ConvertLoader.java | 23 ++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/org/redkale/convert/ConvertLoader.java diff --git a/src/org/redkale/convert/ConvertFactory.java b/src/org/redkale/convert/ConvertFactory.java index 4251a8b3e..1ba157e21 100644 --- a/src/org/redkale/convert/ConvertFactory.java +++ b/src/org/redkale/convert/ConvertFactory.java @@ -16,7 +16,9 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.*; import java.util.regex.Pattern; import java.util.stream.*; +import org.redkale.convert.bson.BsonConvert; import org.redkale.convert.ext.*; +import org.redkale.convert.json.JsonConvert; import org.redkale.util.*; /** @@ -32,6 +34,10 @@ import org.redkale.util.*; @SuppressWarnings("unchecked") public abstract class ConvertFactory { + private static final AtomicBoolean loaderInited = new AtomicBoolean(); + + private static Convert defProtobufConvert; + private final ConvertFactory parent; protected Convert convert; @@ -139,7 +145,7 @@ public abstract class ConvertFactory { @Override public void convertTo(W out, java.sql.Date value) { - out.writeSmallString(value == null ? null : value.toString()); + out.writeSmallString(value == null ? null : value.toString()); } @Override @@ -188,6 +194,25 @@ public abstract class ConvertFactory { return this.parent; } + public static Convert findConvert(ConvertType type) { + if (type == ConvertType.JSON) return JsonConvert.root(); + if (type == ConvertType.BSON) return BsonConvert.root(); + if (loaderInited.get()) { + if (type == ConvertType.PROTOBUF) return defProtobufConvert; + } + synchronized (loaderInited) { + if (!loaderInited.get()) { + Iterator it = ServiceLoader.load(ConvertLoader.class).iterator(); + while (it.hasNext()) { + ConvertLoader cl = it.next(); + if (cl.type() == ConvertType.PROTOBUF) defProtobufConvert = cl.convert(); + } + loaderInited.set(true); + } + } + return null; + } + public abstract ConvertType getConvertType(); public abstract boolean isReversible(); //是否可逆的 diff --git a/src/org/redkale/convert/ConvertLoader.java b/src/org/redkale/convert/ConvertLoader.java new file mode 100644 index 000000000..8b9a3f5a5 --- /dev/null +++ b/src/org/redkale/convert/ConvertLoader.java @@ -0,0 +1,23 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package org.redkale.convert; + +/** + * Convert的扩展实现类加载器 + * + * + * 详情见: https://redkale.org + * + * @author zhangjx + * + * @since 2.2.0 + */ +public interface ConvertLoader { + + public ConvertType type(); + + public Convert convert(); +}