style(#2689): Polish code and extract commonalities (#2849)

* style(#2689): Polish code and extract commonalities

- Increase the readability of collect uris
- Extract the same operation of extending ServiceMeta List
- Use generics to indicate types

* style(#2689): Polish code

- Improve documentation
pull/2870/head
RovingSea 2 years ago committed by chengpu
parent 39ca49c2ae
commit 9a60c85115

@ -19,6 +19,7 @@ package com.alibaba.cloud.appactive.provider;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.servlet.Filter;
import com.alibaba.cloud.nacos.registry.NacosRegistration;
@ -33,7 +34,7 @@ import org.springframework.context.annotation.Configuration;
public class NacosAutoConfig {
@Autowired
private List<FilterRegistrationBean> beanList;
private List<FilterRegistrationBean<? extends Filter>> beanList;
@Autowired(required = false)
private NacosRegistration nacosRegistration;

@ -36,6 +36,7 @@ import org.springframework.boot.web.servlet.FilterRegistrationBean;
/**
* @author raozihao, mageekchiu
* @author <a href="mailto:zihaorao@gmail.com">Steve</a>
* @author <a href="mailto:1262917629@qq.com">RovingSea</a>
*/
public final class URIRegister {
@ -46,68 +47,85 @@ public final class URIRegister {
private URIRegister() {
}
public static void collectUris(List<FilterRegistrationBean> filterRegistrationBeans) {
if (CollectionUtils.isNotEmpty(filterRegistrationBeans)) {
List<ServiceMeta> serviceMetaList = new LinkedList<>();
boolean hasWildChar = false;
for (FilterRegistrationBean filterRegistrationBean : filterRegistrationBeans) {
Filter filter = filterRegistrationBean.getFilter();
if (filter == null) {
continue;
}
if (filter instanceof CoreServiceFilter) {
Collection<String> urlPatterns = filterRegistrationBean
.getUrlPatterns();
for (String urlPattern : urlPatterns) {
if (MATCH_ALL.equalsIgnoreCase(urlPattern)) {
hasWildChar = true;
}
ServiceMeta serviceMeta = new ServiceMeta(urlPattern,
ResourceActiveType.UNIT_RESOURCE_TYPE);
serviceMetaList.add(serviceMeta);
}
}
else if (filter instanceof GlobalServiceFilter) {
Collection<String> urlPatterns = filterRegistrationBean
.getUrlPatterns();
for (String urlPattern : urlPatterns) {
if (MATCH_ALL.equalsIgnoreCase(urlPattern)) {
hasWildChar = true;
}
ServiceMeta serviceMeta = new ServiceMeta(urlPattern,
ResourceActiveType.CENTER_RESOURCE_TYPE);
serviceMetaList.add(serviceMeta);
}
}
else if (filter instanceof GeneralServiceFilter) {
Collection<String> urlPatterns = filterRegistrationBean
.getUrlPatterns();
for (String urlPattern : urlPatterns) {
if (MATCH_ALL.equalsIgnoreCase(urlPattern)) {
hasWildChar = true;
}
ServiceMeta serviceMeta = new ServiceMeta(urlPattern,
ResourceActiveType.NORMAL_RESOURCE_TYPE);
serviceMetaList.add(serviceMeta);
}
}
public static void collectUris(List<FilterRegistrationBean<? extends Filter>> beanList) {
if (CollectionUtils.isEmpty(beanList)) {
return;
}
List<ServiceMeta> serviceMetaList = new LinkedList<>();
boolean hasWildChar = false;
for (FilterRegistrationBean<? extends Filter> filterRegistrationBean : beanList) {
Filter filter = filterRegistrationBean.getFilter();
if (filter == null) {
continue;
}
if (CollectionUtils.isNotEmpty(serviceMetaList)) {
if (!hasWildChar) {
// 保证所有 service(app+uri) 都纳入管理,不然不好做缓存管理
ServiceMeta serviceMeta = new ServiceMeta(MATCH_ALL,
ResourceActiveType.NORMAL_RESOURCE_TYPE);
serviceMetaList.add(serviceMeta);
}
serviceMetaObject = new ServiceMetaObject();
Collections.sort(serviceMetaList);
serviceMetaObject.setServiceMetaList(serviceMetaList);
String meta = JSON.toJSONString(serviceMetaList);
serviceMetaObject.setMeta(meta);
String md5 = DigestUtils.md5Hex(meta.getBytes(StandardCharsets.UTF_8));
serviceMetaObject.setMd5OfList(md5);
Collection<String> urlPatterns = filterRegistrationBean.getUrlPatterns();
if (filter instanceof CoreServiceFilter) {
hasWildChar = collectServiceMetas(serviceMetaList, hasWildChar, urlPatterns,
ResourceActiveType.UNIT_RESOURCE_TYPE);
} else if (filter instanceof GlobalServiceFilter) {
hasWildChar = collectServiceMetas(serviceMetaList, hasWildChar, urlPatterns,
ResourceActiveType.CENTER_RESOURCE_TYPE);
} else if (filter instanceof GeneralServiceFilter) {
hasWildChar = collectServiceMetas(serviceMetaList, hasWildChar, urlPatterns,
ResourceActiveType.NORMAL_RESOURCE_TYPE);
}
}
if (CollectionUtils.isEmpty(serviceMetaList)) {
return;
}
if (!hasWildChar) {
// 保证所有 service(app+uri) 都纳入管理,不然不好做缓存管理
collectServiceMeta(serviceMetaList, MATCH_ALL, ResourceActiveType.NORMAL_RESOURCE_TYPE);
}
initServiceMetaObject(serviceMetaList);
}
/**
* Initialize {@link #serviceMetaObject} based on {@link ServiceMeta} list.
* @param serviceMetaList list needed for initialization
*/
private static void initServiceMetaObject(List<ServiceMeta> serviceMetaList) {
serviceMetaObject = new ServiceMetaObject();
Collections.sort(serviceMetaList);
serviceMetaObject.setServiceMetaList(serviceMetaList);
String meta = JSON.toJSONString(serviceMetaList);
serviceMetaObject.setMeta(meta);
String md5 = DigestUtils.md5Hex(meta.getBytes(StandardCharsets.UTF_8));
serviceMetaObject.setMd5OfList(md5);
}
/**
* Collect {@link ServiceMeta} into the given <i>serviceMetaList</i> according to
* each item of the given <i>urlPatterns</i> and the given <i>resourceActiveType</i>,
* finally determine whether <i>hasWildChar</i> is a new wildChar.
* @param serviceMetaList extended list
* @param hasWildChar keyword to be determined
* @param urlPatterns looped list
* @param resourceActiveType attribute of {@link ServiceMeta}
* @return is new wildChar
*/
private static boolean collectServiceMetas(List<ServiceMeta> serviceMetaList, boolean hasWildChar,
Collection<String> urlPatterns, String resourceActiveType) {
for (String urlPattern : urlPatterns) {
if (MATCH_ALL.equalsIgnoreCase(urlPattern)) {
hasWildChar = true;
}
collectServiceMeta(serviceMetaList, urlPattern, resourceActiveType);
}
return hasWildChar;
}
/**
* Collect {@link ServiceMeta} into the given <i>serviceMetaList</i> according to
* the given <i>urlPattern</i> and the given <i>resourceActiveType</i>.
* @param serviceMetaList extended list
* @param urlPattern attribute of {@link ServiceMeta}
* @param resourceActiveType attribute of {@link ServiceMeta}
*/
private static void collectServiceMeta(List<ServiceMeta> serviceMetaList, String urlPattern,
String resourceActiveType) {
ServiceMeta serviceMeta = new ServiceMeta(urlPattern, resourceActiveType);
serviceMetaList.add(serviceMeta);
}
public static ServiceMetaObject getServiceMetaObject() {

Loading…
Cancel
Save