Skip to content

Commit 802baca

Browse files
committed
Add module enabled packages scan filter.
1 parent ce371be commit 802baca

File tree

8 files changed

+336
-0
lines changed

8 files changed

+336
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 1999-2023 Alibaba Group Holding Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.alibaba.nacos.config.server.filter;
18+
19+
import com.alibaba.nacos.common.utils.StringUtils;
20+
import com.alibaba.nacos.config.server.Config;
21+
import com.alibaba.nacos.sys.env.EnvUtil;
22+
import com.alibaba.nacos.sys.filter.NacosPackageExcludeFilter;
23+
24+
import java.util.Set;
25+
26+
import static com.alibaba.nacos.sys.env.EnvUtil.FUNCTION_MODE_CONFIG;
27+
28+
/**
29+
* Config module enabled filter by spring packages scan.
30+
*
31+
* @author xiweng.yy
32+
*/
33+
public class ConfigEnabledFilter implements NacosPackageExcludeFilter {
34+
35+
@Override
36+
public String getResponsiblePackagePrefix() {
37+
return Config.class.getPackage().getName();
38+
}
39+
40+
@Override
41+
public boolean isExcluded(String className, Set<String> annotationNames) {
42+
String functionMode = EnvUtil.getFunctionMode();
43+
// When not specified config mode or specified all mode, the config module not start and load.
44+
if (StringUtils.isEmpty(functionMode)) {
45+
return false;
46+
}
47+
return !FUNCTION_MODE_CONFIG.equals(functionMode);
48+
}
49+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#
2+
# Copyright 1999-2023 Alibaba Group Holding Ltd.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
com.alibaba.nacos.config.server.filter.ConfigEnabledFilter
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 1999-2023 Alibaba Group Holding Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.alibaba.nacos.istio.config;
18+
19+
import com.alibaba.nacos.common.utils.StringUtils;
20+
import com.alibaba.nacos.istio.IstioApp;
21+
import com.alibaba.nacos.sys.env.EnvUtil;
22+
import com.alibaba.nacos.sys.filter.NacosPackageExcludeFilter;
23+
import org.slf4j.Logger;
24+
import org.slf4j.LoggerFactory;
25+
26+
import java.util.Set;
27+
28+
import static com.alibaba.nacos.sys.env.EnvUtil.FUNCTION_MODE_NAMING;
29+
30+
/**
31+
* Istio module enabled filter by spring packages scan.
32+
*
33+
* @author xiweng.yy
34+
*/
35+
public class IstioEnabledFilter implements NacosPackageExcludeFilter {
36+
37+
private static final Logger LOGGER = LoggerFactory.getLogger(IstioEnabledFilter.class);
38+
39+
private static final String ISTIO_ENABLED_KEY = "nacos.extension.naming.istio.enabled";
40+
41+
@Override
42+
public String getResponsiblePackagePrefix() {
43+
return IstioApp.class.getPackage().getName();
44+
}
45+
46+
@Override
47+
public boolean isExcluded(String className, Set<String> annotationNames) {
48+
String functionMode = EnvUtil.getFunctionMode();
49+
// When not specified naming mode or specified all mode, the naming module not start and load.
50+
if (isNamingDisabled(functionMode)) {
51+
LOGGER.warn("Istio module disabled because function mode is {}, and Istio depend naming module",
52+
functionMode);
53+
return true;
54+
}
55+
boolean istioDisabled = !EnvUtil.getProperty(ISTIO_ENABLED_KEY, Boolean.class, false);
56+
if (istioDisabled) {
57+
LOGGER.warn("Istio module disabled because set {} as false", ISTIO_ENABLED_KEY);
58+
}
59+
return istioDisabled;
60+
}
61+
62+
private boolean isNamingDisabled(String functionMode) {
63+
if (StringUtils.isEmpty(functionMode)) {
64+
return false;
65+
}
66+
return !FUNCTION_MODE_NAMING.equals(functionMode);
67+
}
68+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#
2+
# Copyright 1999-2023 Alibaba Group Holding Ltd.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
com.alibaba.nacos.istio.config.IstioEnabledFilter
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 1999-2023 Alibaba Group Holding Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.alibaba.nacos.naming.config;
18+
19+
import com.alibaba.nacos.common.utils.StringUtils;
20+
import com.alibaba.nacos.naming.NamingApp;
21+
import com.alibaba.nacos.sys.env.EnvUtil;
22+
import com.alibaba.nacos.sys.filter.NacosPackageExcludeFilter;
23+
24+
import java.util.Set;
25+
26+
import static com.alibaba.nacos.sys.env.EnvUtil.FUNCTION_MODE_NAMING;
27+
28+
/**
29+
* Naming module enabled filter by spring packages scan.
30+
*
31+
* @author xiweng.yy
32+
*/
33+
public class NamingEnabledFilter implements NacosPackageExcludeFilter {
34+
35+
@Override
36+
public String getResponsiblePackagePrefix() {
37+
return NamingApp.class.getPackage().getName();
38+
}
39+
40+
@Override
41+
public boolean isExcluded(String className, Set<String> annotationNames) {
42+
String functionMode = EnvUtil.getFunctionMode();
43+
// When not specified naming mode or specified all mode, the naming module not start and load.
44+
if (StringUtils.isEmpty(functionMode)) {
45+
return false;
46+
}
47+
return !FUNCTION_MODE_NAMING.equals(functionMode);
48+
}
49+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#
2+
# Copyright 1999-2023 Alibaba Group Holding Ltd.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
com.alibaba.nacos.naming.config.NamingEnabledFilter
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 1999-2023 Alibaba Group Holding Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.alibaba.nacos.sys.filter;
18+
19+
import java.util.Set;
20+
21+
/**
22+
* Nacos server module execute filter.
23+
*
24+
* @author xiweng.yy
25+
*/
26+
public interface NacosPackageExcludeFilter {
27+
28+
/**
29+
* Get the responsible module package prefix of filter.
30+
*
31+
* @return package prefix
32+
*/
33+
String getResponsiblePackagePrefix();
34+
35+
/**
36+
* According the class name and annotations to judge whether the class should be excluded by spring bean.
37+
*
38+
* @param className name of this class
39+
* @param annotationNames annotations of this class
40+
* @return {@code true} if should be excluded, otherwise {@code false}
41+
*/
42+
boolean isExcluded(String className, Set<String> annotationNames);
43+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 1999-2023 Alibaba Group Holding Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.alibaba.nacos.sys.filter;
18+
19+
import com.alibaba.nacos.common.spi.NacosServiceLoader;
20+
import org.slf4j.Logger;
21+
import org.slf4j.LoggerFactory;
22+
import org.springframework.boot.autoconfigure.SpringBootApplication;
23+
import org.springframework.core.type.classreading.MetadataReader;
24+
import org.springframework.core.type.classreading.MetadataReaderFactory;
25+
import org.springframework.core.type.filter.TypeFilter;
26+
27+
import java.io.IOException;
28+
import java.util.HashMap;
29+
import java.util.Map;
30+
import java.util.Set;
31+
32+
/**
33+
* Nacos server execute filter. To exclude some beans or features by config
34+
*
35+
* @author xiweng.yy
36+
*/
37+
public class NacosTypeExcludeFilter implements TypeFilter {
38+
39+
private static final Logger LOGGER = LoggerFactory.getLogger(NacosTypeExcludeFilter.class);
40+
41+
private final Map<String, NacosPackageExcludeFilter> packageExcludeFilters;
42+
43+
public NacosTypeExcludeFilter() {
44+
this.packageExcludeFilters = new HashMap<>(2);
45+
for (NacosPackageExcludeFilter each : NacosServiceLoader.load(NacosPackageExcludeFilter.class)) {
46+
packageExcludeFilters.put(each.getResponsiblePackagePrefix(), each);
47+
LOGGER.info("Load Nacos package exclude filter success, package prefix {}, filter {}",
48+
each.getResponsiblePackagePrefix(), each.getClass().getCanonicalName());
49+
}
50+
}
51+
52+
@Override
53+
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
54+
throws IOException {
55+
// If no exclude filters, all classes should be load.
56+
if (packageExcludeFilters.isEmpty()) {
57+
return false;
58+
}
59+
boolean isSpringBootApplication = metadataReader.getAnnotationMetadata()
60+
.hasAnnotation(SpringBootApplication.class.getCanonicalName());
61+
String className = metadataReader.getClassMetadata().getClassName();
62+
if (isSpringBootApplication) {
63+
LOGGER.info("Skip @SpringBootApplication annotation for class {} to avoid duplicate scan", className);
64+
return true;
65+
}
66+
for (Map.Entry<String, NacosPackageExcludeFilter> entry : packageExcludeFilters.entrySet()) {
67+
// If match the package exclude filter, judged by filter.
68+
if (className.startsWith(entry.getKey())) {
69+
Set<String> annotations = metadataReader.getAnnotationMetadata().getAnnotationTypes();
70+
return entry.getValue().isExcluded(className, annotations);
71+
}
72+
}
73+
// No match filter, load class
74+
return false;
75+
}
76+
}

0 commit comments

Comments
 (0)