use of com.shulie.instrument.simulator.api.ModuleSpec in project LinkAgent by shulieTech.
the class ModuleSpecUtils method getModuleInfo.
private static ModuleSpec getModuleInfo(File file) {
JarFile jarFile = null;
try {
jarFile = new JarFile(file);
ZipEntry jarEntry = jarFile.getEntry("module.config");
if (jarEntry == null) {
LOGGER.error("SIMULATOR: found a invalid module jar,can't found module.config : {} ", file.getAbsolutePath());
return null;
}
InputStream in = null;
try {
in = jarFile.getInputStream(jarEntry);
Properties properties = new Properties();
properties.load(in);
String version = properties.getProperty("simulator-version");
String sinceVersion = null, untilVersion = null;
if (StringUtils.isBlank(version)) {
sinceVersion = "1.0.0";
untilVersion = "1000.0.0";
} else {
if (StringUtils.indexOf(version, '-') != -1) {
String[] arr = StringUtils.split(version, '-');
if (arr.length > 0) {
sinceVersion = StringUtils.trim(arr[0]);
}
if (arr.length > 1) {
untilVersion = StringUtils.trim(arr[1]);
} else {
untilVersion = "1000.0.0";
}
} else {
sinceVersion = StringUtils.trim(version);
untilVersion = sinceVersion;
}
}
if (StringUtils.isNotBlank(sinceVersion) && !VersionUtils.isValidVersion(sinceVersion)) {
LOGGER.error("SIMULATOR: read module {} err, simulator-version is valid: {} ", file.getName(), version);
return null;
}
if (StringUtils.isNotBlank(untilVersion) && !VersionUtils.isValidVersion(untilVersion)) {
LOGGER.error("SIMULATOR: read module {} err, simulator-version is valid: {} ", file.getName(), version);
return null;
}
String mustUseStr = properties.getProperty("must-use");
boolean mustUse = Boolean.valueOf(mustUseStr);
String moduleId = properties.getProperty("module-id");
String middlewareModuleStr = properties.getProperty("middleware-module");
String asyncStr = properties.getProperty("async");
boolean async = StringUtils.isNotBlank(asyncStr) ? Boolean.valueOf(asyncStr) : true;
ModuleSpec moduleSpec = new ModuleSpec();
moduleSpec.setMustUse(mustUse).setModuleId(moduleId).setFile(file).setExportClasses(splitAndTrim(properties.getProperty("export-class"), ",")).setImportClasses(splitAndTrim(properties.getProperty("import-class"), ",")).setExportPackages(StringUtils.trim(properties.getProperty("export-package"))).setImportPackages(StringUtils.trim(properties.getProperty("import-package"))).setExportResources(StringUtils.trim(properties.getProperty("export-resource"))).setImportResources(StringUtils.trim(properties.getProperty("import-resource"))).setSinceVersion(sinceVersion).setUntilVersion(untilVersion).setAsync(async);
if (StringUtils.isNotBlank(middlewareModuleStr)) {
moduleSpec.setMiddlewareModule(Boolean.valueOf(middlewareModuleStr));
}
final String priority = properties.getProperty("priority", "0");
moduleSpec.setDependencies(splitAndTrim(properties.getProperty("dependencies"), ","));
if (NumberUtils.isDigits(priority)) {
moduleSpec.setPriority(Integer.valueOf(priority));
}
return moduleSpec;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
// ignore
}
}
}
} catch (IOException e) {
LOGGER.error("SIMULATOR: read module jar err:{} ", file.getAbsolutePath(), e);
} catch (Throwable e) {
LOGGER.error("SIMULATOR: load module from jar err:{} ", file.getAbsolutePath(), e);
} finally {
if (jarFile != null) {
try {
jarFile.close();
} catch (IOException e) {
LOGGER.warn("[SIMULATOR: close module jar err:{} ", file.getAbsolutePath(), e);
}
}
}
return null;
}
use of com.shulie.instrument.simulator.api.ModuleSpec in project LinkAgent by shulieTech.
the class DefaultClassLoaderService method unload.
@Override
public void unload(String moduleName, ClassLoaderFactory classLoaderFactory) {
ModuleSpec moduleSpec = this.moduleSpecMap.remove(moduleName);
if (moduleSpec == null) {
return;
}
unloadExportClassAndResourceCache(moduleSpec, classLoaderFactory);
}
use of com.shulie.instrument.simulator.api.ModuleSpec in project LinkAgent by shulieTech.
the class DefaultClassLoaderService method isClassInImport.
@Override
public boolean isClassInImport(String moduleName, String className) {
if (isDisposed) {
return false;
}
ModuleSpec moduleSpec = moduleSpecMap.get(moduleName);
if (moduleSpec == null) {
logger.warn("SIMULATOR: invoke module {} isClassImport {} error cause by moduleSpec is not found. default return false.", moduleName, className);
return false;
}
for (String importName : moduleSpec.getImportClasses()) {
if (className.equals(importName)) {
return true;
}
}
String packageName = ClassUtils.getPackageName(className);
for (String pattern : moduleSpec.getImportExactlyPackages()) {
if (packageName.equals(pattern)) {
return true;
}
}
for (String pattern : moduleSpec.getImportPrefixPackages()) {
if (packageName.startsWith(pattern)) {
return true;
}
}
for (String pattern : moduleSpec.getImportSuffixPackages()) {
if (packageName.endsWith(pattern)) {
return true;
}
}
return false;
}
use of com.shulie.instrument.simulator.api.ModuleSpec in project LinkAgent by shulieTech.
the class DefaultModuleManager method listModuleSpecs.
@Override
public Collection<ModuleSpec> listModuleSpecs() {
Set<ModuleSpec> set = new HashSet<ModuleSpec>();
Collection<ExtensionModule> modules = list();
for (ExtensionModule module : modules) {
final ModuleInfo info = module.getClass().getAnnotation(ModuleInfo.class);
ModuleSpec moduleSpec = getModuleSpec(info.id());
if (moduleSpec != null) {
set.add(moduleSpec);
}
}
return set;
}
use of com.shulie.instrument.simulator.api.ModuleSpec in project LinkAgent by shulieTech.
the class ModuleSpecUtils method loadModuleSpecs.
public static List<ModuleSpec> loadModuleSpecs(List<File> files, boolean isSystemModule) {
if (CollectionUtils.isEmpty(files)) {
return Collections.EMPTY_LIST;
}
List<ModuleSpec> moduleSpecs = new ArrayList<ModuleSpec>();
for (File file : files) {
if (file.isHidden()) {
continue;
}
ModuleSpec moduleSpec = getModuleInfo(file);
if (moduleSpec != null) {
moduleSpec.setSystemModule(isSystemModule);
moduleSpecs.add(moduleSpec);
}
}
Collections.sort(moduleSpecs, new Comparator<ModuleSpec>() {
@Override
public int compare(ModuleSpec o1, ModuleSpec o2) {
if ((o1.isMiddlewareModule() && o2.isMiddlewareModule()) || (!o1.isMiddlewareModule() && !o2.isMiddlewareModule())) {
if (o1.getPriority() == o2.getPriority()) {
return 0;
}
return o1.getPriority() - o2.getPriority() > 0 ? 1 : -1;
}
if (o1.isMiddlewareModule()) {
return 1;
}
return -1;
}
});
return moduleSpecs;
}
Aggregations