35
35
public interface Configurator extends Comparable <Configurator > {
36
36
37
37
/**
38
- * get the configurator url.
38
+ * Get the configurator url.
39
39
*
40
40
* @return configurator url.
41
41
*/
42
42
URL getUrl ();
43
43
44
44
/**
45
45
* Configure the provider url.
46
- * O
47
46
*
48
- * @param url - old rovider url.
47
+ * @param url - old provider url.
49
48
* @return new provider url.
50
49
*/
51
50
URL configure (URL url );
52
51
53
52
54
53
/**
55
- * Convert override urls to map for use when re-refer.
56
- * Send all rules every time, the urls will be reassembled and calculated
54
+ * Convert override urls to map for use when re-refer. Send all rules every time, the urls will be reassembled and
55
+ * calculated
57
56
*
58
- * @param urls Contract:
59
- * </br>1.override://0.0.0.0/...( or override://ip:port...?anyhost=true)¶1=value1... means global rules (all of the providers take effect)
60
- * </br>2.override://ip:port...?anyhost=false Special rules (only for a certain provider)
61
- * </br>3.override:// rule is not supported... ,needs to be calculated by registry itself.
62
- * </br>4.override://0.0.0.0/ without parameters means clearing the override
63
- * @return
57
+ * URL contract:
58
+ * <ol>
59
+ * <li>override://0.0.0.0/...( or override://ip:port...?anyhost=true)¶1=value1... means global rules
60
+ * (all of the providers take effect)</li>
61
+ * <li>override://ip:port...?anyhost=false Special rules (only for a certain provider)</li>
62
+ * <li>override:// rule is not supported... ,needs to be calculated by registry itself</li>
63
+ * <li>override://0.0.0.0/ without parameters means clearing the override</li>
64
+ * </ol>
65
+ *
66
+ * @param urls URL list to convert
67
+ * @return converted configurator list
64
68
*/
65
69
static Optional <List <Configurator >> toConfigurators (List <URL > urls ) {
66
70
if (CollectionUtils .isEmpty (urls )) {
@@ -70,13 +74,13 @@ static Optional<List<Configurator>> toConfigurators(List<URL> urls) {
70
74
ConfiguratorFactory configuratorFactory = ExtensionLoader .getExtensionLoader (ConfiguratorFactory .class )
71
75
.getAdaptiveExtension ();
72
76
73
- List <Configurator > configurators = new ArrayList <Configurator >(urls .size ());
77
+ List <Configurator > configurators = new ArrayList <>(urls .size ());
74
78
for (URL url : urls ) {
75
79
if (Constants .EMPTY_PROTOCOL .equals (url .getProtocol ())) {
76
80
configurators .clear ();
77
81
break ;
78
82
}
79
- Map <String , String > override = new HashMap <String , String >(url .getParameters ());
83
+ Map <String , String > override = new HashMap <>(url .getParameters ());
80
84
//The anyhost parameter of override may be added automatically, it can't change the judgement of changing url
81
85
override .remove (Constants .ANYHOST_KEY );
82
86
if (override .size () == 0 ) {
@@ -88,4 +92,25 @@ static Optional<List<Configurator>> toConfigurators(List<URL> urls) {
88
92
Collections .sort (configurators );
89
93
return Optional .of (configurators );
90
94
}
95
+
96
+ /**
97
+ * Sort by host, then by priority
98
+ * 1. the url with a specific host ip should have higher priority than 0.0.0.0
99
+ * 2. if two url has the same host, compare by priority value;
100
+ */
101
+ default int compareTo (Configurator o ) {
102
+ if (o == null ) {
103
+ return -1 ;
104
+ }
105
+
106
+ int ipCompare = getUrl ().getHost ().compareTo (o .getUrl ().getHost ());
107
+ // host is the same, sort by priority
108
+ if (ipCompare == 0 ) {
109
+ int i = getUrl ().getParameter (Constants .PRIORITY_KEY , 0 );
110
+ int j = o .getUrl ().getParameter (Constants .PRIORITY_KEY , 0 );
111
+ return Integer .compare (i , j );
112
+ } else {
113
+ return ipCompare ;
114
+ }
115
+ }
91
116
}
0 commit comments