You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@sling.apache.org by JCR <jc...@proxymit.net> on 2020/03/28 19:54:54 UTC

Calling OSGi Service from JSP

Hello

I wrote a very simple POJO, built it as OSGi bundle and deployed it to 
Sling11. Here's the source:

package rome.testly.testly1;
import org.osgi.service.component.annotations.Component;

@Component(service=SlingFutil.class )
public class SlingFutil {

     public String nonsense() {
         return "This is nonsense";
     }
}

The component/service appears on the Felix console:

symbolic Name 	testly1
Version 	0.0.1.202003271754
Bundle Location 	/home/juerg/eclipse-workspace/testly1/target/classes
Last Modification 	Fri Mar 27 18:54:01 CET 2020
Start Level 	20
Exported Packages 	---
Imported Packages 	org.osgi.framework,version=1.9.0 from 
org.apache.felix.framework (0) 
<http://localhost:8080/system/console/bundles/0>
org.osgi.service.component,version=1.4.0 from org.apache.felix.scr (50) 
<http://localhost:8080/system/console/bundles/50>
org.slf4j,version=1.7.25 from slf4j.api (20) 
<http://localhost:8080/system/console/bundles/20>
Service ID 477 <http://localhost:8080/system/console/services/477> 
Types: java.lang.Runnable
Component Name: rome.testly.testly1.SimpleDSComponent
Component ID: 323
Service ID 478 <http://localhost:8080/system/console/services/478> 
Types: rome.testly.testly1.SlingFutil
Component Name: rome.testly.testly1.SlingFutil
Component ID: 324



Now I try to call SlingFutil.nonsense() from a JSP:

...
<%@ page import="javax.jcr.*,
         org.apache.sling.api.resource.Resource,
         rome.testly.testly1.*"
%>
...
<%
SlingFutil some = new SlingFutil();
%>
...

At this point, the JSP compiler complains that the type (SlingFutil) is 
unknown.
I also tried

sling.getService(rome.testly.testly1.SlingFutil.class);

instead, but the result is the same.

This used to work well at BND times of Sling, so I don't know what's 
wrong with this R7 code.

Thanks,
Juerg


Re: Calling OSGi Service from JSP

Posted by Jörg Hoh <jh...@googlemail.com.INVALID>.
Hi Jürg

Hi Jörg
>
> The class is declared as @Component (see above). Do I need any other
> declaration to make the export happen?
>
>
>
You need to have the package name appear in the "exported packages" section
of the bundle. The maven bundle plugin has some heuristics for reasonable
defaults.
Can you share your complete project code?

Jörg


-- 
Cheers,
Jörg Hoh,

http://cqdump.wordpress.com
Twitter: @joerghoh

Re: Calling OSGi Service from JSP

Posted by JCR <jc...@proxymit.net>.

