package org.eclipse.osgi.container;
import java.net.URL;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicReference;
import org.eclipse.osgi.container.ModuleRevisionBuilder.GenericInfo;
import org.eclipse.osgi.internal.container.AtomicLazyInitializer;
import org.eclipse.osgi.internal.container.InternalUtils;
import org.osgi.framework.AdminPermission;
import org.osgi.framework.Bundle;
import org.osgi.framework.namespace.HostNamespace;
import org.osgi.framework.namespace.PackageNamespace;
import org.osgi.framework.wiring.*;
import org.osgi.resource.*;
public final class ModuleWiring implements BundleWiring {
class LoaderInitializer implements Callable<ModuleLoader> {
@Override
public ModuleLoader call() throws Exception {
if (!isValid) {
return null;
}
return getRevision().getRevisions().getContainer().adaptor.createModuleLoader(ModuleWiring.this);
}
}
private static final RuntimePermission GET_CLASSLOADER_PERM = new RuntimePermission("getClassLoader");
private static final String DYNAMICALLY_ADDED_IMPORT_DIRECTIVE = "x.dynamically.added";
private final ModuleRevision revision;
private volatile List<ModuleCapability> capabilities;
private volatile List<ModuleRequirement> requirements;
private final Collection<String> substitutedPkgNames;
private final AtomicLazyInitializer<ModuleLoader> loader = new AtomicLazyInitializer<>();
private final LoaderInitializer loaderInitializer = new LoaderInitializer();
private volatile List<ModuleWire> providedWires;
private volatile List<ModuleWire> requiredWires;
volatile boolean isValid = true;
private final AtomicReference<Set<String>> dynamicMissRef = new AtomicReference<>();
ModuleWiring(ModuleRevision revision, List<ModuleCapability> capabilities, List<ModuleRequirement> requirements, List<ModuleWire> providedWires, List<ModuleWire> requiredWires, Collection<String> substitutedPkgNames) {
super();
this.revision = revision;
this.capabilities = capabilities;
this.requirements = requirements;
this.providedWires = providedWires;
this.requiredWires = requiredWires;
this.substitutedPkgNames = substitutedPkgNames.isEmpty() ? Collections.<String> emptyList() : substitutedPkgNames;
}
@Override
public Bundle getBundle() {
return revision.getBundle();
}
@Override
public boolean isCurrent() {
return isValid && revision.isCurrent();
}
@Override
public boolean isInUse() {
return isCurrent() || !providedWires.isEmpty() || isFragmentInUse();
}
private boolean isFragmentInUse() {
if ((BundleRevision.TYPE_FRAGMENT & revision.getTypes()) != 0) {
List<ModuleWire> hostWires = getRequiredModuleWires(HostNamespace.HOST_NAMESPACE);
return hostWires == null ? false : !hostWires.isEmpty();
}
return false;
}
public List<ModuleCapability> getModuleCapabilities(String namespace) {
return getModuleCapabilities(namespace, capabilities);
}
private List<ModuleCapability> getModuleCapabilities(String namespace, List<ModuleCapability> allCapabilities) {
if (!isValid)
return null;
if (namespace == null)
return new ArrayList<>(allCapabilities);
List<ModuleCapability> result = new ArrayList<>();
for (ModuleCapability capability : allCapabilities) {
if (namespace.equals(capability.getNamespace())) {
result.add(capability);
}
}
return result;
}
public List<ModuleRequirement> getModuleRequirements(String namespace) {
return getModuleRequirements(namespace, requirements);
}
List<ModuleRequirement> getPersistentRequirements() {
List<ModuleRequirement> persistentRequriements = getModuleRequirements(null);
if (persistentRequriements == null) {
return null;
}
for (Iterator<ModuleRequirement> iRequirements = persistentRequriements.iterator(); iRequirements.hasNext();) {
ModuleRequirement requirement = iRequirements.next();
if (PackageNamespace.PACKAGE_NAMESPACE.equals(requirement.getNamespace())) {
if ("true".equals(requirement.getDirectives().get(DYNAMICALLY_ADDED_IMPORT_DIRECTIVE))) {
iRequirements.remove();
}
}
}
return persistentRequriements;
}
private List<ModuleRequirement> getModuleRequirements(String namespace, List<ModuleRequirement> allRequirements) {
if (!isValid)
return null;
if (namespace == null)
return new ArrayList<>(allRequirements);
List<ModuleRequirement> result = new ArrayList<>();
for (ModuleRequirement requirement : allRequirements) {
if (namespace.equals(requirement.getNamespace())) {
result.add(requirement);
}
}
return result;
}
@Override
public List<BundleCapability> getCapabilities(String namespace) {
return InternalUtils.asListBundleCapability(getModuleCapabilities(namespace));
}
@Override
public List<BundleRequirement> getRequirements(String namespace) {
return InternalUtils.asListBundleRequirement(getModuleRequirements(namespace));
}
public List<ModuleWire> getProvidedModuleWires(String namespace) {
return getWires(namespace, providedWires);
}
List<ModuleWire> getPersistentProvidedWires() {
return getPersistentWires(providedWires);
}
public List<ModuleWire> getRequiredModuleWires(String namespace) {
return getWires(namespace, requiredWires);
}
List<ModuleWire> getPersistentRequiredWires() {
return getPersistentWires(requiredWires);
}
private List<ModuleWire> getPersistentWires(List<ModuleWire> allWires) {
List<ModuleWire> persistentWires = getWires(null, allWires);
if (persistentWires == null) {
return null;
}
for (Iterator<ModuleWire> iWires = persistentWires.iterator(); iWires.hasNext();) {
ModuleWire wire = iWires.next();
if (PackageNamespace.PACKAGE_NAMESPACE.equals(wire.getRequirement().getNamespace())) {
if ("true".equals(wire.getRequirement().getDirectives().get(DYNAMICALLY_ADDED_IMPORT_DIRECTIVE))) {
iWires.remove();
}
}
}
return persistentWires;
}
@Override
public List<BundleWire> getProvidedWires(String namespace) {
return InternalUtils.asListBundleWire(getWires(namespace, providedWires));
}
@Override
public List<BundleWire> getRequiredWires(String namespace) {
return InternalUtils.asListBundleWire(getWires(namespace, requiredWires));
}
private List<ModuleWire> getWires(String namespace, List<ModuleWire> allWires) {
if (!isValid)
return null;
if (namespace == null)
return new ArrayList<>(allWires);
List<ModuleWire> result = new ArrayList<>();
for (ModuleWire moduleWire : allWires) {
if (namespace.equals(moduleWire.getCapability().getNamespace())) {
result.add(moduleWire);
}
}
return result;
}
@Override
public ModuleRevision getRevision() {
return revision;
}
@Override
public ClassLoader getClassLoader() {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(GET_CLASSLOADER_PERM);
}
if (!isValid) {
return null;
}
ModuleLoader current = getModuleLoader();
if (current == null) {
return null;
}
return current.getClassLoader();
}
public ModuleLoader getModuleLoader() {
return loader.getInitialized(loaderInitializer);
}
void loadFragments(Collection<ModuleRevision> fragments) {
ModuleLoader current = loader.get();
if (current != null) {
current.loadFragments(fragments);
}
}
@Override
public List<URL> findEntries(String path, String filePattern, int options) {
if (!hasResourcePermission())
return Collections.emptyList();
if (!isValid) {
return null;
}
ModuleLoader current = getModuleLoader();
if (current == null) {
return null;
}
return current.findEntries(path, filePattern, options);
}
@Override
public Collection<String> listResources(String path, String filePattern, int options) {
if (!hasResourcePermission())
return Collections.emptyList();
if (!isValid) {
return null;
}
ModuleLoader current = getModuleLoader();
if (current == null) {
return null;
}
return current.listResources(path, filePattern, options);
}
@Override
public List<Capability> getResourceCapabilities(String namespace) {
return InternalUtils.asListCapability(getCapabilities(namespace));
}
@Override
public List<Requirement> getResourceRequirements(String namespace) {
return InternalUtils.asListRequirement(getRequirements(namespace));
}
@Override
public List<Wire> getProvidedResourceWires(String namespace) {
return InternalUtils.asListWire(getWires(namespace, providedWires));
}
@Override
public List<Wire> getRequiredResourceWires(String namespace) {
return InternalUtils.asListWire(getWires(namespace, requiredWires));
}
@Override
public ModuleRevision getResource() {
return revision;
}
void setProvidedWires(List<ModuleWire> providedWires) {
this.providedWires = providedWires;
}
void setRequiredWires(List<ModuleWire> requiredWires) {
this.requiredWires = requiredWires;
}
void setCapabilities(List<ModuleCapability> capabilities) {
this.capabilities = capabilities;
}
void unload() {
invalidate0(true);
}
void invalidate() {
invalidate0(false);
}
private void invalidate0(boolean releaseLoader) {
isValid = false;
ModuleLoader current = releaseLoader ? loader.getAndClear() : loader.get();
revision.getRevisions().getContainer().getAdaptor().invalidateWiring(this, current);
}
void validate() {
this.isValid = true;
}
boolean isSubtituted(ModuleCapability capability) {
if (!PackageNamespace.PACKAGE_NAMESPACE.equals(capability.getNamespace())) {
return false;
}
return substitutedPkgNames.contains(capability.getAttributes().get(PackageNamespace.PACKAGE_NAMESPACE));
}
public boolean isSubstitutedPackage(String packageName) {
return substitutedPkgNames.contains(packageName);
}
public Collection<String> getSubstitutedNames() {
return Collections.unmodifiableCollection(substitutedPkgNames);
}
private boolean hasResourcePermission() {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
try {
sm.checkPermission(new AdminPermission(getBundle(), AdminPermission.RESOURCE));
} catch (SecurityException e) {
return false;
}
}
return true;
}
public void addDynamicImports(ModuleRevisionBuilder builder) {
List<GenericInfo> newImports = builder.getRequirements();
List<ModuleRequirement> newRequirements = new ArrayList<>();
for (GenericInfo info : newImports) {
if (!PackageNamespace.PACKAGE_NAMESPACE.equals(info.getNamespace())) {
throw new IllegalArgumentException("Invalid namespace for package imports: " + info.getNamespace());
}
Map<String, Object> attributes = new HashMap<>(info.getAttributes());
Map<String, String> directives = new HashMap<>(info.getDirectives());
directives.put(DYNAMICALLY_ADDED_IMPORT_DIRECTIVE, "true");
directives.put(PackageNamespace.REQUIREMENT_RESOLUTION_DIRECTIVE, PackageNamespace.RESOLUTION_DYNAMIC);
newRequirements.add(new ModuleRequirement(info.getNamespace(), directives, attributes, revision));
}
ModuleDatabase moduleDatabase = revision.getRevisions().getContainer().moduleDatabase;
moduleDatabase.writeLock();
try {
List<ModuleRequirement> updatedRequirements = new ArrayList<>(requirements);
updatedRequirements.addAll(newRequirements);
requirements = updatedRequirements;
} finally {
moduleDatabase.writeUnlock();
}
}
void addDynamicPackageMiss(String packageName) {
Set<String> misses = dynamicMissRef.get();
if (misses == null) {
dynamicMissRef.compareAndSet(null, Collections.synchronizedSet(new HashSet<String>()));
misses = dynamicMissRef.get();
}
misses.add(packageName);
}
boolean isDynamicPackageMiss(String packageName) {
Set<String> misses = dynamicMissRef.get();
return misses != null && misses.contains(packageName);
}
void removeDynamicPackageMisses(Collection<String> packageNames) {
Set<String> misses = dynamicMissRef.get();
if (misses != null) {
misses.removeAll(packageNames);
}
}
@Override
public String toString() {
return revision.toString();
}
List<Wire> getSubstitutionWires() {
if (substitutedPkgNames.isEmpty()) {
return Collections.emptyList();
}
List<Wire> substitutionWires = new ArrayList<>(substitutedPkgNames.size());
List<ModuleWire> current = requiredWires;
for (ModuleWire wire : current) {
Capability cap = wire.getCapability();
if (PackageNamespace.PACKAGE_NAMESPACE.equals(cap.getNamespace())) {
if (substitutedPkgNames.contains(cap.getAttributes().get(PackageNamespace.PACKAGE_NAMESPACE))) {
substitutionWires.add(wire);
}
}
}
return substitutionWires;
}
}