package net.northking.cctp.element.scanner;

import net.northking.cctp.element.core.ResourceLoader;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * Jar dynamic provider
 * 
 * @author fengxiang.zhang
 * @since 1.0
 */
public class JarResourceProvider extends org.xeustechnologies.jcl.JarClassLoader
		implements ResourceLoader {

	@Override
	public ClassLoader getClassLoader() {
		return this;
	}

	@Override
	public Collection<String> filterResources(String basePackage)  {
		basePackage = StringUtils.replace(basePackage, ".", "/");
		Map<String, byte[]> loadedResources = this.getLoadedResources();
		List<String> resources = new ArrayList<>();
		for (String resourceName : loadedResources.keySet()) {
			if (resourceName.startsWith(basePackage)) {
				resources.add(resourceName);
			}
		}
		return resources;
	}

}