It complains
Found out one solution and gets the work done:
getService(MyService.class);
static <T> T getService(Class<T> serviceClass) {
BundleContext bContext = FrameworkUtil.getBundle(serviceClass).getBundleContext();
ServiceReference sr = bContext.getServiceReference(serviceClass.getName());
return serviceClass.cast(bContext.getService(sr));
}
Any better way to do it?
Topics help categorize Community content and increase your ability to discover relevant content.
Views
Replies
Total Likes
Hi,
You can use it from slingBinding instead:
public class fiddle extends SlingAllMethodsServlet {
//Any OSGI service you will normally use through
MyService service = null;
SlingScriptHelper scriptHelper = null;
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
SlingBindings bindings = (SlingBindings) request.getAttribute(SlingBindings.class.getName());
scriptHelper = bindings.getSling();
//Here you get the OSGI service
service = scriptHelper.getService(MyService.class);
Hope this helps.
Thanks @EstebanBustamante That works as well. Seems it depends on the usecase and need. Both solutions with BundleContext and SlingBindings would work.
Views
Replies
Total Likes
@antarikshruhil9 There is some issue I am seeing with the fiddle as it is not compiling imports. Removing some keywords from import package also not giving any exception/error. That's because it is not able to resolve any Java file.
@antarikshruhil9 Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies