掲示板

Can we add Hook inside a Portlet Plugin project

8年前 に Alex Man によって更新されました。

Can we add Hook inside a Portlet Plugin project

Junior Member 投稿: 70 参加年月日: 16/02/08 最新の投稿
I have created a separate hook plugin project for portal properties customization. When I deployed it got worked successfully. But when I add the hook configuration within a portlet plugin project the hook is not working.

I am adding the hook configuration within portlet plugin project because I need to access some spring services and db access. In the portlet plugin project I am already have those stuffs, so If I can add hook within portlet plugin project it will be easy to access those.

My WEB-INF structure is as given below



When I deploy the project I am getting the portlet deployed successfully and able to access but hook is not deployed

Can anyone please tell me some solution for this
thumbnail
8年前 に David H Nebinger によって更新されました。

RE: Can we add Hook inside a Portlet Plugin project

Liferay Legend 投稿: 14915 参加年月日: 06/09/02 最新の投稿
Hooks operate within the ROOT application context, so the spring beans in your portlet are not going to be available necessarily within the context of the hook.
8年前 に Alex Man によって更新されました。

RE: Can we add Hook inside a Portlet Plugin project

Junior Member 投稿: 70 参加年月日: 16/02/08 最新の投稿
David H Nebinger:
Hooks operate within the ROOT application context, so the spring beans in your portlet are not going to be available necessarily within the context of the hook.

Thanks for the reply, lets say If I can create a separate hook plugin project where I am customizing liferay portal properties, In the custom action class can I access the hibernate database services which is used in portlet plugin project.
thumbnail
8年前 に David H Nebinger によって更新されました。

RE: Can we add Hook inside a Portlet Plugin project

Liferay Legend 投稿: 14915 参加年月日: 06/09/02 最新の投稿
Um, this is an example of a hibernate disaster...

Again the hook runs inside the portal's class loader, not the portlet's. The portal uses the defined class name and the portlet's class loader to create an instance of the class to use within the portal's context, but the portal only refers to the interface and reflection to invoke the defined method(s) of the class. There is no big spring initialization and it's questionable whether you could do anything even in the context of the default constructor to gain access to what you're searching for.

If you had used Service Builder, I would tell you to simply put the service jar into the global class loader and then your hook can easily access your services via the XxxLocalServiceUtil classes which are designed for this purpose.

However, since you've used Hibernate to roll your own data access layer, you're basically screwed. The custom action class is running in the portal's class loader so it will not have access to anything in the portlet, especially spring/hibernate beans instantiated to support the portlet.

Hibernate is not useful in the portal because of constraints such as this.
8年前 に Alex Man によって更新されました。

RE: Can we add Hook inside a Portlet Plugin project

Junior Member 投稿: 70 参加年月日: 16/02/08 最新の投稿
Hmm....actually why I am doing all this is because, I am having a requirement that lets say I have created a new table in liferay database called colors with some values and I need to access the newly created database values in my theme project. I have tried creating a service builder and placed the jar in lib/ext for accessing the color table in theme but it did'nt work out, after that I planned to go with hooks but now the issue is with the hibernate services.

I have posted a thread in Stackoverflow related with this issue
Liferay access DB table in theme: No bean named 'com.colors.themes.service.ColorLocalService' is defined

Like to know your thoughts on this....also like to know if you any other ideas for this
thumbnail
8年前 に David H Nebinger によって更新されました。

RE: Can we add Hook inside a Portlet Plugin project

Liferay Legend 投稿: 14915 参加年月日: 06/09/02 最新の投稿
Posting the same response here, but refer to the stack overflow for the original problem.

First of all you have the ServiceBuilder layer that exposes your data access layer; this part is good.

Your code for the theme is mostly right. You should be using ServiceLocator to find your service, but you're missing the servlet context that is providing the service. For example, if the plugin providing the service is in color-service-portlet.war, then the service locator call will look like:

#set ($myColorService = $serviceLocator.findService("color-service-portlet", "com.colors.themes.service.ColorLocalService"))


The exception that you're seeing is because you are using the portal's form to find a portal service but of course the portal is not exporting that service, so you get the BeanLocatorException.

As far as any ideas, well first would be that Liferay folks aren't necessarily following SO looking for Liferay issues. I frankly have no time for it and feel lucky if I can carve out some time to solve issues here in the forum. I suspect that the Liferay staff may be in the same boat but wouldn't be able to really speak for them.

All I know is that I have over 10k posts here in the forums, but I have less than 100 points on SO. Not because I can't answer questions there, but because I don't have time to.
8年前 に Alex Man によって更新されました。

RE: Can we add Hook inside a Portlet Plugin project

