mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-24 17:32:48 +00:00
Merged 1.4 to HEAD
svn merge svn://svn.alfresco.com:3691/alfresco/BRANCHES/V1.4@4252 svn://svn.alfresco.com:3691/alfresco/BRANCHES/V1.4@4294 . svn revert root\common\common.xml svn resolved root\projects\repository\config\alfresco\script-services-context.xml git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@4634 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
91
source/java/org/alfresco/repo/jscript/Association.java
Normal file
91
source/java/org/alfresco/repo/jscript/Association.java
Normal file
@@ -0,0 +1,91 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package org.alfresco.repo.jscript;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.alfresco.service.ServiceRegistry;
|
||||
import org.alfresco.service.cmr.repository.AssociationRef;
|
||||
import org.alfresco.util.ParameterCheck;
|
||||
import org.mozilla.javascript.Scriptable;
|
||||
|
||||
/**
|
||||
* Object representing an association
|
||||
*
|
||||
* @author Roy Wetherall
|
||||
*/
|
||||
public class Association implements Scopeable, Serializable
|
||||
{
|
||||
/** Serial version UUID*/
|
||||
private static final long serialVersionUID = 897788515655487131L;
|
||||
|
||||
/** Service registry **/
|
||||
private ServiceRegistry services;
|
||||
|
||||
/** Script scope **/
|
||||
private Scriptable scope;
|
||||
|
||||
/** Association reference **/
|
||||
private AssociationRef assocRef;
|
||||
|
||||
public Association(ServiceRegistry services, AssociationRef assocRef)
|
||||
{
|
||||
this(services, assocRef, null);
|
||||
}
|
||||
|
||||
public Association(ServiceRegistry services, AssociationRef assocRef, Scriptable scope)
|
||||
{
|
||||
ParameterCheck.mandatory("Service registry", services);
|
||||
ParameterCheck.mandatory("Association reference", assocRef);
|
||||
this.services = services;
|
||||
this.assocRef = assocRef;
|
||||
if (scope != null)
|
||||
{
|
||||
this.scope = scope;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.alfresco.repo.jscript.Scopeable#setScope(org.mozilla.javascript.Scriptable)
|
||||
*/
|
||||
public void setScope(Scriptable scope)
|
||||
{
|
||||
this.scope = scope;
|
||||
}
|
||||
|
||||
public AssociationRef getAssociationRef()
|
||||
{
|
||||
return this.assocRef;
|
||||
}
|
||||
|
||||
public String getType()
|
||||
{
|
||||
return assocRef.getTypeQName().toString();
|
||||
}
|
||||
|
||||
public String jsGet_type()
|
||||
{
|
||||
return getType();
|
||||
}
|
||||
|
||||
public Node getSource()
|
||||
{
|
||||
return (Node)new ValueConverter().convertValueForScript(this.services, this.scope, null, assocRef.getSourceRef());
|
||||
}
|
||||
|
||||
public Node jsGet_source()
|
||||
{
|
||||
return getSource();
|
||||
}
|
||||
|
||||
public Node getTarget()
|
||||
{
|
||||
return (Node)new ValueConverter().convertValueForScript(this.services, this.scope, null, assocRef.getTargetRef());
|
||||
}
|
||||
|
||||
public Node jsGet_target()
|
||||
{
|
||||
return getTarget();
|
||||
}
|
||||
}
|
108
source/java/org/alfresco/repo/jscript/Behaviour.java
Normal file
108
source/java/org/alfresco/repo/jscript/Behaviour.java
Normal file
@@ -0,0 +1,108 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package org.alfresco.repo.jscript;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.alfresco.service.ServiceRegistry;
|
||||
import org.mozilla.javascript.Scriptable;
|
||||
|
||||
/**
|
||||
* Object representing the behaviour information
|
||||
*
|
||||
* @author Roy Wetherall
|
||||
*/
|
||||
public class Behaviour implements Scopeable, Serializable
|
||||
{
|
||||
/** Serial version UID **/
|
||||
private static final long serialVersionUID = 1936017361886646100L;
|
||||
|
||||
/** Service registry **/
|
||||
private ServiceRegistry services;
|
||||
|
||||
/** Script scope **/
|
||||
private Scriptable scope;
|
||||
|
||||
/** The name of the policy that this behaviour is linked to **/
|
||||
private String name;
|
||||
|
||||
/** The behaviour argument values **/
|
||||
private Object[] args;
|
||||
|
||||
/** Cached js converted argument values **/
|
||||
private Serializable[] jsArgs;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param services the service registry
|
||||
* @param name the name of the policy associated with this behaviour
|
||||
* @param args the argument values
|
||||
*/
|
||||
public Behaviour(ServiceRegistry services, String name, Object[] args)
|
||||
{
|
||||
this.services = services;
|
||||
this.name = name;
|
||||
this.args = args;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.alfresco.repo.jscript.Scopeable#setScope(org.mozilla.javascript.Scriptable)
|
||||
*/
|
||||
public void setScope(Scriptable scope)
|
||||
{
|
||||
this.scope = scope;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the policy name
|
||||
*
|
||||
* @return the name of the policy
|
||||
*/
|
||||
public String getName()
|
||||
{
|
||||
return this.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* JS accessor method
|
||||
*
|
||||
* @return the name of the policy
|
||||
*/
|
||||
public String jsGet_name()
|
||||
{
|
||||
return getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* The argument values
|
||||
*
|
||||
* @return array containing the argument values
|
||||
*/
|
||||
public Serializable[] getArgs()
|
||||
{
|
||||
if (this.jsArgs == null)
|
||||
{
|
||||
ValueConverter valueConverter = new ValueConverter();
|
||||
this.jsArgs = new Serializable[args.length];
|
||||
int index = 0;
|
||||
for (Object arg : this.args)
|
||||
{
|
||||
this.jsArgs[index] = valueConverter.convertValueForScript(services, this.scope, null, (Serializable)arg);
|
||||
index ++;
|
||||
}
|
||||
}
|
||||
return this.jsArgs;
|
||||
}
|
||||
|
||||
/**
|
||||
* JS accessor method
|
||||
*
|
||||
* @return array containing the argument values
|
||||
*/
|
||||
public Serializable[] jsGet_args()
|
||||
{
|
||||
return getArgs();
|
||||
}
|
||||
}
|
122
source/java/org/alfresco/repo/jscript/ChildAssociation.java
Normal file
122
source/java/org/alfresco/repo/jscript/ChildAssociation.java
Normal file
@@ -0,0 +1,122 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package org.alfresco.repo.jscript;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.alfresco.service.ServiceRegistry;
|
||||
import org.alfresco.service.cmr.repository.ChildAssociationRef;
|
||||
import org.alfresco.util.ParameterCheck;
|
||||
import org.mozilla.javascript.Scriptable;
|
||||
|
||||
/**
|
||||
* Object representing a child association
|
||||
*
|
||||
* @author Roy Wetherall
|
||||
*/
|
||||
public class ChildAssociation implements Scopeable, Serializable
|
||||
{
|
||||
/** Serial version UUID **/
|
||||
private static final long serialVersionUID = -2122640697340663213L;
|
||||
|
||||
/** Service registry **/
|
||||
private ServiceRegistry services;
|
||||
|
||||
/** Script scope **/
|
||||
private Scriptable scope;
|
||||
|
||||
/** Child association reference **/
|
||||
private ChildAssociationRef childAssocRef;
|
||||
|
||||
public ChildAssociation(ServiceRegistry services, ChildAssociationRef childAssocRef)
|
||||
{
|
||||
this(services, childAssocRef, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param services
|
||||
* @param childAssocRef
|
||||
*/
|
||||
public ChildAssociation(ServiceRegistry services, ChildAssociationRef childAssocRef, Scriptable scope)
|
||||
{
|
||||
ParameterCheck.mandatory("Service registry", services);
|
||||
ParameterCheck.mandatory("Child association reference", childAssocRef);
|
||||
this.services = services;
|
||||
this.childAssocRef = childAssocRef;
|
||||
if (scope != null)
|
||||
{
|
||||
this.scope = scope;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.alfresco.repo.jscript.Scopeable#setScope(org.mozilla.javascript.Scriptable)
|
||||
*/
|
||||
public void setScope(Scriptable scope)
|
||||
{
|
||||
this.scope = scope;
|
||||
}
|
||||
|
||||
public ChildAssociationRef getChildAssociationRef()
|
||||
{
|
||||
return this.childAssocRef;
|
||||
}
|
||||
|
||||
public String getType()
|
||||
{
|
||||
return childAssocRef.getTypeQName().toString();
|
||||
}
|
||||
|
||||
public String jsGet_type()
|
||||
{
|
||||
return getType();
|
||||
}
|
||||
|
||||
public String getName()
|
||||
{
|
||||
return childAssocRef.getQName().toString();
|
||||
}
|
||||
|
||||
public String jsGet_name()
|
||||
{
|
||||
return getName();
|
||||
}
|
||||
|
||||
public Node getParent()
|
||||
{
|
||||
return (Node)new ValueConverter().convertValueForScript(this.services, this.scope, null, childAssocRef.getParentRef());
|
||||
}
|
||||
|
||||
public Node jsGet_parent()
|
||||
{
|
||||
return getParent();
|
||||
}
|
||||
|
||||
public Node getChild()
|
||||
{
|
||||
return (Node)new ValueConverter().convertValueForScript(this.services, this.scope, null, childAssocRef.getChildRef());
|
||||
}
|
||||
|
||||
public Node jsGet_child()
|
||||
{
|
||||
return getChild();
|
||||
}
|
||||
|
||||
public boolean isPrimary()
|
||||
{
|
||||
return this.childAssocRef.isPrimary();
|
||||
}
|
||||
|
||||
public int getNthSibling()
|
||||
{
|
||||
return this.childAssocRef.getNthSibling();
|
||||
}
|
||||
|
||||
public int jsGet_nthSibling()
|
||||
{
|
||||
return getNthSibling();
|
||||
}
|
||||
}
|
@@ -0,0 +1,87 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package org.alfresco.repo.jscript;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
|
||||
import org.alfresco.error.AlfrescoRuntimeException;
|
||||
import org.alfresco.service.cmr.repository.ScriptException;
|
||||
import org.alfresco.service.cmr.repository.ScriptLocation;
|
||||
import org.alfresco.util.ParameterCheck;
|
||||
|
||||
/**
|
||||
* Classpath script location object.
|
||||
*
|
||||
* @author Roy Wetherall
|
||||
*
|
||||
*/
|
||||
public class ClasspathScriptLocation implements ScriptLocation
|
||||
{
|
||||
/** Classpath location **/
|
||||
private String location;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param location the classpath location
|
||||
*/
|
||||
public ClasspathScriptLocation(String location)
|
||||
{
|
||||
ParameterCheck.mandatory("Location", location);
|
||||
this.location = location;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.alfresco.service.cmr.repository.ScriptLocation#getReader()
|
||||
*/
|
||||
public Reader getReader()
|
||||
{
|
||||
Reader reader = null;
|
||||
try
|
||||
{
|
||||
InputStream stream = getClass().getClassLoader().getResourceAsStream(location);
|
||||
if (stream == null)
|
||||
{
|
||||
throw new AlfrescoRuntimeException("Unable to load classpath resource: " + location);
|
||||
}
|
||||
reader = new InputStreamReader(stream);
|
||||
}
|
||||
catch (Throwable err)
|
||||
{
|
||||
throw new ScriptException("Failed to load classpath resource '" + location + "': " + err.getMessage(), err);
|
||||
}
|
||||
|
||||
return reader;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj)
|
||||
{
|
||||
if (obj == this)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else if (obj == null || !(obj instanceof ClasspathScriptLocation))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
ClasspathScriptLocation other = (ClasspathScriptLocation)obj;
|
||||
return this.location.equals(other.location);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
return 37 * this.location.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return this.location.toString();
|
||||
}
|
||||
|
||||
}
|
@@ -33,9 +33,11 @@ import org.alfresco.service.cmr.repository.ContentReader;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.ScriptException;
|
||||
import org.alfresco.service.cmr.repository.ScriptImplementation;
|
||||
import org.alfresco.service.cmr.repository.ScriptLocation;
|
||||
import org.alfresco.service.cmr.repository.ScriptService;
|
||||
import org.alfresco.service.cmr.repository.TemplateImageResolver;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.alfresco.util.ParameterCheck;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.mozilla.javascript.Context;
|
||||
import org.mozilla.javascript.Scriptable;
|
||||
@@ -165,6 +167,37 @@ public class RhinoScriptService implements ScriptService
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.alfresco.service.cmr.repository.ScriptService#executeScript(org.alfresco.service.cmr.repository.ScriptLocation, java.util.Map)
|
||||
*/
|
||||
public Object executeScript(ScriptLocation location, Map<String, Object> model)
|
||||
throws ScriptException
|
||||
{
|
||||
ParameterCheck.mandatory("Location", location);
|
||||
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("Executing script: " + location.toString());
|
||||
}
|
||||
|
||||
Reader reader = null;
|
||||
try
|
||||
{
|
||||
return executeScriptImpl(location.getReader(), model);
|
||||
}
|
||||
catch (Throwable err)
|
||||
{
|
||||
throw new ScriptException("Failed to execute script '" + location.toString() + "': " + err.getMessage(), err);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (reader != null)
|
||||
{
|
||||
try {reader.close();} catch (IOException ioErr) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.alfresco.service.cmr.repository.ScriptService#executeScriptString(java.lang.String, java.util.Map)
|
||||
|
189
source/java/org/alfresco/repo/jscript/ScriptBehaviour.java
Normal file
189
source/java/org/alfresco/repo/jscript/ScriptBehaviour.java
Normal file
@@ -0,0 +1,189 @@
|
||||
/*
|
||||
* 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.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.repo.policy.BaseBehaviour;
|
||||
import org.alfresco.repo.policy.PolicyException;
|
||||
import org.alfresco.service.ServiceRegistry;
|
||||
import org.alfresco.service.cmr.repository.ScriptLocation;
|
||||
import org.alfresco.util.ParameterCheck;
|
||||
|
||||
|
||||
/**
|
||||
* JavaScript behaviour implementation
|
||||
*
|
||||
* @author Roy Wetherall
|
||||
*/
|
||||
public class ScriptBehaviour extends BaseBehaviour
|
||||
{
|
||||
private ServiceRegistry serviceRegistry;
|
||||
|
||||
private ScriptLocation location;
|
||||
|
||||
public ScriptBehaviour()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
public ScriptBehaviour(ServiceRegistry serviceRegistry, ScriptLocation location)
|
||||
{
|
||||
this(serviceRegistry, location, NotificationFrequency.EVERY_EVENT);
|
||||
}
|
||||
|
||||
public ScriptBehaviour(ServiceRegistry serviceRegistry, ScriptLocation location, NotificationFrequency frequency)
|
||||
{
|
||||
super(frequency);
|
||||
ParameterCheck.mandatory("Location", location);
|
||||
ParameterCheck.mandatory("ServiceRegistry", serviceRegistry);
|
||||
this.location = location;
|
||||
this.serviceRegistry = serviceRegistry;
|
||||
}
|
||||
|
||||
public void setServiceRegistry(ServiceRegistry serviceRegistry)
|
||||
{
|
||||
this.serviceRegistry = serviceRegistry;
|
||||
}
|
||||
|
||||
public void setLocation(ScriptLocation location)
|
||||
{
|
||||
this.location = location;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "JavaScript behaviour[location = " + this.location.toString() + "]";
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public synchronized <T> T getInterface(Class<T> policy)
|
||||
{
|
||||
ParameterCheck.mandatory("Policy class", policy);
|
||||
Object proxy = proxies.get(policy);
|
||||
if (proxy == null)
|
||||
{
|
||||
Method[] policyIFMethods = policy.getMethods();
|
||||
if (policyIFMethods.length != 1)
|
||||
{
|
||||
throw new PolicyException("Policy interface " + policy.getCanonicalName() + " must have only one method");
|
||||
}
|
||||
|
||||
InvocationHandler handler = new JavaScriptInvocationHandler(this);
|
||||
proxy = Proxy.newProxyInstance(policy.getClassLoader(), new Class[]{policy}, handler);
|
||||
proxies.put(policy, proxy);
|
||||
}
|
||||
return (T)proxy;
|
||||
}
|
||||
|
||||
/**
|
||||
* JavaScript Invocation Handler
|
||||
*
|
||||
* @author Roy Wetherall
|
||||
*/
|
||||
private static class JavaScriptInvocationHandler implements InvocationHandler
|
||||
{
|
||||
private ScriptBehaviour behaviour;
|
||||
|
||||
private JavaScriptInvocationHandler(ScriptBehaviour behaviour)
|
||||
{
|
||||
this.behaviour = behaviour;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
|
||||
*/
|
||||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
|
||||
{
|
||||
// Handle Object level methods
|
||||
if (method.getName().equals("toString"))
|
||||
{
|
||||
return toString();
|
||||
}
|
||||
else if (method.getName().equals("hashCode"))
|
||||
{
|
||||
return hashCode();
|
||||
}
|
||||
else if (method.getName().equals("equals"))
|
||||
{
|
||||
if (Proxy.isProxyClass(args[0].getClass()))
|
||||
{
|
||||
return equals(Proxy.getInvocationHandler(args[0]));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Delegate to designated method pointer
|
||||
if (behaviour.isEnabled())
|
||||
{
|
||||
try
|
||||
{
|
||||
behaviour.disable();
|
||||
return invokeScript(method, args);
|
||||
}
|
||||
finally
|
||||
{
|
||||
behaviour.enable();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Object invokeScript(Method method, Object[] args)
|
||||
{
|
||||
// Build the model
|
||||
Map<String, Object> model = new HashMap<String, Object>(1);
|
||||
model.put("behaviour", new org.alfresco.repo.jscript.Behaviour(this.behaviour.serviceRegistry, method.getName(), args));
|
||||
|
||||
// Execute the script
|
||||
return this.behaviour.serviceRegistry.getScriptService().executeScript(this.behaviour.location, model);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj)
|
||||
{
|
||||
if (obj == this)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else if (obj == null || !(obj instanceof JavaScriptInvocationHandler))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
JavaScriptInvocationHandler other = (JavaScriptInvocationHandler)obj;
|
||||
return behaviour.location.equals(other.behaviour.location);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
return 37 * behaviour.location.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "JavaScriptBehaviour[location=" + behaviour.location.toString() + "]";
|
||||
}
|
||||
}
|
||||
}
|
175
source/java/org/alfresco/repo/jscript/ScriptBehaviourTest.java
Normal file
175
source/java/org/alfresco/repo/jscript/ScriptBehaviourTest.java
Normal file
@@ -0,0 +1,175 @@
|
||||
/*
|
||||
* 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.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.model.ContentModel;
|
||||
import org.alfresco.repo.node.NodeServicePolicies;
|
||||
import org.alfresco.repo.policy.PolicyComponent;
|
||||
import org.alfresco.repo.security.authentication.AuthenticationComponent;
|
||||
import org.alfresco.service.ServiceRegistry;
|
||||
import org.alfresco.service.cmr.repository.ChildAssociationRef;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.cmr.repository.ScriptLocation;
|
||||
import org.alfresco.service.cmr.repository.StoreRef;
|
||||
import org.alfresco.service.namespace.NamespaceService;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.alfresco.util.BaseSpringTest;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author Roy Wetherall
|
||||
*/
|
||||
public class ScriptBehaviourTest extends BaseSpringTest
|
||||
{
|
||||
private ServiceRegistry serviceRegistry;
|
||||
private NodeService nodeService;
|
||||
private PolicyComponent policyComponent;
|
||||
|
||||
private StoreRef storeRef;
|
||||
private NodeRef folderNodeRef;
|
||||
|
||||
protected String[] getConfigLocations()
|
||||
{
|
||||
return new String[] { "classpath:org/alfresco/repo/jscript/test-context.xml" };
|
||||
}
|
||||
|
||||
/**
|
||||
* On setup in transaction implementation
|
||||
*/
|
||||
@Override
|
||||
protected void onSetUpInTransaction()
|
||||
throws Exception
|
||||
{
|
||||
// Get the required services
|
||||
this.nodeService = (NodeService)this.applicationContext.getBean("nodeService");
|
||||
this.policyComponent = (PolicyComponent)this.applicationContext.getBean("policyComponent");
|
||||
this.serviceRegistry = (ServiceRegistry)this.applicationContext.getBean("ServiceRegistry");
|
||||
|
||||
AuthenticationComponent authenticationComponent = (AuthenticationComponent)this.applicationContext.getBean("authenticationComponent");
|
||||
authenticationComponent.setCurrentUser("admin");
|
||||
|
||||
// Create the store and get the root node reference
|
||||
this.storeRef = this.nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, "Test_" + System.currentTimeMillis());
|
||||
NodeRef rootNodeRef = this.nodeService.getRootNode(storeRef);
|
||||
|
||||
// Create folder node
|
||||
Map<QName, Serializable> props = new HashMap<QName, Serializable>(1);
|
||||
props.put(ContentModel.PROP_NAME, "TestFolder");
|
||||
ChildAssociationRef childAssocRef = this.nodeService.createNode(
|
||||
rootNodeRef,
|
||||
ContentModel.ASSOC_CHILDREN,
|
||||
QName.createQName("{test}TestFolder"),
|
||||
ContentModel.TYPE_FOLDER,
|
||||
props);
|
||||
this.folderNodeRef = childAssocRef.getChildRef();
|
||||
}
|
||||
|
||||
public void testEnableDiableBehaviour()
|
||||
{
|
||||
// Register the onCreateNode behaviour script
|
||||
ScriptLocation location = new ClasspathScriptLocation("org/alfresco/repo/jscript/test_onCreateNode_cmContent.js");
|
||||
ScriptBehaviour behaviour = new ScriptBehaviour(this.serviceRegistry, location);
|
||||
|
||||
this.policyComponent.bindClassBehaviour(
|
||||
QName.createQName(NodeServicePolicies.OnCreateNodePolicy.NAMESPACE, "onCreateNode"),
|
||||
ContentModel.TYPE_CONTENT,
|
||||
behaviour);
|
||||
|
||||
behaviour.disable();
|
||||
|
||||
// Create a content node
|
||||
Map<QName, Serializable> props = new HashMap<QName, Serializable>(1);
|
||||
props.put(ContentModel.PROP_NAME, "myDoc.txt");
|
||||
ChildAssociationRef childAssoc = this.nodeService.createNode(
|
||||
this.folderNodeRef,
|
||||
ContentModel.ASSOC_CONTAINS,
|
||||
QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "myDoc.txt"),
|
||||
ContentModel.TYPE_CONTENT,
|
||||
props);
|
||||
assertFalse(this.nodeService.hasAspect(childAssoc.getChildRef(), ContentModel.ASPECT_TITLED));
|
||||
|
||||
behaviour.enable();
|
||||
|
||||
Map<QName, Serializable> props2 = new HashMap<QName, Serializable>(1);
|
||||
props2.put(ContentModel.PROP_NAME, "myDoc1.txt");
|
||||
ChildAssociationRef childAssoc2 = this.nodeService.createNode(
|
||||
this.folderNodeRef,
|
||||
ContentModel.ASSOC_CONTAINS,
|
||||
QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "myDoc1.txt"),
|
||||
ContentModel.TYPE_CONTENT,
|
||||
props2);
|
||||
assertTrue(this.nodeService.hasAspect(childAssoc2.getChildRef(), ContentModel.ASPECT_TITLED));
|
||||
}
|
||||
|
||||
public void testClasspathLocationBehaviour()
|
||||
{
|
||||
// Register the onCreateNode behaviour script
|
||||
ScriptLocation location = new ClasspathScriptLocation("org/alfresco/repo/jscript/test_onCreateNode_cmContent.js");
|
||||
ScriptBehaviour behaviour = new ScriptBehaviour(this.serviceRegistry, location);
|
||||
|
||||
this.policyComponent.bindClassBehaviour(
|
||||
QName.createQName(NodeServicePolicies.OnCreateNodePolicy.NAMESPACE, "onCreateNode"),
|
||||
ContentModel.TYPE_CONTENT,
|
||||
behaviour);
|
||||
|
||||
// Create a content node
|
||||
Map<QName, Serializable> props = new HashMap<QName, Serializable>(1);
|
||||
props.put(ContentModel.PROP_NAME, "myDoc.txt");
|
||||
ChildAssociationRef childAssoc = this.nodeService.createNode(
|
||||
this.folderNodeRef,
|
||||
ContentModel.ASSOC_CONTAINS,
|
||||
QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "myDoc.txt"),
|
||||
ContentModel.TYPE_CONTENT,
|
||||
props);
|
||||
|
||||
// Since the behavoiour will have been run check that the titled aspect has been applied
|
||||
assertTrue(this.nodeService.hasAspect(childAssoc.getChildRef(), ContentModel.ASPECT_TITLED));
|
||||
}
|
||||
|
||||
public void testSpringConfiguredBehaviour()
|
||||
{
|
||||
this.nodeService.addAspect(this.folderNodeRef, ContentModel.ASPECT_COUNTABLE, null);
|
||||
assertTrue(this.nodeService.hasAspect(this.folderNodeRef, ContentModel.ASPECT_TITLED));
|
||||
|
||||
// Create a couple of nodes
|
||||
Map<QName, Serializable> props = new HashMap<QName, Serializable>(1);
|
||||
props.put(ContentModel.PROP_NAME, "myDoc.txt");
|
||||
ChildAssociationRef childAssoc = this.nodeService.createNode(
|
||||
this.folderNodeRef,
|
||||
ContentModel.ASSOC_CONTAINS,
|
||||
QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "myDoc.txt"),
|
||||
ContentModel.TYPE_CONTENT,
|
||||
props);
|
||||
Map<QName, Serializable> props2 = new HashMap<QName, Serializable>(1);
|
||||
props2.put(ContentModel.PROP_NAME, "folder2");
|
||||
ChildAssociationRef childAssoc2 = this.nodeService.createNode(
|
||||
this.folderNodeRef,
|
||||
ContentModel.ASSOC_CONTAINS,
|
||||
QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "folder2"),
|
||||
ContentModel.TYPE_FOLDER,
|
||||
props2);
|
||||
|
||||
this.nodeService.addChild(childAssoc2.getChildRef(), childAssoc.getChildRef(), ContentModel.ASSOC_CONTAINS, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "linked"));
|
||||
assertTrue(this.nodeService.hasAspect(childAssoc.getChildRef(), ContentModel.ASPECT_VERSIONABLE));
|
||||
}
|
||||
}
|
@@ -20,6 +20,7 @@ import java.io.StringReader;
|
||||
|
||||
import org.alfresco.error.AlfrescoRuntimeException;
|
||||
import org.alfresco.model.ContentModel;
|
||||
import org.alfresco.repo.search.impl.lucene.LuceneQueryParser;
|
||||
import org.alfresco.service.ServiceRegistry;
|
||||
import org.alfresco.service.cmr.repository.ContentReader;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
@@ -101,9 +102,8 @@ public final class Search implements Scopeable
|
||||
*/
|
||||
public Node findNode(String ref)
|
||||
{
|
||||
String query = ref.replace(":", "\\:");
|
||||
query = query.replace("/", "\\/");
|
||||
Node[] result = query("ID:" + query);
|
||||
String query = "ID:" + LuceneQueryParser.escape(ref);
|
||||
Node[] result = query(query);
|
||||
if (result.length == 1)
|
||||
{
|
||||
return result[0];
|
||||
|
@@ -23,7 +23,10 @@ import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import org.alfresco.service.ServiceRegistry;
|
||||
import org.alfresco.service.cmr.repository.AssociationRef;
|
||||
import org.alfresco.service.cmr.repository.ChildAssociationRef;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.StoreRef;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.mozilla.javascript.Context;
|
||||
import org.mozilla.javascript.NativeArray;
|
||||
@@ -38,7 +41,6 @@ import org.mozilla.javascript.Wrapper;
|
||||
*/
|
||||
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.
|
||||
@@ -63,6 +65,18 @@ public class ValueConverter
|
||||
// so they can be used as objects within a template
|
||||
value = new Node(((NodeRef)value), services, null, scope);
|
||||
}
|
||||
else if (value instanceof QName || value instanceof StoreRef)
|
||||
{
|
||||
value = value.toString();
|
||||
}
|
||||
else if (value instanceof ChildAssociationRef)
|
||||
{
|
||||
value = new ChildAssociation(services, (ChildAssociationRef)value, scope);
|
||||
}
|
||||
else if (value instanceof AssociationRef)
|
||||
{
|
||||
value = new Association(services, (AssociationRef)value, scope);
|
||||
}
|
||||
else if (value instanceof Date)
|
||||
{
|
||||
// convert Date to JavaScript native Date object
|
||||
@@ -110,6 +124,14 @@ public class ValueConverter
|
||||
// convert back to NodeRef
|
||||
value = ((Node)value).getNodeRef();
|
||||
}
|
||||
else if (value instanceof ChildAssociation)
|
||||
{
|
||||
value = ((ChildAssociation)value).getChildAssociationRef();
|
||||
}
|
||||
else if (value instanceof Association)
|
||||
{
|
||||
value = ((Association)value).getAssociationRef();
|
||||
}
|
||||
else if (value instanceof Wrapper)
|
||||
{
|
||||
// unwrap a Java object from a JavaScript wrapper
|
||||
|
51
source/java/org/alfresco/repo/jscript/test-context.xml
Normal file
51
source/java/org/alfresco/repo/jscript/test-context.xml
Normal file
@@ -0,0 +1,51 @@
|
||||
<?xml version='1.0' encoding='UTF-8'?>
|
||||
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>
|
||||
|
||||
<beans>
|
||||
|
||||
<import resource="classpath:alfresco/application-context.xml" />
|
||||
|
||||
<bean id="onAddAspect" class="org.alfresco.repo.policy.registration.ClassPolicyRegistration" parent="policyRegistration">
|
||||
<property name="policyName">
|
||||
<value>{http://www.alfresco.org}onAddAspect</value>
|
||||
</property>
|
||||
<property name="className">
|
||||
<value>{http://www.alfresco.org/model/content/1.0}countable</value>
|
||||
</property>
|
||||
<property name="behaviour">
|
||||
<bean class="org.alfresco.repo.jscript.ScriptBehaviour" parent="scriptBehaviour">
|
||||
<property name="location">
|
||||
<bean class="org.alfresco.repo.jscript.ClasspathScriptLocation">
|
||||
<constructor-arg>
|
||||
<value>org/alfresco/repo/jscript/test_onAddAspect_cmCountable.js</value>
|
||||
</constructor-arg>
|
||||
</bean>
|
||||
</property>
|
||||
</bean>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="onCreateChildAssociation" class="org.alfresco.repo.policy.registration.AssociationPolicyRegistration" parent="policyRegistration">
|
||||
<property name="policyName">
|
||||
<value>{http://www.alfresco.org}onCreateChildAssociation</value>
|
||||
</property>
|
||||
<property name="className">
|
||||
<value>{http://www.alfresco.org/model/content/1.0}folder</value>
|
||||
</property>
|
||||
<property name="associationType">
|
||||
<value>{http://www.alfresco.org/model/content/1.0}contains</value>
|
||||
</property>
|
||||
<property name="behaviour">
|
||||
<bean class="org.alfresco.repo.jscript.ScriptBehaviour" parent="scriptBehaviour">
|
||||
<property name="location">
|
||||
<bean class="org.alfresco.repo.jscript.ClasspathScriptLocation">
|
||||
<constructor-arg>
|
||||
<value>org/alfresco/repo/jscript/test_onCreateChildAssociation.js</value>
|
||||
</constructor-arg>
|
||||
</bean>
|
||||
</property>
|
||||
</bean>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
</beans>
|
@@ -0,0 +1,55 @@
|
||||
logger.log("The counatble aspect has been added");
|
||||
|
||||
var scriptFailed = false;
|
||||
|
||||
// Have a look at the behaviour object that should have been passed
|
||||
if (behaviour == null)
|
||||
{
|
||||
logger.log("The behaviour object has not been set.");
|
||||
scriptFailed = true;
|
||||
}
|
||||
|
||||
// Check the name of the behaviour
|
||||
if (behaviour.name == null && behaviour.name != "onAddAspect")
|
||||
{
|
||||
logger.log("The behaviour name has not been set correctly.");
|
||||
scriptFailed = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.log("Behaviour name: " + behaviour.name);
|
||||
}
|
||||
|
||||
// Check the arguments
|
||||
if (behaviour.args == null)
|
||||
{
|
||||
logger.log("The args have not been set.")
|
||||
scriptFailed = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (behaviour.args.length == 2)
|
||||
{
|
||||
var nodeRef = behaviour.args[0];
|
||||
var aspectType = behaviour.args[1];
|
||||
logger.log("NodeRef: " + nodeRef.id);
|
||||
logger.log("Type: " + aspectType);
|
||||
if (aspectType != "{http://www.alfresco.org/model/content/1.0}countable")
|
||||
{
|
||||
logger.log("Aspect type is incorrect");
|
||||
scriptFailed = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.log("The number of arguments is incorrect.")
|
||||
scriptFailed = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (scriptFailed == false)
|
||||
{
|
||||
nodeRef.addAspect("cm:titled");
|
||||
nodeRef.save();
|
||||
}
|
||||
|
@@ -0,0 +1,51 @@
|
||||
var scriptFailed = false;
|
||||
|
||||
// Have a look at the behaviour object that should have been passed
|
||||
if (behaviour == null)
|
||||
{
|
||||
logger.log("The behaviour object has not been set.");
|
||||
scriptFailed = true;
|
||||
}
|
||||
|
||||
// Check the name of the behaviour
|
||||
if (behaviour.name == null && behaviour.name != "onCreateChildAssociation")
|
||||
{
|
||||
logger.log("The behaviour name has not been set correctly.");
|
||||
scriptFailed = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.log("Behaviour name: " + behaviour.name);
|
||||
}
|
||||
|
||||
// Check the arguments
|
||||
if (behaviour.args == null)
|
||||
{
|
||||
logger.log("The args have not been set.")
|
||||
scriptFailed = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (behaviour.args.length == 1)
|
||||
{
|
||||
var childAssoc = behaviour.args[0];
|
||||
logger.log("Assoc type: " + childAssoc.type);
|
||||
logger.log("Assoc name: " + childAssoc.name);
|
||||
logger.log("Parent node: " + childAssoc.parent.id);
|
||||
logger.log("Child node: " + childAssoc.child.id);
|
||||
logger.log("Is primary: " + childAssoc.isPrimary());
|
||||
logger.log("Nth sibling: " + childAssoc.nthSibling);
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.log("The number of arguments is incorrect.")
|
||||
scriptFailed = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (scriptFailed == false)
|
||||
{
|
||||
childAssoc.child.addAspect("cm:versionable");
|
||||
childAssoc.child.save();
|
||||
}
|
||||
|
@@ -0,0 +1,51 @@
|
||||
var scriptFailed = false;
|
||||
|
||||
// Have a look at the behaviour object that should have been passed
|
||||
if (behaviour == null)
|
||||
{
|
||||
logger.log("The behaviour object has not been set.");
|
||||
scriptFailed = true;
|
||||
}
|
||||
|
||||
// Check the name of the behaviour
|
||||
if (behaviour.name == null && behaviour.name != "onCreateNode")
|
||||
{
|
||||
logger.log("The behaviour name has not been set correctly.");
|
||||
scriptFailed = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.log("Behaviour name: " + behaviour.name);
|
||||
}
|
||||
|
||||
// Check the arguments
|
||||
if (behaviour.args == null)
|
||||
{
|
||||
logger.log("The args have not been set.")
|
||||
scriptFailed = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (behaviour.args.length == 1)
|
||||
{
|
||||
var childAssoc = behaviour.args[0];
|
||||
logger.log("Assoc type: " + childAssoc.type);
|
||||
logger.log("Assoc name: " + childAssoc.name);
|
||||
logger.log("Parent node: " + childAssoc.parent.id);
|
||||
logger.log("Child node: " + childAssoc.child.id);
|
||||
logger.log("Is primary: " + childAssoc.isPrimary());
|
||||
logger.log("Nth sibling: " + childAssoc.nthSibling);
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.log("The number of arguments is incorrect.")
|
||||
scriptFailed = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (scriptFailed == false)
|
||||
{
|
||||
childAssoc.child.addAspect("cm:titled");
|
||||
childAssoc.child.save();
|
||||
}
|
||||
|
Reference in New Issue
Block a user