copy all the related classes to org.apache.dubbo.rpc.cluster.configurator.parser.model

This commit is contained in:
heliang 2024-06-22 16:06:05 +08:00
parent 5cf472ff07
commit 11dc413af0
13 changed files with 757 additions and 3 deletions

View File

@ -17,8 +17,8 @@
package org.apache.dubbo.rpc.cluster.configurator.parser.model;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.rpc.cluster.router.mesh.rule.virtualservice.match.AddressMatch;
import org.apache.dubbo.rpc.cluster.router.mesh.rule.virtualservice.match.ListStringMatch;
import org.apache.dubbo.rpc.cluster.configurator.parser.model.match.AddressMatch;
import org.apache.dubbo.rpc.cluster.configurator.parser.model.match.ListStringMatch;
import java.util.List;

View File

@ -17,7 +17,7 @@
package org.apache.dubbo.rpc.cluster.configurator.parser.model;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.rpc.cluster.router.mesh.rule.virtualservice.match.StringMatch;
import org.apache.dubbo.rpc.cluster.configurator.parser.model.match.StringMatch;
public class ParamMatch {
private String key;

View File

@ -0,0 +1,87 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.rpc.cluster.configurator.parser.model.match;
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
import org.apache.dubbo.common.logger.LoggerFactory;
import java.net.UnknownHostException;
import static org.apache.dubbo.common.constants.CommonConstants.ANYHOST_VALUE;
import static org.apache.dubbo.common.constants.CommonConstants.ANY_VALUE;
import static org.apache.dubbo.common.constants.LoggerCodeConstants.CLUSTER_FAILED_EXEC_CONDITION_ROUTER;
import static org.apache.dubbo.common.utils.NetUtils.matchIpExpression;
import static org.apache.dubbo.common.utils.UrlUtils.isMatchGlobPattern;
public class AddressMatch {
public static final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(AddressMatch.class);
private String wildcard;
private String cird;
private String exact;
public String getWildcard() {
return wildcard;
}
public void setWildcard(String wildcard) {
this.wildcard = wildcard;
}
public String getCird() {
return cird;
}
public void setCird(String cird) {
this.cird = cird;
}
public String getExact() {
return exact;
}
public void setExact(String exact) {
this.exact = exact;
}
public boolean isMatch(String input) {
if (getCird() != null && input != null) {
try {
return input.equals(getCird()) || matchIpExpression(getCird(), input);
} catch (UnknownHostException e) {
logger.error(
CLUSTER_FAILED_EXEC_CONDITION_ROUTER,
"Executing routing rule match expression error.",
"",
String.format(
"Error trying to match cird formatted address %s with input %s in AddressMatch.",
getCird(), input),
e);
}
}
if (getWildcard() != null && input != null) {
if (ANYHOST_VALUE.equals(getWildcard()) || ANY_VALUE.equals(getWildcard())) {
return true;
}
// FIXME
return isMatchGlobPattern(getWildcard(), input);
}
if (getExact() != null && input != null) {
return input.equals(getExact());
}
return false;
}
}

View File

@ -0,0 +1,36 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.rpc.cluster.configurator.parser.model.match;
public class BoolMatch {
private Boolean exact;
public Boolean getExact() {
return exact;
}
public void setExact(Boolean exact) {
this.exact = exact;
}
public boolean isMatch(boolean input) {
if (exact != null) {
return input == exact;
}
return false;
}
}

View File

@ -0,0 +1,60 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.rpc.cluster.configurator.parser.model.match;
public class DoubleMatch {
private Double exact;
private DoubleRangeMatch range;
private Double mod;
public Double getExact() {
return exact;
}
public void setExact(Double exact) {
this.exact = exact;
}
public DoubleRangeMatch getRange() {
return range;
}
public void setRange(DoubleRangeMatch range) {
this.range = range;
}
public Double getMod() {
return mod;
}
public void setMod(Double mod) {
this.mod = mod;
}
public boolean isMatch(Double input) {
if (exact != null && mod == null) {
return input.equals(exact);
} else if (range != null) {
return range.isMatch(input);
} else if (exact != null) {
Double result = input % mod;
return result.equals(exact);
}
return false;
}
}

View File

@ -0,0 +1,50 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.rpc.cluster.configurator.parser.model.match;
public class DoubleRangeMatch {
private Double start;
private Double end;
public Double getStart() {
return start;
}
public void setStart(Double start) {
this.start = start;
}
public Double getEnd() {
return end;
}
public void setEnd(Double end) {
this.end = end;
}
public boolean isMatch(Double input) {
if (start != null && end != null) {
return input.compareTo(start) >= 0 && input.compareTo(end) < 0;
} else if (start != null) {
return input.compareTo(start) >= 0;
} else if (end != null) {
return input.compareTo(end) < 0;
} else {
return false;
}
}
}

View File

@ -0,0 +1,73 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.rpc.cluster.configurator.parser.model.match;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.cluster.router.mesh.util.TracingContextProvider;
import java.util.Map;
import java.util.Set;
public class DubboAttachmentMatch {
private Map<String, StringMatch> tracingContext;
private Map<String, StringMatch> dubboContext;
public Map<String, StringMatch> getTracingContext() {
return tracingContext;
}
public void setTracingContext(Map<String, StringMatch> tracingContext) {
this.tracingContext = tracingContext;
}
public Map<String, StringMatch> getDubboContext() {
return dubboContext;
}
public void setDubboContext(Map<String, StringMatch> dubboContext) {
this.dubboContext = dubboContext;
}
public boolean isMatch(Invocation invocation, Set<TracingContextProvider> contextProviders) {
// Match Dubbo Context
if (dubboContext != null) {
for (Map.Entry<String, StringMatch> entry : dubboContext.entrySet()) {
String key = entry.getKey();
if (!entry.getValue().isMatch(invocation.getAttachment(key))) {
return false;
}
}
}
// Match Tracing Context
if (tracingContext != null) {
for (Map.Entry<String, StringMatch> entry : tracingContext.entrySet()) {
String key = entry.getKey();
boolean match = false;
for (TracingContextProvider contextProvider : contextProviders) {
if (entry.getValue().isMatch(contextProvider.getValue(invocation, key))) {
match = true;
}
}
if (!match) {
return false;
}
}
}
return true;
}
}

View File

@ -0,0 +1,87 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.rpc.cluster.configurator.parser.model.match;
public class DubboMethodArg {
private int index;
private String type;
private ListStringMatch str_value;
private ListDoubleMatch num_value;
private BoolMatch bool_value;
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public ListStringMatch getStr_value() {
return str_value;
}
public void setStr_value(ListStringMatch str_value) {
this.str_value = str_value;
}
public ListDoubleMatch getNum_value() {
return num_value;
}
public void setNum_value(ListDoubleMatch num_value) {
this.num_value = num_value;
}
public BoolMatch getBool_value() {
return bool_value;
}
public void setBool_value(BoolMatch bool_value) {
this.bool_value = bool_value;
}
public boolean isMatch(Object input) {
if (str_value != null) {
return input instanceof String && str_value.isMatch((String) input);
} else if (num_value != null) {
return num_value.isMatch(Double.valueOf(input.toString()));
} else if (bool_value != null) {
return input instanceof Boolean && bool_value.isMatch((Boolean) input);
}
return false;
}
@Override
public String toString() {
return "DubboMethodArg{" + "index="
+ index + ", type='"
+ type + '\'' + ", str_value="
+ str_value + ", num_value="
+ num_value + ", bool_value="
+ bool_value + '}';
}
}

View File

@ -0,0 +1,133 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.rpc.cluster.configurator.parser.model.match;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.support.RpcUtils;
import java.util.List;
import java.util.Map;
public class DubboMethodMatch {
private StringMatch name_match;
private Integer argc;
private List<DubboMethodArg> args;
private List<StringMatch> argp;
private Map<String, StringMatch> headers;
public StringMatch getName_match() {
return name_match;
}
public void setName_match(StringMatch name_match) {
this.name_match = name_match;
}
public Integer getArgc() {
return argc;
}
public void setArgc(Integer argc) {
this.argc = argc;
}
public List<DubboMethodArg> getArgs() {
return args;
}
public void setArgs(List<DubboMethodArg> args) {
this.args = args;
}
public List<StringMatch> getArgp() {
return argp;
}
public void setArgp(List<StringMatch> argp) {
this.argp = argp;
}
public Map<String, StringMatch> getHeaders() {
return headers;
}
public void setHeaders(Map<String, StringMatch> headers) {
this.headers = headers;
}
@Override
public String toString() {
return "DubboMethodMatch{" + "name_match="
+ name_match + ", argc="
+ argc + ", args="
+ args + ", argp="
+ argp + ", headers="
+ headers + '}';
}
public boolean isMatch(Invocation invocation) {
StringMatch nameMatch = getName_match();
if (nameMatch != null && !nameMatch.isMatch(RpcUtils.getMethodName(invocation))) {
return false;
}
Integer argc = getArgc();
Object[] arguments = invocation.getArguments();
if (argc != null
&& ((argc != 0 && (arguments == null || arguments.length == 0)) || (argc != arguments.length))) {
return false;
}
List<StringMatch> argp = getArgp();
Class<?>[] parameterTypes = invocation.getParameterTypes();
if (argp != null && argp.size() > 0) {
if (parameterTypes == null || parameterTypes.length == 0) {
return false;
}
if (argp.size() != parameterTypes.length) {
return false;
}
for (int index = 0; index < argp.size(); index++) {
boolean match = argp.get(index).isMatch(parameterTypes[index].getName())
|| argp.get(index).isMatch(parameterTypes[index].getSimpleName());
if (!match) {
return false;
}
}
}
List<DubboMethodArg> args = getArgs();
if (args != null && args.size() > 0) {
if (arguments == null || arguments.length == 0) {
return false;
}
for (DubboMethodArg dubboMethodArg : args) {
int index = dubboMethodArg.getIndex();
if (index >= arguments.length) {
throw new IndexOutOfBoundsException("DubboMethodArg index >= parameters.length");
}
if (!dubboMethodArg.isMatch(arguments[index])) {
return false;
}
}
}
return true;
}
}

View File

@ -0,0 +1,41 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.rpc.cluster.configurator.parser.model.match;
import java.util.List;
public class ListBoolMatch {
private List<BoolMatch> oneof;
public List<BoolMatch> getOneof() {
return oneof;
}
public void setOneof(List<BoolMatch> oneof) {
this.oneof = oneof;
}
public boolean isMatch(boolean input) {
for (BoolMatch boolMatch : oneof) {
if (boolMatch.isMatch(input)) {
return true;
}
}
return false;
}
}

View File

@ -0,0 +1,41 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.rpc.cluster.configurator.parser.model.match;
import java.util.List;
public class ListDoubleMatch {
private List<DoubleMatch> oneof;
public List<DoubleMatch> getOneof() {
return oneof;
}
public void setOneof(List<DoubleMatch> oneof) {
this.oneof = oneof;
}
public boolean isMatch(Double input) {
for (DoubleMatch doubleMatch : oneof) {
if (doubleMatch.isMatch(input)) {
return true;
}
}
return false;
}
}

View File

@ -0,0 +1,41 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.rpc.cluster.configurator.parser.model.match;
import java.util.List;
public class ListStringMatch {
private List<StringMatch> oneof;
public List<StringMatch> getOneof() {
return oneof;
}
public void setOneof(List<StringMatch> oneof) {
this.oneof = oneof;
}
public boolean isMatch(String input) {
for (StringMatch stringMatch : oneof) {
if (stringMatch.isMatch(input)) {
return true;
}
}
return false;
}
}

View File

@ -0,0 +1,105 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.rpc.cluster.configurator.parser.model.match;
import static org.apache.dubbo.common.constants.CommonConstants.ANY_VALUE;
public class StringMatch {
private String exact;
private String prefix;
private String regex;
private String noempty;
private String empty;
private String wildcard;
public String getExact() {
return exact;
}
public void setExact(String exact) {
this.exact = exact;
}
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getRegex() {
return regex;
}
public void setRegex(String regex) {
this.regex = regex;
}
public String getNoempty() {
return noempty;
}
public void setNoempty(String noempty) {
this.noempty = noempty;
}
public String getEmpty() {
return empty;
}
public void setEmpty(String empty) {
this.empty = empty;
}
public String getWildcard() {
return wildcard;
}
public void setWildcard(String wildcard) {
this.wildcard = wildcard;
}
public boolean isMatch(String input) {
if (getExact() != null && input != null) {
return input.equals(getExact());
} else if (getPrefix() != null && input != null) {
return input.startsWith(getPrefix());
} else if (getRegex() != null && input != null) {
return input.matches(getRegex());
} else if (getWildcard() != null && input != null) {
// only supports "*"
return input.equals(getWildcard()) || ANY_VALUE.equals(getWildcard());
} else if (getEmpty() != null) {
return input == null || "".equals(input);
} else if (getNoempty() != null) {
return input != null && input.length() > 0;
} else {
return false;
}
}
@Override
public String toString() {
return "StringMatch{" + "exact='"
+ exact + '\'' + ", prefix='"
+ prefix + '\'' + ", regex='"
+ regex + '\'' + ", noempty='"
+ noempty + '\'' + ", empty='"
+ empty + '\'' + '}';
}
}