-
Notifications
You must be signed in to change notification settings - Fork 24
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
1 parent
5e0fe4c
commit 9ef74e3
Showing
14 changed files
with
319 additions
and
214 deletions.
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
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
60 changes: 0 additions & 60 deletions
60
...re-framework/src/main/java/com/jd/live/agent/core/bootstrap/config/ConfigEnvSupplier.java
This file was deleted.
Oops, something went wrong.
124 changes: 124 additions & 0 deletions
124
...ore-framework/src/main/java/com/jd/live/agent/core/bootstrap/env/AbstractEnvSupplier.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,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); | ||
} | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
...ramework/src/main/java/com/jd/live/agent/core/bootstrap/env/config/ConfigEnvSupplier.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,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); | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...ramework/src/main/java/com/jd/live/agent/core/bootstrap/env/spring/SpringEnvSupplier.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,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); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.