On 08.04.2020 15:58, Oliver Lietz wrote:
> On Wednesday, April 8, 2020 1:42:17 PM CEST JCR wrote:
>> On 07.04.20 12:06, Bertrand Delacretaz wrote:
>>> Hi,
> Hi,
>
>>> On Tue, Apr 7, 2020 at 11:37 AM JCR <jc...@proxymit.net> wrote:
>>>> ...But what I
>>>> observe is, that MANIFEST.MF gets overriden on every change and even
>>>> package-info is present, the package in there gets set back to to
>>>> private...>
>>> It looks like your build setup is causing trouble.
>>>
>>> I recommend that you compare your project with one of ours that you
>>> can gradually adapt to your needs.
>>>
>>> You might start with
>>> https://github.com/apache/sling-org-apache-sling-commons-mime for
>>> example, which is a small standalone module change the public package
>>> and/or class names for your initial test, build, verify that you can
>>> use those services from your JSP and then gradually adapt or compare
>>> with your code.
>>>
>>> Hope this helps,
>>> -Bertrand
>> Hi Bertrand,
>>
>> Nice hearing from you!
>>
>> Well, the problem is that there's a lot of outdated docu around. For
>> instance, above commons-mime package contains a bnd.bnd file, which is
>> not generated anymore (well, perhaps it is, but just process temporary).
>> Among many others, the examples at felix.apache.org are not current,
>> too. So, I've been chasing a lot of phantoms in the past few days. That
>> wasn't the fun Sling usually provides.
>>
>> As mentioned in my mail, there must be some hidden detail. Well, after
>> days of research, I found it in the readme of the bnd-maven-plugin
>> (https://github.com/bndtools/bnd/blob/master/maven/bnd-maven-plugin/README.m
>> d):
>>
>> <configuration>
>>           <bnd><![CDATA[
>> -exportcontents:\
>> rome.testx.testaa
>> -sources: true
>> ]]></bnd>
>> </configuration>
>>
>> So this is the secret sauce that does the job. That needs to be added to
>> the project's pom.xml.
> Well, it's not a secret. See my previous mail where I pointed to Sling Parent
> and the configuration for bnd Maven Plugin. It contains the default for all
> Sling bundle modules. Additional configuration is added in bnd.bnd file (the
> file is not generated but created manually).
>
> Regards,
> O.
>
>> Keywords: solved, osgi, bundle, bnd-maven-plugin 1.2.2, export-package,
>> felix
>>
>> -Juerg
>>
@Oliver: Agree, I understood your email only after getting further explanation. I did not understand the parent syntax 
in the first place as I am not a build specialist (remember, I sent this to the users list). My open question is if the 
current sling maven plugin fully supports OSGi R7 annotations (I expected @Component in combination with 
package-info.java to do export, also after watching the preso from adaptTo by Chris Schneider)?
Anyway, thanks a lot for your support!
-J

Re: Calling OSGi Service from JSP

Posted by Oliver Lietz <ap...@oliverlietz.de>.
On Wednesday, April 8, 2020 1:42:17 PM CEST JCR wrote:
> On 07.04.20 12:06, Bertrand Delacretaz wrote:
> > Hi,

Hi,

> > On Tue, Apr 7, 2020 at 11:37 AM JCR <jc...@proxymit.net> wrote:
> >> ...But what I
> >> observe is, that MANIFEST.MF gets overriden on every change and even
> >> package-info is present, the package in there gets set back to to
> >> private...> 
> > It looks like your build setup is causing trouble.
> > 
> > I recommend that you compare your project with one of ours that you
> > can gradually adapt to your needs.
> > 
> > You might start with
> > https://github.com/apache/sling-org-apache-sling-commons-mime for
> > example, which is a small standalone module change the public package
> > and/or class names for your initial test, build, verify that you can
> > use those services from your JSP and then gradually adapt or compare
> > with your code.
> > 
> > Hope this helps,
> > -Bertrand
> 
> Hi Bertrand,
> 
> Nice hearing from you!
> 
> Well, the problem is that there's a lot of outdated docu around. For
> instance, above commons-mime package contains a bnd.bnd file, which is
> not generated anymore (well, perhaps it is, but just process temporary).
> Among many others, the examples at felix.apache.org are not current,
> too. So, I've been chasing a lot of phantoms in the past few days. That
> wasn't the fun Sling usually provides.
> 
> As mentioned in my mail, there must be some hidden detail. Well, after
> days of research, I found it in the readme of the bnd-maven-plugin
> (https://github.com/bndtools/bnd/blob/master/maven/bnd-maven-plugin/README.m
> d):
> 
> <configuration>
>          <bnd><![CDATA[
> -exportcontents:\
> rome.testx.testaa
> -sources: true
> ]]></bnd>
> </configuration>
> 
> So this is the secret sauce that does the job. That needs to be added to
> the project's pom.xml.

Well, it's not a secret. See my previous mail where I pointed to Sling Parent 
and the configuration for bnd Maven Plugin. It contains the default for all 
Sling bundle modules. Additional configuration is added in bnd.bnd file (the 
file is not generated but created manually).

Regards,
O.

> Keywords: solved, osgi, bundle, bnd-maven-plugin 1.2.2, export-package,
> felix
> 
> -Juerg






Re: Calling OSGi Service from JSP

Posted by Bertrand Delacretaz <bd...@apache.org>.
Hi Juerg,

