This commit is contained in:
Redkale
2017-05-27 10:25:53 +08:00
parent 24c90b015a
commit 92b3d0bbd4
4 changed files with 40 additions and 30 deletions

View File

@@ -360,21 +360,30 @@ public final class Application {
this.resourceFactory.register(JsonFactory.root().getConvert()); this.resourceFactory.register(JsonFactory.root().getConvert());
//只有WatchService才能加载Application、WatchFactory //只有WatchService才能加载Application、WatchFactory
final Application application = this; final Application application = this;
this.resourceFactory.register((ResourceFactory rf, final Object src, String resourceName, Field field, final Object attachment) -> { this.resourceFactory.register(new ResourceFactory.ResourceLoader() {
try {
Resource res = field.getAnnotation(Resource.class); @Override
if (res == null || !res.name().isEmpty()) return false; public void load(ResourceFactory rf, final Object src, String resourceName, Field field, final Object attachment) {
if (!(src instanceof WatchService) || Sncp.isRemote((Service) src)) return false; //远程模式不得注入 try {
Class type = field.getType(); Resource res = field.getAnnotation(Resource.class);
if (type == Application.class) { if (res == null || !res.name().isEmpty()) return;
field.set(src, application); if (!(src instanceof WatchService) || Sncp.isRemote((Service) src)) return; //远程模式不得注入
Class type = field.getType();
if (type == Application.class) {
field.set(src, application);
// } else if (type == WatchFactory.class) { // } else if (type == WatchFactory.class) {
// field.set(src, application.watchFactory); // field.set(src, application.watchFactory);
}
} catch (Exception e) {
logger.log(Level.SEVERE, "Resource inject error", e);
} }
} catch (Exception e) {
logger.log(Level.SEVERE, "Resource inject error", e);
} }
return false;
@Override
public boolean autoNone() {
return false;
}
}, Application.class, WatchFactory.class); }, Application.class, WatchFactory.class);
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
initResources(); initResources();

View File

