package org.glassfish.hk2.osgiresourcelocator;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleReference;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class ResourceFinderImpl extends ResourceFinder {
private BundleContext bundleContext;
public ResourceFinderImpl() {
ClassLoader cl = getClass().getClassLoader();
if (cl instanceof BundleReference) {
bundleContext = BundleReference.class.cast(cl).getBundle().getBundleContext();
}
if (bundleContext == null) {
throw new RuntimeException("There is no bundle context available yet. " +
"Instatiate this class in STARTING or ACTIVE state only");
}
}
URL findEntry1(String path) {
for (Bundle bundle : bundleContext.getBundles()) {
URL url = bundle.getEntry(path);
if (url != null) return url;
}
return null;
}
List<URL> findEntries1(String path) {
List<URL> urls = new ArrayList<URL>();
for (Bundle bundle : bundleContext.getBundles()) {
URL url = bundle.getEntry(path);
if (url != null) urls.add(url);
}
return urls;
}
}