11

I am using Day CQ. I want to store some data in a SQL Server DB, using the connection pool available in the Felix console. I can do this from a JSP, by using the "sling" object of type SlingScriptHelper defined in the defineObjects tag

sling.getService(DataSourcePool.class).

However, I want to use a servlet created in an OSGi bundle to handle requests from the client. The servlet doesn't have a defineObjects tag, so the "sling" object is not defined. I don't see a way to create a valid SlingScriptHelper object in my servlet, but it seems like it has to be possible.

Is there a way?

Bertrand Delacretaz
  • 6,100
  • 19
  • 24
joelt
  • 2,672
  • 2
  • 24
  • 32

3 Answers3

17

To get a service from a java OSGi component you don't need the SlingScriptHelper, you can either use the BundleContext.getService(...) method, or use SCR annotations to let SCR inject the service in your component.

As an example, you can look at how some components in Sling's Slingbucks sample use SCR annotations, the ConfirmedOrdersObserver class for example gets the SlingRepository in this way:

   @Reference
   private SlingRepository repository;

See http://felix.apache.org/site/apache-felix-maven-scr-plugin.html for the Maven plugin that handles these annotations.

cmptrgeekken
  • 8,052
  • 3
  • 29
  • 35
Bertrand Delacretaz
  • 6,100
  • 19
  • 24
  • I was able to use the SCR annotation approach. How would I get an instance of the BundleContext, though? That was something I wasn't able to figure out. – joelt Dec 26 '11 at 21:30
  • I haven't tested this out, but I'm thinking it could be done using the activate() or perhaps bind() methods. – joelt Dec 29 '11 at 02:38
  • 2
    In the activate(..) and deactivate(..) take a osgi ComponentContext object. From this obj you can derive the BundleContext. You shouldn't need the bundle context object tho as Bertrand indicated; you just need to use DCS instantiate your objects in your servlet (which is sling service). – empire29 Jan 10 '12 at 02:23
  • empire29 is correct, if you use SCR you can get the BundleContext from the activate/deactivate method parameters. You usually don't need BundleContext.getService(...) when you use SCR, @Reference will be sufficient in most cases. – Bertrand Delacretaz Jan 14 '12 at 15:11
9

You can use the BundleContext to get to the Service, by using the #getServiceReference and #getService methods. For example, if you were interested in the ResourceResolverFactory, you could get it like so:

BundleContext bundleContext = FrameworkUtil.getBundle(MyClass.class).getBundleContext();
ServiceReference factoryRef =
     bundleContext.getServiceReference(ResourceResolverFactory.class.getName());
ResourceResolverFactory resolverFactory = 
    (ResourceResolverFactory) bundleContext.getService(factoryRef);
Reporter
  • 3,897
  • 5
  • 33
  • 47
Elias Peterson
  • 221
  • 2
  • 4
  • I tried using @Reference annotations to add services to my bundle component. For some reason the references were null. Using this code above in the constructor of my bundle, to explicitly get the services, did the trick. Thanks :) – Jeremy Grand-Scrutton Aug 22 '13 at 08:34
  • Thanks! @Reference threw me an "Resource not modifiable error". This worked! – Jakolcz Oct 25 '13 at 11:56
1
YourClass obj = this.getSlingScriptHelper().getService(yourclass.class);
obj.whatever();
Reporter
  • 3,897
  • 5
  • 33
  • 47
naga
  • 11
  • 1