Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Cristhian Camilo Lopez
Background Thread Use PermissionChecker on liferay services api
January 5, 2012 3:56 PM
Answer

Cristhian Camilo Lopez

Rank: New Member

Posts: 15

Join Date: April 10, 2008

Recent Posts

Hello Guys,

I'm developing a portlet to check the consistence between the DL metadata on the database and the data stored on the data store hook.
I need to use de DLFileEntryServiceUtil to delete the invalid entries. Due the long time it could take, I launch the process on other thread on server, but I'm getting a exception because the PermissionChecker isn't set, after some debug I find that it's because it's on other thread.

Which is the best practice to launch background threads and use liferay api ???. Thanks in advance.

 1
 22012-01-05 16:55:42,693 INFO  [net.cristhiank.liferay.checker.LibraryChecker] (LiferayChecker/documents) Checking 1385 entries
 32012-01-05 16:56:53,761 ERROR [net.cristhiank.liferay.checker.LibraryChecker] (LiferayChecker/documents) class javax.jcr.version.VersionException
 42012-01-05 16:56:53,761 WARN  [net.cristhiank.liferay.checker.LibraryChecker] (LiferayChecker/documents) Entry Version 1.1 not found on com.liferay.documentlibrary.util.JCRHook store: enrociclina.jpg
 52012-01-05 16:56:53,764 ERROR [net.cristhiank.liferay.checker.portlet.LiferayCheckerPortlet] (LiferayChecker/documents)
 6com.liferay.portal.security.auth.PrincipalException: PermissionChecker not initialized
 7    at com.liferay.portal.service.base.PrincipalBean.getPermissionChecker(PrincipalBean.java:81)
 8    at com.liferay.portlet.documentlibrary.service.impl.DLFileEntryServiceImpl.deleteFileEntry(DLFileEntryServiceImpl.java:106)
 9    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
10    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
11    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
12    at java.lang.reflect.Method.invoke(Method.java:597)
13    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
14    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
15    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
16    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
17    at com.liferay.portal.dao.jdbc.aop.DynamicDataSourceTransactionInterceptor.invoke(DynamicDataSourceTransactionInterceptor.java:44)
18    at com.liferay.portal.spring.aop.ChainableMethodAdvice.invoke(ChainableMethodAdvice.java:58)
19    at com.liferay.portal.spring.aop.ChainableMethodAdvice.invoke(ChainableMethodAdvice.java:58)
20    at com.liferay.portal.spring.aop.ChainableMethodAdvice.invoke(ChainableMethodAdvice.java:58)
21    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
22    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
23    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
24    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
25    at $Proxy482.deleteFileEntry(Unknown Source)
26    at com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil.deleteFileEntry(DLFileEntryServiceUtil.java:73)
27    at net.cristhiank.liferay.checker.LibraryChecker.checkDocumentLibrary(LibraryChecker.java:73)
28    at net.cristhiank.liferay.checker.portlet.LiferayCheckerPortlet$2.run(LiferayCheckerPortlet.java:46)
29    at java.lang.Thread.run(Thread.java:662)
Amos Fong
RE: Background Thread Use PermissionChecker on liferay services api
January 5, 2012 9:00 PM
Answer

Amos Fong

LIFERAY STAFF

Rank: Liferay Legend

Posts: 1844

Join Date: October 7, 2008

Recent Posts

Hi Cristhian,

You probably don't even need a permission check at this point because you probably don't have a specific user performing the action; it seems like backend background stuff. So you can use the DLFileEntryLocalServiceUtil instead which does not do any permission checking.
Cristhian Camilo Lopez
RE: Background Thread Use PermissionChecker on liferay services api
January 6, 2012 4:48 AM
Answer

Cristhian Camilo Lopez

Rank: New Member

Posts: 15

Join Date: April 10, 2008

Recent Posts

Hi Amos,

Thank you so much for the answer, you're right, and it's now working like a charm.
Just to know, how could I start background process if in the future I would like use some ThreadLocal objects from Liferay ?

Regards,

Cristhian
Amos Fong
RE: Background Thread Use PermissionChecker on liferay services api
January 8, 2012 6:33 PM
Answer

Amos Fong

LIFERAY STAFF

Rank: Liferay Legend

Posts: 1844

Join Date: October 7, 2008

Recent Posts

I found this code in LayoutsRemotePublisherMessageListener.java which is background process that needs permission checking.

This looks like the code needed to initialize the permissionChecker:

1        PrincipalThreadLocal.setName(userId);
2
3        User user = UserLocalServiceUtil.getUserById(userId);
4
5        PermissionChecker permissionChecker =
6            PermissionCheckerFactoryUtil.create(user, false);
7
8        PermissionThreadLocal.setPermissionChecker(permissionChecker);
Ricardo Piccoli
RE: Background Thread Use PermissionChecker on liferay services api
March 12, 2013 4:08 AM
Answer

Ricardo Piccoli

Rank: New Member

Posts: 6

Join Date: February 14, 2013

Recent Posts

Amos Fong, you just saved my life! emoticon
Jay Patel
RE: Background Thread Use PermissionChecker on liferay services api
September 26, 2013 6:53 AM
Answer

Jay Patel

Rank: Regular Member

Posts: 116

Join Date: February 23, 2010

Recent Posts

Thanks Amos, you saved a lot of sweat emoticon
Mark Smith
RE: Background Thread Use PermissionChecker on liferay services api
January 29, 2014 5:45 AM
Answer

Mark Smith

Rank: Junior Member

Posts: 54

Join Date: January 18, 2014

Recent Posts

Thank you very much Amos!!! emoticon