package org.eclipse.osgi.internal.loader.buddy;
import java.io.IOException;
import java.net.URL;
import java.util.*;
import org.eclipse.osgi.container.ModuleCapability;
import org.eclipse.osgi.container.ModuleWiring;
import org.eclipse.osgi.container.namespaces.EquinoxModuleDataNamespace;
import org.eclipse.osgi.internal.loader.BundleLoader;
public class RegisteredPolicy extends DependentPolicy {
public RegisteredPolicy(BundleLoader requester) {
super(requester);
if (allDependents == null)
return;
String requesterName = requester.getWiring().getRevision().getSymbolicName();
for (Iterator<ModuleWiring> iter = allDependents.iterator(); iter.hasNext();) {
ModuleWiring wiring = iter.next();
List<ModuleCapability> moduleDatas = wiring.getRevision().getModuleCapabilities(EquinoxModuleDataNamespace.MODULE_DATA_NAMESPACE);
@SuppressWarnings("unchecked")
List<String> registeredList = (List<String>) (moduleDatas.isEmpty() ? null : moduleDatas.get(0).getAttributes().get(EquinoxModuleDataNamespace.CAPABILITY_BUDDY_REGISTERED));
if (registeredList == null || registeredList.isEmpty()) {
iter.remove();
} else {
boolean contributes = false;
for (String registeredName : registeredList) {
if (registeredName.equals(requesterName)) {
contributes = true;
break;
}
}
if (!contributes) {
iter.remove();
}
}
}
if (allDependents.size() == 0)
allDependents = null;
}
@Override
public Class<?> loadClass(String name) {
if (allDependents == null)
return null;
Class<?> result = null;
int size = allDependents.size();
for (int i = 0; i < size && result == null; i++) {
ModuleWiring searchWiring = allDependents.get(i);
BundleLoader searchLoader = (BundleLoader) searchWiring.getModuleLoader();
if (searchLoader != null) {
try {
result = searchLoader.findClass(name);
} catch (ClassNotFoundException e) {
continue;
}
}
}
return result;
}
@Override
public URL loadResource(String name) {
if (allDependents == null)
return null;
URL result = null;
int size = allDependents.size();
for (int i = 0; i < size && result == null; i++) {
ModuleWiring searchWiring = allDependents.get(i);
BundleLoader searchLoader = (BundleLoader) searchWiring.getModuleLoader();
if (searchLoader != null) {
result = searchLoader.findResource(name);
}
}
return result;
}
@Override
public Enumeration<URL> loadResources(String name) {
if (allDependents == null)
return null;
Enumeration<URL> results = null;
int size = allDependents.size();
for (int i = 0; i < size; i++) {
try {
ModuleWiring searchWiring = allDependents.get(i);
BundleLoader searchLoader = (BundleLoader) searchWiring.getModuleLoader();
if (searchLoader != null) {
results = BundleLoader.compoundEnumerations(results, searchLoader.findResources(name));
}
} catch (IOException e) {
}
}
return results;
}
}