Skip to content

Commit

Permalink
opt test
Browse files Browse the repository at this point in the history
  • Loading branch information
清英 committed Apr 1, 2018
1 parent e1d7459 commit c58593d
Show file tree
Hide file tree
Showing 14 changed files with 151 additions and 79 deletions.
4 changes: 2 additions & 2 deletions jarslink-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.alipay.jarslink</groupId>
<artifactId>jarslink-api</artifactId>
<version>1.5.0.20180217</version>
<version>1.6.1.20180301</version>
<name>Alipay JarsLink API</name>
<url>https://github.com/alibaba/jarslink</url>
<packaging>jar</packaging>
Expand All @@ -18,7 +18,7 @@
<apache.commons.collections.version>3.2.1</apache.commons.collections.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<java.version>1.6</java.version>
</properties>

<organization>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.stream.Collectors;

import static com.google.common.base.Preconditions.checkNotNull;

Expand Down Expand Up @@ -61,7 +62,7 @@ public class ModuleConfig extends ToStringObject {
* <p>
* <strong>xml中的bean不能依赖注解bean,注解bean可以依赖xml定义的bean</strong>
*/
private Set<String> scanPackages = new CopyOnWriteArraySet<String>();
private Set<String> scanPackages = new CopyOnWriteArraySet<>();

/**
* 模块的版本,如1.0.0.20120609 版本变化会触发模块重新部署
Expand Down Expand Up @@ -101,9 +102,7 @@ public List<URL> getModuleUrl() {

public List<String> getModuleUrlPath() {
List<String> moduleUrls = Lists.newArrayList();
for (URL url : moduleUrl) {
moduleUrls.add(url.toString());
}
moduleUrls.addAll(moduleUrl.stream().map(URL::toString).collect(Collectors.toList()));
return moduleUrls;
}

Expand Down Expand Up @@ -147,21 +146,22 @@ public void setName(String name) {
*
* @param packageName 要添加的spring scan-base-package配置
*/
public void addScanPackage(String packageName) {
//不验证空字符串
public ModuleConfig addScanPackage(String packageName) {
checkNotNull(packageName, "packageName must not be null");
scanPackages.add(packageName);
return this;
}

/**
* 移除指定现有的spring scan-base-package
*
* @param packageName 要移除的之前配置的spring scan-base-package
*/
public void removeScanPackage(String packageName) {
public ModuleConfig removeScanPackage(String packageName) {
//不验证空字符串
checkNotNull(packageName, "packageName must not be null");
scanPackages.remove(packageName);
return this;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,11 @@ public interface ModuleLoader {
*/
Module load(ModuleConfig moduleConfig);

/**
* 卸载一个模块
*
* @param module
*/
void unload(Module module);

}
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ private void refreshModuleConfigs() {
private Collection<ModuleConfig> filterEnabledModule() {
List<ModuleConfig> moduleConfigs = queryModuleConfigs();
if (moduleConfigs == null || moduleConfigs.isEmpty()) {
return new ArrayList<ModuleConfig>();
return new ArrayList<>();
}
return Collections2.filter(moduleConfigs, new Predicate<ModuleConfig>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class ModuleClassLoader extends URLClassLoader {
/**
* 需要排除的包
*/
private final Set<String> excludedPackages = new HashSet<String>();
private final Set<String> excludedPackages = new HashSet<>();

/**
* 需要子加载器优先加载的包
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ public Module load(ModuleConfig moduleConfig) {
moduleApplicationContext);
}

@Override
public void unload(Module module) {
if (module != null) {
module.destroy();
}
}

/**
* 根据本地临时文件Jar,初始化模块自己的ClassLoader,初始化Spring Application Context,同时要设置当前线程上下文的ClassLoader问模块的ClassLoader
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,16 @@
* 模块管理,包含获取模块,执行模块里的方法
*
* @author tengfei.fangtf
*
* @version $Id: ModuleManagerImpl.java, v 0.1 Mar 20, 2017 4:04:32 PM tengfei.fangtf Exp $
*/
public class ModuleManagerImpl implements ModuleManager, DisposableBean {

private static final Logger LOGGER = LoggerFactory
.getLogger(ModuleManagerImpl.class);

/**
* 已注册的所有模块,key:moduleName
* 已注册的所有模块,key:moduleName upperCase
*/
private final ConcurrentHashMap<String, RuntimeModule> allModules = new ConcurrentHashMap();

Expand Down Expand Up @@ -114,9 +115,9 @@ public Module register(Module module) {
runtimeModule = new RuntimeModule().withName(name).withDefaultVersion(version).addModule(module);
allModules.put(name.toUpperCase(), runtimeModule);
} else {
oldModule = runtimeModule.getModule(runtimeModule.getDefaultVersion());
oldModule = runtimeModule.getDefaultModule();
runtimeModule.addModule(module).setDefaultVersion(version);
if (oldModule!=null && module.getModuleConfig().isNeedUnloadOldVersion() && !runtimeModule.getModules().isEmpty()) {
if (oldModule != null && module.getModuleConfig().isNeedUnloadOldVersion() && !runtimeModule.getModules().isEmpty()) {
runtimeModule.getModules().remove(oldModule.getVersion());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public Module getModule(String version) {
return modules.get(version);
}

public Module getDefaultModule() {
return modules.get(getDefaultVersion());
}

public RuntimeModule addModule(Module module) {
modules.put(module.getVersion(), module);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@

import com.alipay.jarslink.api.Action;
import com.alipay.jarslink.api.Module;
import com.alipay.jarslink.api.ModuleRuntimeException;
import com.alipay.jarslink.api.ModuleConfig;
import com.alipay.jarslink.api.ModuleRuntimeException;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -69,7 +68,7 @@ public class SpringModule implements Module {

private final ConfigurableApplicationContext applicationContext;

public SpringModule(ModuleConfig moduleConfig,String version, String name,
public SpringModule(ModuleConfig moduleConfig, String version, String name,
ConfigurableApplicationContext applicationContext) {
this.moduleConfig = moduleConfig;
this.applicationContext = applicationContext;
Expand Down Expand Up @@ -114,7 +113,7 @@ private <T> Map<String, T> scanActions(ApplicationContext applicationContext, Cl
if (LOGGER.isInfoEnabled()) {
LOGGER.info("JarsLink Scan actions finish: {}", ToStringBuilder.reflectionToString(actions));
}
return ImmutableMap.copyOf(actions);
return actions;
}

@Override
Expand Down Expand Up @@ -180,6 +179,9 @@ public void destroy() {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Close application context: {}", applicationContext);
}
if (!actions.isEmpty()) {
actions.clear();
}
//close spring context
closeQuietly(applicationContext);
//clean classloader
Expand All @@ -193,7 +195,6 @@ public void destroy() {
*/
public static void clear(ClassLoader classLoader) {
checkNotNull(classLoader, "classLoader is null");
//Introspector缓存BeanInfo类来获得更好的性能。卸载时刷新所有Introspector的内部缓存。
Introspector.flushCaches();
//从已经使用给定类加载器加载的缓存中移除所有资源包
ResourceBundle.clearCache(classLoader);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Alipay.com Inc.
* Copyright (c) 2004-2018 All Rights Reserved.
*/
package com.alipay.jarslink.api.impl;

import com.alipay.jarslink.api.Module;
import com.alipay.jarslink.api.ModuleLoader;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static com.alipay.jarslink.api.impl.ModuleManagerTest.buildModuleConfig;

/**
*
* @author tengfei.fang
* @version $Id: ModuleLoaderImplTest.java, v 0.1 2018年04月01日 12:12 PM tengfei.fang Exp $
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:META-INF/spring/jarslink.xml"})
public class ModuleLoaderImplTest {

@Autowired
private ModuleLoader moduleLoader;

@Test
public void shouldLoadModule() {
//1:加载模块
Module module = loadModule();
Assert.assertEquals("demo", module.getName());
Assert.assertEquals("1.0.0.20170621", module.getVersion());
Assert.assertEquals(3, module.getActions().size());
Assert.assertNotNull(module);
Assert.assertNotNull(module.getCreation());
Assert.assertNotNull(module.getChildClassLoader());
//卸载模块
moduleLoader.unload(module);

Assert.assertTrue(module.getActions().isEmpty());
Assert.assertNotNull(module.getChildClassLoader());
}

@Test
public void shouldLoadMultiModule() {
for (int i = 0; i < 10; i++) {
//1:加载模块
String name = "demo" + i;
Module module = loadModule(name);
Assert.assertNotNull(module);
//卸载模块
moduleLoader.unload(module);
Assert.assertTrue(module.getActions().isEmpty());
}

}

private Module loadModule() {
return moduleLoader.load(buildModuleConfig(true));
}

private Module loadModule(String name) {
return moduleLoader.load(buildModuleConfig(name, true));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public void shouldAddMultiVersionModule() {

Module demo2 = moduleManager.find(DEMO_MODULE, "2.0");
Assert.assertNotNull(demo2);

Assert.assertEquals(2, moduleManager.getModules().size());

removeModule();
}

Expand Down

0 comments on commit c58593d

Please sign in to comment.