Fórumok

Background Thread Use PermissionChecker on liferay services api

Cristhian Camilo Lopez, módosítva 12 év-val korábban

Background Thread Use PermissionChecker on liferay services api

New Member Bejegyzések: 15 Csatlakozás dátuma: 2008.04.11. Legújabb bejegyzések
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.


2012-01-05 16:55:42,693 INFO  [net.cristhiank.liferay.checker.LibraryChecker] (LiferayChecker/documents) Checking 1385 entries
2012-01-05 16:56:53,761 ERROR [net.cristhiank.liferay.checker.LibraryChecker] (LiferayChecker/documents) class javax.jcr.version.VersionException
2012-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
2012-01-05 16:56:53,764 ERROR [net.cristhiank.liferay.checker.portlet.LiferayCheckerPortlet] (LiferayChecker/documents) 
com.liferay.portal.security.auth.PrincipalException: PermissionChecker not initialized
	at com.liferay.portal.service.base.PrincipalBean.getPermissionChecker(PrincipalBean.java:81)
	at com.liferay.portlet.documentlibrary.service.impl.DLFileEntryServiceImpl.deleteFileEntry(DLFileEntryServiceImpl.java:106)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
	at com.liferay.portal.dao.jdbc.aop.DynamicDataSourceTransactionInterceptor.invoke(DynamicDataSourceTransactionInterceptor.java:44)
	at com.liferay.portal.spring.aop.ChainableMethodAdvice.invoke(ChainableMethodAdvice.java:58)
	at com.liferay.portal.spring.aop.ChainableMethodAdvice.invoke(ChainableMethodAdvice.java:58)
	at com.liferay.portal.spring.aop.ChainableMethodAdvice.invoke(ChainableMethodAdvice.java:58)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
	at $Proxy482.deleteFileEntry(Unknown Source)
	at com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil.deleteFileEntry(DLFileEntryServiceUtil.java:73)
	at net.cristhiank.liferay.checker.LibraryChecker.checkDocumentLibrary(LibraryChecker.java:73)
	at net.cristhiank.liferay.checker.portlet.LiferayCheckerPortlet$2.run(LiferayCheckerPortlet.java:46)
	at java.lang.Thread.run(Thread.java:662)
thumbnail
Amos Fong, módosítva 12 év-val korábban

RE: Background Thread Use PermissionChecker on liferay services api (Válasz)

Liferay Legend Bejegyzések: 2047 Csatlakozás dátuma: 2008.10.07. Legújabb bejegyzések
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, módosítva 12 év-val korábban

RE: Background Thread Use PermissionChecker on liferay services api

New Member Bejegyzések: 15 Csatlakozás dátuma: 2008.04.11. Legújabb bejegyzések
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
thumbnail
Amos Fong, módosítva 12 év-val korábban

RE: Background Thread Use PermissionChecker on liferay services api (Válasz)

Liferay Legend Bejegyzések: 2047 Csatlakozás dátuma: 2008.10.07. Legújabb bejegyzések
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:

		PrincipalThreadLocal.setName(userId);

		User user = UserLocalServiceUtil.getUserById(userId);

		PermissionChecker permissionChecker =
			PermissionCheckerFactoryUtil.create(user, false);

		PermissionThreadLocal.setPermissionChecker(permissionChecker);
Ricardo Piccoli, módosítva 11 év-val korábban

RE: Background Thread Use PermissionChecker on liferay services api

New Member Bejegyzések: 6 Csatlakozás dátuma: 2013.02.14. Legújabb bejegyzések
Amos Fong, you just saved my life! emoticon
thumbnail
Jay Patel, módosítva 10 év-val korábban

RE: Background Thread Use PermissionChecker on liferay services api

Regular Member Bejegyzések: 118 Csatlakozás dátuma: 2010.02.24. Legújabb bejegyzések
Thanks Amos, you saved a lot of sweat emoticon
Mark Smith, módosítva 10 év-val korábban

RE: Background Thread Use PermissionChecker on liferay services api

Junior Member Bejegyzések: 54 Csatlakozás dátuma: 2014.01.18. Legújabb bejegyzések
Thank you very much Amos!!! emoticon
Venkat P, módosítva 7 év-val korábban

RE: Background Thread Use PermissionChecker on liferay services api

New Member Bejegyzések: 12 Csatlakozás dátuma: 2015.09.04. Legújabb bejegyzések
How to do permission checker in webservice soap.