Rhinoscript Engine:

- Allow actions to be invoked via JavaScript

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@3499 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
David Caruana
2006-08-14 18:04:08 +00:00
parent 5e0163001f
commit 354834c25f
12 changed files with 758 additions and 179 deletions

View File

@@ -43,6 +43,11 @@ public class ParameterDefinitionImpl implements ParameterDefinition, Serializabl
*/
private QName type;
/**
* Is this a multi-valued parameter?
*/
private boolean isMultiValued;
/**
* The display label
*/
@@ -70,8 +75,30 @@ public class ParameterDefinitionImpl implements ParameterDefinition, Serializabl
this.type = type;
this.displayLabel = displayLabel;
this.isMandatory = isMandatory;
this.isMultiValued = false;
}
/**
* Constructor
*
* @param name the name of the parameter
* @param type the type of the parameter
* @param displayLabel the display label
*/
public ParameterDefinitionImpl(
String name,
QName type,
boolean isMandatory,
String displayLabel,
boolean isMultiValued)
{
this.name = name;
this.type = type;
this.displayLabel = displayLabel;
this.isMandatory = isMandatory;
this.isMultiValued = isMultiValued;
}
/**
* @see org.alfresco.service.cmr.action.ParameterDefinition#getName()
*/
@@ -96,6 +123,14 @@ public class ParameterDefinitionImpl implements ParameterDefinition, Serializabl
return this.isMandatory;
}
/**
* @see org.alfresco.service.cmr.action.ParameterDefinition#isMultiValued()
*/
public boolean isMultiValued()
{
return this.isMultiValued;
}
/**
* @see org.alfresco.service.cmr.action.ParameterDefinition#getDisplayLabel()
*/

View File

