package org.eclipse.osgi.internal.loader;
import java.security.AccessController;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.eclipse.osgi.container.ModuleCapability;
import org.eclipse.osgi.framework.util.SecureAction;
import org.eclipse.osgi.internal.loader.sources.FilteredSourcePackage;
import org.eclipse.osgi.internal.loader.sources.PackageSource;
import org.eclipse.osgi.internal.loader.sources.SingleSourcePackage;
import org.osgi.framework.namespace.PackageNamespace;
public class BundleLoaderSources {
static SecureAction secureAction = AccessController.doPrivileged(SecureAction.createSecureAction());
private final ConcurrentMap<String, PackageSource> pkgSources;
private final BundleLoader loader;
public BundleLoaderSources(BundleLoader loader) {
this.pkgSources = new ConcurrentHashMap<>();
this.loader = loader;
}
PackageSource getPackageSource(String pkgName) {
PackageSource pkgSource = pkgSources.get(pkgName);
if (pkgSource != null) {
return pkgSource;
}
PackageSource newSource = new SingleSourcePackage(pkgName, loader);
PackageSource existingSource = pkgSources.putIfAbsent(newSource.getId(), newSource);
return existingSource != null ? existingSource : newSource;
}
boolean forceSourceCreation(ModuleCapability packageCapability) {
Map<String, String> directives = packageCapability.getDirectives();
return directives.get(PackageNamespace.CAPABILITY_EXCLUDE_DIRECTIVE) != null || directives.get(PackageNamespace.CAPABILITY_EXCLUDE_DIRECTIVE) != null;
}
public PackageSource createPackageSource(ModuleCapability packageCapability, boolean storeSource) {
PackageSource pkgSource = null;
String name = (String) packageCapability.getAttributes().get(PackageNamespace.PACKAGE_NAMESPACE);
String includes = packageCapability.getDirectives().get(PackageNamespace.CAPABILITY_INCLUDE_DIRECTIVE);
String excludes = packageCapability.getDirectives().get(PackageNamespace.CAPABILITY_EXCLUDE_DIRECTIVE);
if (includes != null || excludes != null) {
pkgSource = new FilteredSourcePackage(name, loader, includes, excludes);
}
if (storeSource) {
if (pkgSource != null) {
PackageSource existingSource = pkgSources.putIfAbsent(pkgSource.getId(), pkgSource);
if (existingSource != null) {
pkgSource = existingSource;
}
}
} else {
if (pkgSource == null) {
pkgSource = getPackageSource(name);
if (pkgSource.getClass() != SingleSourcePackage.class)
return new SingleSourcePackage(name, loader);
}
}
return pkgSource;
}
}