@@ -92,8 +92,8 @@ public class NodeHttpServer extends NodeServer {
final ResourceFactory regFactory = application.getResourceFactory(); final ResourceFactory regFactory = application.getResourceFactory();
resourceFactory.register((ResourceFactory rf, final Object src, final String resourceName, Field field, Object attachment) -> { //主要用于单点的服务 resourceFactory.register((ResourceFactory rf, final Object src, final String resourceName, Field field, Object attachment) -> { //主要用于单点的服务
try { try {
if (field.getAnnotation(Resource.class) == null) return true; if (field.getAnnotation(Resource.class) == null) return;
if (!(src instanceof WebSocketServlet)) return true; if (!(src instanceof WebSocketServlet)) return;
synchronized (regFactory) { synchronized (regFactory) {
Service nodeService = (Service) rf.find(resourceName, WebSocketNode.class); Service nodeService = (Service) rf.find(resourceName, WebSocketNode.class);
if (nodeService == null) { if (nodeService == null) {
@@ -107,7 +107,6 @@ public class NodeHttpServer extends NodeServer {
} catch (Exception e) { } catch (Exception e) {
logger.log(Level.SEVERE, "WebSocketNode inject error", e); logger.log(Level.SEVERE, "WebSocketNode inject error", e);
} }
return true;
}, WebSocketNode.class); }, WebSocketNode.class);
} }

View File

@@ -136,7 +136,7 @@ public abstract class NodeServer {
if (this.sncpAddress != null) { if (this.sncpAddress != null) {
this.resourceFactory.register(RESNAME_SNCP_ADDR, this.sncpAddress); this.resourceFactory.register(RESNAME_SNCP_ADDR, this.sncpAddress);
this.resourceFactory.register(RESNAME_SNCP_ADDR, SocketAddress.class, this.sncpAddress); this.resourceFactory.register(RESNAME_SNCP_ADDR, SocketAddress.class, this.sncpAddress);
this.resourceFactory.register(RESNAME_SNCP_ADDR, String.class, this.sncpAddress.getHostString()+":" + this.sncpAddress.getPort()); this.resourceFactory.register(RESNAME_SNCP_ADDR, String.class, this.sncpAddress.getHostString() + ":" + this.sncpAddress.getPort());
} }
if (this.sncpGroup != null) this.resourceFactory.register(RESNAME_SNCP_GROUP, this.sncpGroup); if (this.sncpGroup != null) this.resourceFactory.register(RESNAME_SNCP_GROUP, this.sncpGroup);
{ {
@@ -215,10 +215,10 @@ public abstract class NodeServer {
resourceFactory.register((ResourceFactory rf, final Object src, String resourceName, Field field, final Object attachment) -> { resourceFactory.register((ResourceFactory rf, final Object src, String resourceName, Field field, final Object attachment) -> {
try { try {
Resource res = field.getAnnotation(Resource.class); Resource res = field.getAnnotation(Resource.class);
if (res == null || !res.name().startsWith("properties.")) return true; if (res == null || !res.name().startsWith("properties.")) return;
if ((src instanceof Service) && Sncp.isRemote((Service) src)) return true; //远程模式不得注入 DataSource if ((src instanceof Service) && Sncp.isRemote((Service) src)) return; //远程模式不得注入 DataSource
Class type = field.getType(); Class type = field.getType();
if (type != AnyValue.class && type != AnyValue[].class) return true; if (type != AnyValue.class && type != AnyValue[].class) return;
Object resource = null; Object resource = null;
final AnyValue properties = resources == null ? null : resources.getAnyValue("properties"); final AnyValue properties = resources == null ? null : resources.getAnyValue("properties");
if (properties != null && type == AnyValue.class) { if (properties != null && type == AnyValue.class) {
@@ -232,14 +232,13 @@ public abstract class NodeServer {
} catch (Exception e) { } catch (Exception e) {
logger.log(Level.SEVERE, "Resource inject error", e); logger.log(Level.SEVERE, "Resource inject error", e);
} }
return true;
}, AnyValue.class, AnyValue[].class); }, AnyValue.class, AnyValue[].class);
//------------------------------------- 注册DataSource -------------------------------------------------------- //------------------------------------- 注册DataSource --------------------------------------------------------
resourceFactory.register((ResourceFactory rf, final Object src, String resourceName, Field field, final Object attachment) -> { resourceFactory.register((ResourceFactory rf, final Object src, String resourceName, Field field, final Object attachment) -> {
try { try {
if (field.getAnnotation(Resource.class) == null) return true; if (field.getAnnotation(Resource.class) == null) return;
if ((src instanceof Service) && Sncp.isRemote((Service) src)) return true; //远程模式不得注入 DataSource if ((src instanceof Service) && Sncp.isRemote((Service) src)) return; //远程模式不得注入 DataSource
DataSource source = DataSources.createDataSource(resourceName); DataSource source = DataSources.createDataSource(resourceName);
application.dataSources.add(source); application.dataSources.add(source);
appResFactory.register(resourceName, DataSource.class, source); appResFactory.register(resourceName, DataSource.class, source);
@@ -265,14 +264,13 @@ public abstract class NodeServer {
} catch (Exception e) { } catch (Exception e) {
logger.log(Level.SEVERE, "DataSource inject error", e); logger.log(Level.SEVERE, "DataSource inject error", e);
} }
return true;
}, DataSource.class); }, DataSource.class);
//------------------------------------- 注册CacheSource -------------------------------------------------------- //------------------------------------- 注册CacheSource --------------------------------------------------------
resourceFactory.register((ResourceFactory rf, final Object src, final String resourceName, Field field, final Object attachment) -> { resourceFactory.register((ResourceFactory rf, final Object src, final String resourceName, Field field, final Object attachment) -> {
try { try {
if (field.getAnnotation(Resource.class) == null) return true; if (field.getAnnotation(Resource.class) == null) return;
if ((src instanceof Service) && Sncp.isRemote((Service) src)) return true; //远程模式不需要注入 CacheSource if ((src instanceof Service) && Sncp.isRemote((Service) src)) return; //远程模式不需要注入 CacheSource
final Service srcService = (Service) src; final Service srcService = (Service) src;
SncpClient client = Sncp.getSncpClient(srcService); SncpClient client = Sncp.getSncpClient(srcService);
Transport sameGroupTransport = Sncp.getSameGroupTransport(srcService); Transport sameGroupTransport = Sncp.getSameGroupTransport(srcService);
@@ -308,7 +306,6 @@ public abstract class NodeServer {
} catch (Exception e) { } catch (Exception e) {
logger.log(Level.SEVERE, "DataSource inject error", e); logger.log(Level.SEVERE, "DataSource inject error", e);
} }
return true;
}, CacheSource.class); }, CacheSource.class);
} }
@@ -379,7 +376,6 @@ public abstract class NodeServer {
if (entry.isExpect()) { if (entry.isExpect()) {
ResourceFactory.ResourceLoader resourceLoader = (ResourceFactory rf, final Object src, final String resourceName, Field field, final Object attachment) -> { ResourceFactory.ResourceLoader resourceLoader = (ResourceFactory rf, final Object src, final String resourceName, Field field, final Object attachment) -> {
runner.accept(rf, true); runner.accept(rf, true);
return true;
}; };
ResourceType rty = entry.getType().getAnnotation(ResourceType.class); ResourceType rty = entry.getType().getAnnotation(ResourceType.class);
Class[] resTypes = rty == null ? new Class[]{} : rty.value(); Class[] resTypes = rty == null ? new Class[]{} : rty.value();

View File

@@ -349,7 +349,8 @@ public final class ResourceFactory {
if (re == null) { if (re == null) {
ResourceLoader it = findLoader(genctype, field); ResourceLoader it = findLoader(genctype, field);
if (it != null) { if (it != null) {
autoregnull = it.load(this, src, rcname, field, attachment); it.load(this, src, rcname, field, attachment);
autoregnull = it.autoNone();
re = findEntry(rcname, genctype); re = findEntry(rcname, genctype);
} }
} }
@@ -365,7 +366,8 @@ public final class ResourceFactory {
if (re == null) { if (re == null) {
ResourceLoader it = findLoader(classtype, field); ResourceLoader it = findLoader(classtype, field);
if (it != null) { if (it != null) {
autoregnull = it.load(this, src, rcname, field, attachment); it.load(this, src, rcname, field, attachment);
autoregnull = it.autoNone();
re = findEntry(rcname, classtype); re = findEntry(rcname, classtype);
} }
} }
@@ -534,8 +536,12 @@ public final class ResourceFactory {
@FunctionalInterface @FunctionalInterface
public static interface ResourceLoader { public static interface ResourceLoader {
// 返回true 表示如果资源不存在会在ResourceFactory里注入默认值null返回false表示资源不存在表示每次调用ResourceLoader自行处理 public void load(ResourceFactory factory, Object src, String resourceName, Field field, Object attachment);
public boolean load(ResourceFactory factory, Object src, String resourceName, Field field, Object attachment);
// 返回true 表示调用ResourceLoader之后资源仍不存在则会在ResourceFactory里注入默认值null返回false表示资源不存在下次仍会调用ResourceLoader自行处理
default boolean autoNone() {
return true;
}
} }
} }