On Wed, Apr 8, 2020 at 1:42 PM JCR <jc...@proxymit.net> wrote:
> ...So this is the secret sauce that does the job. That needs to be added to
> the project's pom.xml...

Ok, glad you found out and sorry if our (lack of) examples made your
life harder than it should be!

-Bertrand

Re: Calling OSGi Service from JSP

Posted by JCR <jc...@proxymit.net>.
On 07.04.20 12:06, Bertrand Delacretaz wrote:
> Hi,
>
> On Tue, Apr 7, 2020 at 11:37 AM JCR <jc...@proxymit.net> wrote:
>> ...But what I
>> observe is, that MANIFEST.MF gets overriden on every change and even
>> package-info is present, the package in there gets set back to to private...
> It looks like your build setup is causing trouble.
>
> I recommend that you compare your project with one of ours that you
> can gradually adapt to your needs.
>
> You might start with
> https://github.com/apache/sling-org-apache-sling-commons-mime for
> example, which is a small standalone module change the public package
> and/or class names for your initial test, build, verify that you can
> use those services from your JSP and then gradually adapt or compare
> with your code.
>
> Hope this helps,
> -Bertrand

Hi Bertrand,

Nice hearing from you!

Well, the problem is that there's a lot of outdated docu around. For 
instance, above commons-mime package contains a bnd.bnd file, which is 
not generated anymore (well, perhaps it is, but just process temporary). 
Among many others, the examples at felix.apache.org are not current, 
too. So, I've been chasing a lot of phantoms in the past few days. That 
wasn't the fun Sling usually provides.

