use of org.osgi.framework.Bundle in project jetty.project by eclipse.
the class OSGiWebInfConfiguration method configure.
/* ------------------------------------------------------------ */
/**
* Allow fragments to supply some resources that are added to the baseResource of the webapp.
*
* The resources can be either prepended or appended to the baseResource.
*
* @see org.eclipse.jetty.webapp.WebInfConfiguration#configure(org.eclipse.jetty.webapp.WebAppContext)
*/
@Override
public void configure(WebAppContext context) throws Exception {
TreeMap<String, Resource> prependedResourcesPath = new TreeMap<String, Resource>();
TreeMap<String, Resource> appendedResourcesPath = new TreeMap<String, Resource>();
Bundle bundle = (Bundle) context.getAttribute(OSGiWebappConstants.JETTY_OSGI_BUNDLE);
if (bundle != null) {
Set<Bundle> fragments = (Set<Bundle>) context.getAttribute(FRAGMENT_AND_REQUIRED_BUNDLES);
if (fragments != null && !fragments.isEmpty()) {
// looked up.
for (Bundle frag : fragments) {
String path = Util.getManifestHeaderValue(OSGiWebappConstants.JETTY_WAR_FRAGMENT_FOLDER_PATH, OSGiWebappConstants.JETTY_WAR_FRAGMENT_RESOURCE_PATH, frag.getHeaders());
convertFragmentPathToResource(path, frag, appendedResourcesPath);
path = Util.getManifestHeaderValue(OSGiWebappConstants.JETTY_WAR_PATCH_FRAGMENT_FOLDER_PATH, OSGiWebappConstants.JETTY_WAR_PREPEND_FRAGMENT_RESOURCE_PATH, frag.getHeaders());
convertFragmentPathToResource(path, frag, prependedResourcesPath);
}
if (!appendedResourcesPath.isEmpty()) {
LinkedHashSet<Resource> resources = new LinkedHashSet<Resource>();
//Add in any existing setting of extra resource dirs
Set<Resource> resourceDirs = (Set<Resource>) context.getAttribute(WebInfConfiguration.RESOURCE_DIRS);
if (resourceDirs != null && !resourceDirs.isEmpty())
resources.addAll(resourceDirs);
//Then append the values from JETTY_WAR_FRAGMENT_FOLDER_PATH
resources.addAll(appendedResourcesPath.values());
context.setAttribute(WebInfConfiguration.RESOURCE_DIRS, resources);
}
}
}
super.configure(context);
// place the prepended resources at the beginning of the contexts's resource base
if (!prependedResourcesPath.isEmpty()) {
Resource[] resources = new Resource[1 + prependedResourcesPath.size()];
System.arraycopy(prependedResourcesPath.values().toArray(new Resource[prependedResourcesPath.size()]), 0, resources, 0, prependedResourcesPath.size());
resources[resources.length - 1] = context.getBaseResource();
context.setBaseResource(new ResourceCollection(resources));
}
}
use of org.osgi.framework.Bundle in project jetty.project by eclipse.
the class TestJettyOSGiBootContextAsService method testContextHandlerAsOSGiService.
/**
*/
@Test
public void testContextHandlerAsOSGiService() throws Exception {
// now test the context
HttpClient client = new HttpClient();
try {
client.start();
ContentResponse response = client.GET("http://127.0.0.1:" + TestJettyOSGiBootCore.DEFAULT_HTTP_PORT + "/acme/index.html");
assertEquals(HttpStatus.OK_200, response.getStatus());
String content = new String(response.getContent());
assertTrue(content.indexOf("<h1>Test OSGi Context</h1>") != -1);
} finally {
client.stop();
}
ServiceReference[] refs = bundleContext.getServiceReferences(ContextHandler.class.getName(), null);
assertNotNull(refs);
assertEquals(1, refs.length);
ContextHandler ch = (ContextHandler) bundleContext.getService(refs[0]);
assertEquals("/acme", ch.getContextPath());
// Stop the bundle with the ContextHandler in it and check the jetty
// Context is destroyed for it.
// TODO: think of a better way to communicate this to the test, other
// than checking stderr output
Bundle testWebBundle = TestOSGiUtil.getBundle(bundleContext, "org.eclipse.jetty.osgi.testcontext");
assertNotNull("Could not find the org.eclipse.jetty.test-jetty-osgi-context.jar bundle", testWebBundle);
assertTrue("The bundle org.eclipse.jetty.testcontext is not correctly resolved", testWebBundle.getState() == Bundle.ACTIVE);
testWebBundle.stop();
}
use of org.osgi.framework.Bundle in project jetty.project by eclipse.
the class TestOSGiUtil method debugBundles.
protected static void debugBundles(BundleContext bundleContext) {
Map<String, Bundle> bundlesIndexedBySymbolicName = new HashMap<String, Bundle>();
System.err.println("Active " + Bundle.ACTIVE);
System.err.println("RESOLVED " + Bundle.RESOLVED);
System.err.println("INSTALLED " + Bundle.INSTALLED);
for (Bundle b : bundleContext.getBundles()) {
bundlesIndexedBySymbolicName.put(b.getSymbolicName(), b);
System.err.println(" " + b.getSymbolicName() + " " + b.getLocation() + " " + b.getVersion() + " " + b.getState());
}
}
use of org.osgi.framework.Bundle in project jetty.project by eclipse.
the class TestOSGiUtil method assertActiveBundle.
protected static void assertActiveBundle(BundleContext bundleContext, String symbolicName) throws Exception {
Bundle b = getBundle(bundleContext, symbolicName);
Assert.assertNotNull(b);
Assert.assertEquals(b.getSymbolicName() + " must be active.", Bundle.ACTIVE, b.getState());
}
use of org.osgi.framework.Bundle in project jetty.project by eclipse.
the class PackageAdminServiceTracker method collectFragmentsAndRequiredBundles.
/**
* Returns the fragments and the required-bundles. Collects them
* transitively when the directive 'visibility:=reexport' is added to a
* required-bundle.
*
* @param bundle the bundle
* @param admin the admin package
* @param deps The map of fragment and required bundles associated to the value of the
* jetty-web attribute.
* @param onlyReexport true to collect resources and web-fragments
* transitively if and only if the directive visibility is
* reexport.
*/
protected void collectFragmentsAndRequiredBundles(Bundle bundle, PackageAdmin admin, Map<String, Bundle> deps, boolean onlyReexport) {
Bundle[] fragments = admin.getFragments(bundle);
if (fragments != null) {
// bundles that extend it
for (Bundle f : fragments) {
if (!deps.keySet().contains(f.getSymbolicName())) {
deps.put(f.getSymbolicName(), f);
collectRequiredBundles(f, admin, deps, onlyReexport);
}
}
}
collectRequiredBundles(bundle, admin, deps, onlyReexport);
}
Aggregations