1

I'm pretty new in osgi world, and probably missing something, and i'm having troubles exposing different versions of the same sevice for liferay.

Here is what i am trying to do (and sorry for my english):

I wrote a service, wrapped it in a bundle and successfully deployed it on osgi. My bnd.bnd file looks like this

Bundle-Name: it.peernetwork.lr.pilot.test-services
Bundle-SymbolicName: pilot--test-services
Bundle-Version: 7.1.0
Bundle-Activator: it.peernetwork.lr.pilot.testservices.impl.TestServicesActivator
Export-Package: it.peernetwork.lr.pilot.testservices

and, once packaged, the manifest file is like this

Manifest-Version: 1.0
Bnd-LastModified: 1542646653910
Bundle-Activator: it.peernetwork.lr.pilot.testservices.impl.TestServic
 esActivator
Bundle-ClassPath: .,lib/pn--logger-7.1.0.jar,lib/pn--services-base-7.1
 .0.jar,lib/pn--prop-files-7.1.0.jar,lib/gson-2.8.5.jar,lib/pn--expand
 o-values-7.1.0.jar
Bundle-ManifestVersion: 2
Bundle-Name: it.peernetwork.lr.pilot.test-services
Bundle-SymbolicName: pilot--test-services
Bundle-Version: 7.1.0
Created-By: 1.8.0_191 (Oracle Corporation)
Export-Package: it.peernetwork.lr.pilot.testservices;version="7.1.0"
Import-Package: com.liferay.expando.kernel.exception;version="[1.0,2)"
 ,com.liferay.expando.kernel.model;version="[1.1,2)",com.liferay.expan
 do.kernel.service;version="[1.1,2)",com.liferay.portal.kernel.excepti
 on;version="[7.2,8)",com.liferay.portal.kernel.model;version="[2.0,3)
 ",it.peernetwork.lr.pilot.testservices,org.osgi.framework;version="[1
 .8,2)"
Javac-Debug: on
Javac-Deprecation: off
Javac-Encoding: UTF-8
Private-Package: it.peernetwork.lr.pilot.test.services.impl,it.peernet
 work.lr.pilot.testservices.impl,it.peernetwork.lr.pilot.testservices.
 x,lib,it.peernetwork.lr.logger,it.peernetwork.lr.servicesbase,it.peer
 network.lr.propfiles,it.peernetwork.lr.propfiles.utils,com.google.gso
 n,com.google.gson.annotations,com.google.gson.internal,com.google.gso
 n.internal.bind,com.google.gson.internal.bind.util,com.google.gson.in
 ternal.reflect,com.google.gson.reflect,com.google.gson.stream,it.peer
 network.lr.expandovalues
Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.8))"
Tool: Bnd-3.2.0.201605172007

This manifest file declares

Export-Package: it.peernetwork.lr.pilot.testservices;version="7.1.0"

and i think this is correct.

Once deployed (successfully) i check the bundle state with the command bundle __BUNDLE_ID__ that gives me

pilot--test-services_7.1.0 [1014]
  Id=1014, Status=ACTIVE      Data Root=/home/ltrioschi/development/liferay-osgi/liferay-ce-portal-7.1.0-ga1/osgi/state/org.eclipse.osgi/1014/data
  "Registered Services"
    {it.peernetwork.lr.pilot.testservices.UselessService}={service.id=1607, service.bundleid=1014, service.scope=singleton}
  No services in use.
  Exported packages
    it.peernetwork.lr.pilot.testservices; version="7.1.0"[exported]
  Imported packages
    com.liferay.expando.kernel.exception; version="1.0.0" <org.eclipse.osgi_3.10.200.v20150831-0856 [0]>
    com.liferay.expando.kernel.model; version="1.1.0" <org.eclipse.osgi_3.10.200.v20150831-0856 [0]>
    com.liferay.expando.kernel.service; version="1.1.0" <org.eclipse.osgi_3.10.200.v20150831-0856 [0]>
    com.liferay.portal.kernel.exception; version="7.2.0" <org.eclipse.osgi_3.10.200.v20150831-0856 [0]>
    com.liferay.portal.kernel.model; version="2.0.0" <org.eclipse.osgi_3.10.200.v20150831-0856 [0]>
    org.osgi.framework; version="1.8.0" <org.eclipse.osgi_3.10.200.v20150831-0856 [0]>
  No fragment bundles
  No required bundles