As mentioned in my mail, there must be some hidden detail. Well, after 
days of research, I found it in the readme of the bnd-maven-plugin 
(https://github.com/bndtools/bnd/blob/master/maven/bnd-maven-plugin/README.md):

<configuration>
         <bnd><![CDATA[
-exportcontents:\
rome.testx.testaa
-sources: true
]]></bnd>
</configuration>

So this is the secret sauce that does the job. That needs to be added to 
the project's pom.xml.

Keywords: solved, osgi, bundle, bnd-maven-plugin 1.2.2, export-package, 
felix

-Juerg



Re: Calling OSGi Service from JSP

Posted by Bertrand Delacretaz <bd...@apache.org>.
Hi,

On Tue, Apr 7, 2020 at 11:37 AM JCR <jc...@proxymit.net> wrote:
> ...But what I
> observe is, that MANIFEST.MF gets overriden on every change and even
> package-info is present, the package in there gets set back to to private...

It looks like your build setup is causing trouble.

I recommend that you compare your project with one of ours that you
can gradually adapt to your needs.

You might start with
https://github.com/apache/sling-org-apache-sling-commons-mime for
example, which is a small standalone module change the public package
and/or class names for your initial test, build, verify that you can
use those services from your JSP and then gradually adapt or compare
with your code.

Hope this helps,
-Bertrand

Re: Calling OSGi Service from JSP

Posted by JCR <jc...@proxymit.net>.
On 30.03.20 16:18, JCR wrote:
>
>> On Sunday, March 29, 2020 5:14:48 PM CEST JCR wrote:
>>>> Am Sa., 28. März 2020 um 20:55 Uhr schrieb JCR <jc...@proxymit.net>:
>>>>> Hello
>> Hi Juerg,
>>
>>>>> I wrote a very simple POJO, built it as OSGi bundle and deployed 
>>>>> it to
>>>>> Sling11. Here's the source:
>>>>>
>>>>> package rome.testly.testly1;
>>>>> import org.osgi.service.component.annotations.Component;
>>>>>
>>>>> @Component(service=SlingFutil.class )
>>>>> public class SlingFutil {
>>>>>
>>>>>        public String nonsense() {
>>>>>                   return "This is nonsense";
>>>>>               }
>>>>>
>>>>> }
>>>>>
>>>>> The component/service appears on the Felix console:
>>>>>
>>>>> symbolic Name   testly1
>>>>> Version         0.0.1.202003271754
>>>>> Bundle Location
>>>>>
>>>>>    /home/juerg/eclipse-workspace/testly1/target/classes
>>>>>
>>>>> Last Modification       Fri Mar 27 18:54:01 CET 2020
>>>>> Start Level     20
>>>>> Exported Packages       ---
>>>>> Imported Packages       org.osgi.framework,version=1.9.0 from
>>>>> org.apache.felix.framework (0)
>>>>> <http://localhost:8080/system/console/bundles/0>
>>>>> org.osgi.service.component,version=1.4.0 from org.apache.felix.scr 
>>>>> (50)
>>>>> <http://localhost:8080/system/console/bundles/50>
>>>>> org.slf4j,version=1.7.25 from slf4j.api (20)
>>>>> <http://localhost:8080/system/console/bundles/20>
>>>>> Service ID 477 <http://localhost:8080/system/console/services/477>
>>>>> Types: java.lang.Runnable
>>>>> Component Name: rome.testly.testly1.SimpleDSComponent
>>>>> Component ID: 323
>>>>> Service ID 478 <http://localhost:8080/system/console/services/478>
>>>>> Types: rome.testly.testly1.SlingFutil
>>>>> Component Name: rome.testly.testly1.SlingFutil
>>>>> Component ID: 324
>>>>>
>>>>>
>>>>>
>>>>> Now I try to call SlingFutil.nonsense() from a JSP:
>>>>>
>>>>> ...
>>>>> <%@ page import="javax.jcr.*,
>>>>>
>>>>>            org.apache.sling.api.resource.Resource,
>>>>>            rome.testly.testly1.*"
>>>>>
>>>>> %>
>>>>> ...
>>>>> <%
>>>>> SlingFutil some = new SlingFutil();
>>>>> %>
>>>>> ...
>>>>>
>>>>> At this point, the JSP compiler complains that the type 
>>>>> (SlingFutil) is
>>>>> unknown.
>>>>> I also tried
>>>>>
>>>>> sling.getService(rome.testly.testly1.SlingFutil.class);
>>>>>
>>>>> instead, but the result is the same.
>>>>>
>>>>> This used to work well at BND times of Sling, so I don't know what's
>>>>> wrong with this R7 code.
>>>>>
>>>>> Thanks,
>>>>> Juerg
>>>> On 28.03.20 21:15, Jörg Hoh wrote:
>>>>> Hi Juerg,
>>>>>
>>>>> Looks like your bundle does not export that class, so it's not 
>>>>> available
>>>>> outside of that bundle.
>>>>>
>>>>> Jörg
>>> Hi Jörg
>>>
>>> The class is declared as @Component (see above). Do I need any other
>>> declaration to make the export happen?
>
> You need to have the package name appear in the "exported packages" 
> section
> of the bundle. The maven bundle plugin has some heuristics for reasonable
> defaults.
> Can you share your complete project code?
>
> Jörg
>
>
> On 29.03.20 21:08, Oliver Lietz wrote:
>
>> It depends on the plugin (and version) you use. See the configuration 
>> in Sling
>> Parent for bnd Maven Plugin:
>>
>> -exportcontents: 
>> ${removeall;${packages;VERSIONED};${packages;CONDITIONAL}}
>>
>> https://github.com/apache/sling-parent/blob/master/sling-bundle-parent/
>> pom.xml#L71
>>
>> And here is an example for a versioned (and therefore exported) package:
>>
>> @Version("2.3.4")
>> package org.apache.sling.api;
>>
>> https://github.com/apache/sling-org-apache-sling-api/blob/master/src/main/ 
>>
>> java/org/apache/sling/api/package-info.java
>>
>> Regards,
>> O.
>>
>>
>>> Thanks,
>>> Juerg
>
> @Jörg: You can find the OSGi bundle code here: 
> https://gitlab.com/yumeier/testly/-/tree/master/testly3
>             the calling JSP is here: 
> https://gitlab.com/yumeier/testlyclient
>
> @Oliver: -exportcontents: happy to check this, but gives me a hard 
> time to find this line. Where (file) exactly would it be located?
>               I added the package⁻info.java (see also testly3 repo 
> indicated above).
>
> Due to some changes in the project structure (adding gitlab), the 
> SlingFutil service does not appear anymore in the Felix console, while 
> the generated class SimpleDSComponent appears seemingly without a 
> problem. I still assume that I don't need to add the lifecycle methods 
> in the/(each) POJO, right?
>
> Thanks, Juerg
>
>
Not progressing. Package rome.testx.testaa with all its classes should 
be exported so it is usable by JSPs under jcr_root/apps (I am told).

I'm using version 1.2.2 of 
org.apache.sling.ide.m2e-feature.feature.group, hence the latest stable one.

I added this package-info.java:

@Version("1.0.0")
package rome.testx.testaa;

import org.osgi.annotation.versioning.Version;

According to this thread, this should export the package. But what I 
observe is, that MANIFEST.MF gets overriden on every change and even 
package-info is present, the package in there gets set back to to private:

Manifest-Version: 1.0
Bnd-LastModified: 1586195731720
Bundle-ManifestVersion: 2
Bundle-Name: testaa
Bundle-SymbolicName: testaa
Bundle-Version: 0.0.2.202004061755
Created-By: 11.0.6 (Ubuntu)
Import-Package: org.osgi.framework;version="[1.5,2)",org.osgi.service.
  component;version="[1.1,2)",org.slf4j;version="[1.7,2)"
Private-Package: rome.testx.testaa;version="1.0.0"
Provide-Capability: osgi.service;objectClass:List<String>="java.lang.R
  unnable"
Require-Capability: osgi.extender;filter:="(&(osgi.extender=osgi.compo
  nent)(version>=1.3.0)(!(version>=2.0.0)))",osgi.ee;filter:="(&(osgi.e
  e=JavaSE)(version=1.8))"
Service-Component: OSGI-INF/rome.testx.testaa.Radio.xml,OSGI-INF/rome.
  testx.testaa.SimpleDSComponent.xml
Tool: Bnd-3.5.0.201709291849

Consequently, the package remains unavailable to JSPs, the package is 
not visible to them and much less Radio.class.

There must be some hidden tweak. Thanks for any hint.
-Juerg



Re: Calling OSGi Service from JSP

Posted by JCR <jc...@proxymit.net>.
> On Sunday, March 29, 2020 5:14:48 PM CEST JCR wrote:
>>> Am Sa., 28. März 2020 um 20:55 Uhr schrieb JCR <jc...@proxymit.net>:
>>>> Hello
> Hi Juerg,
>
>>>> I wrote a very simple POJO, built it as OSGi bundle and deployed it to
>>>> Sling11. Here's the source:
>>>>
>>>> package rome.testly.testly1;
>>>> import org.osgi.service.component.annotations.Component;
>>>>
>>>> @Component(service=SlingFutil.class )
>>>> public class SlingFutil {
>>>>
>>>>        public String nonsense() {
>>>>        
>>>>            return "This is nonsense";
>>>>        
>>>>        }
>>>>
>>>> }
>>>>
>>>> The component/service appears on the Felix console:
>>>>
>>>> symbolic Name   testly1
>>>> Version         0.0.1.202003271754
>>>> Bundle Location
>>>>
>>>>    /home/juerg/eclipse-workspace/testly1/target/classes
>>>>
>>>> Last Modification       Fri Mar 27 18:54:01 CET 2020
>>>> Start Level     20
>>>> Exported Packages       ---
>>>> Imported Packages       org.osgi.framework,version=1.9.0 from
>>>> org.apache.felix.framework (0)
>>>> <http://localhost:8080/system/console/bundles/0>
>>>> org.osgi.service.component,version=1.4.0 from org.apache.felix.scr (50)
>>>> <http://localhost:8080/system/console/bundles/50>
>>>> org.slf4j,version=1.7.25 from slf4j.api (20)
>>>> <http://localhost:8080/system/console/bundles/20>
>>>> Service ID 477 <http://localhost:8080/system/console/services/477>
>>>> Types: java.lang.Runnable
>>>> Component Name: rome.testly.testly1.SimpleDSComponent
>>>> Component ID: 323
>>>> Service ID 478 <http://localhost:8080/system/console/services/478>
>>>> Types: rome.testly.testly1.SlingFutil
>>>> Component Name: rome.testly.testly1.SlingFutil
>>>> Component ID: 324
>>>>
>>>>
>>>>
>>>> Now I try to call SlingFutil.nonsense() from a JSP:
>>>>
>>>> ...
>>>> <%@ page import="javax.jcr.*,
>>>>
>>>>            org.apache.sling.api.resource.Resource,
>>>>            rome.testly.testly1.*"
>>>>
>>>> %>
>>>> ...
>>>> <%
>>>> SlingFutil some = new SlingFutil();
>>>> %>
>>>> ...
>>>>
>>>> At this point, the JSP compiler complains that the type (SlingFutil) is
>>>> unknown.
>>>> I also tried
>>>>
>>>> sling.getService(rome.testly.testly1.SlingFutil.class);
>>>>
>>>> instead, but the result is the same.
>>>>
>>>> This used to work well at BND times of Sling, so I don't know what's
>>>> wrong with this R7 code.
>>>>
>>>> Thanks,
>>>> Juerg
>>> On 28.03.20 21:15, Jörg Hoh wrote:
>>>> Hi Juerg,
>>>>
>>>> Looks like your bundle does not export that class, so it's not available
>>>> outside of that bundle.
>>>>
>>>> Jörg
>> Hi Jörg
>>
>> The class is declared as @Component (see above). Do I need any other
>> declaration to make the export happen?

You need to have the package name appear in the "exported packages" section
of the bundle. The maven bundle plugin has some heuristics for reasonable
defaults.
Can you share your complete project code?

Jörg


On 29.03.20 21:08, Oliver Lietz wrote:

> It depends on the plugin (and version) you use. See the configuration in Sling
> Parent for bnd Maven Plugin:
>
> -exportcontents: ${removeall;${packages;VERSIONED};${packages;CONDITIONAL}}
>
> https://github.com/apache/sling-parent/blob/master/sling-bundle-parent/
> pom.xml#L71
>
> And here is an example for a versioned (and therefore exported) package:
>
> @Version("2.3.4")
> package org.apache.sling.api;
>
> https://github.com/apache/sling-org-apache-sling-api/blob/master/src/main/
> java/org/apache/sling/api/package-info.java
>
> Regards,
> O.
>
>
>> Thanks,
>> Juerg

@Jörg: You can find the OSGi bundle code here: 
https://gitlab.com/yumeier/testly/-/tree/master/testly3
             the calling JSP is here: 
https://gitlab.com/yumeier/testlyclient

@Oliver: -exportcontents: happy to check this, but gives me a hard time 
to find this line. Where (file) exactly would it be located?
               I added the package⁻info.java (see also testly3 repo 
indicated above).

Due to some changes in the project structure (adding gitlab), the 
SlingFutil service does not appear anymore in the Felix console, while 
the generated class SimpleDSComponent appears seemingly without a 
problem. I still assume that I don't need to add the lifecycle methods 
in the/(each) POJO, right?

Thanks, Juerg


Re: Calling OSGi Service from JSP

Posted by Oliver Lietz <ap...@oliverlietz.de>.
On Sunday, March 29, 2020 5:14:48 PM CEST JCR wrote:
> > Am Sa., 28. März 2020 um 20:55 Uhr schrieb JCR <jc...@proxymit.net>:
> >> Hello

Hi Juerg,

