掲示板

Problem with groovy script

8年前 に Franck Liénard によって更新されました。

Problem with groovy script

New Member 投稿: 5 参加年月日: 11/02/13 最新の投稿
Hi all,

I'm writing a groovy script to display layouts for all instances of portlets in the portal.
As we have a lot of pages, I had to have the code executed in a thread so the request doesn't get timeout.
// Get all pages for all portlets
import com.liferay.portal.service.*
import com.liferay.portal.util.*
import javax.servlet.http.HttpServletRequest
import java.io.*
import java.lang.Thread

def portletsInstances = { actionRequest, request ->
	// Log File : /app/qal/IPORTAIL/MAIN/tomcat7/s1/logs/appli/portlets_instances.log
	def outputFile = new File("""${System.getProperty("env.PWD")}/logs/appli/portlets_instances.log""")
	def writer = new PrintWriter(outputFile.getPath(), "UTF-8")

	try {
		def companyId = PortalUtil.getCompanyId(request)
		def portlets = PortletLocalServiceUtil.getPortlets(companyId)

		def getLayouts = {portletId ->
			def flayouts = new ArrayList()

			if (!portletId.isEmpty()) {
				def portlet = PortletLocalServiceUtil.getPortletById(portletId)
				def layouts = LayoutLocalServiceUtil.getLayouts(-1,-1)
				layouts.each { layout ->
					def layoutTypePortlet = layout.getLayoutType()
					def actualPortletList = layoutTypePortlet.getPortletIds()
					actualPortletList.each { layoutPortlet ->
						if (layoutPortlet.contains(portlet.getPortletId()) && !flayouts.contains(layout)) {
							flayouts.add(layout)
						}
					}
				}
			}
			writer.println("${flayouts.size()} pages found")
			flayouts.each { flayout ->
				if (flayout.isTypePortlet()) {
					def url = flayout.getRegularURL(request)
					writer.println("url : ${url}")
					writer.println("""
						<a href="${url}" target="_blank">${flayout.getName(actionRequest.getLocale())}</a>
					""")
				}
			}
		}

		portlets.each { p -&gt;
			if (p.isActive() &amp;&amp; !p.isSystem() &amp;&amp; 
				(p.getControlPanelEntryCategory() == null || p.getControlPanelEntryCategory().isEmpty())) {
				def pid = p.getPortletId()
				def pName = PortalUtil.getPortletTitle(p, request.getLocale())
				writer.println("Portlet : ${pName}")
				
				getLayouts(pid)
				writer.println("============================================================================================")
				
			}
		}
		
	} catch (e) {
		e.printStackTrace(writer);
	}
	writer.close()
}

// Delete previous log file
try {
	def logFile = "logs/appli/portlets_instances.log"
	def outputFile = new File("""${System.getProperty("env.PWD")}/${logFile}""")
	if (outputFile.exists() &amp;&amp; outputFile.isFile()) {
		def deleted = outputFile.delete()
		if (deleted) {
			out.println(outputFile.getPath() + ' deleted successfully')
		} else {
			out.println(outputFile.getPath() + ' couldn\'t be deleted')
		}
	} else {
		out.println(outputFile.getPath() + ' not found')
	}
} catch (e) {
	out.println(e)
}

def request = PortalUtil.getHttpServletRequest(actionRequest)
Thread.start portletsInstances.curry(actionRequest, request)


When I execute the script, I get the following error :
Portlet : Web Proxy
0 pages found
============================================================================================
Portlet : javax.portlet.title.afiframe_WAR_afiframeportlet
3 pages found
java.lang.NullPointerException
	at com.liferay.portal.model.impl.LayoutImpl._getURL(LayoutImpl.java:721)
	at com.liferay.portal.model.impl.LayoutImpl.getRegularURL(LayoutImpl.java:318)
	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.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoCachedMethodSite.invoke(PojoMetaMethodSite.java:188)
	at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:52)
	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)
	at Script58$_run_closure1_closure2_closure5.doCall(Script58.groovy:36)
	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.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)
	at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
	at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:273)
	at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:886)
	at groovy.lang.Closure.call(Closure.java:276)
	at groovy.lang.Closure.call(Closure.java:289)
	at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:1198)
	at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:1174)
	at org.codehaus.groovy.runtime.dgm$109.invoke(Unknown Source)
	at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:270)
	at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:52)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:124)
	at Script58$_run_closure1_closure2.doCall(Script58.groovy:34)
	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.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)
	at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
	at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:273)
	at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:886)
	at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:39)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:124)
	at Script58$_run_closure1_closure3.doCall(Script58.groovy:52)
	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.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)
	at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
	at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:273)
	at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:886)
	at groovy.lang.Closure.call(Closure.java:276)
	at groovy.lang.Closure.call(Closure.java:289)
	at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:1198)
	at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:1174)
	at org.codehaus.groovy.runtime.dgm$109.invoke(Unknown Source)
	at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:270)
	at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:52)
	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:124)
	at Script58$_run_closure1.doCall(Script58.groovy:45)
	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.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)
	at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
	at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:273)
	at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:886)
	at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:930)
	at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:886)
	at groovy.lang.Closure.call(Closure.java:276)
	at groovy.lang.Closure.call(Closure.java:271)
	at groovy.lang.Closure.run(Closure.java:354)
	at java.lang.Thread.run(Thread.java:662)


This script works when not executed in a thread. What's wrong with it?
thumbnail
8年前 に David H Nebinger によって更新されました。

RE: Problem with groovy script

Liferay Legend 投稿: 14919 参加年月日: 06/09/02 最新の投稿
_getURL() takes an HTTP request object as a parameter. I'm guessing it uses this to construct a valid URL based off of the current request (host name, port, path, etc).

Running not in a thread works because you have a request but in a thread context you probably do not.
8年前 に Franck Liénard によって更新されました。

RE: Problem with groovy script

New Member 投稿: 5 参加年月日: 11/02/13 最新の投稿
Hello David,

Thank you very much for answering.
You're right, I tried to use getFriendlyURL which takes no argument before posting and forgot the request argument when I restored my previous code .
I used a closure and the curry method to pass the actionRequest and request to my thread so the API can access the context.
The request argument is used upper in the closure without any problem.