This commit is contained in:
wentch
2016-01-05 17:41:54 +08:00
parent 9b32908906
commit 1568f492ff
6 changed files with 37 additions and 40 deletions

View File

@@ -79,7 +79,7 @@ public final class ServiceWrapper<T extends Service> {
} }
public String getName() { public String getName() {
return service.name(); return name;
} }
public boolean isRemote() { public boolean isRemote() {

View File

@@ -126,6 +126,7 @@ public abstract class Sncp {
* </pre></blockquote> * </pre></blockquote>
* *
* <blockquote><pre> * <blockquote><pre>
* &#64;Resource(name = "")
* &#64;SncpDyn(remote = false) * &#64;SncpDyn(remote = false)
* public final class _DynLocalTestService extends TestService{ * public final class _DynLocalTestService extends TestService{
* *
@@ -141,11 +142,6 @@ public abstract class Sncp {
* private String _selfstring; * private String _selfstring;
* *
* &#64;Override * &#64;Override
* public final String name() {
* return "";
* }
*
* &#64;Override
* public String toString() { * public String toString() {
* return _selfstring == null ? super.toString() : _selfstring; * return _selfstring == null ? super.toString() : _selfstring;
* } * }
@@ -214,6 +210,11 @@ public abstract class Sncp {
AnnotationVisitor av0; AnnotationVisitor av0;
cw.visit(V1_8, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, newDynName, null, supDynName, null); cw.visit(V1_8, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, newDynName, null, supDynName, null);
{
av0 = cw.visitAnnotation("Ljavax/annotation/Resource;", true);
av0.visit("name", name);
av0.visitEnd();
}
{ {
av0 = cw.visitAnnotation(sncpDynDesc, true); av0 = cw.visitAnnotation(sncpDynDesc, true);
av0.visit("remote", Boolean.FALSE); av0.visit("remote", Boolean.FALSE);
@@ -252,13 +253,6 @@ public abstract class Sncp {
mv.visitMaxs(1, 1); mv.visitMaxs(1, 1);
mv.visitEnd(); mv.visitEnd();
} }
{ // name()
mv = new AsmMethodVisitor(cw.visitMethod(ACC_PUBLIC + ACC_FINAL, "name", "()Ljava/lang/String;", null, null));
mv.visitLdcInsn(name);
mv.visitInsn(ARETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
{ // toString() { // toString()
mv = new AsmMethodVisitor(cw.visitMethod(ACC_PUBLIC, "toString", "()Ljava/lang/String;", null, null)); mv = new AsmMethodVisitor(cw.visitMethod(ACC_PUBLIC, "toString", "()Ljava/lang/String;", null, null));
mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 0);
@@ -775,6 +769,7 @@ public abstract class Sncp {
/** /**
* <blockquote><pre> * <blockquote><pre>
* &#64;Resource(name = "")
* &#64;SncpDyn(remote = true) * &#64;SncpDyn(remote = true)
* public final class _DynRemoteTestService extends TestService{ * public final class _DynRemoteTestService extends TestService{
* *
@@ -788,11 +783,6 @@ public abstract class Sncp {
* private String _selfstring; * private String _selfstring;
* *
* &#64;Override * &#64;Override
* public final String name() {
* return "";
* }
*
* &#64;Override
* public String toString() { * public String toString() {
* return _selfstring == null ? super.toString() : _selfstring; * return _selfstring == null ? super.toString() : _selfstring;
* } * }
@@ -875,6 +865,11 @@ public abstract class Sncp {
AnnotationVisitor av0; AnnotationVisitor av0;
cw.visit(V1_8, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, newDynName, null, supDynName, null); cw.visit(V1_8, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, newDynName, null, supDynName, null);
{
av0 = cw.visitAnnotation("Ljavax/annotation/Resource;", true);
av0.visit("name", name);
av0.visitEnd();
}
{ {
av0 = cw.visitAnnotation(sncpDynDesc, true); av0 = cw.visitAnnotation(sncpDynDesc, true);
av0.visit("remote", Boolean.TRUE); av0.visit("remote", Boolean.TRUE);
@@ -919,13 +914,6 @@ public abstract class Sncp {
mv.visitMaxs(0, 2); mv.visitMaxs(0, 2);
mv.visitEnd(); mv.visitEnd();
} }
{ // name()
mv = new AsmMethodVisitor(cw.visitMethod(ACC_PUBLIC + ACC_FINAL, "name", "()Ljava/lang/String;", null, null));
mv.visitLdcInsn(name);
mv.visitInsn(ARETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
{ // toString() { // toString()
mv = new AsmMethodVisitor(cw.visitMethod(ACC_PUBLIC, "toString", "()Ljava/lang/String;", null, null)); mv = new AsmMethodVisitor(cw.visitMethod(ACC_PUBLIC, "toString", "()Ljava/lang/String;", null, null));
mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 0);

View File

@@ -156,6 +156,10 @@ public class CacheSourceService<K extends Serializable, V extends Object> implem
destroy(null); destroy(null);
} }
public String name() {
return this.getClass().getAnnotation(Resource.class).name();
}
@Override @Override
public void destroy(AnyValue conf) { public void destroy(AnyValue conf) {
if (scheduler != null) scheduler.shutdownNow(); if (scheduler != null) scheduler.shutdownNow();

View File

@@ -19,7 +19,9 @@ import javax.annotation.Resource;
/** /**
* 暂时不实现 * 暂时不实现
* *
* <p> 详情见: http://www.redkale.org * <p>
* 详情见: http://www.redkale.org
*
* @author zhangjx * @author zhangjx
*/ */
@AutoLoad(false) @AutoLoad(false)
@@ -64,6 +66,10 @@ public class DataSQLListenerService implements DataSQLListener, Service {
} }
public String name() {
return this.getClass().getAnnotation(Resource.class).name();
}
@Override @Override
public void destroy(AnyValue config) { public void destroy(AnyValue config) {
if (syncfile != null) syncfile.close(); if (syncfile != null) syncfile.close();

View File

@@ -41,12 +41,4 @@ public interface Service {
} }
/**
* Service的name 一个Service在同一进程内可以包含多个实例 使用name区分
*
* @return 资源名
*/
default String name() {
return "";
}
} }

View File

@@ -15,7 +15,9 @@ import javax.annotation.*;
/** /**
* 如果Resource(name = "$") 表示资源name采用所属对象的name * 如果Resource(name = "$") 表示资源name采用所属对象的name
* *
* <p> 详情见: http://www.redkale.org * <p>
* 详情见: http://www.redkale.org
*
* @author zhangjx * @author zhangjx
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@@ -185,10 +187,15 @@ public final class ResourceFactory {
String tname = rc.name(); String tname = rc.name();
if (tname.contains(RESOURCE_PARENT_NAME)) { if (tname.contains(RESOURCE_PARENT_NAME)) {
try { try {
String srcname = (String) src.getClass().getMethod("name").invoke(src); Resource res = src.getClass().getAnnotation(Resource.class);
tname = tname.replace(RESOURCE_PARENT_NAME, srcname); if (res == null) {
String srcname = (String) src.getClass().getMethod("name").invoke(src);
tname = tname.replace(RESOURCE_PARENT_NAME, srcname);
} else {
tname = res.name();
}
} catch (Exception e) { // 获取src中的name()方法的值, 异常则忽略 } catch (Exception e) { // 获取src中的name()方法的值, 异常则忽略
logger.log(Level.SEVERE, src.getClass().getName() + " not found [public String name()] method", e); logger.log(Level.SEVERE, src.getClass().getName() + " not found @Resource on Class or [public String name()] method", e);
} }
} }
final String rcname = tname; final String rcname = tname;