Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement pull request #3412 on master branch #3418

Merged
merged 2 commits into from
Feb 2, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.extension.SPI;
import org.apache.dubbo.common.utils.ArrayUtils;

import java.util.Arrays;
import java.util.Comparator;

/**
Expand All @@ -44,75 +46,92 @@ public int compare(Object o1, Object o2) {
return 0;
}

// to support com.alibab.dubbo.common.extension.Activate
String[] a1Before, a2Before, a1After, a2After;
int a1Order, a2Order;
Class<?> inf = null;
if (o1.getClass().getInterfaces().length > 0) {
inf = o1.getClass().getInterfaces()[0];
Class<?> inf = findSpi(o1.getClass());

if (inf.getInterfaces().length > 0) {
inf = inf.getInterfaces()[0];
}
}
ActivateInfo a1 = parseActivate(o1.getClass());
ActivateInfo a2 = parseActivate(o2.getClass());

Activate a1 = o1.getClass().getAnnotation(Activate.class);
if (a1 != null) {
a1Before = a1.before();
a1After = a1.after();
a1Order = a1.order();
} else {
com.alibaba.dubbo.common.extension.Activate oa1 = o1.getClass().getAnnotation(com.alibaba.dubbo.common.extension.Activate.class);
a1Before = oa1.before();
a1After = oa1.after();
a1Order = oa1.order();
}
Activate a2 = o2.getClass().getAnnotation(Activate.class);
if (a2 != null) {
a2Before = a2.before();
a2After = a2.after();
a2Order = a2.order();
} else {
com.alibaba.dubbo.common.extension.Activate oa2 = o2.getClass().getAnnotation(com.alibaba.dubbo.common.extension.Activate.class);
a2Before = oa2.before();
a2After = oa2.after();
a2Order = oa2.order();
}
if ((a1Before.length > 0 || a1After.length > 0
|| a2Before.length > 0 || a2After.length > 0)
&& inf != null && inf.isAnnotationPresent(SPI.class)) {
if ((a1.applicableToCompare() || a2.applicableToCompare()) && inf != null) {
ExtensionLoader<?> extensionLoader = ExtensionLoader.getExtensionLoader(inf);
if (a1Before.length > 0 || a1After.length > 0) {
if (a1.applicableToCompare()) {
String n2 = extensionLoader.getExtensionName(o2.getClass());
for (String before : a1Before) {
if (before.equals(n2)) {
return -1;
}
if (a1.isLess(n2)) {
return -1;
}
for (String after : a1After) {
if (after.equals(n2)) {
return 1;
}

if (a1.isMore(n2)) {
return 1;
}
}
if (a2Before.length > 0 || a2After.length > 0) {

if (a2.applicableToCompare()) {
String n1 = extensionLoader.getExtensionName(o1.getClass());
for (String before : a2Before) {
if (before.equals(n1)) {
return 1;
}
if (a2.isLess(n1)) {
return 1;
}
for (String after : a2After) {
if (after.equals(n1)) {
return -1;
}

if (a2.isMore(n1)) {
return -1;
}
}
}
int n1 = a1 == null ? 0 : a1Order;
int n2 = a2 == null ? 0 : a2Order;
int n1 = a1 == null ? 0 : a1.order;
int n2 = a2 == null ? 0 : a2.order;
// never return 0 even if n1 equals n2, otherwise, o1 and o2 will override each other in collection like HashSet
return n1 > n2 ? 1 : -1;
}

private Class<?> findSpi(Class clazz) {
if (clazz.getInterfaces().length <= 0) {
return null;
}

for (Class<?> intf : clazz.getInterfaces()) {
if (intf.isAnnotationPresent(SPI.class)) {
return intf;
} else {
Class result = findSpi(intf);
if (result != null) {
return result;
}
}
}

return null;
}

private ActivateInfo parseActivate(Class<?> clazz) {
ActivateInfo info = new ActivateInfo();
if (clazz.isAnnotationPresent(Activate.class)) {
Activate activate = clazz.getAnnotation(Activate.class);
info.before = activate.before();
info.after = activate.after();
info.order = activate.order();
} else {
com.alibaba.dubbo.common.extension.Activate activate = clazz.getAnnotation(
beiwei30 marked this conversation as resolved.
Show resolved Hide resolved
com.alibaba.dubbo.common.extension.Activate.class);
info.before = activate.before();
info.after = activate.after();
info.order = activate.order();
}
return info;
}

private static class ActivateInfo {
private String[] before;
private String[] after;
private int order;

private boolean applicableToCompare() {
return ArrayUtils.isNotEmpty(before) || ArrayUtils.isNotEmpty(after);
}

private boolean isLess(String name) {
return Arrays.asList(before).contains(name);
}

private boolean isMore(String name) {
return Arrays.asList(after).contains(name);
}
}
}