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();
}
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();
}
}
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;
}
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");
}
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);
}
}
Aggregations