-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
255 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,13 @@ | ||
plugins { | ||
id 'java' | ||
id 'java-library' | ||
} | ||
|
||
dependencies { | ||
compileOnly 'org.projectlombok:lombok' | ||
annotationProcessor 'org.projectlombok:lombok' | ||
testCompileOnly 'org.projectlombok:lombok' | ||
testAnnotationProcessor 'org.projectlombok:lombok' | ||
|
||
api 'com.fasterxml.jackson.core:jackson-databind' | ||
} |
44 changes: 44 additions & 0 deletions
44
pangu-common/src/main/java/com/yuebaix/pangu/common/util/JacksonUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.yuebaix.pangu.common.util; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import lombok.SneakyThrows; | ||
|
||
public final class JacksonUtil { | ||
private static final ObjectMapper objectMapperHolder; | ||
static { | ||
ObjectMapper ob = new ObjectMapper(); | ||
//允许序列化空POJO | ||
ob.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); | ||
//把时间按照字符串输出 | ||
ob.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); | ||
//POJO中的null值不输出 | ||
ob.setSerializationInclusion(JsonInclude.Include.NON_NULL); | ||
//在遇到未知属性的时候不抛出异常 | ||
ob.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); | ||
objectMapperHolder = ob; | ||
} | ||
private JacksonUtil() {} | ||
|
||
public static ObjectMapper getObjectMapperHolder() { | ||
return objectMapperHolder; | ||
} | ||
|
||
@SneakyThrows | ||
public static String write(Object obj) { | ||
return getObjectMapperHolder().writeValueAsString(obj); | ||
} | ||
|
||
@SneakyThrows | ||
public static <T> T read(String cnt, Class<T> clazz) { | ||
return getObjectMapperHolder().readValue(cnt, clazz); | ||
} | ||
|
||
@SneakyThrows | ||
public static <T> T read(String cnt, TypeReference<T> typeRef) { | ||
return getObjectMapperHolder().readValue(cnt, typeRef); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
pangu-spring-boot-starter/src/main/java/com/yuebaix/pangu/support/PanGuScan.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package com.yuebaix.pangu.support; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.yuebaix.pangu.autoconfigure.common.PanGuStarterConst; | ||
import com.yuebaix.pangu.common.util.JacksonUtil; | ||
import com.yuebaix.pangu.core.PanGuCoreConst; | ||
import lombok.Data; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.context.EnvironmentAware; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.core.type.classreading.MetadataReader; | ||
import org.springframework.core.type.classreading.MetadataReaderFactory; | ||
import org.springframework.core.type.filter.TypeFilter; | ||
import org.springframework.util.CollectionUtils; | ||
import org.springframework.util.StringUtils; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* desc: a brief configurable PatternTypeFilter based on @ComponentScan | ||
* | ||
* usage: | ||
* | ||
* # 1.setting | ||
* # active config profile | ||
* pangu.scan.active=all | ||
* # config detail.profile name as key, json as value.first exclude then include. | ||
* pangu.scan.config={"all":{"exclude":[".*\.modulePkg\.pkg\.className"], "include":[".*\.modulePkg\.pkg\..*"]}} | ||
* | ||
* # 2.code (better right above @SpringBootApplication) | ||
* @ComponentScan( | ||
* useDefaultFilters = false, | ||
* includeFilters = { | ||
* @ComponentScan.Filter(type = FilterType.CUSTOM, classes = PanGuScan.IncludeTypeFilter.class) | ||
* }, | ||
* excludeFilters = { | ||
* @ComponentScan.Filter(type = FilterType.CUSTOM, classes = PanGuScan.ExcludeTypeFilter.class) | ||
* } | ||
* ) | ||
* @SpringBootApplication | ||
* public class App { | ||
* public static void main(String[] args) { | ||
* SpringApplication.run(App.class, args); | ||
* } | ||
* } | ||
*/ | ||
@Slf4j | ||
public abstract class PanGuScan implements EnvironmentAware { | ||
private static final int CHOICE_EXCLUDE = 0; | ||
private static final int CHOICE_INCLUDE = 1; | ||
|
||
private String activeProfile; | ||
private PanGuScanTypeFilterPatternConfig activePatternConfig; | ||
|
||
@Override | ||
public void setEnvironment(Environment environment) { | ||
String active = environment.getProperty(PanGuStarterConst.PAN_GU_SCAN_ACTIVE); | ||
if (!StringUtils.isEmpty(active)) { | ||
String config = environment.getProperty(PanGuStarterConst.PAN_GU_SCAN_CONFIG); | ||
if (!StringUtils.isEmpty(active)) { | ||
Map<String, PanGuScanTypeFilterConfig> configProfiles = JacksonUtil.read(config, new TypeReference<Map<String, PanGuScanTypeFilterConfig>>(){}); | ||
PanGuScanTypeFilterConfig activeConfig = configProfiles.get(active); | ||
if (activeConfig != null) { | ||
PanGuScanTypeFilterPatternConfig activePatternConfig = fromConfig(activeConfig); | ||
this.activeProfile = active; | ||
this.activePatternConfig = activePatternConfig; | ||
log.debug(PanGuCoreConst.PAN_GU_TRACE_PREFIX + "PanGuScan " + this.getClass().getSimpleName() + | ||
" Initialized | active:{} | config:{}", active, JacksonUtil.write(activeConfig)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
protected boolean matchInternal(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory, int choice) { | ||
String className = metadataReader.getClassMetadata().getClassName(); | ||
if (CHOICE_EXCLUDE == choice) { | ||
for (Pattern pattern : activePatternConfig.getExcludePattern()) { | ||
if (isMatch(pattern, className)) { | ||
log.debug(PanGuCoreConst.PAN_GU_TRACE_PREFIX + "PanGuScan Exclude | active:{} | pattern:{} | className:{}", | ||
activeProfile, pattern.pattern(), className); | ||
return true; | ||
} | ||
} | ||
} else if (CHOICE_INCLUDE == choice) { | ||
for (Pattern pattern : activePatternConfig.getIncludePattern()) { | ||
if (isMatch(pattern, className)) { | ||
log.debug(PanGuCoreConst.PAN_GU_TRACE_PREFIX + "PanGuScan Include | active:{} | pattern:{} | className:{}", | ||
activeProfile, pattern.pattern(), className); | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private boolean isMatch(Pattern pattern, CharSequence content) { | ||
if (content == null || pattern == null) { | ||
return false; | ||
} | ||
return pattern.matcher(content).matches(); | ||
} | ||
|
||
private PanGuScanTypeFilterPatternConfig fromConfig(PanGuScanTypeFilterConfig config) { | ||
PanGuScanTypeFilterPatternConfig patternConfig = new PanGuScanTypeFilterPatternConfig(); | ||
if (config != null) { | ||
List<String> exclude = config.getExclude(); | ||
patternConfig.setExcludePattern(strToPattern(exclude)); | ||
List<String> include = config.getInclude(); | ||
patternConfig.setIncludePattern(strToPattern(include)); | ||
} | ||
return patternConfig; | ||
} | ||
|
||
private List<Pattern> strToPattern(List<String> strList) { | ||
List<Pattern> patternList = Collections.emptyList(); | ||
if (!CollectionUtils.isEmpty(strList)) { | ||
patternList = strList.stream().map(Pattern::compile).collect(Collectors.toList()); | ||
} | ||
return patternList; | ||
} | ||
|
||
@Data | ||
private static final class PanGuScanTypeFilterConfig { | ||
private List<String> include; | ||
private List<String> exclude; | ||
} | ||
|
||
@Data | ||
private static final class PanGuScanTypeFilterPatternConfig { | ||
private List<Pattern> includePattern; | ||
private List<Pattern> excludePattern; | ||
} | ||
|
||
public static final class ExcludeTypeFilter extends PanGuScan implements TypeFilter { | ||
@Override | ||
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { | ||
return matchInternal(metadataReader, metadataReaderFactory, CHOICE_EXCLUDE); | ||
} | ||
} | ||
|
||
public static final class IncludeTypeFilter extends PanGuScan implements TypeFilter { | ||
@Override | ||
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { | ||
return matchInternal(metadataReader, metadataReaderFactory, CHOICE_INCLUDE); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
pangu-test/src/main/java/com/yuebaix/pangu/test/PanGuTestApp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
pangu-test/src/main/java/com/yuebaix/pangu/test/controller/demo/DemoController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.yuebaix.pangu.test.controller.demo; | ||
|
||
import com.yuebaix.pangu.common.util.JacksonUtil; | ||
import com.yuebaix.pangu.web.base.BaseReq; | ||
import com.yuebaix.pangu.web.base.BaseResp; | ||
import io.swagger.annotations.ApiOperation; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.Collections; | ||
|
||
@RestController | ||
@RequestMapping("/demo/demo") | ||
public class DemoController { | ||
@ApiOperation("示例") | ||
@GetMapping("/check") | ||
public BaseResp check(BaseReq req) { | ||
return BaseResp.success(JacksonUtil.write(Collections.singletonMap("say", "demo"))); | ||
} | ||
} |
5 changes: 4 additions & 1 deletion
5
pangu-test/src/main/java/com/yuebaix/pangu/test/controller/demo/TestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,21 @@ | ||
package com.yuebaix.pangu.test.controller.demo; | ||
|
||
import com.yuebaix.pangu.common.util.JacksonUtil; | ||
import com.yuebaix.pangu.web.base.BaseReq; | ||
import com.yuebaix.pangu.web.base.BaseResp; | ||
import io.swagger.annotations.ApiOperation; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.Collections; | ||
|
||
@RestController | ||
@RequestMapping("/demo/test") | ||
public class TestController { | ||
@ApiOperation("测试") | ||
@GetMapping("/check") | ||
public BaseResp check(BaseReq req) { | ||
return BaseResp.success(); | ||
return BaseResp.success(JacksonUtil.write(Collections.singletonMap("say", "test"))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters