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:
109
source/java/org/alfresco/repo/policy/BaseBehaviour.java
Normal file
109
source/java/org/alfresco/repo/policy/BaseBehaviour.java
Normal file
@@ -0,0 +1,109 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package org.alfresco.repo.policy;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Stack;
|
||||
|
||||
import org.alfresco.repo.policy.Behaviour.NotificationFrequency;
|
||||
import org.alfresco.util.ParameterCheck;
|
||||
|
||||
/**
|
||||
* Base behaviour implementation
|
||||
*
|
||||
* @author Roy Wetherall
|
||||
*/
|
||||
public abstract class BaseBehaviour implements Behaviour
|
||||
{
|
||||
/** The notification frequency */
|
||||
protected NotificationFrequency frequency = NotificationFrequency.EVERY_EVENT;
|
||||
|
||||
/** Disabled stack **/
|
||||
private StackThreadLocal disabled = new StackThreadLocal();
|
||||
|
||||
/** Proxies **/
|
||||
protected Map<Class, Object> proxies = new HashMap<Class, Object>();
|
||||
|
||||
/**
|
||||
* Default constructor
|
||||
*/
|
||||
public BaseBehaviour()
|
||||
{
|
||||
// Default constructor
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param frequency the notification frequency
|
||||
*/
|
||||
public BaseBehaviour(NotificationFrequency frequency)
|
||||
{
|
||||
ParameterCheck.mandatory("Frequency", frequency);
|
||||
this.frequency = frequency;
|
||||
}
|
||||
|
||||
public void setNotificationFrequency(NotificationFrequency frequency)
|
||||
{
|
||||
this.frequency = frequency;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable this behaviour for the curent thread
|
||||
*/
|
||||
public void disable()
|
||||
{
|
||||
Stack<Integer> stack = disabled.get();
|
||||
stack.push(hashCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable this behaviour for the current thread
|
||||
*/
|
||||
public void enable()
|
||||
{
|
||||
Stack<Integer> stack = disabled.get();
|
||||
if (stack.peek().equals(hashCode()) == false)
|
||||
{
|
||||
throw new PolicyException("Cannot enable " + this.toString() + " at this time - mismatched with disable calls");
|
||||
}
|
||||
stack.pop();
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates whether the this behaviour is current enabled or not
|
||||
*
|
||||
* @return true if the behaviour is enabled, false otherwise
|
||||
*/
|
||||
public boolean isEnabled()
|
||||
{
|
||||
Stack<Integer> stack = disabled.get();
|
||||
return stack.search(hashCode()) == -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the notification frequency
|
||||
*
|
||||
* @return the notification frequency
|
||||
*/
|
||||
public NotificationFrequency getNotificationFrequency()
|
||||
{
|
||||
return frequency;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stack specific Thread Local
|
||||
*
|
||||
* @author David Caruana
|
||||
*/
|
||||
class StackThreadLocal extends ThreadLocal<Stack<Integer>>
|
||||
{
|
||||
@Override
|
||||
protected Stack<Integer> initialValue()
|
||||
{
|
||||
return new Stack<Integer>();
|
||||
}
|
||||
}
|
||||
}
|
@@ -20,9 +20,6 @@ import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Stack;
|
||||
|
||||
import org.alfresco.util.ParameterCheck;
|
||||
|
||||
@@ -36,24 +33,14 @@ import org.alfresco.util.ParameterCheck;
|
||||
* @author David Caruana
|
||||
*
|
||||
*/
|
||||
public class JavaBehaviour implements Behaviour
|
||||
public class JavaBehaviour extends BaseBehaviour
|
||||
{
|
||||
// The object instance holding the method
|
||||
private Object instance;
|
||||
Object instance;
|
||||
|
||||
// The method name
|
||||
private String method;
|
||||
String method;
|
||||
|
||||
// Notification Frequency
|
||||
private NotificationFrequency frequency;
|
||||
|
||||
// Cache of interface proxies (by interface class)
|
||||
private Map<Class, Object> proxies = new HashMap<Class, Object>();
|
||||
|
||||
// Enable / Disable invocation of behaviour
|
||||
private StackThreadLocal disabled = new StackThreadLocal();
|
||||
|
||||
|
||||
/**
|
||||
* Construct.
|
||||
*
|
||||
@@ -73,76 +60,33 @@ public class JavaBehaviour implements Behaviour
|
||||
*/
|
||||
public JavaBehaviour(Object instance, String method, NotificationFrequency frequency)
|
||||
{
|
||||
ParameterCheck.mandatory("Instance", instance);
|
||||
super(frequency);
|
||||
ParameterCheck.mandatory("Instance", instance);
|
||||
ParameterCheck.mandatory("Method", method);
|
||||
this.instance = instance;
|
||||
this.method = method;
|
||||
this.frequency = frequency;
|
||||
this.instance = instance;
|
||||
}
|
||||
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.repo.policy.Behaviour#getInterface(java.lang.Class)
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public synchronized <T> T getInterface(Class<T> policy)
|
||||
{
|
||||
ParameterCheck.mandatory("Policy class", policy);
|
||||
Object proxy = proxies.get(policy);
|
||||
if (proxy == null)
|
||||
{
|
||||
InvocationHandler handler = getInvocationHandler(instance, method, policy);
|
||||
proxy = Proxy.newProxyInstance(policy.getClassLoader(), new Class[]{policy}, handler);
|
||||
proxies.put(policy, proxy);
|
||||
}
|
||||
return (T)proxy;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.repo.policy.Behaviour#disable()
|
||||
*/
|
||||
public void disable()
|
||||
{
|
||||
Stack<Integer> stack = disabled.get();
|
||||
stack.push(hashCode());
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.repo.policy.Behaviour#enable()
|
||||
*/
|
||||
public void enable()
|
||||
{
|
||||
Stack<Integer> stack = disabled.get();
|
||||
if (stack.peek().equals(hashCode()) == false)
|
||||
{
|
||||
throw new PolicyException("Cannot enable " + this.toString() + " at this time - mismatched with disable calls");
|
||||
}
|
||||
stack.pop();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.repo.policy.Behaviour#isEnabled()
|
||||
*/
|
||||
public boolean isEnabled()
|
||||
{
|
||||
Stack<Integer> stack = disabled.get();
|
||||
return stack.search(hashCode()) == -1;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.alfresco.repo.policy.Behaviour#getNotificationFrequency()
|
||||
*/
|
||||
public NotificationFrequency getNotificationFrequency()
|
||||
{
|
||||
return frequency;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "Java method[class=" + instance.getClass().getName() + ", method=" + method + "]";
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public synchronized <T> T getInterface(Class<T> policy)
|
||||
{
|
||||
ParameterCheck.mandatory("Policy class", policy);
|
||||
Object proxy = proxies.get(policy);
|
||||
if (proxy == null)
|
||||
{
|
||||
InvocationHandler handler = getInvocationHandler(instance, method, policy);
|
||||
proxy = Proxy.newProxyInstance(policy.getClassLoader(), new Class[]{policy}, handler);
|
||||
proxies.put(policy, proxy);
|
||||
}
|
||||
return (T)proxy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the Invocation Handler.
|
||||
@@ -153,7 +97,7 @@ public class JavaBehaviour implements Behaviour
|
||||
* @param policyIF the policy interface class
|
||||
* @return the invocation handler
|
||||
*/
|
||||
private <T> InvocationHandler getInvocationHandler(Object instance, String method, Class<T> policyIF)
|
||||
<T> InvocationHandler getInvocationHandler(Object instance, String method, Class<T> policyIF)
|
||||
{
|
||||
Method[] policyIFMethods = policyIF.getMethods();
|
||||
if (policyIFMethods.length != 1)
|
||||
@@ -171,23 +115,7 @@ public class JavaBehaviour implements Behaviour
|
||||
{
|
||||
throw new PolicyException("Method " + method + " not found or accessible on " + instance.getClass(), e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Stack specific Thread Local
|
||||
*
|
||||
* @author David Caruana
|
||||
*/
|
||||
private class StackThreadLocal extends ThreadLocal<Stack<Integer>>
|
||||
{
|
||||
@Override
|
||||
protected Stack<Integer> initialValue()
|
||||
{
|
||||
return new Stack<Integer>();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Java Method Invocation Handler
|
||||
|
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package org.alfresco.repo.policy.registration;
|
||||
|
||||
import org.alfresco.service.namespace.QName;
|
||||
|
||||
/**
|
||||
* Deals with the registration of an association policy
|
||||
*
|
||||
* @author Roy Wetherall
|
||||
*
|
||||
*/
|
||||
public class AssociationPolicyRegistration extends PolicyRegistration
|
||||
{
|
||||
/** The association type **/
|
||||
private QName associationType;
|
||||
|
||||
/**
|
||||
* Set the association type
|
||||
*
|
||||
* @param associationType the association type
|
||||
*/
|
||||
public void setAssociationType(String associationType)
|
||||
{
|
||||
this.associationType = QName.createQName(associationType);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.alfresco.repo.policy.registration.PolicyRegistration#register()
|
||||
*/
|
||||
@Override
|
||||
public void register()
|
||||
{
|
||||
// Register the association behaviour
|
||||
if (this.associationType == null)
|
||||
{
|
||||
this.policyComponent.bindAssociationBehaviour(this.policyName, this.className, this.behaviour);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.policyComponent.bindAssociationBehaviour(this.policyName, this.className, this.associationType, this.behaviour);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package org.alfresco.repo.policy.registration;
|
||||
|
||||
/**
|
||||
* Deal with the registration of a class policy
|
||||
*
|
||||
* @author Roy Wetherall
|
||||
*
|
||||
*/
|
||||
public class ClassPolicyRegistration extends PolicyRegistration
|
||||
{
|
||||
/**
|
||||
* @see org.alfresco.repo.policy.registration.PolicyRegistration#register()
|
||||
*/
|
||||
@Override
|
||||
public void register()
|
||||
{
|
||||
// Register the class behaviour
|
||||
this.policyComponent.bindClassBehaviour(this.policyName, this.className, this.behaviour);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package org.alfresco.repo.policy.registration;
|
||||
|
||||
import org.alfresco.repo.policy.Behaviour;
|
||||
import org.alfresco.repo.policy.PolicyComponent;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
|
||||
/**
|
||||
* Bean that can be configured in spring to register a policy bahaviour
|
||||
*
|
||||
* @author Roy Wetherall
|
||||
*/
|
||||
public abstract class PolicyRegistration
|
||||
{
|
||||
/** The policy componenet **/
|
||||
protected PolicyComponent policyComponent;
|
||||
|
||||
/** The policy name **/
|
||||
protected QName policyName;
|
||||
|
||||
/** The class name **/
|
||||
protected QName className;
|
||||
|
||||
/** The behaviour **/
|
||||
protected Behaviour behaviour;
|
||||
|
||||
/**
|
||||
* Set the policy component
|
||||
*
|
||||
* @param policyComponent the policy componenet
|
||||
*/
|
||||
public void setPolicyComponent(PolicyComponent policyComponent)
|
||||
{
|
||||
this.policyComponent = policyComponent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the policy name
|
||||
*
|
||||
* @param policyName the policy name
|
||||
*/
|
||||
public void setPolicyName(String policyName)
|
||||
{
|
||||
this.policyName = QName.createQName(policyName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the class name
|
||||
*
|
||||
* @param className the class name
|
||||
*/
|
||||
public void setClassName(String className)
|
||||
{
|
||||
this.className = QName.createQName(className);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the behaviour
|
||||
*
|
||||
* @param behaviour the behaviour
|
||||
*/
|
||||
public void setBehaviour(Behaviour behaviour)
|
||||
{
|
||||
this.behaviour = behaviour;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the behaviour with the policy component for the policy and type specified. Called
|
||||
* as the init method of the bean.
|
||||
*
|
||||
* TODO supoort service registration?
|
||||
*/
|
||||
public abstract void register();
|
||||
}
|
Reference in New Issue
Block a user