use of org.osgi.service.packageadmin.ExportedPackage in project sling by apache.
the class ClassLoadingTest method testLoading.
/**
* This method tests the dynamic class loading through the package admin.
* The returned class changes from map to array list.
*/
@Test
public void testLoading() throws Exception {
final Sequence sequence = this.context.sequence("load-sequence");
final BundleContext bundleContext = this.context.mock(BundleContext.class);
final PackageAdmin packageAdmin = this.context.mock(PackageAdmin.class);
final ExportedPackage ep = this.context.mock(ExportedPackage.class);
final Bundle bundle = this.context.mock(Bundle.class);
this.context.checking(new Expectations() {
{
allowing(bundleContext).createFilter(with(any(String.class)));
will(returnValue(null));
allowing(bundleContext).getServiceReferences(with(any(String.class)), with((String) null));
will(returnValue(null));
allowing(bundleContext).addServiceListener(with(any(ServiceListener.class)), with(any(String.class)));
allowing(bundleContext).removeServiceListener(with(any(ServiceListener.class)));
allowing(packageAdmin).getExportedPackages("org.apache.sling.test");
will(returnValue(new ExportedPackage[] { ep }));
allowing(ep).getExportingBundle();
will(returnValue(bundle));
allowing(ep).isRemovalPending();
will(returnValue(false));
allowing(bundle).getBundleId();
will(returnValue(2L));
allowing(bundle).getState();
will(returnValue(Bundle.ACTIVE));
allowing(bundle).getSymbolicName();
will(returnValue("org.apache.sling.test"));
allowing(bundle).getVersion();
will(returnValue(new Version("1.0.0")));
one(bundle).loadClass("org.apache.sling.test.A");
inSequence(sequence);
will(returnValue(java.util.Map.class));
one(bundle).loadClass("org.apache.sling.test.A");
inSequence(sequence);
will(returnValue(java.util.Map.class));
one(bundle).loadClass("org.apache.sling.test.A");
inSequence(sequence);
will(returnValue(java.util.ArrayList.class));
}
});
DynamicClassLoaderManagerImpl manager = new DynamicClassLoaderManagerImpl(bundleContext, packageAdmin, null, new DynamicClassLoaderManagerFactory(bundleContext, packageAdmin));
final ClassLoader cl = manager.getDynamicClassLoader();
final Class<?> c1 = cl.loadClass("org.apache.sling.test.A");
Assert.assertEquals("java.util.Map", c1.getName());
final Class<?> c2 = cl.loadClass("org.apache.sling.test.A");
Assert.assertEquals("java.util.Map", c2.getName());
// as we cache the result, we still get the map!
final Class<?> c3 = cl.loadClass("org.apache.sling.test.A");
Assert.assertEquals("java.util.Map", c3.getName());
}
use of org.osgi.service.packageadmin.ExportedPackage in project sling by apache.
the class ExportedPackageServlet method doGet.
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
final String packName = request.getParameter("package");
final ExportedPackage p = packageAdmin.getExportedPackage(packName);
if (p == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND, "Package not found: " + packName);
} else {
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
response.getWriter().write("PACKAGE FOUND: ");
response.getWriter().write(p.toString());
response.getWriter().flush();
}
}
use of org.osgi.service.packageadmin.ExportedPackage in project geronimo-xbean by apache.
the class BundleClassFinder method scanImportPackages.
private void scanImportPackages(Collection<String> classes, Bundle host, Bundle fragment) {
BundleDescription description = new BundleDescription(fragment.getHeaders());
List<BundleDescription.ImportPackage> imports = description.getExternalImports();
for (BundleDescription.ImportPackage packageImport : imports) {
String packageName = packageImport.getName();
if (discoveryFilter.packageDiscoveryRequired(packageName)) {
ExportedPackage[] exports = packageAdmin.getExportedPackages(packageName);
Bundle wiredBundle = isWired(host, exports);
if (wiredBundle != null) {
Set<String> allClasses = findAllClasses(wiredBundle, packageName);
classes.addAll(allClasses);
}
}
}
}
use of org.osgi.service.packageadmin.ExportedPackage in project geronimo-xbean by apache.
the class BundleUtils method getWiredBundles.
public static LinkedHashSet<Bundle> getWiredBundles(PackageAdmin packageAdmin, Bundle bundle) {
BundleDescription description = new BundleDescription(bundle.getHeaders());
// handle static wire via Import-Package
List<BundleDescription.ImportPackage> imports = description.getExternalImports();
LinkedHashSet<Bundle> wiredBundles = new LinkedHashSet<Bundle>();
for (BundleDescription.ImportPackage packageImport : imports) {
ExportedPackage[] exports = packageAdmin.getExportedPackages(packageImport.getName());
Bundle wiredBundle = getWiredBundle(bundle, exports);
if (wiredBundle != null) {
wiredBundles.add(wiredBundle);
}
}
// handle dynamic wire via DynamicImport-Package
if (!description.getDynamicImportPackage().isEmpty()) {
for (Bundle b : bundle.getBundleContext().getBundles()) {
if (!wiredBundles.contains(b)) {
ExportedPackage[] exports = packageAdmin.getExportedPackages(b);
Bundle wiredBundle = getWiredBundle(bundle, exports);
if (wiredBundle != null) {
wiredBundles.add(wiredBundle);
}
}
}
}
return wiredBundles;
}
use of org.osgi.service.packageadmin.ExportedPackage in project geronimo-xbean by apache.
the class BundleAssignableClassFinder method initialize.
private void initialize() {
BundleDescription description = new BundleDescription(bundle.getHeaders());
List<BundleDescription.ImportPackage> imports = description.getExternalImports();
for (BundleDescription.ImportPackage packageImport : imports) {
String packageName = packageImport.getName();
ExportedPackage[] exports = packageAdmin.getExportedPackages(packageName);
Bundle wiredBundle = isWired(bundle, exports);
if (wiredBundle != null) {
wiredImportedPackageNames.add(packageName.replace('.', '/'));
break;
}
}
}
Aggregations