@@ -311,7 +311,7 @@ public class MailActionExecuter extends ActionExecuterAbstractBase
protected void addParameterDefinitions(List<ParameterDefinition> paramList)
{
paramList.add(new ParameterDefinitionImpl(PARAM_TO, DataTypeDefinition.TEXT, false, getParamDisplayLabel(PARAM_TO)));
paramList.add(new ParameterDefinitionImpl(PARAM_TO_MANY, DataTypeDefinition.ANY, false, getParamDisplayLabel(PARAM_TO_MANY)));
paramList.add(new ParameterDefinitionImpl(PARAM_TO_MANY, DataTypeDefinition.TEXT, false, getParamDisplayLabel(PARAM_TO_MANY), true));
paramList.add(new ParameterDefinitionImpl(PARAM_SUBJECT, DataTypeDefinition.TEXT, true, getParamDisplayLabel(PARAM_SUBJECT)));
paramList.add(new ParameterDefinitionImpl(PARAM_TEXT, DataTypeDefinition.TEXT, true, getParamDisplayLabel(PARAM_TEXT)));
paramList.add(new ParameterDefinitionImpl(PARAM_FROM, DataTypeDefinition.TEXT, false, getParamDisplayLabel(PARAM_FROM)));

View File

@@ -0,0 +1,319 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.repo.jscript;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.action.Action;
import org.alfresco.service.cmr.action.ActionDefinition;
import org.alfresco.service.cmr.action.ActionService;
import org.alfresco.service.cmr.action.ParameterDefinition;
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
import org.alfresco.service.namespace.QName;
import org.mozilla.javascript.Scriptable;
/**
* Scripted Action service for describing and executing actions against Nodes.
*
* @author davidc
*/
public final class Actions implements Scopeable
{
/** Repository Service Registry */
private ServiceRegistry services;
/** Root scope for this object */
private Scriptable scope;
/**
* Constructor
*
* @param services repository service registry
*/
public Actions(ServiceRegistry services)
{
this.services = services;
}
/**
* @see org.alfresco.repo.jscript.Scopeable#setScope(org.mozilla.javascript.Scriptable)
*/
public void setScope(Scriptable scope)
{
this.scope = scope;
}
/**
* Gets the list of registered action names
*
* @return the registered action names
*/
public String[] getRegistered()
{
ActionService actionService = services.getActionService();
List<ActionDefinition> defs = actionService.getActionDefinitions();
String[] registered = new String[defs.size()];
int i = 0;
for (ActionDefinition def : defs)
{
registered[i++] = def.getName();
}
return registered;
}
public String[] jsGet_registered()
{
return getRegistered();
}
/**
* Create an Action
*
* @param actionName the action name
* @return the action
*/
public ScriptAction createAction(String actionName)
{
ScriptAction scriptAction = null;
ActionService actionService = services.getActionService();
ActionDefinition actionDef = actionService.getActionDefinition(actionName);
if (actionDef != null)
{
Action action = actionService.createAction(actionName);
scriptAction = new ScriptAction(action, actionDef);
scriptAction.setScope(scope);
}
return scriptAction;
}
/**
* Scriptable Action
*
* @author davidc
*/
public final class ScriptAction implements Serializable, Scopeable
{
private static final long serialVersionUID = 5794161358406531996L;
/** Root scope for this object */
private Scriptable scope;
/** Converter with knowledge of action parameter values */
private ActionValueConverter converter;
/** Action state */
private Action action;
private ActionDefinition actionDef;
private ScriptableParameterMap<String, Serializable> parameters = null;
/**
* Construct
*
* @param action Alfresco action
*/
public ScriptAction(Action action, ActionDefinition actionDef)
{
this.action = action;
this.actionDef = actionDef;
this.converter = new ActionValueConverter();
}
/**
* @see org.alfresco.repo.jscript.Scopeable#setScope(org.mozilla.javascript.Scriptable)
*/
public void setScope(Scriptable scope)
{
this.scope = scope;
}
/**
* Return all the properties known about this node.
*
* The Map returned implements the Scriptable interface to allow access to the properties via
* JavaScript associative array access. This means properties of a node can be access thus:
* <code>node.properties["name"]</code>
*
* @return Map of properties for this Node.
*/
@SuppressWarnings("synthetic-access")
public Map<String, Serializable> getParameters()
{
if (this.parameters == null)
{
// this Map implements the Scriptable interface for native JS syntax property access
this.parameters = new ScriptableParameterMap<String, Serializable>();
Map<String, Serializable> actionParams = this.action.getParameterValues();
for (Map.Entry<String, Serializable> entry : actionParams.entrySet())
{
String name = entry.getKey();
this.parameters.put(name, converter.convertActionParamForScript(name, entry.getValue()));
}
this.parameters.setModified(false);
}
return this.parameters;
}
public Map<String, Serializable>jsGet_parameters()
{
return getParameters();
}
/**
* Execute action
*
* @param node the node to execute action upon
*/
@SuppressWarnings("synthetic-access")
public void execute(Node node)
{
if (this.parameters.isModified())
{
Map<String, Serializable> actionParams = action.getParameterValues();
actionParams.clear();
for (Map.Entry<String, Serializable> entry : this.parameters.entrySet())
{
// perform the conversion from script wrapper object to repo serializable values
String name = entry.getKey();
Serializable value = converter.convertActionParamForRepo(name, entry.getValue());
actionParams.put(name, value);
}
}
services.getActionService().executeAction(action, node.getNodeRef());
}
/**
* Value converter with specific knowledge of action parameters
*
* @author davidc
*/
private class ActionValueConverter extends ValueConverter
{
/**
* Convert Action Parameter for Script usage
*
* @param paramName parameter name
* @param value value to convert
* @return converted value
*/
@SuppressWarnings("synthetic-access")
public Serializable convertActionParamForScript(String paramName, Serializable value)
{
ParameterDefinition paramDef = actionDef.getParameterDefintion(paramName);
if (paramDef != null && paramDef.getType().equals(DataTypeDefinition.QNAME))
{
return ((QName)value).toPrefixString(services.getNamespaceService());
}
else
{
return convertValueForScript(services, scope, null, value);
}
}
/**
* Convert Action Parameter for Java usage
*
* @param paramName parameter name
* @param value value to convert
* @return converted value
*/
@SuppressWarnings("synthetic-access")
public Serializable convertActionParamForRepo(String paramName, Serializable value)
{
ParameterDefinition paramDef = actionDef.getParameterDefintion(paramName);
if (paramDef != null && paramDef.getType().equals(DataTypeDefinition.QNAME))
{
return QName.createQName((String)value, services.getNamespaceService());
}
else
{
return convertValueForRepo(value);
}
}
}
}
/**
* Scripted Parameter map with modified flag.
*
* @author davidc
*/
public static final class ScriptableParameterMap<K,V> extends ScriptableHashMap<K,V>
{
private static final long serialVersionUID = 574661815973241554L;
private boolean modified = false;
/**
* Is this a modified parameter map?
*
* @return true => modified
*/
/*package*/ boolean isModified()
{
return modified;
}
/**
* Set explicitly whether this map is modified
*
* @param modified true => modified, false => not modified
*/
/*package*/ void setModified(boolean modified)
{
this.modified = modified;
}
/* (non-Javadoc)
* @see org.mozilla.javascript.Scriptable#getClassName()
*/
@Override
public String getClassName()
{
return "ScriptableParameterMap";
}
/* (non-Javadoc)
* @see org.mozilla.javascript.Scriptable#delete(java.lang.String)
*/
@Override
public void delete(String name)
{
super.delete(name);
setModified(true);
}
/* (non-Javadoc)
* @see org.mozilla.javascript.Scriptable#put(java.lang.String, org.mozilla.javascript.Scriptable, java.lang.Object)
*/
@Override
public void put(String name, Scriptable start, Object value)
{
super.put(name, start, value);
setModified(true);
}
}
}

View File

@@ -20,9 +20,6 @@ import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -61,12 +58,8 @@ import org.alfresco.service.namespace.QName;
import org.alfresco.service.namespace.RegexQNamePattern;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.NativeArray;
import org.mozilla.javascript.ScriptRuntime;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.Wrapper;
import org.springframework.util.StringUtils;
/**
@@ -93,6 +86,9 @@ public final class Node implements Serializable, Scopeable
/** Root scope for this object */
private Scriptable scope;
/** Node Value Converter */
private NodeValueConverter converter = null;
/** Cached values */
private NodeRef nodeRef;
private String name;
@@ -117,6 +113,7 @@ public final class Node implements Serializable, Scopeable
// NOTE: see the reset() method when adding new cached members!
// ------------------------------------------------------------------------------
// Construction
@@ -158,6 +155,7 @@ public final class Node implements Serializable, Scopeable
this.nodeService = services.getNodeService();
this.imageResolver = resolver;
this.scope = scope;
this.converter = new NodeValueConverter();
}
/**
@@ -407,7 +405,7 @@ public final class Node implements Serializable, Scopeable
Serializable propValue = props.get(qname);
// perform the conversion to a script safe value and store
this.properties.put(qname.toString(), convertValueForScript(qname, propValue));
this.properties.put(qname.toString(), converter.convertValueForScript(qname, propValue));
}
}
@@ -900,148 +898,13 @@ public final class Node implements Serializable, Scopeable
Serializable value = (Serializable)this.properties.get(key);
// perform the conversion from script wrapper object to repo serializable values
value = convertValueForRepo(value);
value = converter.convertValueForRepo(value);
props.put(createQName(key), value);
}
this.nodeService.setProperties(this.nodeRef, props);
}
/**
* Convert an object from any script wrapper value to a valid repository serializable value.
* This includes converting JavaScript Array objects to Lists of valid objects.
*
* @param value Value to convert from script wrapper object to repo serializable value
*
* @return valid repo value
*/
private static Serializable convertValueForRepo(Serializable value)
{
if (value == null)
{
return null;
}
else if (value instanceof Node)
{
// convert back to NodeRef
value = ((Node)value).getNodeRef();
}
else if (value instanceof ScriptContentData)
{
// convert back to ContentData
value = ((ScriptContentData)value).contentData;
}
else if (value instanceof Wrapper)
{
// unwrap a Java object from a JavaScript wrapper
// recursively call this method to convert the unwrapped value
value = convertValueForRepo((Serializable)((Wrapper)value).unwrap());
}
else if (value instanceof ScriptableObject)
{
// a scriptable object will probably indicate a multi-value property
// set using a JavaScript Array object
ScriptableObject values = (ScriptableObject)value;
if (value instanceof NativeArray)
{
// convert JavaScript array of values to a List of Serializable objects
Object[] propIds = values.getIds();
List<Serializable> propValues = new ArrayList<Serializable>(propIds.length);
for (int i=0; i<propIds.length; i++)
{
// work on each key in turn
Object propId = propIds[i];
// we are only interested in keys that indicate a list of values
if (propId instanceof Integer)
{
// get the value out for the specified key
Serializable val = (Serializable)values.get((Integer)propId, values);
// recursively call this method to convert the value
propValues.add(convertValueForRepo(val));
}
}
value = (Serializable)propValues;
}
else
{
// TODO: add code here to use the dictionary and convert to correct value type
Object javaObj = Context.jsToJava(value, Date.class);
if (javaObj instanceof Serializable)
{
value = (Serializable)javaObj;
}
}
}
else if (value instanceof Serializable[])
{
// convert back a list of Java values
Serializable[] array = (Serializable[])value;
ArrayList<Serializable> list = new ArrayList<Serializable>(array.length);
for (int i=0; i<array.length; i++)
{
list.add(convertValueForRepo(array[i]));
}
value = list;
}
return value;
}
/**
* Convert an object from any repository serialized value to a valid script object.
* This includes converting Collection multi-value properties into JavaScript Array objects.
*
* @param qname QName of the property value for conversion
* @param value Property value
*
* @return Value safe for scripting usage
*/
private Serializable convertValueForScript(QName qname, Serializable value)
{
// perform conversions from Java objects to JavaScript scriptable instances
if (value == null)
{
return null;
}
else if (value instanceof NodeRef)
{
// NodeRef object properties are converted to new Node objects
// so they can be used as objects within a template
value = new Node(((NodeRef)value), this.services, this.imageResolver, this.scope);
}
else if (value instanceof ContentData)
{
// ContentData object properties are converted to ScriptContentData objects
// so the content and other properties of those objects can be accessed
value = new ScriptContentData((ContentData)value, qname);
}
else if (value instanceof Date)
{
// convert Date to JavaScript native Date object
// call the "Date" constructor on the root scope object - passing in the millisecond
// value from the Java date - this will construct a JavaScript Date with the same value
Date date = (Date)value;
Object val = ScriptRuntime.newObject(
Context.getCurrentContext(), this.scope, "Date", new Object[] {date.getTime()});
value = (Serializable)val;
}
else if (value instanceof Collection)
{
// recursively convert each value in the collection
Collection<Serializable> collection = (Collection<Serializable>)value;
Serializable[] array = new Serializable[collection.size()];
int index = 0;
for (Serializable obj : collection)
{
array[index++] = convertValueForScript(qname, obj);
}
value = array;
}
// simple numbers and strings are wrapped automatically by Rhino
return value;
}
/**
* Re-sets the type of the node. Can be called in order specialise a node to a sub-type.
@@ -1346,7 +1209,7 @@ public final class Node implements Serializable, Scopeable
{
// get the value out for the specified key - make sure it is Serializable
Object value = props.get((String)propId, props);
value = convertValueForRepo((Serializable)value);
value = converter.convertValueForRepo((Serializable)value);
aspectProps.put(createQName((String)propId), (Serializable)value);
}
}
@@ -1770,6 +1633,68 @@ public final class Node implements Serializable, Scopeable
}
// ------------------------------------------------------------------------------
// Value Conversion
/**
* Value converter with knowledge of Node specific value types
*/
private final class NodeValueConverter extends ValueConverter
{
/**
* Convert an object from any repository serialized value to a valid script object.
* This includes converting Collection multi-value properties into JavaScript Array objects.
*
* @param qname QName of the property value for conversion
* @param value Property value
*
* @return Value safe for scripting usage
*/
public Serializable convertValueForScript(QName qname, Serializable value)
{
return convertValueForScript(services, scope, qname, value);
}
/* (non-Javadoc)
* @see org.alfresco.repo.jscript.ValueConverter#convertValueForScript(org.alfresco.service.ServiceRegistry, org.mozilla.javascript.Scriptable, org.alfresco.service.namespace.QName, java.io.Serializable)
*/
@Override
public Serializable convertValueForScript(ServiceRegistry services, Scriptable scope, QName qname, Serializable value)
{
if (value instanceof ContentData)
{
// ContentData object properties are converted to ScriptContentData objects
// so the content and other properties of those objects can be accessed
value = new ScriptContentData((ContentData)value, qname);
}
else
{
value = super.convertValueForScript(services, scope, qname, value);
}
return value;
}
/* (non-Javadoc)
* @see org.alfresco.repo.jscript.ValueConverter#convertValueForRepo(java.io.Serializable)
*/
@Override
public Serializable convertValueForRepo(Serializable value)
{
if (value instanceof ScriptContentData)
{
// convert back to ContentData
value = ((ScriptContentData)value).contentData;
}
else
{
value = super.convertValueForRepo(value);
}
return value;
}
}
// ------------------------------------------------------------------------------
// Inner Classes
@@ -1789,7 +1714,7 @@ public final class Node implements Serializable, Scopeable
this.contentData = contentData;
this.property = property;
}
/**
* @return the content stream
*/

