Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持获取自定义的 Agent 启动参数 #163

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.IOException;
import java.lang.instrument.Instrumentation;
import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -371,43 +372,50 @@ private static String getPropertiesFilePath(final Map<String, String> featureMap
);
}

// 如果featureMap中有对应的key值,则将featureMap中的[K,V]对合并到featureSB中
private static void appendFromFeatureMap(final StringBuilder featureSB,
final Map<String, String> featureMap,
final String key,
final String defaultValue) {
if (featureMap.containsKey(key)) {
featureSB.append(format("%s=%s;", key, getDefault(featureMap, key, defaultValue)));
/**
* 将featureMap中的[K,V]对转换为featureString
*/
private static String toFeatureString(final Map<String, String> featureMap) {
// 若有设置 home 则使用,没有则使用默认
String sandboxHome = featureMap == null || !featureMap.containsKey(KEY_SANDBOX_HOME)
? DEFAULT_SANDBOX_HOME : featureMap.get(KEY_SANDBOX_HOME);
// 保留 featureMap 中的所有配置项
Map<String, String> map = copyMap(featureMap);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

通过一个 map 来存放,最后再统一序列化,这样代码更加清晰,更易于维护。

map.put(KEY_SANDBOX_HOME, sandboxHome);
map.put("cfg", getSandboxCfgPath(sandboxHome));
map.put("system_module", getSandboxModulePath(sandboxHome));
map.put("mode", LAUNCH_MODE);
map.put("sandbox_home", sandboxHome);
map.put("user_module", SANDBOX_USER_MODULE_PATH);
map.put("provider", getSandboxProviderPath(sandboxHome));
map.put("namespace", getNamespace(featureMap));
// 设置默认 ip
if (!map.containsKey(KEY_SERVER_IP)) {
map.put(KEY_SERVER_IP, DEFAULT_IP);
}
// 设置默认 port
if (!map.containsKey(KEY_SERVER_PORT)) {
map.put(KEY_SERVER_PORT, DEFAULT_PORT);
}
// 将 map 按 key=value;key=value 的格式序列化
final StringBuilder featureSB = new StringBuilder(";");
for (Map.Entry<String, String> entry : map.entrySet()) {
featureSB.append(entry.getKey()).append("=").append(entry.getValue()).append(";");
}
return featureSB.toString();
}

// 将featureMap中的[K,V]对转换为featureString
private static String toFeatureString(final Map<String, String> featureMap) {
final String sandboxHome = getSandboxHome(featureMap);
final StringBuilder featureSB = new StringBuilder(
format(
";cfg=%s;system_module=%s;mode=%s;sandbox_home=%s;user_module=%s;provider=%s;namespace=%s;",
getSandboxCfgPath(sandboxHome),
// SANDBOX_CFG_PATH,
getSandboxModulePath(sandboxHome),
// SANDBOX_MODULE_PATH,
LAUNCH_MODE,
sandboxHome,
// SANDBOX_HOME,
SANDBOX_USER_MODULE_PATH,
getSandboxProviderPath(sandboxHome),
// SANDBOX_PROVIDER_LIB_PATH,
getNamespace(featureMap)
)
);

// 合并IP(如有)
appendFromFeatureMap(featureSB, featureMap, KEY_SERVER_IP, DEFAULT_IP);

// 合并PORT(如有)
appendFromFeatureMap(featureSB, featureMap, KEY_SERVER_PORT, DEFAULT_PORT);

return featureSB.toString();
/**
* 拷贝一个 map
*/
private static Map<String, String> copyMap(Map<String, String> mapToCopy) {
Map<String, String> map = new HashMap<String, String>(mapToCopy != null ? mapToCopy.size() : 16);
if (mapToCopy != null && mapToCopy.size() > 0) {
for (Map.Entry<String, String> entry : mapToCopy.entrySet()) {
map.put(entry.getKey(), entry.getValue());
}
}
return map;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.alibaba.jvm.sandbox.api.Information;

import java.net.InetSocketAddress;
import java.util.Map;

/**
* 沙箱配置信息
Expand Down Expand Up @@ -102,8 +103,7 @@ public interface ConfigInfo {
* 沙箱事件对象池单个事件类型缓存最小数量,{@link #isEnableEventPool()}==true时候有意义
*
* @return 单个事件类型缓存最小数量
* @deprecated 已经被废弃,推荐使用{@link #getEventPoolMaxIdlePerEvent()}
* @deprecated 后续不再支持事件池
* @deprecated 已经被废弃,后续不再支持事件池,推荐使用{@link #getEventPoolMaxIdlePerEvent()}
*/
@Deprecated
int getEventPoolKeyMin();
Expand All @@ -112,8 +112,7 @@ public interface ConfigInfo {
* 沙箱事件对象池单个事件类型缓存最大数量,{@link #isEnableEventPool()}==true时候有意义
*
* @return 单个事件类型缓存最大数量
* @deprecated 已被废弃,推荐使用{@link #getEventPoolMaxTotalPerEvent()}
* @deprecated 后续不再支持事件池
* @deprecated 已被废弃,后续不再支持事件池,推荐使用{@link #getEventPoolMaxTotalPerEvent()}
*/
@Deprecated
int getEventPoolKeyMax();
Expand All @@ -122,8 +121,7 @@ public interface ConfigInfo {
* 沙箱事件对象池所有事件类型缓存最大总数量,{@link #isEnableEventPool()}==true时候有意义
*
* @return 所有事件类型缓存最大总数量
* @deprecated 已被废弃,推荐使用{@link #getEventPoolMaxTotal()}
* @deprecated 后续不再支持事件池
* @deprecated 已被废弃,后续不再支持事件池,推荐使用{@link #getEventPoolMaxTotal()}
*/
@Deprecated
int getEventPoolTotal();
Expand Down Expand Up @@ -184,4 +182,18 @@ public interface ConfigInfo {
*/
String getVersion();

/**
* 获取 Agent 的启动参数
*
* @param key 键
* @return 值
*/
String getProperty(String key);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

提供两个获取启动参数的方法,一个是获取单个的,一个是获取全部的。


/**
* 获取所有的 Agent 启动参数
*
* @return 所有的 Agent 启动参数
*/
Map<String, String> getProperties();
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@ public synchronized File[] getUserModuleLibFiles() {
final Collection<File> foundModuleJarFiles = new LinkedHashSet<File>();
for (final String path : getUserModuleLibPaths()) {
final File fileOfPath = new File(path);
if(fileOfPath.isDirectory()) {
if (fileOfPath.isDirectory()) {
foundModuleJarFiles.addAll(FileUtils.listFiles(new File(path), new String[]{"jar"}, false));
} else {
if(StringUtils.endsWithIgnoreCase(fileOfPath.getPath(), ".jar")) {
if (StringUtils.endsWithIgnoreCase(fileOfPath.getPath(), ".jar")) {
foundModuleJarFiles.add(fileOfPath);
}
}
Expand Down Expand Up @@ -280,4 +280,23 @@ public String getProviderLibPath() {
return featureMap.get(KEY_PROVIDER_LIB_PATH);
}

/**
* 获取 Agent 的启动参数
*
* @param key 键
* @return 值
*/
public String getProperty(String key) {
return featureMap.get(key);
}

/**
* 获取所有的 Agent 启动参数
*
* @return Agent 的所有启动参数
*/
public Map<String, String> getProperties() {
return featureMap;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.util.Map;

/**
* 默认配置信息实现
Expand Down Expand Up @@ -132,4 +133,14 @@ public String getVersion() {
IOUtils.closeQuietly(is);
}
}

@Override
public String getProperty(String key) {
return cfg.getProperty(key);
}

@Override
public Map<String, String> getProperties() {
return cfg.getProperties();
}
}