Author: ludovic Date: 2007-09-05 19:52:43 +0200 (Wed, 05 Sep 2007) New Revision: 4728
Added: xwiki-platform/core/branches/xwiki-core-1.1/src/main/java/com/xpn/xwiki/render/groovy/XWikiPageClassLoader.java Modified: xwiki-platform/core/branches/xwiki-core-1.1/src/main/java/com/xpn/xwiki/XWiki.java xwiki-platform/core/branches/xwiki-core-1.1/src/main/java/com/xpn/xwiki/api/XWiki.java Log: XWIKI-1671 XWiki Core APIs allowing groovy or velocity scripts to refer to classes in XWiki documents attached jar files Modified: xwiki-platform/core/branches/xwiki-core-1.1/src/main/java/com/xpn/xwiki/XWiki.java =================================================================== --- xwiki-platform/core/branches/xwiki-core-1.1/src/main/java/com/xpn/xwiki/XWiki.java 2007-09-05 12:48:21 UTC (rev 4727) +++ xwiki-platform/core/branches/xwiki-core-1.1/src/main/java/com/xpn/xwiki/XWiki.java 2007-09-05 17:52:43 UTC (rev 4728) @@ -42,6 +42,7 @@ import com.xpn.xwiki.render.XWikiRenderingEngine; import com.xpn.xwiki.render.XWikiVelocityRenderer; import com.xpn.xwiki.render.groovy.XWikiGroovyRenderer; +import com.xpn.xwiki.render.groovy.XWikiPageClassLoader; import com.xpn.xwiki.stats.api.XWikiStatsService; import com.xpn.xwiki.stats.impl.SearchEngineRule; import com.xpn.xwiki.stats.impl.XWikiStatsServiceImpl; @@ -5038,21 +5039,49 @@ } public Object parseGroovyFromString(String script, XWikiContext context) - throws XWikiException + throws XWikiException { - if (getRenderingEngine().getRenderer("groovy") != null) + if (getRenderingEngine().getRenderer("groovy") == null) + return null; + else return ((XWikiGroovyRenderer) getRenderingEngine().getRenderer("groovy")) - .parseGroovyFromString(script, context); - return null; + .parseGroovyFromString(script, context); } + public Object parseGroovyFromString(String script, String jarWikiPage, XWikiContext context) + throws XWikiException + { + if (getRenderingEngine().getRenderer("groovy") == null) + return null; + + + XWikiPageClassLoader pcl = new XWikiPageClassLoader(jarWikiPage, context); + Object prevParentClassLoader = context.get("parentclassloader"); + try { + context.put("parentclassloader", pcl); + return parseGroovyFromString(script, context); + } finally { + if (prevParentClassLoader==null) + context.remove("parentclassloader"); + else + context.put("parentclassloader", prevParentClassLoader); + } + } + public Object parseGroovyFromPage(String fullname, XWikiContext context) - throws XWikiException + throws XWikiException { return parseGroovyFromString(context.getWiki().getDocument(fullname, context) - .getContent(), context); + .getContent(), context); } + public Object parseGroovyFromPage(String fullName, String jarWikiPage, XWikiContext context) + throws XWikiException + { + return parseGroovyFromString(context.getWiki().getDocument(fullName, context) + .getContent(), context); + } + public String getMacroList(XWikiContext context) { String macrosmapping = ""; Modified: xwiki-platform/core/branches/xwiki-core-1.1/src/main/java/com/xpn/xwiki/api/XWiki.java =================================================================== --- xwiki-platform/core/branches/xwiki-core-1.1/src/main/java/com/xpn/xwiki/api/XWiki.java 2007-09-05 12:48:21 UTC (rev 4727) +++ xwiki-platform/core/branches/xwiki-core-1.1/src/main/java/com/xpn/xwiki/api/XWiki.java 2007-09-05 17:52:43 UTC (rev 4728) @@ -2403,6 +2403,22 @@ * @return An object instanciating this class * @throws XWikiException */ + public Object parseGroovyFromPage(String fullname, String jarWikiPage) throws XWikiException + { + XWikiDocument doc = xwiki.getDocument(fullname, getXWikiContext()); + if (xwiki.getRightService().hasProgrammingRights(doc, getXWikiContext())) + return xwiki.parseGroovyFromString(doc.getContent(), jarWikiPage, getXWikiContext()); + return "groovy_missingrights"; + } + + /** + * Priviledged API to retrieve an object instanciated from groovy code in a String Groovy + * scripts compilation is cached + * + * @param fullname // script containing a Groovy class definition (public class MyClass { ... }) + * @return An object instanciating this class + * @throws XWikiException + */ public Object parseGroovyFromPage(String fullname) throws XWikiException { XWikiDocument doc = xwiki.getDocument(fullname, getXWikiContext()); Added: xwiki-platform/core/branches/xwiki-core-1.1/src/main/java/com/xpn/xwiki/render/groovy/XWikiPageClassLoader.java =================================================================== --- xwiki-platform/core/branches/xwiki-core-1.1/src/main/java/com/xpn/xwiki/render/groovy/XWikiPageClassLoader.java 2007-09-05 12:48:21 UTC (rev 4727) +++ xwiki-platform/core/branches/xwiki-core-1.1/src/main/java/com/xpn/xwiki/render/groovy/XWikiPageClassLoader.java 2007-09-05 17:52:43 UTC (rev 4728) @@ -0,0 +1,76 @@ +package com.xpn.xwiki.render.groovy; + +import com.xpn.xwiki.XWikiContext; +import com.xpn.xwiki.XWikiException; +import com.xpn.xwiki.doc.XWikiDocument; +import com.xpn.xwiki.doc.XWikiAttachment; + +import java.net.URLClassLoader; +import java.net.URL; +import java.net.URLStreamHandlerFactory; +import java.util.List; +import java.util.ArrayList; + +/** + * Copyright 2006,XpertNet SARL,and individual contributors as indicated + * by the contributors.txt. + * <p/> + * This is free software;you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation;either version2.1of + * the License,or(at your option)any later version. + * <p/> + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY;without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the GNU + * Lesser General Public License for more details. + * <p/> + * You should have received a copy of the GNU Lesser General Public + * License along with this software;if not,write to the Free + * Software Foundation,Inc.,51 Franklin St,Fifth Floor,Boston,MA + * 02110-1301 USA,or see the FSF site:http://www.fsf.org. + * + * @author ldubost + */ + +public class XWikiPageClassLoader extends URLClassLoader { + + public XWikiPageClassLoader(URL[] urls, ClassLoader parent) { + super(urls, parent); + } + + public XWikiPageClassLoader(URL[] urls) { + super(urls); + } + + public XWikiPageClassLoader(URL[] urls, ClassLoader parent, URLStreamHandlerFactory factory) { + super(urls, parent, factory); + } + + public XWikiPageClassLoader(String jarWikiPage, XWikiContext context) throws XWikiException { + this(jarWikiPage, Thread.currentThread().getContextClassLoader(), context); + } + + public XWikiPageClassLoader(String jarWikiPage, ClassLoader parent, XWikiContext context) throws XWikiException { + super(new URL[0], parent); + XWikiDocument doc = context.getWiki().getDocument(jarWikiPage, context); + List urlList = new ArrayList(); + if (!doc.isNew()) { + List attachList = doc.getAttachmentList(); + for (int i=0;i<attachList.size();i++) { + XWikiAttachment attach = (XWikiAttachment) attachList.get(i); + String filename = attach.getFilename(); + if (filename.endsWith(".jar")) { + try{ + urlList.add(new URL(doc.getExternalAttachmentURL(filename, "download", context))); + } catch (Exception e) {}; + } + } + } + if (urlList.size()>0) { + for (int i=0;i<urlList.size();i++) { + addURL((URL) urlList.get(i)); + } + } + } +} _______________________________________________ notifications mailing list notifications@xwiki.org http://lists.xwiki.org/mailman/listinfo/notifications