Then i wrote a portlet that requires this service. The bnd.dnd file is

Bundle-Name: it.peernetwork.lr.pilot.test-portlet
Bundle-SymbolicName: pilot--test-portlet
Bundle-Version: 7.1.0

Import-Package: \
    it.peernetwork.lr.pilot.testservices;version=[7.1.0],\
    *

-metatype: *

And once deployed it loads correctly the service and uses it with no issues.

Now...my problem is i need a new version of the service, but i do not want to undeploy the current version.

So i wrote the new version of the service and the new bnd.bnd file is

Bundle-Name: it.peernetwork.lr.pilot.test-services
Bundle-SymbolicName: pilot--test-services
Bundle-Version: 7.1.1
Bundle-Activator: it.peernetwork.lr.pilot.testservices.impl.TestServicesActivator
Export-Package: it.peernetwork.lr.pilot.testservices

(the only difference is the Bundle-Version)

Once packaged the only difference in manifest file is the Export-Package line

Export-Package: it.peernetwork.lr.pilot.testservices;version="7.1.1"

and looks like it smoothly deploys on osgi

g! lb pilot
START LEVEL 20
   ID|State      |Level|Name
 1014|Active     |   10|it.peernetwork.lr.pilot.test-services (7.1.0)
 1015|Active     |   10|it.peernetwork.lr.pilot.test-services (7.1.1)

but with the command bundle ___NEW_BUNDLE_ID___ i get

No exported packages

instead of

Exported packages
  it.peernetwork.lr.pilot.testservices; version="7.1.1"[exported]

(that i expected). It means (to me) that the bundle is deployed, but none of its services are exposed.

Then i updated my portlets bundle (bnd.bnd) this way

Bundle-Name: it.peernetwork.lr.pilot.test-portlet
Bundle-SymbolicName: pilot--test-portlet
Bundle-Version: 7.1.0

Import-Package: \
    it.peernetwork.lr.pilot.testservices;version=[7.1.1],\
    *

-metatype: *

(changed version in Import-Package) and deployed on osgi. Deploy was correct, but it still uses the old version of the services bundle (7.1.0) even if Import-Package declares version 7.1.1. It does not give error because of the "missing" requested service version.

Can someone give me some tips about what i am doing wrong?

Thanks in advance.


UPDATE 1

Dependencies in build.gradle file have been updated accordingly with the version specified in bnd.bnd.


UPDATE 2

@quatax

The updated portlet bundle's manifest file has Import-Package: it.peernetwork.lr.pilot.testservices;version="[7.1.1]" (followed by all other required imports)...seems correct to me...


UPDATE 3

@Neil Bartlett (Service version updated to 8.0.0)

I updated pilot--test-services bnd.bnd file setting Bundle-Version: 8.0.0. The whole bnd.bnd file is

Bundle-Name: it.peernetwork.lr.pilot.test-services
Bundle-SymbolicName: pilot--test-services
Bundle-Version: 8.0.0
Bundle-Activator: it.peernetwork.lr.pilot.testservices.impl.TestServicesActivator
Export-Package: it.peernetwork.lr.pilot.testservices

Manifest file is

Manifest-Version: 1.0
Bnd-LastModified: 1543316524201
Bundle-Activator: it.peernetwork.lr.pilot.testservices.impl.TestServic
 esActivator
Bundle-ClassPath: .,lib/pn--logger-7.1.0.jar,lib/pn--services-base-7.1
 .0.jar,lib/pn--prop-files-7.1.0.jar,lib/gson-2.8.5.jar,lib/pn--expand
 o-values-7.1.0.jar
Bundle-ManifestVersion: 2
Bundle-Name: it.peernetwork.lr.pilot.test-services
Bundle-SymbolicName: pilot--test-services
Bundle-Version: 8.0.0
Created-By: 1.8.0_191 (Oracle Corporation)
Export-Package: it.peernetwork.lr.pilot.testservices;version="8.0.0"
Import-Package: com.liferay.expando.kernel.exception;version="[1.0,2)"
 ,com.liferay.expando.kernel.model;version="[1.1,2)",com.liferay.expan
 do.kernel.service;version="[1.1,2)",com.liferay.portal.kernel.excepti
 on;version="[7.2,8)",com.liferay.portal.kernel.model;version="[2.0,3)
 ",it.peernetwork.lr.pilot.testservices,org.osgi.framework;version="[1
 .8,2)"