View File

@@ -50,30 +50,18 @@ public class RhinoScriptService implements ScriptService
{
private static final Logger logger = Logger.getLogger(RhinoScriptService.class);
/** The permission-safe node service */
private NodeService nodeService;
/** The Content Service to use */
private ContentService contentService;
/** Repository Service Registry */
private ServiceRegistry services;
/**
* Set the node service
* Set the Service Registry
*
* @param nodeService The permission-safe node service
* @param service registry
*/
public void setNodeService(NodeService nodeService)
public void setServiceRegistry(ServiceRegistry services)
{
this.nodeService = nodeService;
}
/**
* Set the content service
*
* @param contentService The ContentService to use
*/
public void setContentService(ContentService contentService)
{
this.contentService = contentService;
this.services = services;
}
/**
@@ -136,7 +124,7 @@ public class RhinoScriptService implements ScriptService
Reader reader = null;
try
{
if (this.nodeService.exists(scriptRef) == false)
if (this.services.getNodeService().exists(scriptRef) == false)
{
throw new AlfrescoRuntimeException("Script Node does not exist: " + scriptRef);
}
@@ -145,7 +133,7 @@ public class RhinoScriptService implements ScriptService
{
contentProp = ContentModel.PROP_CONTENT;
}
ContentReader cr = this.contentService.getReader(scriptRef, contentProp);
ContentReader cr = this.services.getContentService().getReader(scriptRef, contentProp);
if (cr == null || cr.exists() == false)
{
throw new AlfrescoRuntimeException("Script Node content not found: " + scriptRef);
@@ -224,9 +212,18 @@ public class RhinoScriptService implements ScriptService
// you need one. However, initStandardObjects is an expensive method to call and it
// allocates a fair amount of memory.
Scriptable scope = cx.initStandardObjects();
// there's always a model, if only to hold the util objects
if (model == null)
{
model = new HashMap<String, Object>();
}
// add useful util objects
model.put("actions", new Actions(services));
model.put("logger", new ScriptLogger());
// insert supplied object model into root of the default scope
if (model != null)
{
for (String key : model.keySet())
{
@@ -346,9 +343,7 @@ public class RhinoScriptService implements ScriptService
model.put("space", new Node(space, services, resolver));
}
// add other useful util objects
model.put("search", new Search(services, companyHome.getStoreRef(), resolver));
model.put("logger", new ScriptLogger());
return model;
}

View File

@@ -24,6 +24,7 @@ import java.util.Map;
import junit.framework.TestCase;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.repo.dictionary.DictionaryComponent;
import org.alfresco.repo.dictionary.DictionaryDAO;
@@ -267,7 +268,114 @@ public class RhinoScriptTest extends TestCase
});
}
public void testScriptActions()
{
TransactionUtil.executeInUserTransaction(
transactionService,
new TransactionUtil.TransactionWork<Object>()
{
public Object doWork() throws Exception
{
StoreRef store = nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, "rhino_" + System.currentTimeMillis());
NodeRef root = nodeService.getRootNode(store);
try
{
// create a content object
ChildAssociationRef childRef = nodeService.createNode(
root,
BaseNodeServiceTest.ASSOC_TYPE_QNAME_TEST_CHILDREN,
QName.createQName(BaseNodeServiceTest.NAMESPACE, "script_content"),
BaseNodeServiceTest.TYPE_QNAME_TEST_CONTENT,
null);
NodeRef contentNodeRef = childRef.getChildRef();
ContentWriter writer = contentService.getWriter(
contentNodeRef,
BaseNodeServiceTest.PROP_QNAME_TEST_CONTENT,
true);
writer.setMimetype("application/x-javascript");
writer.putContent(TESTSCRIPT1);
// create an Alfresco scriptable Node object
// the Node object is a wrapper similar to the TemplateNode concept
Map<String, Object> model = new HashMap<String, Object>();
model.put("doc", new Node(childRef.getChildRef(), serviceRegistry, null));
model.put("root", new Node(root, serviceRegistry, null));
// execute to add aspect via action
Object result = scriptService.executeScript(TESTSCRIPT_CLASSPATH2, model);
System.out.println("Result from TESTSCRIPT_CLASSPATH2: " + result.toString());
assertTrue((Boolean)result); // we know the result is a boolean
// ensure aspect has been added via script
assertTrue(nodeService.hasAspect(childRef.getChildRef(), ContentModel.ASPECT_LOCKABLE));
}
catch (Throwable err)
{
err.printStackTrace();
fail(err.getMessage());
}
return null;
}
});
}
public void xtestScriptActionsMail()
{
TransactionUtil.executeInUserTransaction(
transactionService,
new TransactionUtil.TransactionWork<Object>()
{
public Object doWork() throws Exception
{
StoreRef store = nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, "rhino_" + System.currentTimeMillis());
NodeRef root = nodeService.getRootNode(store);
try
{
// create a content object
ChildAssociationRef childRef = nodeService.createNode(
root,
BaseNodeServiceTest.ASSOC_TYPE_QNAME_TEST_CHILDREN,
QName.createQName(BaseNodeServiceTest.NAMESPACE, "script_content"),
BaseNodeServiceTest.TYPE_QNAME_TEST_CONTENT,
null);
NodeRef contentNodeRef = childRef.getChildRef();
ContentWriter writer = contentService.getWriter(
contentNodeRef,
BaseNodeServiceTest.PROP_QNAME_TEST_CONTENT,
true);
writer.setMimetype("application/x-javascript");
writer.putContent(TESTSCRIPT1);
// create an Alfresco scriptable Node object
// the Node object is a wrapper similar to the TemplateNode concept
Map<String, Object> model = new HashMap<String, Object>();
model.put("doc", new Node(childRef.getChildRef(), serviceRegistry, null));
model.put("root", new Node(root, serviceRegistry, null));
// execute to add aspect via action
Object result = scriptService.executeScript(TESTSCRIPT_CLASSPATH3, model);
System.out.println("Result from TESTSCRIPT_CLASSPATH3: " + result.toString());
assertTrue((Boolean)result); // we know the result is a boolean
}
catch (Throwable err)
{
err.printStackTrace();
fail(err.getMessage());
}
return null;
}
});
}
private static final String TESTSCRIPT_CLASSPATH1 = "org/alfresco/repo/jscript/test_script1.js";
private static final String TESTSCRIPT_CLASSPATH2 = "org/alfresco/repo/jscript/test_script2.js";
private static final String TESTSCRIPT_CLASSPATH3 = "org/alfresco/repo/jscript/test_script3.js";
private static final String TESTSCRIPT1 =
"var id = root.id;\r\n" +

View File

@@ -24,8 +24,10 @@ import org.mozilla.javascript.Scriptable;
/**
* @author Kevin Roast
*/
public class ScriptableHashMap<K,V> extends LinkedHashMap implements Scriptable
public class ScriptableHashMap<K,V> extends LinkedHashMap<K, V> implements Scriptable
{
private static final long serialVersionUID = 3664761893203964569L;
private Scriptable parentScope;
private Scriptable prototype;
@@ -88,10 +90,11 @@ public class ScriptableHashMap<K,V> extends LinkedHashMap implements Scriptable
/**
* @see org.mozilla.javascript.Scriptable#put(java.lang.String, org.mozilla.javascript.Scriptable, java.lang.Object)
*/
@SuppressWarnings("unchecked")
public void put(String name, Scriptable start, Object value)
{
// add the property to the underlying QName map
put(name, value);
put((K)name, (V)value);
}
/**

View File

@@ -0,0 +1,170 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.repo.jscript;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.namespace.QName;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.NativeArray;
import org.mozilla.javascript.ScriptRuntime;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.Wrapper;
/**
* Value conversion allowing safe usage of values in Script and Java.
*/
public class ValueConverter
{
/**
* Convert an object from any repository serialized value to a valid script object.
* This includes converting Collection multi-value properties into JavaScript Array objects.
*
* @param services Repository Services Registry
* @param scope Scripting scope
* @param qname QName of the property value for conversion
* @param value Property value
*
* @return Value safe for scripting usage
*/
public Serializable convertValueForScript(ServiceRegistry services, Scriptable scope, QName qname, Serializable value)
{
// perform conversions from Java objects to JavaScript scriptable instances
if (value == null)
{
return null;
}
else if (value instanceof NodeRef)
{
// NodeRef object properties are converted to new Node objects
// so they can be used as objects within a template
value = new Node(((NodeRef)value), services, null, scope);
}
else if (value instanceof Date)
{
// convert Date to JavaScript native Date object
// call the "Date" constructor on the root scope object - passing in the millisecond
// value from the Java date - this will construct a JavaScript Date with the same value
Date date = (Date)value;
Object val = ScriptRuntime.newObject(
Context.getCurrentContext(), scope, "Date", new Object[] {date.getTime()});
value = (Serializable)val;
}
else if (value instanceof Collection)
{
// recursively convert each value in the collection
Collection<Serializable> collection = (Collection<Serializable>)value;
Serializable[] array = new Serializable[collection.size()];
int index = 0;
for (Serializable obj : collection)
{
array[index++] = convertValueForScript(services, scope, qname, obj);
}
value = array;
}
// simple numbers and strings are wrapped automatically by Rhino
return value;
}
/**
* Convert an object from any script wrapper value to a valid repository serializable value.
* This includes converting JavaScript Array objects to Lists of valid objects.
*
* @param value Value to convert from script wrapper object to repo serializable value
*
* @return valid repo value
*/
public Serializable convertValueForRepo(Serializable value)
{
if (value == null)
{
return null;
}
else if (value instanceof Node)
{
// convert back to NodeRef
value = ((Node)value).getNodeRef();
}
else if (value instanceof Wrapper)
{
// unwrap a Java object from a JavaScript wrapper
// recursively call this method to convert the unwrapped value
value = convertValueForRepo((Serializable)((Wrapper)value).unwrap());
}
else if (value instanceof ScriptableObject)
{
// a scriptable object will probably indicate a multi-value property
// set using a JavaScript Array object
ScriptableObject values = (ScriptableObject)value;
if (value instanceof NativeArray)
{
// convert JavaScript array of values to a List of Serializable objects
Object[] propIds = values.getIds();
List<Serializable> propValues = new ArrayList<Serializable>(propIds.length);
for (int i=0; i<propIds.length; i++)
{
// work on each key in turn
Object propId = propIds[i];
// we are only interested in keys that indicate a list of values
if (propId instanceof Integer)
{
// get the value out for the specified key
Serializable val = (Serializable)values.get((Integer)propId, values);
// recursively call this method to convert the value
propValues.add(convertValueForRepo(val));
}
}
value = (Serializable)propValues;
}
else
{
// TODO: add code here to use the dictionary and convert to correct value type
Object javaObj = Context.jsToJava(value, Date.class);
if (javaObj instanceof Serializable)
{
value = (Serializable)javaObj;
}
}
}
else if (value instanceof Serializable[])
{
// convert back a list of Java values
Serializable[] array = (Serializable[])value;
ArrayList<Serializable> list = new ArrayList<Serializable>(array.length);
for (int i=0; i<array.length; i++)
{
list.add(convertValueForRepo(array[i]));
}
value = list;
}
return value;
}
}

View File

@@ -0,0 +1,9 @@
// create add action
var addAspectAction = actions.createAction("add-features");
addAspectAction.parameters["aspect-name"] = "cm:lockable";
// execute action against passed in node
addAspectAction.execute(doc);
// return
true;

View File

@@ -0,0 +1,13 @@
// create mail action
var mail = actions.createAction("mail");
mail.parameters["to"] = "davidc@alfresco.com";
mail.parameters["subject"] = "Hello from JavaScript";
mail.parameters["from"] = "david.caruana@alfresco.org";
mail.parameters["template"] = root.childByNamePath("Company Home/Data Dictionary/Email Templates/notify_user_email.ftl");
mail.parameters["text"] = "some text, in case template is not found";
// execute action against passed in node
mail.execute(doc);
// return
true;

View File

@@ -41,6 +41,11 @@ public interface ParameterDefinition
*/
public QName getType();
/**
* Is multi-valued?
*/
public boolean isMultiValued();
/**
* Indicates whether the parameter is mandatory or not.
* <p>