> >> I wrote a very simple POJO, built it as OSGi bundle and deployed it to
> >> Sling11. Here's the source:
> >> 
> >> package rome.testly.testly1;
> >> import org.osgi.service.component.annotations.Component;
> >> 
> >> @Component(service=SlingFutil.class )
> >> public class SlingFutil {
> >> 
> >>       public String nonsense() {
> >>       
> >>           return "This is nonsense";
> >>       
> >>       }
> >> 
> >> }
> >> 
> >> The component/service appears on the Felix console:
> >> 
> >> symbolic Name   testly1
> >> Version         0.0.1.202003271754
> >> Bundle Location
> >> 
> >>   /home/juerg/eclipse-workspace/testly1/target/classes
> >> 
> >> Last Modification       Fri Mar 27 18:54:01 CET 2020
> >> Start Level     20
> >> Exported Packages       ---
> >> Imported Packages       org.osgi.framework,version=1.9.0 from
> >> org.apache.felix.framework (0)
> >> <http://localhost:8080/system/console/bundles/0>
> >> org.osgi.service.component,version=1.4.0 from org.apache.felix.scr (50)
> >> <http://localhost:8080/system/console/bundles/50>
> >> org.slf4j,version=1.7.25 from slf4j.api (20)
> >> <http://localhost:8080/system/console/bundles/20>
> >> Service ID 477 <http://localhost:8080/system/console/services/477>
> >> Types: java.lang.Runnable
> >> Component Name: rome.testly.testly1.SimpleDSComponent
> >> Component ID: 323
> >> Service ID 478 <http://localhost:8080/system/console/services/478>
> >> Types: rome.testly.testly1.SlingFutil
> >> Component Name: rome.testly.testly1.SlingFutil
> >> Component ID: 324
> >> 
> >> 
> >> 
> >> Now I try to call SlingFutil.nonsense() from a JSP:
> >> 
> >> ...
> >> <%@ page import="javax.jcr.*,
> >> 
> >>           org.apache.sling.api.resource.Resource,
> >>           rome.testly.testly1.*"
> >> 
> >> %>
> >> ...
> >> <%
> >> SlingFutil some = new SlingFutil();
> >> %>
> >> ...
> >> 
> >> At this point, the JSP compiler complains that the type (SlingFutil) is
> >> unknown.
> >> I also tried
> >> 
> >> sling.getService(rome.testly.testly1.SlingFutil.class);
> >> 
> >> instead, but the result is the same.
> >> 
> >> This used to work well at BND times of Sling, so I don't know what's
> >> wrong with this R7 code.
> >> 
> >> Thanks,
> >> Juerg
> > 
> > On 28.03.20 21:15, Jörg Hoh wrote:
> >> Hi Juerg,
> >> 
> >> Looks like your bundle does not export that class, so it's not available
> >> outside of that bundle.
> >> 
> >> Jörg
> 
> Hi Jörg
> 
> The class is declared as @Component (see above). Do I need any other
> declaration to make the export happen?

It depends on the plugin (and version) you use. See the configuration in Sling 
Parent for bnd Maven Plugin:

-exportcontents: ${removeall;${packages;VERSIONED};${packages;CONDITIONAL}}

https://github.com/apache/sling-parent/blob/master/sling-bundle-parent/
pom.xml#L71

And here is an example for a versioned (and therefore exported) package:

@Version("2.3.4")
package org.apache.sling.api;

https://github.com/apache/sling-org-apache-sling-api/blob/master/src/main/
java/org/apache/sling/api/package-info.java

Regards,
O.


> Thanks,
> Juerg





Re: Calling OSGi Service from JSP

Posted by JCR <jc...@proxymit.net>.
> Am Sa., 28. März 2020 um 20:55 Uhr schrieb JCR <jc...@proxymit.net>:
>
>> Hello
>>
>> I wrote a very simple POJO, built it as OSGi bundle and deployed it to
>> Sling11. Here's the source:
>>
>> package rome.testly.testly1;
>> import org.osgi.service.component.annotations.Component;
>>
>> @Component(service=SlingFutil.class )
>> public class SlingFutil {
>>
>>       public String nonsense() {
>>           return "This is nonsense";
>>       }
>> }
>>
>> The component/service appears on the Felix console:
>>
>> symbolic Name   testly1
>> Version         0.0.1.202003271754
>> Bundle Location
>>   /home/juerg/eclipse-workspace/testly1/target/classes
>> Last Modification       Fri Mar 27 18:54:01 CET 2020
>> Start Level     20
>> Exported Packages       ---
>> Imported Packages       org.osgi.framework,version=1.9.0 from
>> org.apache.felix.framework (0)
>> <http://localhost:8080/system/console/bundles/0>
>> org.osgi.service.component,version=1.4.0 from org.apache.felix.scr (50)
>> <http://localhost:8080/system/console/bundles/50>
>> org.slf4j,version=1.7.25 from slf4j.api (20)
>> <http://localhost:8080/system/console/bundles/20>
>> Service ID 477 <http://localhost:8080/system/console/services/477>
>> Types: java.lang.Runnable
>> Component Name: rome.testly.testly1.SimpleDSComponent
>> Component ID: 323
>> Service ID 478 <http://localhost:8080/system/console/services/478>
>> Types: rome.testly.testly1.SlingFutil
>> Component Name: rome.testly.testly1.SlingFutil
>> Component ID: 324
>>
>>
>>
>> Now I try to call SlingFutil.nonsense() from a JSP:
>>
>> ...
>> <%@ page import="javax.jcr.*,
>>           org.apache.sling.api.resource.Resource,
>>           rome.testly.testly1.*"
>> %>
>> ...
>> <%
>> SlingFutil some = new SlingFutil();
>> %>
>> ...
>>
>> At this point, the JSP compiler complains that the type (SlingFutil) is
>> unknown.
>> I also tried
>>
>> sling.getService(rome.testly.testly1.SlingFutil.class);
>>
>> instead, but the result is the same.
>>
>> This used to work well at BND times of Sling, so I don't know what's
>> wrong with this R7 code.
>>
>> Thanks,
>> Juerg
>>
>>
> On 28.03.20 21:15, Jörg Hoh wrote:
>> Hi Juerg,
>>
>> Looks like your bundle does not export that class, so it's not available
>> outside of that bundle.
>>
>> Jörg

