Search in sources :

Example 1 with ServiceReference

use of org.osgi.framework.ServiceReference 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 2 with ServiceReference

use of org.osgi.framework.ServiceReference in project jetty.project by eclipse.

the class TestOSGiUtil method testHttpServiceGreetings.

protected static void testHttpServiceGreetings(BundleContext bundleContext, String protocol, int port) throws Exception {
    assertActiveBundle(bundleContext, "org.eclipse.jetty.osgi.boot");
    assertActiveBundle(bundleContext, "org.eclipse.jetty.osgi.httpservice");
    assertActiveBundle(bundleContext, "org.eclipse.equinox.http.servlet");
    // in the OSGi world this would be bad code and we should use a bundle
    // tracker.
    // here we purposely want to make sure that the httpService is actually
    // ready.
    ServiceReference sr = bundleContext.getServiceReference(HttpService.class.getName());
    Assert.assertNotNull("The httpServiceOSGiBundle is started and should " + "have deployed a service reference for HttpService", sr);
    HttpService http = (HttpService) bundleContext.getService(sr);
    http.registerServlet("/greetings", new HttpServlet() {

        private static final long serialVersionUID = 1L;

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.getWriter().write("Hello");
        }
    }, null, null);
    // now test the servlet
    HttpClient client = protocol.equals("https") ? new HttpClient(newSslContextFactory()) : new HttpClient();
    try {
        client.start();
        ContentResponse response = client.GET(protocol + "://127.0.0.1:" + port + "/greetings");
        Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
        String content = new String(response.getContent());
        Assert.assertEquals("Hello", content);
    } finally {
        client.stop();
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpService(org.osgi.service.http.HttpService) HttpServlet(javax.servlet.http.HttpServlet) HttpClient(org.eclipse.jetty.client.HttpClient) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ServiceReference(org.osgi.framework.ServiceReference)

Example 3 with ServiceReference

use of org.osgi.framework.ServiceReference in project jetty.project by eclipse.

the class PackageAdminServiceTracker method setup.

/**
     * @return true if the fragments were activated by this method.
     */
private boolean setup() {
    ServiceReference sr = _context.getServiceReference(PackageAdmin.class.getName());
    _fragmentsWereActivated = sr != null;
    if (sr != null)
        invokeFragmentActivators(sr);
    sr = _context.getServiceReference(StartLevel.class.getName());
    if (sr != null) {
        _startLevel = (StartLevel) _context.getService(sr);
        try {
            _maxStartLevel = Integer.parseInt(System.getProperty("osgi.startLevel", "6"));
        } catch (Exception e) {
            // nevermind default on the usual.
            _maxStartLevel = 6;
        }
    }
    return _fragmentsWereActivated;
}
Also used : PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference)

Example 4 with ServiceReference

use of org.osgi.framework.ServiceReference in project jersey by jersey.

the class Activator method start.

@Override
public synchronized void start(BundleContext bundleContext) throws Exception {
    this.bc = bundleContext;
    logger.info("STARTING HTTP SERVICE BUNDLE");
    this.tracker = new ServiceTracker(this.bc, HttpService.class.getName(), null) {

        @Override
        public Object addingService(ServiceReference serviceRef) {
            httpService = (HttpService) super.addingService(serviceRef);
            registerServlets();
            return httpService;
        }

        @Override
        public void removedService(ServiceReference ref, Object service) {
            if (httpService == service) {
                unregisterServlets();
                httpService = null;
            }
            super.removedService(ref, service);
        }
    };
    this.tracker.open();
    logger.info("HTTP SERVICE BUNDLE STARTED");
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) HttpService(org.osgi.service.http.HttpService) ServiceReference(org.osgi.framework.ServiceReference)

Example 5 with ServiceReference

use of org.osgi.framework.ServiceReference in project jersey by jersey.

the class Activator method sendAdminEvent.

private void sendAdminEvent() {
    ServiceReference eaRef = bc.getServiceReference(EventAdmin.class.getName());
    if (eaRef != null) {
        EventAdmin ea = (EventAdmin) bc.getService(eaRef);
        ea.sendEvent(new Event("jersey/test/DEPLOYED", new HashMap<String, String>() {

            {
                put("context-path", "/");
            }
        }));
        bc.ungetService(eaRef);
    }
}
Also used : EventAdmin(org.osgi.service.event.EventAdmin) HashMap(java.util.HashMap) Event(org.osgi.service.event.Event) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

ServiceReference (org.osgi.framework.ServiceReference)1690 Test (org.junit.Test)926 Properties (java.util.Properties)396 Architecture (org.apache.felix.ipojo.architecture.Architecture)263 CheckService (org.apache.felix.ipojo.runtime.core.test.services.CheckService)233 BundleContext (org.osgi.framework.BundleContext)231 InstanceDescription (org.apache.felix.ipojo.architecture.InstanceDescription)227 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)215 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)183 ArrayList (java.util.ArrayList)167 Bundle (org.osgi.framework.Bundle)144 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)141 Hashtable (java.util.Hashtable)124 IOException (java.io.IOException)107 CheckService (org.apache.felix.ipojo.runtime.core.services.CheckService)92 Dictionary (java.util.Dictionary)82 Configuration (org.osgi.service.cm.Configuration)74 CheckService (org.apache.felix.ipojo.handler.temporal.services.CheckService)70 FooService (org.apache.felix.ipojo.handler.temporal.services.FooService)70 CheckService (org.apache.felix.ipojo.handler.transaction.services.CheckService)65