Javac-Debug: on
Javac-Deprecation: off
Javac-Encoding: UTF-8
Private-Package: it.peernetwork.lr.pilot.test.services.impl,it.peernet
 work.lr.pilot.testservices.impl,it.peernetwork.lr.pilot.testservices.
 x,lib,it.peernetwork.lr.logger,it.peernetwork.lr.servicesbase,it.peer
 network.lr.propfiles,it.peernetwork.lr.propfiles.utils,com.google.gso
 n,com.google.gson.annotations,com.google.gson.internal,com.google.gso
 n.internal.bind,com.google.gson.internal.bind.util,com.google.gson.in
 ternal.reflect,com.google.gson.reflect,com.google.gson.stream,it.peer
 network.lr.expandovalues
Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.8))"
Tool: Bnd-3.2.0.201605172007

. When i deploy it it seems ok (starts and status is "Active"), but bundle ___ID___ says No exported packages

Then i updated pilot--test-portlet's build.gradle setting the correct version of the dependency (8.0.0). It's manifes file says Import-Package: it.peernetwork.lr.pilot.testservices;version="[8.0,9). When i deploy this package it does not start (status "Installed" and error Unresolved requirement: Import-Package: it.peernetwork.lr.pilot.testservices; version="[8.0.0,9.0.0)")


UPDATE 4 -- -- RESOLVED Thank you very much @Neil. Thanks to your tips i solved my issue. The right advice was this.

Explicitally declaring

Import-Package: it.peernetwork.lr.pilot.testservices;version="[7.2,8)",\
        *

in the pilot--test-services's bnd.bnd file (self import, with the opportune version range) makes osgi export correctly the package and loads the correct class instances when required.

The complete bnd.bnd file is

Bundle-Name: it.peernetwork.lr.pilot.test-services
Bundle-SymbolicName: pilot--test-services
Bundle-Version: 7.2.0
Bundle-Activator: it.peernetwork.lr.pilot.testservices.impl.TestServicesActivator
Export-Package: it.peernetwork.lr.pilot.testservices

Import-Package: it.peernetwork.lr.pilot.testservices;version="[7.2,8)",\
        *

In pilot--test-portlet's build.gradle file i only had to update the dependency version

compileOnly group: "it.peernetwork.lr", name: "pilot--test-services", version: "7.2.0"

I tried with version 7.1.0, 7.2.0 and 8.0.0 and works smoothly. All "services" bundles are deployed on osgi. Deploying the portlet bundle with different dependency versions always takes the right service.

Thank you again.

Trio
  • 78
  • 7
  • Did you take a look in the log? Has there been a `org.osgi.framework.BundleException` while deploying your Test-Portlet? – qutax Nov 20 '18 at 23:24
  • Hi @quatax. I have no exceptions in tomcat's log (it says the bundle "STARTED") and the portlet bundle has `Status=ACTIVE` on osgi (seen by `bundle ___PORTLET_BUNDLE_ID___` command). I know no more log files i can check...if there are more logs, please tell me where to find them, so i can take a look. – Trio Nov 21 '18 at 08:15
  • It was more or less a wild guess. What does your updated Test-Portlet's manifest say about the imported packages? Could you upload your project somewhere (e.g. GitHub)? It would definitely help to see the whole project, including all gradle files - otherwise I'd have to keep guessing. – qutax Nov 21 '18 at 09:43
  • I updated the answer with the required info. I'm sorry but i am not allowed to public this code as it's company property. I'll try to give you all informations you need here, but i cannot share the whole project (don't know if java files can be relevant in this context) – Trio Nov 21 '18 at 09:54
  • As you can't share the whole project (and nobody would be interested in the exact code anyways), I recommend to build an [MCVE](https://stackoverflow.com/help/mcve) - not only could it be shared easily, but it's a great way to isolate the exact issue at hand and learn about the underlying basics. On a related note: I see it as weird that one bundle is dependent on an *exact* version of another bundle. Consider using semantic versioning and make your portlet dependent on the API of the services. Then deploy the required version of the services. Update major version only if absolutely necessary – Olaf Kock Nov 23 '18 at 08:41
  • Thank you Olaf, i'll try to provide an MCVE as soon as possibile. It could take me a while because i use some "core dependencies" with the standard functionalities of portlets and services that i cannot share. – Trio Nov 23 '18 at 14:44

