Fórum

How to get an OSGi service into the JSP

thumbnail
Alex Weirig, modificado 7 Anos atrás.

How to get an OSGi service into the JSP

Junior Member Postagens: 36 Data de Entrada: 13/07/09 Postagens Recentes
Hello,

I'm trying to create a portlet using OSGi and I basically have everything setup and running.
I have my API bundle, and all my services bundles. Now I want to use a searchcontainer in a JSP.
The searchcontainer needs some data (e.g. row count and results) to display.
My OSGi Service does the search and holds the data but how do I access this service in my JSP, since you can't use @Reference in the JSP.

I noticed that in Liferay you use the xxxLocalServiceUtil class with static methods and you use a PortalBeanLocatorUtil to grab a copy of the service.

I'm not sure if it is a good choice to have the service injected in the Portlet class (since the portlet class is a @Component one can inject the service with @Reference) but then in the JSP you don't have any reference the portlet class instance?

What is the best practice to do this?

Many thanks in advance

Alex
thumbnail
David H Nebinger, modificado 7 Anos atrás.

RE: How to get an OSGi service into the JSP

Liferay Legend Postagens: 14915 Data de Entrada: 02/09/06 Postagens Recentes
I wouldn't try the annotation approach in JSP either.

Actually I'd go ahead and use the XxxLocalServiceUtil classes. They do have the service reference injected correctly so you can still trust that everything is wired up correctly.
thumbnail
Alex Weirig, modificado 7 Anos atrás.

RE: How to get an OSGi service into the JSP

Junior Member Postagens: 36 Data de Entrada: 13/07/09 Postagens Recentes
Hi Dave,

thank you for your answer.

Do I understand you correctly that you would recommend me to write LocalServiceUtil classes (which would have to be @Component) for all my services with static methods and then have my service injected using @Reference on a static field in order to be able to access my service in the JSP.

I think that is rather cumbersome ... I thought or hoped that there would be a less code intensive approach, because now you have your service class and another "static facade" just for the JSP side.

I wondered if one could pass the service to the JSP using a (render)request attribute and what performance impact this could have. A simple rendercommand would then do the trick.

Alex
thumbnail
David H Nebinger, modificado 7 Anos atrás.

RE: How to get an OSGi service into the JSP (Resposta)

Liferay Legend Postagens: 14915 Data de Entrada: 02/09/06 Postagens Recentes
No, if you're using SB (you are using SB, right?) those XxxLocalServiceUtil classes are already created for you in the API modules.
thumbnail
Alex Weirig, modificado 7 Anos atrás.

RE: How to get an OSGi service into the JSP

Junior Member Postagens: 36 Data de Entrada: 13/07/09 Postagens Recentes
Hi Dave,

no I'm not using SB because my (business) services run outside of Liferay in different Karaf runtimes and the database is a separate SQL database.

I'm using ECF to communicate between Liferay and the Karaf servers.

The architecture is:

Liferay
(portlet and localadminservice -> uses business service from Karaf)
|
\/
Karaf
(business services and SQL access)
|
\/
Karaf
(legacy business service and legacy database access)

The business service in Karaf gets and writes the data from the new database it also updates the legacy database using the legacy services in the 2nd Karaf. Once the legacy system is replace that Karaf will disappear.

Alex
thumbnail
David H Nebinger, modificado 7 Anos atrás.

RE: How to get an OSGi service into the JSP (Resposta)

Liferay Legend Postagens: 14915 Data de Entrada: 02/09/06 Postagens Recentes
Okay, well an alternative might be to follow the blade example: https://github.com/liferay/liferay-blade-samples/tree/master/liferay-gradle/blade.servicebuilder.web.

Here they inject the services as request attributes and pull them out of attributes in init.jsp. On the java side you can use the @Reference annotation to have the service injected and, from there, pushed into the attributes so you can get it to the JSP.



Come meet me at the NAS!
thumbnail
Alex Weirig, modificado 7 Anos atrás.

RE: How to get an OSGi service into the JSP

Junior Member Postagens: 36 Data de Entrada: 13/07/09 Postagens Recentes
Hi Dave,

that's what I was thinking about but I was concerned about possible performance issues.

I prefer this approach over the *Util class with the static methods.

Thanks for your support

Alex
thumbnail
David H Nebinger, modificado 7 Anos atrás.

RE: How to get an OSGi service into the JSP

Liferay Legend Postagens: 14915 Data de Entrada: 02/09/06 Postagens Recentes
There shouldn't be any performance issues. The service is injected when the portlet is instantiated, and the attribute is set just before JSP class evaluation. I can't see that any real overhead is being injected in this example...






Come meet me at the NAS!