package com.mkyong.rest; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.BundleException; import org.osgi.framework.BundleReference; import org.osgi.framework.ServiceReference; import org.osgi.service.cm.ConfigurationAdmin; import osgiserver.DictionaryService; //http://localhost:8080/MyRestService/rest/message/helloworld @Path("/message") public class MessageRestService { //@Resource private BundleContext context; @GET @Path("{param}") public String printMessage(@PathParam("param") String msg) { String result = "Restful Service Invoked : " + msg; context = getBundleContextFromClass(); invokeDictionaryService(); return result; } private BundleContext getBundleContextFromClass() { BundleReference bref = (BundleReference) ConfigurationAdmin.class.getClassLoader(); Bundle bundle = bref.getBundle(); if (bundle.getState() != Bundle.ACTIVE) { try { bundle.start(); } catch (BundleException ex) { // log.errorf(ex, "Cannot start bundle: %s", bundle); } } return bundle.getBundleContext(); } private void invokeDictionaryService(){ System.out.println("client In Rest...." + context); System.out.println("ctxt...." + context); // Query for all service references matching any language. try { ServiceReference[] refs = context.getServiceReferences( DictionaryService.class.getName(), null); if (refs != null) { System.out.println("Enter a blank line to exit."); DictionaryService dictionary = (DictionaryService) context .getService(refs[0]); if (dictionary.check("osgi")) { System.out.println("Correct."); } else { System.out.println("Incorrect."); } context.ungetService(refs[0]); } else { System.out.println("Couldn't find any dictionary service..."); } } catch (Exception e) { System.out.println("**********Excpetion"); e.printStackTrace(); } } }