Merged BRANCHES/DEV/WEB-PROTO-3.0 to HEAD

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@8565 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Kevin Roast
2008-03-19 12:13:38 +00:00
parent 2d306ef79f
commit 3f7bfeaf4e
6 changed files with 15 additions and 225 deletions

View File

@@ -40,7 +40,7 @@ import org.alfresco.filesys.alfresco.DesktopActionException;
import org.alfresco.filesys.alfresco.DesktopParams; import org.alfresco.filesys.alfresco.DesktopParams;
import org.alfresco.filesys.alfresco.DesktopResponse; import org.alfresco.filesys.alfresco.DesktopResponse;
import org.alfresco.jlan.server.filesys.DiskSharedDevice; 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.cmr.repository.ScriptService;
import org.alfresco.service.transaction.TransactionService; import org.alfresco.service.transaction.TransactionService;

View File

@@ -29,7 +29,7 @@ import java.io.InputStreamReader;
import java.io.Reader; import java.io.Reader;
import org.alfresco.error.AlfrescoRuntimeException; 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.service.cmr.repository.ScriptLocation;
import org.alfresco.util.ParameterCheck; import org.alfresco.util.ParameterCheck;

View File

@@ -30,7 +30,6 @@ import java.io.InputStream;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.StringTokenizer; import java.util.StringTokenizer;
@@ -38,13 +37,15 @@ import java.util.StringTokenizer;
import org.alfresco.error.AlfrescoRuntimeException; import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.model.ContentModel; import org.alfresco.model.ContentModel;
import org.alfresco.repo.processor.BaseProcessor; 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.FileInfo;
import org.alfresco.service.cmr.model.FileNotFoundException; import org.alfresco.service.cmr.model.FileNotFoundException;
import org.alfresco.service.cmr.repository.ContentIOException; import org.alfresco.service.cmr.repository.ContentIOException;
import org.alfresco.service.cmr.repository.ContentReader; import org.alfresco.service.cmr.repository.ContentReader;
import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.ProcessorExtension; 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.ScriptLocation;
import org.alfresco.service.cmr.repository.ScriptProcessor; import org.alfresco.service.cmr.repository.ScriptProcessor;
import org.alfresco.service.cmr.repository.StoreRef; import org.alfresco.service.cmr.repository.StoreRef;
@@ -63,14 +64,11 @@ import org.springframework.util.FileCopyUtils;
* *
* @author Kevin Roast * @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 Log logger = LogFactory.getLog(RhinoScriptProcessor.class);
private static final String IMPORT_PREFIX = "<import";
private static final String IMPORT_RESOURCE = "resource=\"";
private static final String PATH_CLASSPATH = "classpath:"; private static final String PATH_CLASSPATH = "classpath:";
private static final String SCRIPT_ROOT = "_root";
/** Wrap Factory */ /** Wrap Factory */
private static WrapFactory wrapFactory = new RhinoWrapFactory(); private static WrapFactory wrapFactory = new RhinoWrapFactory();
@@ -190,9 +188,8 @@ public class RhinoScriptProcessor extends BaseProcessor implements ScriptProcess
} }
} }
/** /**
* Resolve the imports in the specified script. Include directives are of the following form: * Resolve the imports in the specified script. Supported include directives are of the following form:
* <pre> * <pre>
* <import resource="classpath:alfresco/includeme.js"> * <import resource="classpath:alfresco/includeme.js">
* <import resource="workspace://SpacesStore/6f73de1b-d3b4-11db-80cb-112e6c2ea048"> * <import resource="workspace://SpacesStore/6f73de1b-d3b4-11db-80cb-112e6c2ea048">
@@ -214,142 +211,7 @@ public class RhinoScriptProcessor extends BaseProcessor implements ScriptProcess
*/ */
private String resolveScriptImports(String script) private String resolveScriptImports(String script)
{ {
// use a linked hashmap to preserve order of includes - the key in the collection is used return ScriptResourceHelper.resolveScriptImports(script, this, logger);
// to resolve multiple includes of the same scripts and therefore cyclic includes also
Map<String, String> scriptlets = new LinkedHashMap<String, String>(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<String, String> scripts)
{
int index = 0;
// skip any initial whitespace
for (; index<script.length(); index++)
{
if (Character.isWhitespace(script.charAt(index)) == false)
{
break;
}
}
// look for the "<import" directive marker
if (script.startsWith(IMPORT_PREFIX, index))
{
// skip whitespace between "<import" and "resource"
boolean afterWhitespace = false;
index += IMPORT_PREFIX.length() + 1;
for (; index<script.length(); index++)
{
if (Character.isWhitespace(script.charAt(index)) == false)
{
afterWhitespace = true;
break;
}
}
if (afterWhitespace == true && script.startsWith(IMPORT_RESOURCE, index))
{
// found an import line!
index += IMPORT_RESOURCE.length();
int resourceStart = index;
for (; index<script.length(); index++)
{
if (script.charAt(index) == '"' && script.charAt(index + 1) == '>')
{
// 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<script.length(); index++)
{
if (Character.isWhitespace(script.charAt(index)) == false)
{
break;
}
}
recurseScriptImports(location, script.substring(index), scripts);
return;
}
}
// if we get here, we failed to find the end of an import line
throw new ScriptException(
"Malformed 'import' line - must be first in file, no comments and strictly of the form:" +
"\r\n<import resource=\"...\">");
}
else
{
throw new ScriptException(
"Malformed 'import' line - must be first in file, no comments and strictly of the form:" +
"\r\n<import resource=\"...\">");
}
}
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);
}
} }
/** /**
@@ -366,7 +228,7 @@ public class RhinoScriptProcessor extends BaseProcessor implements ScriptProcess
* *
* @throws AlfrescoRuntimeException on any IO or ContentIO error * @throws AlfrescoRuntimeException on any IO or ContentIO error
*/ */
private String loadScriptResource(String resource) public String loadScriptResource(String resource)
{ {
String result = null; String result = null;
@@ -462,7 +324,7 @@ public class RhinoScriptProcessor extends BaseProcessor implements ScriptProcess
* *
* @throws AlfrescoRuntimeException * @throws AlfrescoRuntimeException
*/ */
private Object executeScriptImpl(String script, Map<String, Object> origModel, boolean secure) private Object executeScriptImpl(String script, Map<String, Object> model, boolean secure)
throws AlfrescoRuntimeException throws AlfrescoRuntimeException
{ {
long startTime = 0; long startTime = 0;
@@ -472,7 +334,7 @@ public class RhinoScriptProcessor extends BaseProcessor implements ScriptProcess
} }
// Convert the model // Convert the model
Map<String, Object> model = convertToRhinoModel(origModel); model = convertToRhinoModel(model);
// check that rhino script engine is available // check that rhino script engine is available
Context cx = Context.enter(); Context cx = Context.enter();
@@ -509,10 +371,9 @@ public class RhinoScriptProcessor extends BaseProcessor implements ScriptProcess
// add the global scripts // add the global scripts
for (ProcessorExtension ex : this.processorExtensions.values()) 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 // insert supplied object model into root of the default scope
for (String key : model.keySet()) 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); Object result = cx.evaluateString(scope, script, "AlfrescoScript", 1, null);
// extract java object result if wrapped by Rhino // extract java object result if wrapped by Rhino
result = valueConverter.convertValueForRepo((Serializable)result); return valueConverter.convertValueForRepo((Serializable)result);
return result;
} }
catch (Throwable err) catch (Throwable err)
{ {

View File

@@ -28,9 +28,9 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.alfresco.model.ContentModel; import org.alfresco.model.ContentModel;
import org.alfresco.scripts.ScriptException;
import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService; 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.ScriptLocation;
import org.alfresco.service.cmr.repository.ScriptProcessor; import org.alfresco.service.cmr.repository.ScriptProcessor;
import org.alfresco.service.cmr.repository.ScriptService; import org.alfresco.service.cmr.repository.ScriptService;

View File

@@ -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);
}
}

View File

@@ -26,6 +26,7 @@ package org.alfresco.service.cmr.repository;
import java.util.Map; import java.util.Map;
import org.alfresco.scripts.ScriptException;
import org.alfresco.service.Auditable; import org.alfresco.service.Auditable;
import org.alfresco.service.PublicService; import org.alfresco.service.PublicService;
import org.alfresco.service.namespace.QName; import org.alfresco.service.namespace.QName;