From 6e78946d372b4935010a596cdcf5e0aa045ebe9c Mon Sep 17 00:00:00 2001 From: wentch <22250530@qq.com> Date: Wed, 23 Dec 2015 15:12:06 +0800 Subject: [PATCH] --- src/org/redkale/boot/NodeServer.java | 2 ++ src/org/redkale/convert/Factory.java | 22 +++++++++++++++++++ .../redkale/service/CacheSourceService.java | 15 +++++++++---- src/org/redkale/source/CacheSource.java | 2 +- 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/org/redkale/boot/NodeServer.java b/src/org/redkale/boot/NodeServer.java index 21987d6cd..4b612bbde 100644 --- a/src/org/redkale/boot/NodeServer.java +++ b/src/org/redkale/boot/NodeServer.java @@ -21,6 +21,7 @@ import java.util.*; import java.util.function.Consumer; import java.util.logging.*; import javax.annotation.*; +import javax.persistence.*; import org.redkale.net.*; import org.redkale.net.http.*; import org.redkale.service.*; @@ -209,6 +210,7 @@ public abstract class NodeServer { Type genericType = field.getGenericType(); ParameterizedType pt = (genericType instanceof ParameterizedType) ? (ParameterizedType) genericType : null; Type valType = pt == null ? null : pt.getActualTypeArguments()[1]; + source.setNeedStore(field.getAnnotation(Transient.class) == null); source.setStoreType(pt == null ? Serializable.class : (Class) pt.getActualTypeArguments()[0], valType instanceof Class ? (Class) valType : Object.class); application.cacheSources.add(source); regFactory.register(resourceName, CacheSource.class, source); diff --git a/src/org/redkale/convert/Factory.java b/src/org/redkale/convert/Factory.java index 5985dfae8..af41d54f9 100644 --- a/src/org/redkale/convert/Factory.java +++ b/src/org/redkale/convert/Factory.java @@ -276,6 +276,28 @@ public abstract class Factory { if (type instanceof ParameterizedType) { final ParameterizedType pts = (ParameterizedType) type; clazz = (Class) (pts).getRawType(); + } else if (type instanceof TypeVariable) { // e.g. + final TypeVariable tv = (TypeVariable) type; + Class cz = tv.getBounds().length == 0 ? Object.class : null; + for (Type f : tv.getBounds()) { + if (f instanceof Class) { + cz = (Class) f; + break; + } + } + clazz = cz; + if (cz == null) throw new ConvertException("not support the type (" + type + ")"); + } else if (type instanceof WildcardType) { // e.g. + final WildcardType wt = (WildcardType) type; + Class cz = null; + for (Type f : wt.getUpperBounds()) { + if (f instanceof Class) { + cz = (Class) f; + break; + } + } + clazz = cz; + if (cz == null) throw new ConvertException("not support the type (" + type + ")"); } else if (type instanceof Class) { clazz = (Class) type; } else { diff --git a/src/org/redkale/service/CacheSourceService.java b/src/org/redkale/service/CacheSourceService.java index 5ffb50567..217243f3f 100644 --- a/src/org/redkale/service/CacheSourceService.java +++ b/src/org/redkale/service/CacheSourceService.java @@ -26,7 +26,7 @@ import org.redkale.util.*; * @author zhangjx */ @AutoLoad(false) -public class CacheSourceService implements CacheSource, Service, AutoCloseable { +public class CacheSourceService implements CacheSource, Service, AutoCloseable { @Resource(name = "APP_HOME") private File home; @@ -34,6 +34,8 @@ public class CacheSourceService implements CacheSourc @Resource private JsonConvert convert; + private boolean needStore = true; + private Class keyType; private Type objValueType; @@ -53,7 +55,7 @@ public class CacheSourceService implements CacheSourc public CacheSourceService() { } - public CacheSourceService setStoreType(Class keyType, Class valueType) { + public final CacheSourceService setStoreType(Class keyType, Class valueType) { this.keyType = keyType; this.objValueType = valueType; this.setValueType = TypeToken.createParameterizedType(null, CopyOnWriteArraySet.class, valueType); @@ -61,11 +63,16 @@ public class CacheSourceService implements CacheSourc return this; } + public final void setNeedStore(boolean needStore) { + this.needStore = needStore; + } + @Override public void init(AnyValue conf) { final CacheSourceService self = this; AnyValue prop = conf == null ? null : conf.getAnyValue("property"); if (keyType == null && prop != null) { + this.needStore = prop.getBoolValue("store-value", true); String storeKeyStr = prop.getValue("key-type"); String storeValueStr = prop.getValue("value-type"); if (storeKeyStr != null && storeValueStr != null) { @@ -111,7 +118,7 @@ public class CacheSourceService implements CacheSourc boolean datasync = false; //是否从远程同步过数据 //----------同步数据……----------- // TODO - if (this.keyType == null) return; + if (!this.needStore) return; try { CacheEntry.initCreator(); File store = new File(home, "cache/" + name()); @@ -149,7 +156,7 @@ public class CacheSourceService implements CacheSourc @Override public void destroy(AnyValue conf) { if (scheduler != null) scheduler.shutdownNow(); - if (this.keyType == null || Sncp.isRemote(this) || container.isEmpty()) return; + if (!this.needStore || Sncp.isRemote(this) || container.isEmpty()) return; try { CacheEntry.initCreator(); File store = new File(home, "cache/" + name()); diff --git a/src/org/redkale/source/CacheSource.java b/src/org/redkale/source/CacheSource.java index ab72fe25f..f706c6668 100644 --- a/src/org/redkale/source/CacheSource.java +++ b/src/org/redkale/source/CacheSource.java @@ -16,7 +16,7 @@ import java.util.*; * @see http://www.redkale.org * @author zhangjx */ -public interface CacheSource { +public interface CacheSource { default boolean isOpen() { return true;