diff --git a/source/java/org/alfresco/filesys/repo/desk/JavaScriptDesktopAction.java b/source/java/org/alfresco/filesys/repo/desk/JavaScriptDesktopAction.java index 7d64b5b70e..62556f5a42 100644 --- a/source/java/org/alfresco/filesys/repo/desk/JavaScriptDesktopAction.java +++ b/source/java/org/alfresco/filesys/repo/desk/JavaScriptDesktopAction.java @@ -40,7 +40,7 @@ import org.alfresco.filesys.alfresco.DesktopActionException; import org.alfresco.filesys.alfresco.DesktopParams; import org.alfresco.filesys.alfresco.DesktopResponse; import org.alfresco.jlan.server.filesys.DiskSharedDevice; -import org.alfresco.service.cmr.repository.ScriptException; +import org.alfresco.scripts.ScriptException; import org.alfresco.service.cmr.repository.ScriptService; import org.alfresco.service.transaction.TransactionService; diff --git a/source/java/org/alfresco/repo/jscript/ClasspathScriptLocation.java b/source/java/org/alfresco/repo/jscript/ClasspathScriptLocation.java index 57b2c36d54..c33c2aa18e 100644 --- a/source/java/org/alfresco/repo/jscript/ClasspathScriptLocation.java +++ b/source/java/org/alfresco/repo/jscript/ClasspathScriptLocation.java @@ -29,7 +29,7 @@ import java.io.InputStreamReader; import java.io.Reader; import org.alfresco.error.AlfrescoRuntimeException; -import org.alfresco.service.cmr.repository.ScriptException; +import org.alfresco.scripts.ScriptException; import org.alfresco.service.cmr.repository.ScriptLocation; import org.alfresco.util.ParameterCheck; diff --git a/source/java/org/alfresco/repo/jscript/RhinoScriptProcessor.java b/source/java/org/alfresco/repo/jscript/RhinoScriptProcessor.java index c70daa1919..cf51a4aca0 100644 --- a/source/java/org/alfresco/repo/jscript/RhinoScriptProcessor.java +++ b/source/java/org/alfresco/repo/jscript/RhinoScriptProcessor.java @@ -30,7 +30,6 @@ import java.io.InputStream; import java.io.Serializable; import java.util.ArrayList; import java.util.HashMap; -import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.StringTokenizer; @@ -38,13 +37,15 @@ import java.util.StringTokenizer; import org.alfresco.error.AlfrescoRuntimeException; import org.alfresco.model.ContentModel; import org.alfresco.repo.processor.BaseProcessor; +import org.alfresco.scripts.ScriptException; +import org.alfresco.scripts.ScriptResourceHelper; +import org.alfresco.scripts.ScriptResourceLoader; import org.alfresco.service.cmr.model.FileInfo; import org.alfresco.service.cmr.model.FileNotFoundException; import org.alfresco.service.cmr.repository.ContentIOException; import org.alfresco.service.cmr.repository.ContentReader; import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.ProcessorExtension; -import org.alfresco.service.cmr.repository.ScriptException; import org.alfresco.service.cmr.repository.ScriptLocation; import org.alfresco.service.cmr.repository.ScriptProcessor; import org.alfresco.service.cmr.repository.StoreRef; @@ -63,14 +64,11 @@ import org.springframework.util.FileCopyUtils; * * @author Kevin Roast */ -public class RhinoScriptProcessor extends BaseProcessor implements ScriptProcessor +public class RhinoScriptProcessor extends BaseProcessor implements ScriptProcessor, ScriptResourceLoader { private static final Log logger = LogFactory.getLog(RhinoScriptProcessor.class); - private static final String IMPORT_PREFIX = " * * @@ -214,142 +211,7 @@ public class RhinoScriptProcessor extends BaseProcessor implements ScriptProcess */ private String resolveScriptImports(String script) { - // use a linked hashmap to preserve order of includes - the key in the collection is used - // to resolve multiple includes of the same scripts and therefore cyclic includes also - Map scriptlets = new LinkedHashMap(8, 1.0f); - - // perform a recursive resolve of all script imports - recurseScriptImports(SCRIPT_ROOT, script, scriptlets); - - if (scriptlets.size() == 1) - { - // quick exit for single script with no includes - if (logger.isTraceEnabled()) - logger.trace("Script content resolved to:\r\n" + script); - - return script; - } - else - { - // calculate total size of buffer required for the script and all includes - int length = 0; - for (String scriptlet : scriptlets.values()) - { - length += scriptlet.length(); - } - // append the scripts together to make a single script - StringBuilder result = new StringBuilder(length); - for (String scriptlet : scriptlets.values()) - { - result.append(scriptlet); - } - - if (logger.isTraceEnabled()) - logger.trace("Script content resolved to:\r\n" + result.toString()); - - return result.toString(); - } - } - - /** - * Recursively resolve imports in the specified scripts, adding the imports to the - * specific list of scriplets to combine later. - * - * @param location Script location - used to ensure duplicates are not added - * @param script The script to recursively resolve imports for - * @param scripts The collection of scriplets to execute with imports resolved and removed - */ - private void recurseScriptImports(String location, String script, Map scripts) - { - int index = 0; - // skip any initial whitespace - for (; index') - { - // found end of import line - so we have a resource path - String resource = script.substring(resourceStart, index); - - if (logger.isDebugEnabled()) - logger.debug("Found script resource import: " + resource); - - if (scripts.containsKey(resource) == false) - { - // load the script resource (and parse any recursive includes...) - String includedScript = loadScriptResource(resource); - if (includedScript != null) - { - if (logger.isDebugEnabled()) - logger.debug("Succesfully located script '" + resource + "'"); - recurseScriptImports(resource, includedScript, scripts); - } - } - else - { - if (logger.isDebugEnabled()) - logger.debug("Note: already imported resource: " + resource); - } - - // continue scanning this script for additional includes - // skip the last two characters of the import directive - for (index += 2; index"); - } - else - { - throw new ScriptException( - "Malformed 'import' line - must be first in file, no comments and strictly of the form:" + - "\r\n"); - } - } - else - { - // no (further) includes found - include the original script content - if (logger.isDebugEnabled()) - logger.debug("Imports resolved, adding resource '" + location); - if (logger.isTraceEnabled()) - logger.trace(script); - scripts.put(location, script); - } + return ScriptResourceHelper.resolveScriptImports(script, this, logger); } /** @@ -366,7 +228,7 @@ public class RhinoScriptProcessor extends BaseProcessor implements ScriptProcess * * @throws AlfrescoRuntimeException on any IO or ContentIO error */ - private String loadScriptResource(String resource) + public String loadScriptResource(String resource) { String result = null; @@ -462,7 +324,7 @@ public class RhinoScriptProcessor extends BaseProcessor implements ScriptProcess * * @throws AlfrescoRuntimeException */ - private Object executeScriptImpl(String script, Map origModel, boolean secure) + private Object executeScriptImpl(String script, Map model, boolean secure) throws AlfrescoRuntimeException { long startTime = 0; @@ -472,7 +334,7 @@ public class RhinoScriptProcessor extends BaseProcessor implements ScriptProcess } // Convert the model - Map model = convertToRhinoModel(origModel); + model = convertToRhinoModel(model); // check that rhino script engine is available Context cx = Context.enter(); @@ -509,10 +371,9 @@ public class RhinoScriptProcessor extends BaseProcessor implements ScriptProcess // add the global scripts for (ProcessorExtension ex : this.processorExtensions.values()) { - model.put(ex.getExtensionName(), ex); + model.put(ex.getExtensionName(), ex); } - // insert supplied object model into root of the default scope for (String key : model.keySet()) { @@ -535,8 +396,7 @@ public class RhinoScriptProcessor extends BaseProcessor implements ScriptProcess Object result = cx.evaluateString(scope, script, "AlfrescoScript", 1, null); // extract java object result if wrapped by Rhino - result = valueConverter.convertValueForRepo((Serializable)result); - return result; + return valueConverter.convertValueForRepo((Serializable)result); } catch (Throwable err) { diff --git a/source/java/org/alfresco/repo/processor/ScriptServiceImpl.java b/source/java/org/alfresco/repo/processor/ScriptServiceImpl.java index a3d1707643..344ba2f7a6 100644 --- a/source/java/org/alfresco/repo/processor/ScriptServiceImpl.java +++ b/source/java/org/alfresco/repo/processor/ScriptServiceImpl.java @@ -28,9 +28,9 @@ import java.util.HashMap; import java.util.Map; import org.alfresco.model.ContentModel; +import org.alfresco.scripts.ScriptException; import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeService; -import org.alfresco.service.cmr.repository.ScriptException; import org.alfresco.service.cmr.repository.ScriptLocation; import org.alfresco.service.cmr.repository.ScriptProcessor; import org.alfresco.service.cmr.repository.ScriptService; diff --git a/source/java/org/alfresco/service/cmr/repository/ScriptException.java b/source/java/org/alfresco/service/cmr/repository/ScriptException.java deleted file mode 100644 index 82b2441371..0000000000 --- a/source/java/org/alfresco/service/cmr/repository/ScriptException.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright (C) 2005-2007 Alfresco Software Limited. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program 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 General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - - * As a special exception to the terms and conditions of version 2.0 of - * the GPL, you may redistribute this Program in connection with Free/Libre - * and Open Source Software ("FLOSS") applications as described in Alfresco's - * FLOSS exception. You should have recieved a copy of the text describing - * the FLOSS exception, and it is also available here: - * http://www.alfresco.com/legal/licensing" - */ -package org.alfresco.service.cmr.repository; - -import org.alfresco.error.AlfrescoRuntimeException; - -/** - * @author Kevin Roast - */ -public class ScriptException extends AlfrescoRuntimeException -{ - private static final long serialVersionUID = 1739480648583299623L; - - /** - * @param msgId - */ - public ScriptException(String msgId) - { - super(msgId); - } - - /** - * @param msgId - * @param cause - */ - public ScriptException(String msgId, Throwable cause) - { - super(msgId, cause); - } - - /** - * @param msgId - * @param params - */ - public ScriptException(String msgId, Object[] params) - { - super(msgId, params); - } - - /** - * @param msgId - * @param msgParams - * @param cause - */ - public ScriptException(String msgId, Object[] msgParams, Throwable cause) - { - super(msgId, msgParams, cause); - } -} diff --git a/source/java/org/alfresco/service/cmr/repository/ScriptService.java b/source/java/org/alfresco/service/cmr/repository/ScriptService.java index 2f98499bd1..992b93d669 100644 --- a/source/java/org/alfresco/service/cmr/repository/ScriptService.java +++ b/source/java/org/alfresco/service/cmr/repository/ScriptService.java @@ -26,6 +26,7 @@ package org.alfresco.service.cmr.repository; import java.util.Map; +import org.alfresco.scripts.ScriptException; import org.alfresco.service.Auditable; import org.alfresco.service.PublicService; import org.alfresco.service.namespace.QName;