Hi Jörg

The class is declared as @Component (see above). Do I need any other 
declaration to make the export happen?

Thanks,
Juerg


Re: Calling OSGi Service from JSP

Posted by Jörg Hoh <jh...@googlemail.com.INVALID>.
Hi Juerg,

Looks like your bundle does not export that class, so it's not available
outside of that bundle.

Jörg

Am Sa., 28. März 2020 um 20:55 Uhr schrieb JCR <jc...@proxymit.net>:

> Hello
>
> I wrote a very simple POJO, built it as OSGi bundle and deployed it to
> Sling11. Here's the source:
>
> package rome.testly.testly1;
> import org.osgi.service.component.annotations.Component;
>
> @Component(service=SlingFutil.class )
> public class SlingFutil {
>
>      public String nonsense() {
>          return "This is nonsense";
>      }
> }
>
> The component/service appears on the Felix console:
>
> symbolic Name   testly1
> Version         0.0.1.202003271754
> Bundle Location
>  /home/juerg/eclipse-workspace/testly1/target/classes
> Last Modification       Fri Mar 27 18:54:01 CET 2020
> Start Level     20
> Exported Packages       ---
> Imported Packages       org.osgi.framework,version=1.9.0 from
> org.apache.felix.framework (0)
> <http://localhost:8080/system/console/bundles/0>
> org.osgi.service.component,version=1.4.0 from org.apache.felix.scr (50)
> <http://localhost:8080/system/console/bundles/50>
> org.slf4j,version=1.7.25 from slf4j.api (20)
> <http://localhost:8080/system/console/bundles/20>
> Service ID 477 <http://localhost:8080/system/console/services/477>
> Types: java.lang.Runnable
> Component Name: rome.testly.testly1.SimpleDSComponent
> Component ID: 323
> Service ID 478 <http://localhost:8080/system/console/services/478>
> Types: rome.testly.testly1.SlingFutil
> Component Name: rome.testly.testly1.SlingFutil
> Component ID: 324
>
>
>
> Now I try to call SlingFutil.nonsense() from a JSP:
>
> ...
> <%@ page import="javax.jcr.*,
>          org.apache.sling.api.resource.Resource,
>          rome.testly.testly1.*"
> %>
> ...
> <%
> SlingFutil some = new SlingFutil();
> %>
> ...
>
> At this point, the JSP compiler complains that the type (SlingFutil) is
> unknown.
> I also tried
>
> sling.getService(rome.testly.testly1.SlingFutil.class);
>
> instead, but the result is the same.
>
> This used to work well at BND times of Sling, so I don't know what's
> wrong with this R7 code.
>
> Thanks,
> Juerg
>
>

-- 
Cheers,
Jörg Hoh,

http://cqdump.wordpress.com
Twitter: @joerghoh