1 Answers1

3

I recommend deleting the Import-Package instruction from your bnd.bnd file for the pilot--test-portlet bundle. It is not necessary: bnd will generate all the imports you need based on the actual requirements in your code.

Your original bundle pilot--test-services both exports and imports the package it.peernetwork.lr.pilot.testservices. This is correct when your bundle makes use of the package itself in addition to exporting it. Therefore when you install both v7.1.0 and v7.1.1, the v7.1.0 bundle will import the package from the other version instead of exporting the older version. The pilot--test-portlet correctly imports from version 7.1.1.

In other words, everything here is working the way it should.

Neil Bartlett
  • 23,743
  • 4
  • 44
  • 77
  • Hello Neil and thank you for the answer. If i remove `Import-Package` the import in manifest file becomes `it.peernetwork.lr.pilot.testservices;version="[7.1,8)"`. This is ok in this scenario so i'll do it, but if i want (in another project maybe) refer to a specific version of a bundle, the way i was going is the correct one, right? Anyway, you sad: **The pilot--test-portlet correctly imports from version 7.1.1.** It does not. I have different log messages in these versions and i can tell the version used by `pilot--test-portlet` is 7.1.0. Maybe i am missing somethig... – Trio Nov 23 '18 at 14:38
  • You said ***Your original bundle pilot--test-services both exports and imports the package it.peernetwork.lr.pilot.testservices***. I do not understand what you mean with " both exports and imports"...i only export it...how do i import? – Trio Nov 23 '18 at 14:49
  • Oh yes I see. The "newer" bundle is importing from the "older" bundle. This is also a valid outcome. – Neil Bartlett Nov 23 '18 at 14:56
  • Regarding the import and export, you can see this in the manifest for `pilot--test-services`. Bnd has generated this because your bundle both exports the package and uses it internally. In order to reduce class space fragmentation, importing the package from the other version is better than creating a second export. I cannot see any good reason for exporting both version 7.1.0 and version 7.1.1 of the same package! – Neil Bartlett Nov 23 '18 at 14:58
  • I've checked manifest file of `pilot--test-services` and there is the package both in import and export. Now i got it. Thank you. **Oh yes I see. The "newer" bundle is importing from the "older" bundle. This is also a valid outcome**: this means i have no "explicit" way to "choose" wich version of a service my portlet will use? OSGi loads "random" class and i can only undeploy old versions or change major release? – Trio Nov 23 '18 at 16:01
  • I did try to deploy `pilot--test-services` version 8.0.0...manifest says `Export-Package: it.peernetwork.lr.pilot.testservices;version="8.0.0"` and it deploys and starts but `bundle ___ID___` says `No exported packages` and `pilot--test-portlet` (who has `t.peernetwork.lr.pilot.testservices;version="[8.0,9)"` as import in manifest file) does not start (`Unresolved requirement: Import-Package: it.peernetwork.lr.pilot.testservices; version="[8.0.0,9.0.0)"`). I'm kinda confused now...i should be able to deploy different major releases of the same package right? – Trio Nov 23 '18 at 16:32
  • Yes of course. Please update the question text with full details of the change you made and the outcome. – Neil Bartlett Nov 26 '18 at 09:35
  • There is not sufficient information in the lastest update. Please post the manifest of `pilot--test-services`. – Neil Bartlett Nov 27 '18 at 11:00
  • Hello Neil. I updated the question posting the whole manifest file of pilot--test-services. – Trio Nov 27 '18 at 11:05
  • Okay there's the problem. The bundle `pilot--test-services` is importing package `it.peernetwork.lr.pilot.testservices` with no version range. Therefore although you export it as version 8.0.0, you are still importing the 7.x version and this take precedence over the export. That import would not normally be generated by bnd so there might be something weird in the bnd.bnd for `pilot--test-services`. Please post the latest state of that file. – Neil Bartlett Nov 27 '18 at 15:07
  • I updated the question with the `bnd.bnd` file. I had i try putting a `Import-Package` that "auto-import" `pilot--test-services` in itself, but was with another version (maybe 7.1.1 --> `Import-Package: it.peernetwork.lr.pilot.testservices; version="[7.1.1]`). Will try with 8.0.0 as soon as i can and post the result – Trio Nov 27 '18 at 17:13