New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DS component with PostgreSQL DataSourceFactory @Requirement fails to load when an @Activate method is added #1029

Closed
steinarb opened this Issue Dec 5, 2017 · 14 comments

Comments

Projects
None yet
3 participants
@steinarb

steinarb commented Dec 5, 2017

I have the following OSGi Declarative Services (DS) component that loads fine as long as I only have requirements for an OSGi LogService and a DataSourceFactory service.

When I added an activate method, like this:

 @Component(service={Servlet.class}, property={"alias=/sonar-collector"} )
 public class SonarCollectorServlet extends HttpServlet {
     ...
     @Activate
     public void activate(Map<String, Object> config) {
     }
     ...
 }

then the component failed to load, because of missing dependencies:

 karaf@root()> feature:repo-add mvn:no.priv.bang.sonar.sonar-collector/sonar-collector-webhook/LATEST/xml/features
 Adding feature url mvn:no.priv.bang.sonar.sonar-collector/sonar-collector-webhook/LATEST/xml/features
 karaf@root()> feature:install sonar-collector-webhook
 Error executing command: Unable to resolve root: missing requirement [root] osgi.identity; osgi.identity=sonar-collector-webhook; type=karaf.feature; version="[1.0.0.SNAPSHOT,1.0.0.SNAPSHOT]"; filter:="(&(osgi.identity=sonar-collector-webhook)(type=karaf.feature)(version>=1.0.0.SNAPSHOT)(version<=1.0.0.SNAPSHOT))" [caused by: Unable to resolve sonar-collector-webhook/1.0.0.SNAPSHOT: missing requirement [sonar-collector-webhook/1.0.0.SNAPSHOT] osgi.identity; osgi.identity=no.priv.bang.sonar.sonar-collector-webhook; type=osgi.bundle; version="[1.0.0.SNAPSHOT,1.0.0.SNAPSHOT]"; resolution:=mandatory [caused by: Unable to resolve no.priv.bang.sonar.sonar-collector-webhook/1.0.0.SNAPSHOT: missing requirement [no.priv.bang.sonar.sonar-collector-webhook/1.0.0.SNAPSHOT] osgi.service; effective:=active; filter:="(objectClass=org.osgi.service.jdbc.DataSourceFactory)"]]
 karaf@root()>

What's strange about this, that the missing depenency the error message complains about, ie. org.osgi.service.jdbc.DataSourceFactory, is essential to the servlet's operation. Without a DataSourceFactory, no database can be contacted and no data can be saved (and data is saved to the database).

If I removed the

@Activate

annotation then the component loaded without issues.

If I removed the DataSourceFactory requirement the component loaded as well (but, of course, didn't do anything meaningful because I also had to remove all code doing something with databases).

@steinarb

This comment has been minimized.

steinarb commented Dec 10, 2017

I have a workaround: adding the missing capability to the karaf feature, "negating" the requirement for the feature.

It works, ie. the activator runs without failing, but since I don't know what kind of side effects this could have, it is not something I would like to rely on for the long run.

The workaround is here.

@davecramer

This comment has been minimized.

Member

davecramer commented Dec 10, 2017

@steinarb

This comment has been minimized.

steinarb commented Dec 10, 2017

@davecramer

This comment has been minimized.

Member

davecramer commented Dec 10, 2017

That would be awesome! Thanks

Don't suppose there is any way to test this in travis ?

@steinarb

This comment has been minimized.

steinarb commented Dec 10, 2017

@vlsi

This comment has been minimized.

Member

vlsi commented Dec 10, 2017

If there already are pax-exam tests in there I could take a look...?

@steinarb , could you please check #507 out?

@steinarb

This comment has been minimized.

steinarb commented Dec 10, 2017

It's Pax Exam, so it would have been possible for me to add a test in the pgjdbc-osgi-test project if the pull request is accepted.

I would also need a place outside pgjdbc-osgi-test to create a test bundle requiring the PostgreSQL DataSourceFactory. Perhaps add a module pgjdbc-osgi-test-bundles on the top level with packaging pom, and add test bundles under that?

@steinarb

This comment has been minimized.

steinarb commented Dec 10, 2017

I've created a pull request on pgjdbc-parent-poms that won't show up here since it's a different github project.

But the change had to be there, since that's were the maven-bundle-plugin configuration resides.

The fix has been tested by:

  1. Reverting the workaround in my code and verify that this reintroduced the problem
  2. Built pgjdbc from git
  3. Verified that this issue was still present when my code depended on 42.1.5-SNAPSHOT, ie. I got an error trying to install the feature
  4. Make this change in pgjdbc-parent-poms/pgjdbc-core-parent/pom.xml
  5. Build pgjdbc-parent-poms 1.1.3-SNAPSHOT
  6. making pgjdbc/pgjdbc/pom.xml depend on 1.1.3-SNAPSHOT
  7. build pgjdbc version 42.1.5-SNAPSHOT
  8. making my code depend on pgjdbc 42.1.5-SNAPSHOT rather than 42.1.4
  9. build my code withe reverted workaround and changed postgresql jdbc dependency
  10. verify that the feature loaded without any issues
@davecramer

This comment has been minimized.

Member

davecramer commented Dec 12, 2017

@steinarb so you don't have any issues with the shaded jar ?

@steinarb

This comment has been minimized.

steinarb commented Dec 13, 2017

"Shaded jar"? Is that the com.ongres.scram stuff in the posgresql-42.1.5-SNAPSHOT.jar?

I interact with the pgjdbc driver through the DataSourceFactory interface in an OSGi context in apache karaf, and I don't try to load any classes directly from the jar/bundle.

And the bundle isn't exporting the shaded stuff either, so there is no risk of indirect classpath pollution.

I.e. all that's internal to the bundle that provides the DataSourceFactory, stays inside the bundle as far as OSGi is concerned.

@davecramer

This comment has been minimized.

Member

davecramer commented Dec 13, 2017

Yes, that is it. OK, so you had no issues with loading the driver using osgi even with the scram stuff.

Good

@steinarb

This comment has been minimized.

steinarb commented Dec 13, 2017

None that I noticed but I was mostly concerned with the bundle loading.

Let me try again and verify that the driver actually does the job it was expected to do.
(I need to have a sonarqube installation running on this machine first so it will take a little time)

@steinarb

This comment has been minimized.

steinarb commented Dec 13, 2017

Summary: the postgresql-42.1.5-SNAPSHOT.jar is working perfectly as far as I can tell.

What I did:

  1. Started my servlet and let it connect to a local PostgreSQL on a Win10 64 bit computer (the database connection uses Liquibase to check the state of the schema so this means that selects are working)
  2. Modified the karaf config for the servlet to make the servlet reconnect to a remote PostgreSQL running on a debian 64 bit client (the servlet still running on a Win10 computer)
  3. Quick started a SonarQube installation locally on the Win10 computer
  4. Added http://localhost:8181/sonar-collector as a webhook in SonarQube
  5. Ran a sonar analysis on the servlet itself and observed that the analysis ended up in the database

No error messages in the karaf.log (or anywhere else)

@davecramer

This comment has been minimized.

Member

davecramer commented Dec 13, 2017

Thanks for the feedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment