下面列出了怎么用org.springframework.util.SystemPropertyUtils的API类实例代码及写法,或者点击链接到github查看源代码。
public Set<Class<?>> doScan(String basePackage) {
Set<Class<?>> classes = new HashSet<Class<?>>();
try {
String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + org.springframework.util.ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils.resolvePlaceholders(basePackage)) + "/**/*.class";
Resource[] resources = this.resourcePatternResolver.getResources(packageSearchPath);
for (int i = 0; i < resources.length; i++) {
Resource resource = resources[i];
if (resource.isReadable()) {
MetadataReader metadataReader = this.metadataReaderFactory.getMetadataReader(resource);
if ((includeFilters.size() == 0 && excludeFilters.size() == 0) || matches(metadataReader)) {
try {
classes.add(Class.forName(metadataReader.getClassMetadata().getClassName()));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
}
} catch (IOException ex) {
throw new BeanDefinitionStoreException("I/O failure during classpath scanning", ex);
}
return classes;
}
private String[] getResources(String packageName) {
try {
// 搜索资源
String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils.resolvePlaceholders(packageName)) + "/" + DEFAULT_RESOURCE_PATTERN;
Resource[] resources = resourcePatternResolver.getResources(packageSearchPath);
// 提取资源
Set<String> names = new HashSet<String>();
for (Resource resource : resources) {
if (!resource.isReadable()) {
continue;
}
// 判断是否静态资源
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource);
ClassMetadata classMetadata = metadataReader.getClassMetadata();
names.add(classMetadata.getClassName());
}
return names.toArray(new String[0]);
} catch (IOException exception) {
String message = "无法获取资源";
LOGGER.error(message, exception);
throw new StorageConfigurationException(message, exception);
}
}
private void initModuleConfiguration() {
if (!scanModuleConfigurations) {
this.moduleDefaultConfiguration = new ConcurrentMapConfiguration();
return;
}
HashMap<String, Object> base = new HashMap<>();
Set<Class<?>> types = reflections
.getTypesAnnotatedWith(PropertySource.class);
for (Class<?> type : types) {
PropertySource propertySource = type
.getAnnotation(PropertySource.class);
String[] propertiesFiles = propertySource.value();
for (String propertyFile : propertiesFiles) {
Properties properties = new Properties();
try (InputStream is = resourceLoader.getResource(SystemPropertyUtils.resolvePlaceholders(propertyFile))
.getInputStream()) {
properties.load(is);
LOGGER.debug("Initializing module properties from path " + propertyFile);
} catch (Exception e) {
BootstrapException.resourceLoadingFailed(propertyFile, e);
}
join(base, properties, propertyFile, propertiesFiles);
}
}
this.moduleDefaultConfiguration = new ConcurrentMapConfiguration(base);
}
public static OutputStream createFile(String path) {
try {
Path p = Paths.get(SystemPropertyUtils.resolvePlaceholders(path));
if (Files.exists(p)) {
Files.delete(p);
}
File file = new File(path);
if (!file.getParentFile().exists()) {
if (!file.getParentFile().mkdirs()) {
throw new RuntimeException("Unable to create parent file(s) " + file.getParent());
}
}
return Files.newOutputStream(p);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Finds all the classes which have a BeanTag or BeanTags annotation
*
* @param basePackage the package to start in
* @return classes which have BeanTag or BeanTags annotation
* @throws IOException
* @throws ClassNotFoundException
*/
protected static List<Class<?>> findTagClasses(String basePackage) throws IOException, ClassNotFoundException {
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resourcePatternResolver);
List<Class<?>> classes = new ArrayList<Class<?>>();
String resolvedBasePackage = ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils.resolvePlaceholders(
basePackage));
String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
resolvedBasePackage + "/" + "**/*.class";
Resource[] resources = resourcePatternResolver.getResources(packageSearchPath);
for (Resource resource : resources) {
if (resource.isReadable()) {
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource);
if (metadataReader != null && isBeanTag(metadataReader)) {
classes.add(Class.forName(metadataReader.getClassMetadata().getClassName()));
}
}
}
return classes;
}
public PropertySourcesPlaceholdersResolver(Iterable<PropertySource<?>> sources,
PropertyPlaceholderHelper helper) {
this.sources = sources;
this.helper = (helper != null) ? helper
: new PropertyPlaceholderHelper(SystemPropertyUtils.PLACEHOLDER_PREFIX,
SystemPropertyUtils.PLACEHOLDER_SUFFIX,
SystemPropertyUtils.VALUE_SEPARATOR, true);
}
private String[] getResources(String packageName) {
try {
// 搜索资源
String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils.resolvePlaceholders(packageName)) + "/" + DEFAULT_RESOURCE_PATTERN;
Resource[] resources = resourcePatternResolver.getResources(packageSearchPath);
// 提取资源
Set<String> names = new HashSet<String>();
String name = ResourceConfiguration.class.getName();
for (Resource resource : resources) {
if (!resource.isReadable()) {
continue;
}
// 判断是否静态资源
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource);
AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
if (!annotationMetadata.hasAnnotation(name)) {
continue;
}
ClassMetadata classMetadata = metadataReader.getClassMetadata();
names.add(classMetadata.getClassName());
}
return names.toArray(new String[0]);
} catch (IOException exception) {
String message = "无法获取资源";
logger.error(message, exception);
throw new StorageException(message, exception);
}
}
private String[] getResources(String packageName) {
try {
// 搜索资源
String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils.resolvePlaceholders(packageName)) + "/" + DEFAULT_RESOURCE_PATTERN;
Resource[] resources = resourcePatternResolver.getResources(packageSearchPath);
// 提取资源
Set<String> names = new HashSet<String>();
String document = Document.class.getName();
for (Resource resource : resources) {
if (!resource.isReadable()) {
continue;
}
// 判断是否静态资源
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource);
AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
if (!annotationMetadata.hasAnnotation(document)) {
continue;
}
ClassMetadata classMetadata = metadataReader.getClassMetadata();
names.add(classMetadata.getClassName());
}
return names.toArray(new String[0]);
} catch (IOException exception) {
String message = "无法获取资源";
LOGGER.error(message, exception);
throw new StorageConfigurationException(message, exception);
}
}
private String[] getResources(String packageName) {
try {
// 搜索资源
String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils.resolvePlaceholders(packageName)) + "/" + DEFAULT_RESOURCE_PATTERN;
Resource[] resources = resourcePatternResolver.getResources(packageSearchPath);
// 提取资源
Set<String> names = new HashSet<String>();
String entity = Entity.class.getName();
String persistent = Persistent.class.getName();
for (Resource resource : resources) {
if (!resource.isReadable()) {
continue;
}
// 判断是否静态资源
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource);
AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
if (!annotationMetadata.hasAnnotation(entity) && !annotationMetadata.hasAnnotation(persistent)) {
continue;
}
ClassMetadata classMetadata = metadataReader.getClassMetadata();
names.add(classMetadata.getClassName());
}
return names.toArray(new String[0]);
} catch (IOException exception) {
String message = "无法获取资源";
LOGGER.error(message, exception);
throw new StorageConfigurationException(message, exception);
}
}
private String[] getResources(String packageName) {
try {
// 搜索资源
String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils.resolvePlaceholders(packageName)) + "/" + DEFAULT_RESOURCE_PATTERN;
Resource[] resources = resourcePatternResolver.getResources(packageSearchPath);
// 提取资源
Set<String> names = new HashSet<String>();
String document = LuceneConfiguration.class.getName();
for (Resource resource : resources) {
if (!resource.isReadable()) {
continue;
}
// 判断是否静态资源
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource);
AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
if (!annotationMetadata.hasAnnotation(document)) {
continue;
}
ClassMetadata classMetadata = metadataReader.getClassMetadata();
names.add(classMetadata.getClassName());
}
return names.toArray(new String[0]);
} catch (IOException exception) {
String message = "无法获取资源";
LOGGER.error(message, exception);
throw new StorageConfigurationException(message, exception);
}
}
private String[] getResources(String packageName) {
try {
// 搜索资源
String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils.resolvePlaceholders(packageName)) + "/" + DEFAULT_RESOURCE_PATTERN;
Resource[] resources = resourcePatternResolver.getResources(packageSearchPath);
// 提取资源
Set<String> names = new HashSet<String>();
String name = CacheConfiguration.class.getName();
for (Resource resource : resources) {
if (!resource.isReadable()) {
continue;
}
// 判断是否静态资源
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource);
AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
if (!annotationMetadata.hasAnnotation(name)) {
continue;
}
ClassMetadata classMetadata = metadataReader.getClassMetadata();
names.add(classMetadata.getClassName());
}
return names.toArray(new String[0]);
} catch (IOException exception) {
String message = "无法获取资源";
LOGGER.error(message, exception);
throw new CacheConfigurationException(message, exception);
}
}
/**
* 获取指定包下的静态资源对象
*
* @param packageName 包名
* @return
* @throws IOException
*/
private String[] getResources(String packageName) {
try {
// 搜索资源
String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils.resolvePlaceholders(packageName)) + "/" + DEFAULT_RESOURCE_PATTERN;
Resource[] resources = this.resourcePatternResolver.getResources(packageSearchPath);
// 提取资源
Set<String> names = new HashSet<String>();
String name = ProtocolConfiguration.class.getName();
for (Resource resource : resources) {
if (!resource.isReadable()) {
continue;
}
// 判断是否静态资源
MetadataReader metadataReader = this.metadataReaderFactory.getMetadataReader(resource);
ClassMetadata classMetadata = metadataReader.getClassMetadata();
AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
if (annotationMetadata.hasAnnotation(name)) {
names.add(classMetadata.getClassName());
} else {
String[] interfaceNames = classMetadata.getInterfaceNames();
for (String interfaceName : interfaceNames) {
metadataReader = this.metadataReaderFactory.getMetadataReader(interfaceName);
annotationMetadata = metadataReader.getAnnotationMetadata();
if (annotationMetadata.hasAnnotation(name)) {
names.add(classMetadata.getClassName());
break;
}
}
}
}
return names.toArray(new String[0]);
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}
private String[] getResources(String packageName) {
try {
// 搜索资源
String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils.resolvePlaceholders(packageName)) + "/" + DEFAULT_RESOURCE_PATTERN;
Resource[] resources = resourcePatternResolver.getResources(packageSearchPath);
// 提取资源
Set<String> names = new HashSet<String>();
String name = CommunicationModule.class.getName();
for (Resource resource : resources) {
if (!resource.isReadable()) {
continue;
}
// 判断是否静态资源
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource);
AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
if (!annotationMetadata.hasAnnotation(name)) {
continue;
}
ClassMetadata classMetadata = metadataReader.getClassMetadata();
names.add(classMetadata.getClassName());
}
return names.toArray(new String[0]);
} catch (IOException exception) {
String message = "无法获取资源";
LOGGER.error(message, exception);
throw new CommunicationConfigurationException(message, exception);
}
}
private Resource[] pathToResources(String path) {
String resourcePath = ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils.resolvePlaceholders(path));
String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + resourcePath + "/*.class";
try {
return resourcePatternResolver.getResources(packageSearchPath);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* Finds all the model classes and the model error class.
*
* @throws MojoExecutionException if a class couldn't be instantiated or an I/O error occurred.
*/
private void findModelClasses() throws MojoExecutionException
{
try
{
log.debug("Finding model classes.");
// Get the model classes as resources.
modelClasses = new HashSet<>();
// Loop through each model resource and add each one to the set of model classes.
for (Resource resource : ResourceUtils.getResources(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils.resolvePlaceholders(modelJavaPackage)) +
"/**/*.class"))
{
if (resource.isReadable())
{
MetadataReader metadataReader = new CachingMetadataReaderFactory(new PathMatchingResourcePatternResolver()).getMetadataReader(resource);
Class<?> clazz = Class.forName(metadataReader.getClassMetadata().getClassName());
modelClasses.add(clazz);
log.debug("Found model class \"" + clazz.getName() + "\".");
// If the model error class name is configured and matches this class, then hold onto it.
if (clazz.getSimpleName().equals(modelErrorClassName))
{
log.debug("Found model error class \"" + clazz.getName() + "\".");
modelErrorClass = clazz;
}
}
}
}
catch (IOException | ClassNotFoundException e)
{
throw new MojoExecutionException("Error finding model classes. Reason: " + e.getMessage(), e);
}
}
public Hasor build(Object parentObject, ApplicationContext applicationContext) throws IOException {
Hasor hasorBuild = (parentObject == null) ? Hasor.create() : Hasor.create(parentObject);
hasorBuild.parentClassLoaderWith(applicationContext.getClassLoader());
//
// make sure mainConfig
String config = this.mainConfig;
if (!StringUtils.isBlank(config)) {
config = SystemPropertyUtils.resolvePlaceholders(config);
Resource resource = StringUtils.isNotBlank(config) ? applicationContext.getResource(config) : null;
if (resource != null) {
hasorBuild.mainSettingWith(resource.getURI());
}
}
//
// merge Properties
if (this.envProperties != null) {
this.envProperties.forEach((k, v) -> {
hasorBuild.addVariable(k.toString(), v.toString());
});
}
if (this.refProperties != null) {
this.refProperties.forEach((k, v) -> {
hasorBuild.addVariable(k.toString(), v.toString());
});
}
if (this.customProperties != null) {
this.customProperties.forEach((k, v) -> {
hasorBuild.addVariable(k.toString(), v.toString());
});
}
//
// import Properties to Settings
if (this.useProperties) {
hasorBuild.importVariablesToSettings();
}
//
return hasorBuild.addModules(this.loadModules);
}
@SuppressWarnings({"unchecked", "rawtypes"})
private void initApplicationFileConfiguration() {
if (applicationPropertiesPath == null) {
LOGGER.debug("No client application properties to configure. Skipping...");
applicationFileConfiguration = new ConcurrentMapConfiguration();
return;
}
this.applicationFileConfiguration = new ConcurrentCompositeConfiguration();
String path = SystemPropertyUtils.resolvePlaceholders(applicationPropertiesPath);
LOGGER.debug("Configuring client application properties from path " + applicationPropertiesPath);
Map applicationProperties = new Properties();
if (SystemUtils.IS_OS_WINDOWS) {
if (path.startsWith("file://")) {
if (!path.startsWith("file:///")) {
path = path.replaceFirst(Pattern.quote("file://"), "file:///");
}
}
}
try (InputStream is = resourceLoader.getResource(path).getInputStream()) {
((Properties) applicationProperties).load(is);
} catch (Exception e) {
BootstrapException.resourceLoadingFailed(path, applicationPropertiesPath, e);
}
Map environmentApplicationProperties = getEnvironmentSpecificProperties(path);
if (environmentApplicationProperties != null) {
((ConcurrentCompositeConfiguration) this.applicationFileConfiguration).addConfiguration(new ConcurrentMapConfiguration(environmentApplicationProperties));
}
((ConcurrentCompositeConfiguration) this.applicationFileConfiguration).addConfiguration(new ConcurrentMapConfiguration(applicationProperties));
if (applicationFileConfiguration.containsKey(BootstrapConfigKeys.PUBLISH_DEFAULTS_KEY.getPropertyName())) {
this.publishDefaults = applicationFileConfiguration.getBoolean(BootstrapConfigKeys.PUBLISH_DEFAULTS_KEY.getPropertyName());
}
}
@SuppressWarnings("unchecked")
public static void writePropertiesToFile(String path, Set<Path> filesCreated, SimpleEntry<String, String>... entries) {
Properties properties = new Properties();
for (SimpleEntry<String, String> entry : entries) {
properties.put(entry.getKey(), entry.getValue());
}
Path p = Paths.get(SystemPropertyUtils.resolvePlaceholders(path));
try (OutputStream os = createFile(p.toString())) {
properties.store(os, "test properties");
filesCreated.add(p);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
static boolean isTransport(ClassNode node, String type) {
for (AnnotationNode annotationNode : node.getAnnotations()) {
String annotation = "EnableBinding";
if (PatternMatchUtils.simpleMatch(annotation,
annotationNode.getClassNode().getName())) {
Expression expression = annotationNode.getMembers().get("transport");
String transport = expression == null ? "rabbit" : expression.getText();
if (transport != null) {
transport = SystemPropertyUtils.resolvePlaceholders(transport);
}
return transport.equals(type);
}
}
return false;
}
public static String expandEnvironmentVars(String text) {
if (text == null) {
return text;
}
return SystemPropertyUtils.resolvePlaceholders(text);
}
protected String resolveBasePackage(String basePackage) {
return ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils.resolvePlaceholders(basePackage));
}
private static String resolveBasePackage(String basePackage) {
return ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils.resolvePlaceholders(
basePackage));
}
private String resolveBasePackage(String basePackage) {
return ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils.resolvePlaceholders(basePackage));
}
/**
* Resolve the specified base package into a pattern specification for
* the package search path.
* <p>The default implementation resolves placeholders against system properties,
* and converts a "."-based package path to a "/"-based resource path.
* @param basePackage the base package as specified by the user
* @return the pattern specification to be used for package searching
*/
protected String resolveBasePackage(String basePackage) {
return ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils.resolvePlaceholders(basePackage));
}
/**
* Resolve the specified base package into a pattern specification for
* the package search path.
* <p>The default implementation resolves placeholders against system properties,
* and converts a "."-based package path to a "/"-based resource path.
* @param basePackage the base package as specified by the user
* @return the pattern specification to be used for package searching
*/
protected String resolveBasePackage(String basePackage) {
return ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils.resolvePlaceholders(basePackage));
}
/**
* 转换成包名,ant风格
* @author Frodez
* @date 2019-12-14
*/
public static String getPackagePath(String pattern) {
String packagePath = ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils.resolvePlaceholders(pattern));
return StrUtil.concat(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX, packagePath, DefStr.CLASS_SUFFIX);
}
/**
* Resolve the package name to a canonical path, in case there any place holders.
*
* @param basePackage the base package to be scanned.
* @return the canonical version of the package name.
*/
private static String resolveBasePackage(String basePackage) {
return ClassUtils.convertClassNameToResourcePath(
SystemPropertyUtils.resolvePlaceholders(basePackage));
}