Get DataSourcePool for use in servlet | Community
Skip to main content
New Participant
October 16, 2015
Solved

Get DataSourcePool for use in servlet

  • October 16, 2015
  • 18 replies
  • 5486 views

I'm trying to get DataSourcePool instance for servlet in OSGi bundle but the standard approach doesn't work. I used to get DataSourcePool via Activator like this

public class Activator implements BundleActivator { private DataSourcePool source; public void start(BundleContext context) throws Exception { ServiceReference dspRef = context.getServiceReference(DataSourcePool.class.getName()); source = (DataSourcePool)context.getService(dspRef); } public static DataSourcePool getDataSourcePool(){ return source; } }

but since I started to develop in Eclipse, this doesn't work anymore. I have this project structure in Eclipse

project-default project-default-bundle \src \main \java \cz \package \sub1 \sub2 \Activator.java \other \package \servlets \MyServlet.java project-default-components project-default-content

When I try to get the DataSourcePool in MyServlet.java, the return value from cz.package.Activator.getDataSourcePool(); is null. I also tried to use @Reference but it just gives me HTTP 403 Forbidden error after compiling and running the servlet.

Thanks for any help

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Jakolcz

OK, so after a lot of googling I've found the cause and solution of this problem. The problem is with JDK7 Version 51.0 and above. Oracle added new functionality which adds StackMapTable to the bytecode and I guess that by using the SCR annotations the maven messes the bytecode so it can't be read properly. Temporary solution is to run CQ with this parameter

-XX:-UseSplitVerifier

but it looks like this will be deprecated in Java 8 and removed in upcoming versions.

So from my point of view it looks like 

  • I have to wait until proper version of SCR anotations plugin or Maven will be released
  • I can use older JDK to compile my source code

Am I right? Or is there something else that I don't see?

18 replies

JakolczAuthor
New Participant
October 16, 2015

Scott Brodersen wrote...

Is your servlet path listed in the Sling Servlet Resolver (org.apache.sling.servlets.resolver.SlingServletResolver) service?

 

scott

 

Hi, yes it is. I have the path /myservices/ set there.

Scott_Brodersen
New Participant
October 16, 2015

The Felix docs use a slightly different syntax for the Properties annotation:

 

@Properties({@Property(name = "prop1", value = "value1"),@Property(name = "prop2", value = "value2")})

 

edit: linky http://felix.apache.org/documentation/subprojects/apache-felix-maven-scr-plugin/scr-annotations.html :)

Employee
October 16, 2015

This was fixed in version 1.9.0 of the maven-scr-plugin. See https://issues.apache.org/jira/browse/FELIX-3568

smacdonald2008
New Participant
October 16, 2015

Nice pickup - i am using an older JDK version - jdk1.6.0_18. This is why I did not experience any issues. I will add this note to the articles so other people do not run into this issue. 

JakolczAuthor
New Participant
October 16, 2015

Scott Brodersen wrote...

The Felix docs use a slightly different syntax for the Properties annotation:

 

@Properties({@Property(name = "prop1", value = "value1"),@Property(name = "prop2", value = "value2")})

 

edit: linky http://felix.apache.org/documentation/subprojects/apache-felix-maven-scr-plugin/scr-annotations.html :)

 

I've changed it to this, but I still get the HTTP 403 Forbidden...

JakolczAuthor
New Participant
October 16, 2015

justin_at_adobe wrote...

This was fixed in version 1.9.0 of the maven-scr-plugin. See https://issues.apache.org/jira/browse/FELIX-3568

 

Thanks for that! I didn't know I'm not using the newest version of maven-scr-plugin. I'm actually using version 1.7.4 but how can I download newer version? This is what I have in my pom.xml file

<dependency> <groupId>org.apache.felix</groupId> <artifactId>org.apache.felix.scr.annotations</artifactId> <version>1.9.6</version> <scope>provided</scope> </dependency>

but when I run maven build it still says that it uses version 1.7.4. How can I use newer version? (I'm newbie with Maven).

Employee
October 16, 2015

You need to look for a block that looks like this:

<plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-scr-plugin</artifactId> <version>1.7.4</version> <executions> <execution> <id>generate-scr-descriptor</id> <goals> <goal>scr</goal> </goals> </execution> </executions> </plugin>
JakolczAuthor
New Participant
October 16, 2015

justin_at_adobe wrote...

You need to look for a block that looks like this:

  1. <plugin>
  2. <groupId>org.apache.felix</groupId>
  3. <artifactId>maven-scr-plugin</artifactId>
  4. <version>1.7.4</version>
  5. <executions>
  6. <execution>
  7. <id>generate-scr-descriptor</id>
  8. <goals>
  9. <goal>scr</goal>
  10. </goals>
  11. </execution>
  12. </executions>
  13. </plugin>

 

 

Thank you!