Junior Member 投稿: 70 参加年月日: 16/02/08 最新の投稿
David H Nebinger:
You should be using ServiceLocator to find your service, but you're missing the servlet context that is providing the service. For example, if the plugin providing the service is in color-service-portlet.war, then the service locator call will look like:

#set ($myColorService = $serviceLocator.findService("color-service-portlet", "com.colors.themes.service.ColorLocalService"))



My service builder portlet project name is customfunctions-portlet, when I build the service.xml I got customfunctions-portlet-service.jar in lib folder. I copied and placed in my tomcat lib/ext folder and restarted the server.

In the velocity template I have used the following code
 #set ($myColorService = $serviceLocator.findService("customfunctions-portlet", "com.colors.themes.service.ColorsLocalServiceUtil"))


but still I am getting the following exception

10:32:44,032 ERROR [http-bio-8081-exec-10][PortletBeanLocatorUtil:42] BeanLocator is null for servlet context customfunctions-portlet
10:32:44,060 ERROR [http-bio-8081-exec-10][ServiceLocator:53] com.liferay.portal.kernel.bean.BeanLocatorException: BeanLocator has not been set for servlet context customfunctions-portlet


You can see my full code stuff at customfunctions-portlet
thumbnail
8年前 に David H Nebinger によって更新されました。

RE: Can we add Hook inside a Portlet Plugin project

Liferay Legend 投稿: 14915 参加年月日: 06/09/02 最新の投稿
First you should always include full stack trace, partials don't often provide enough context.

Second since you're accessing from another plugin, you should include the declaration of the "required-deployment-context" within the theme's liferay-plugin-package.properties file. This should ensure that the theme is not loaded/available until the service providing plugin has been started.
8年前 に Alex Man によって更新されました。

RE: Can we add Hook inside a Portlet Plugin project

Junior Member 投稿: 70 参加年月日: 16/02/08 最新の投稿
David H Nebinger:
since you're accessing from another plugin, you should include the declaration of the "required-deployment-context" within the theme's liferay-plugin-package.properties file. This should ensure that the theme is not loaded/available until the service providing plugin has been started.


I have done that too but still the same issue. Finally I deployed the portlet directly through eclipse then it got worked.

But according to my understanding this is my thought about the approach
  • portal-service.jar which is placed under tomcat-7.0.42\lib\ext contains default services for accessing liferay default database tables. These services can be accessed using serviceLocator in theme velocity templates.
  • Since I have created a new table named colors, for that I have created a service builder portlet plugin project named customfunctions-portlet. Wrote the finder methods in service.xml, compiled and build the jar file under the name customfunctions-portlet-service.jar
  • I have placed the generated customfunctions-portlet-service.jar file under tomcat-7.0.42\lib\ext along with other jars like as shown below

    Note: I have not deployed the customfunctions-portlet project in tomcat.
  • Added required-deployment-context=customfunctions-portlet within the theme's liferay-plugin-package.properties file.
  • Added velocity.engine.restricted.variables= in portal-ext.properties.
  • Used the below code to check the new service is working or not, but got BeanLocator is null for servlet context customfunctions-portlet
     #set ($myColorService = $serviceLocator.findService("customfunctions-portlet", "com.colors.themes.service.ColorsLocalService"))  
     $myColorService.getColorsesCount()


  • But when I deployed the customfunctions-portlet everything work fine, what I thought is that the theme velocity template can access the color table services from customfunctions-portlet-service.jar which is placed under tomcat-7.0.42\lib\ext and no need to deploy the customfunctions-portlet portlet project

    Like to know your thoughts on this
thumbnail
8年前 に David H Nebinger によって更新されました。

RE: Can we add Hook inside a Portlet Plugin project

Liferay Legend 投稿: 14915 参加年月日: 06/09/02 最新の投稿
The service jar is just an interface jar, not an implementation. The customfunctions-portlet has the implementation and must be deployed for the service to work.
8年前 に Alex Man によって更新されました。

RE: Can we add Hook inside a Portlet Plugin project

Junior Member 投稿: 70 参加年月日: 16/02/08 最新の投稿
David H Nebinger:
The service jar is just an interface jar, not an implementation. The customfunctions-portlet has the implementation and must be deployed for the service to work.


Finally the very last question

In the above implementation we have used Service Builder within the portlet and have accessed the services in theme velocity template, Similarly Instead of Service Builder can we use hibernate providing some interface like that of service builder.
thumbnail
8年前 に David H Nebinger によって更新されました。

RE: Can we add Hook inside a Portlet Plugin project (回答)

Liferay Legend 投稿: 14915 参加年月日: 06/09/02 最新の投稿
no