Java源码示例:com.alibaba.jvm.sandbox.api.Information
示例1
@Command("detail")
public void detail(final Map<String, String> param,
final PrintWriter writer) throws ModuleException {
final String uniqueId = param.get("id");
if (StringUtils.isBlank(uniqueId)) {
// 如果参数不对,则认为找不到对应的沙箱模块,返回400
writer.println("id parameter was required.");
return;
}
final Module module = moduleManager.get(uniqueId);
if (null == module) {
writer.println(String.format("module[id=%s] is not existed.", uniqueId));
return;
}
final Information info = module.getClass().getAnnotation(Information.class);
final boolean isActivated = moduleManager.isActivated(info.id());
final int cCnt = moduleManager.cCnt(info.id());
final int mCnt = moduleManager.mCnt(info.id());
final File jarFile = moduleManager.getJarFile(info.id());
String sb = "" +
" ID : " + info.id() + "\n" +
" VERSION : " + info.version() + "\n" +
" AUTHOR : " + info.author() + "\n" +
"JAR_FILE : " + jarFile.getPath() + "\n" +
" STATE : " + (isActivated ? "ACTIVE" : "FROZEN") + "\n" +
" MODE : " + ArrayUtils.toString(info.mode()) + "\n" +
" CLASS : " + module.getClass().getName() + "\n" +
" LOADER : " + module.getClass().getClassLoader() + "\n" +
" cCnt : " + cCnt + "\n" +
" mCnt : " + mCnt;
output(writer, sb);
}
示例2
@Command("unload")
public void unload(final Map<String, String> param,
final PrintWriter writer) {
int total = 0;
final String idsStringPattern = getParamWithDefault(param, "ids", EMPTY);
for (final Module module : search(idsStringPattern)) {
final Information info = module.getClass().getAnnotation(Information.class);
try {
moduleManager.unload(info.id());
total++;
} catch (ModuleException me) {
logger.warn("unload module[id={};] occur error={}.", me.getUniqueId(), me.getErrorCode(), me);
}
}
output(writer, "total %s module unloaded.", total);
}
示例3
@Command("active")
public void active(final Map<String, String> param,
final PrintWriter writer) throws ModuleException {
int total = 0;
final String idsStringPattern = getParamWithDefault(param, "ids", EMPTY);
for (final Module module : search(idsStringPattern)) {
final Information info = module.getClass().getAnnotation(Information.class);
final boolean isActivated = moduleManager.isActivated(info.id());
if (!isActivated) {
try {
moduleManager.active(info.id());
total++;
} catch (ModuleException me) {
logger.warn("active module[id={};] occur error={}.", me.getUniqueId(), me.getErrorCode(), me);
}// try
} else {
total++;
}
}// for
output(writer, "total %s module activated.", total);
}
示例4
@Command("frozen")
public void frozen(final Map<String, String> param,
final PrintWriter writer) throws ModuleException {
int total = 0;
final String idsStringPattern = getParamWithDefault(param, "ids", EMPTY);
for (final Module module : search(idsStringPattern)) {
final Information info = module.getClass().getAnnotation(Information.class);
final boolean isActivated = moduleManager.isActivated(info.id());
if (isActivated) {
try {
moduleManager.frozen(info.id());
total++;
} catch (ModuleException me) {
logger.warn("frozen module[id={};] occur error={}.", me.getUniqueId(), me.getErrorCode(), me);
}
} else {
total++;
}
}
output(writer, "total %s module frozen.", total);
}
示例5
/**
* 获取沙箱的启动模式
* 默认按照ATTACH模式启动
*
* @return 沙箱的启动模式
*/
public Information.Mode getLaunchMode() {
if (isLaunchByAgentMode()) {
return Information.Mode.AGENT;
}
if (isLaunchByAttachMode()) {
return Information.Mode.ATTACH;
}
return Information.Mode.ATTACH;
}
示例6
private Collection<Module> search(final String idsStringPattern) {
final Collection<Module> foundModules = new ArrayList<Module>();
for (Module module : moduleManager.list()) {
final Information moduleInfo = module.getClass().getAnnotation(Information.class);
if (!matching(moduleInfo.id(), idsStringPattern)) {
continue;
}
foundModules.add(module);
}
return foundModules;
}
示例7
@Command("list")
public void list(final PrintWriter writer) throws IOException {
int total = 0;
for (final Module module : moduleManager.list()) {
final Information info = module.getClass().getAnnotation(Information.class);
try {
final boolean isActivated = moduleManager.isActivated(info.id());
final boolean isLoaded = moduleManager.isLoaded(info.id());
final int cCnt = moduleManager.cCnt(info.id());
final int mCnt = moduleManager.mCnt(info.id());
// 找到模块计数
total++;
//|id|isActivated|isLoaded|cCnt|mCnt|version|author|
//|################|########|########|#######|#######|############|
output(writer, "%-20s\t%-8s\t%-8s\t%-5s\t%-5s\t%-15s\t%s",
info.id(),
isActivated ? "ACTIVE" : "FROZEN",
isLoaded ? "LOADED" : "UNLOADED",
cCnt,
mCnt,
info.version(),
info.author()
);
} catch (ModuleException me) {
logger.warn("get module info occur error when list modules, module[id={};class={};], error={}, ignore this module.",
me.getUniqueId(), module.getClass(), me.getErrorCode(), me);
}
}
output(writer, "total=%s", total);
}
示例8
ModuleJarLoader(final File moduleJarFile,
final Information.Mode mode) {
this.moduleJarFile = moduleJarFile;
this.mode = mode;
}
示例9
ModuleLibLoader(final File moduleLibDir,
final Information.Mode mode) {
this.moduleLibDir = moduleLibDir;
this.mode = mode;
}
示例10
@Override
public Information.Mode getMode() {
return cfg.getLaunchMode();
}
示例11
/**
* 获取沙箱的加载模式
*
* @return 沙箱加载模式
*/
Information.Mode getMode();