Search in sources :

Example 1 with Bundle

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));
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Bundle(org.osgi.framework.Bundle) Resource(org.eclipse.jetty.util.resource.Resource) TreeMap(java.util.TreeMap) ResourceCollection(org.eclipse.jetty.util.resource.ResourceCollection)

Example 2 with Bundle

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();
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Bundle(org.osgi.framework.Bundle) CoreOptions.mavenBundle(org.ops4j.pax.exam.CoreOptions.mavenBundle) HttpClient(org.eclipse.jetty.client.HttpClient) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 3 with Bundle

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());
    }
}
Also used : HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle)

Example 4 with Bundle

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());
}
Also used : Bundle(org.osgi.framework.Bundle)

Example 5 with Bundle

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);
}
Also used : Bundle(org.osgi.framework.Bundle)

Aggregations

Bundle (org.osgi.framework.Bundle)2490 Test (org.junit.Test)659 URL (java.net.URL)388 BundleContext (org.osgi.framework.BundleContext)311 File (java.io.File)298 ArrayList (java.util.ArrayList)292 IOException (java.io.IOException)278 BundleException (org.osgi.framework.BundleException)270 HashMap (java.util.HashMap)149 ServiceReference (org.osgi.framework.ServiceReference)145 Path (org.eclipse.core.runtime.Path)126 Hashtable (java.util.Hashtable)124 HashSet (java.util.HashSet)95 InputStream (java.io.InputStream)94 IStatus (org.eclipse.core.runtime.IStatus)86 Status (org.eclipse.core.runtime.Status)82 List (java.util.List)80 Map (java.util.Map)74 BundleWiring (org.osgi.framework.wiring.BundleWiring)74 Version (org.osgi.framework.Version)73