Skip to content

Commit

Permalink
Support load resource from WAR
Browse files Browse the repository at this point in the history
  • Loading branch information
hexiaofeng committed May 30, 2024
1 parent 5e0fe4c commit 9ef74e3
Show file tree
Hide file tree
Showing 14 changed files with 319 additions and 214 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
*
* @return The type (or name) of the extension.
*/
String value() default "";
String[] value() default "";

/**
* Specifies the provider or vendor of the extension. This can be used for informational purposes or to distinguish
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public interface ObjectParser {
*/
String YAML = "yaml";

/**
* Represents the identifier for YAML format.
*/
String YML = "yml";

/**
* Represents the identifier for JSON format.
*/
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Copyright © ${year} ${owner} (${email})
*
* Licensed 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 com.jd.live.agent.core.bootstrap.env;

import com.jd.live.agent.bootstrap.classloader.ResourcerType;
import com.jd.live.agent.bootstrap.logger.Logger;
import com.jd.live.agent.bootstrap.logger.LoggerFactory;
import com.jd.live.agent.core.bootstrap.EnvSupplier;
import com.jd.live.agent.core.bootstrap.env.config.ConfigEnvSupplier;
import com.jd.live.agent.core.inject.annotation.Inject;
import com.jd.live.agent.core.inject.annotation.InjectLoader;
import com.jd.live.agent.core.parser.ConfigParser;
import com.jd.live.agent.core.util.StringUtils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
* AbstractEnvSupplier is an abstract class that provides a mechanism to load environment config
* from various resources using different parsers.
*/
public abstract class AbstractEnvSupplier implements EnvSupplier {

private static final Logger logger = LoggerFactory.getLogger(ConfigEnvSupplier.class);

private final String[] resources;

private final static String[] PREFIX = new String[]{"", "BOOT-INF/classes/", "WEB-INF/classes/"};

@Inject
@InjectLoader(ResourcerType.CORE_IMPL)
private Map<String, ConfigParser> parsers;

/**
* Constructs an AbstractEnvSupplier with the specified resources.
*
* @param resources the resource paths to load configuration from.
*/
public AbstractEnvSupplier(String... resources) {
this.resources = resources;
}

/**
* Retrieves the configuration by iterating through the resources and prefixes.
*
* @return a map containing the configuration, or null if no configuration is found.
*/
protected Map<String, Object> loadConfigs() {
if (resources != null) {
for (String resource : resources) {
for (String prefix : PREFIX) {
Map<String, Object> result = loadConfigs(resource, prefix);
if (result != null) {
return result;
}
}
}
}
return null;
}

/**
* Retrieves the configuration for a specific resource and prefix.
*
* @param resource the resource path.
* @param prefix the prefix to be added to the resource path.
* @return a map containing the configuration, or null if no configuration is found.
*/
protected Map<String, Object> loadConfigs(String resource, String prefix) {
resource = prefix != null && !prefix.isEmpty() && !resource.startsWith(prefix)
? StringUtils.url(prefix, resource)
: resource;
int pos = resource.lastIndexOf('.');
String ext = pos > 0 ? resource.substring(pos + 1) : "";
ConfigParser parser = parsers.get(ext);
if (parser != null) {
URL url = ClassLoader.getSystemClassLoader().getResource(resource);
if (url != null) {
try {
Map<String, Object> result = parse(url.openStream(), parser);
logger.info("Successfully load config from " + url);
return result;
} catch (Throwable e) {
logger.warn("Failed to load config from " + url, e);
return new HashMap<>();
}
}
}
return null;
}

/**
* Parses the input stream using the specified parser.
*
* @param inputStream the input stream to parse.
* @param parser the parser to use for parsing the input stream.
* @return a map containing the parsed configuration.
* @throws Exception if an error occurs during parsing.
*/
protected Map<String, Object> parse(InputStream inputStream, ConfigParser parser) throws Exception {
try (Reader reader = new BufferedReader(new InputStreamReader(inputStream))) {
return parser.parse(reader);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright © ${year} ${owner} (${email})
*
* Licensed 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 com.jd.live.agent.core.bootstrap.env.config;

import com.jd.live.agent.core.bootstrap.env.AbstractEnvSupplier;
import com.jd.live.agent.core.extension.annotation.Extension;
import com.jd.live.agent.core.inject.annotation.Injectable;

import java.util.Map;

@Injectable
@Extension("ConfigEnvSupplier")
public class ConfigEnvSupplier extends AbstractEnvSupplier {

private static final String RESOURCE_LIVE_AGENT_PROPERTIES = "live-agent.properties";

public ConfigEnvSupplier() {
super(RESOURCE_LIVE_AGENT_PROPERTIES);
}

@Override
public void process(Map<String, Object> env) {
Map<String, Object> map = loadConfigs();
if (map != null) {
map.forEach(env::putIfAbsent);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright © ${year} ${owner} (${email})
*
* Licensed 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 com.jd.live.agent.core.bootstrap.env.spring;

import com.jd.live.agent.core.bootstrap.env.AbstractEnvSupplier;
import com.jd.live.agent.core.extension.annotation.Extension;
import com.jd.live.agent.core.inject.annotation.Injectable;
import com.jd.live.agent.core.instance.Application;
import com.jd.live.agent.core.util.type.ValuePath;

import java.util.Map;

@Injectable
@Extension("SpringEnvSupplier")
public class SpringEnvSupplier extends AbstractEnvSupplier {

private static final String KEY_SPRING_APPLICATION_NAME = "spring.application.name";

private static final String KEY_SPRING_APPLICATION_NAME1 = "spring-application-name";

private static final String RESOURCE_SPRINGBOOT_APPLICATION_PROPERTIES = "application.properties";

private static final String RESOURCE_SPRINGBOOT_APPLICATION_YAML = "application.yaml";

private static final String RESOURCE_SPRINGBOOT_APPLICATION_YML = "application.yml";

private static final ValuePath APP_PATH = new ValuePath(KEY_SPRING_APPLICATION_NAME);

public SpringEnvSupplier() {
super(RESOURCE_SPRINGBOOT_APPLICATION_PROPERTIES,
RESOURCE_SPRINGBOOT_APPLICATION_YAML,
RESOURCE_SPRINGBOOT_APPLICATION_YML);
}

@Override
public void process(Map<String, Object> env) {
if (!env.containsKey(Application.KEY_APPLICATION_NAME)) {
Map<String, Object> configs = loadConfigs();
if (configs != null) {
String name = (String) configs.get(KEY_SPRING_APPLICATION_NAME);
name = name == null ? (String) configs.get(KEY_SPRING_APPLICATION_NAME1) : name;
name = name == null ? (String) APP_PATH.get(configs) : name;
if (name != null && !name.isEmpty()) {
env.put(Application.KEY_APPLICATION_NAME, name);
}
}
}
}
}
Loading

0 comments on commit 9ef74e3

Please sign in to comment.