14 Replies Latest reply: Jun 4, 2012 10:02 PM by kmkwarrior RSS

    OSGI bundle creation using third party jar files

    kmkwarrior Community Member

      Hi,

       

      I have a requirement to integrate our CQ5.5 based application with a third party. As part of this, I have to use some jar files. After lot of digging, I realised that the way to use external jar files is to create an OSGI bundle wrapping these jar files.

      So, I tried to do the same, but when I see the service details in system/console, I see errors. Can someone please suggest what could have gone wrong?

       

      I have to use 3 jar files. So, I placed them in lib folder as shown in the screenshot below.

      1.png

       

      Ibuilt the bundle using .bnd file. It generated a jar file in install folder and I am able to to see its details in system/console/bundles. When I try to view details of the OSGI bundle that is installed, I see errors as follows:

      2.png

        • 1. Re: OSGI bundle creation using third party jar files
          kmkwarrior Community Member

          Can anyone help me with this?

          • 2. Re: OSGI bundle creation using third party jar files
            justin_at_adobe Adobe Employee

            When you embed JARs like this, the contents of those JARs become part of your bundle and any packages used by those JARs are, by necessity, added to the Import-Package directive.

             

            Given that CQ includes commons-lang and commons-code, I wonder why you're embedding them.

            • 3. Re: OSGI bundle creation using third party jar files
              kmkwarrior Community Member

              Hi Justin,

               

              Thanks for your response. I received 3 JAR files, commons-lang.jar, commons-code.jar and oauth-core.jar to include as part of OSGi bundle. So, I included all these 3 jar files. But, I observed that for commons-lang and commons-code it is using the ones available with CQ5 setup. So, I also tried to have only one jar file, oauth-core.jar, in the bundle. But, I still get errors. Given below are the steps I am using to create a bundle that embeds 3rd party (external) jar files.

               

              step 1: Bundle creation

              1.png

               

              Step 2: Bnd file verification

              1.png

               

              Step 3: Add external JAR file to libs folder

              1.png

               

              Step 4: Build the bundle and verify in system/console/bundles/. Build doesn't have error but it  still shows error as follows:

              1.png

               

              Can you please let me know what could have gone wrong?

               

              Thanks

              • 4. Re: OSGI bundle creation using third party jar files
                justin_at_adobe Adobe Employee

                Those unresolved packages are dependencies of your bundle and are not resolvable in the container. Nothing went wrong.

                • 5. Re: OSGI bundle creation using third party jar files
                  kmkwarrior Community Member

                  I am sorry, I didn't understand this quite well. Does this mean that the bundle is well and I can proceed further. But I am not able to start this bundle.

                  • 6. Re: OSGI bundle creation using third party jar files
                    justin_at_adobe Adobe Employee

                    For a bundle to start, all of its imports (unless they are optional) must be exported into the OSGi container. In this case, you have two batches of problematic imports:

                    * net.oauth.* - these appear to be exported by the bundle itself, so once the other issue is resolved, this will be as well.

                    * org.apache.http.* - IIRC, these are packages exported by Apache HTTP Client 4.x. This is not available by default in CQ 5.5, so you will need to obtain this bundle and install it. I belive the standard distribution of HTTP Client is an OSGi bundle, but I imagine some Googling will confirm that for you.

                     

                    I would, btw, not recommend using Export-Package: *. This means that the entire contents of your bundle is exported. Typically (although there are exceptions), this isn't warranted. OSGi bundles should export as little as possible. In many cases, bundles don't need to export ANYTHING because they are simply implementing service interfaces defined in other bundles and exposing those implementations via the Service Registry.

                    • 7. Re: OSGI bundle creation using third party jar files
                      kmkwarrior Community Member

                      Thanks for your response.

                       

                      If I understand this correctly, I need to first resolve "org.apache.http.*" related problems. This would then automatically resolve "net.oauth.*" related problem.

                       

                      In order to resolve, "org.apache.http.*" related problem, I downloaded Apache HTTP Client 4.2 OSGi bundle from http://mirror.symnds.com/software/Apache//httpcomponents/httpcore/binary/httpcomponents-co re-4.2-osgi-bin.zip and installed it on my CQ instance. This now shows the packages list as shown below:

                      1.png

                      Now, if I try to create a bundle adding external jar oauth-core.jar file, it still shows errors as follows:

                      1.png

                      Any idea what could have gone wrong?

                       

                      Regarding usage of 'Export-Package: *', What I am trying to do is - I have a requirement to integrate my CQ based application with a third party. The third party gives me 4 jar files and I have to make use of functions present in those jar files. As per my understanding, in order to be able to use those external jar files, I need to create OSGi bundles using the jar files. I used the steps I shared in my earlier post to create an OSGi bundle using jar file. Since, I do not know, which function I might have to use as part of integration, I am trying to include everything that is part of jar files into OSGi bundle. Let me know if my approach for this task is wrong. I hope this would help to understand my task.

                      • 8. Re: OSGI bundle creation using third party jar files
                        justin_at_adobe Adobe Employee

                        It's not clear to me from the screenshots what is going wrong. Can you post the actual Import-Package directive - in plain text, not a screenshot?

                         

                        Regarding Export-Package, your understanding isn't necessarily accurate. Only the packages which will be used by other bundles (or, in the case of CQ, JSPs) need to be exported. Without knowing what your bundle does or how other code interact with it, I can't provide any better guidance other than to say that 9 times out of 10, when I see Export-Package: *, it's wrong.

                        • 9. Re: OSGI bundle creation using third party jar files
                          kmkwarrior Community Member

                          Here is my bnd file with all directives:

                          Export-Package: *

                          Import-Package: *

                          Private-Package: *

                          # Include-Resource:

                          Bundle-Name: External Jar OSGI

                          Bundle-Description: This OSGI bundle contains external jar file

                          Bundle-SymbolicName: com.self.learning.externaljarosgi

                          Bundle-Version: 1.0.0-SNAPSHOT

                          Bundle-Activator: com.self.learning.osgi.oauth.Activator

                           

                          And here is what is shown in systems/console/bundle:

                          Imported Packagesjavax.crypto from org.apache.felix.framework (0)

                          javax.crypto.spec from org.apache.felix.framework (0)

                          javax.servlet from cqse-httpservice (21)

                          javax.servlet.http from cqse-httpservice (21)

                          net.oauth -- Cannot be resolved

                          net.oauth.client -- Cannot be resolved

                          net.oauth.client.httpclient3 -- Cannot be resolved

                          net.oauth.client.httpclient4 -- Cannot be resolved

                          net.oauth.server -- Cannot be resolved

                          net.oauth.signature -- Cannot be resolved

                          org.apache.commons.codec.binary from org.apache.commons.codec (46)

                          org.apache.commons.httpclient from com.day.commons.osgi.wrapper.commons-httpclient (77)

                          org.apache.commons.httpclient.methods from com.day.commons.osgi.wrapper.commons-httpclient (77)

                          org.apache.http -- Cannot be resolved

                          org.apache.http.client -- Cannot be resolved

                          org.apache.http.client.methods -- Cannot be resolved

                          org.apache.http.entity -- Cannot be resolved

                          org.apache.http.impl.client -- Cannot be resolved

                          org.apache.http.params -- Cannot be resolved

                          org.apache.http.util -- Cannot be resolved

                          org.osgi.framework from org.apache.felix.framework (0)

                          org.slf4j from slf4j.api (11)

                           

                           

                          Regarding Export-Package, I know the function, class and its package I need to use. Would it be fine if I just export that package instead of using '*'? In such case, if this has dependency on some function in one of the 4 jar files I need to use, how does it get resolved? Do I need to Export those packages as well?

                          • 10. Re: OSGI bundle creation using third party jar files
                            justin_at_adobe Adobe Employee

                            As I said, please post the actual Import-Package directive.

                             

                            Yes - you should only export the package you need.

                            • 11. Re: OSGI bundle creation using third party jar files
                              kmkwarrior Community Member

                              Hi,

                               

                              Please find below Import-Package directive information:

                               

                              Import-Package: com, com.gsfn, com.self.learning.osgi.oauth, javax.crypto, javax.crypto.spec, javax.servlet, javax.servlet.http, net.oauth, net.oauth.client, net.oauth.client.httpclient3, net.oauth.client.httpclient4, net.oauth.server, net.oauth.signature, org.apache.commons.codec.binary, org.apache.commons.httpclient, org.apache.commons.httpclient.methods, org.apache.commons.lang, org.apache.http, org.apache.http.client, org.apache.http.client.methods, org.apache.http.entity, org.apache.http.impl.client, org.apache.http.params, org.apache.http.util, org.osgi.framework, org.slf4j

                              • 12. Re: OSGI bundle creation using third party jar files
                                justin_at_adobe Adobe Employee

                                I think you also need another httpcomponents bundle. The client bundles don't seem to be exported by the httpcomponents bundle you did install. Sorry - I don't know much about the packaging of HttpClient 4.x. I find the 3.x API much easier to use and haven't needed to upgrade any of my code.

                                 

                                Is it possible that this oauth JAR supports both HTTP Client 3.x and 4.x? I see both packages. If so, perhaps you don't need to import org.apache.http.* at all and can just import org.apache.commons.httpclient.* (which is exported by a CQ-provided bundle).

                                • 13. Re: OSGI bundle creation using third party jar files
                                  kmkwarrior Community Member

                                  Please ignore previous one. I was making changes with additional jar files. Here is the import directive which is related to the screenshots I posted earlier:

                                   

                                  Import-Package: com.self.learning.osgi.oauth, javax.crypto, javax.crypto.spec, javax.servlet, javax.servlet.http, net.oauth, net.oauth.client, net.oauth.client.httpclient3, net.oauth.client.httpclient4, net.oauth.server, net.oauth.signature, org.apache.commons.codec.binary, org.apache.commons.httpclient, org.apache.commons.httpclient.methods, org.apache.http, org.apache.http.client, org.apache.http.client.methods, org.apache.http.entity, org.apache.http.impl.client, org.apache.http.params, org.apache.http.util, org.osgi.framework, org.slf4j

                                  • 14. Re: OSGI bundle creation using third party jar files
                                    kmkwarrior Community Member

                                    Thanks a lot for your help. I used the jars httpclient-4.2.jar, httpcore-4.2.jar and oauth-core.jar. And it all started working very well.