001
014
015 package com.liferay.portal.workflow;
016
017 import com.liferay.portal.kernel.util.StringPool;
018 import com.liferay.portal.kernel.util.StringUtil;
019 import com.liferay.portal.kernel.util.Time;
020 import com.liferay.portal.kernel.workflow.WorkflowDefinition;
021 import com.liferay.portal.kernel.workflow.WorkflowException;
022 import com.liferay.portal.service.LockLocalServiceUtil;
023
024 import org.aspectj.lang.ProceedingJoinPoint;
025 import org.aspectj.lang.Signature;
026
027
031 public class WorkflowLockingAdvice {
032
033 public Object invoke(ProceedingJoinPoint proceedingJoinPoint)
034 throws Throwable {
035
036 Signature signature = proceedingJoinPoint.getSignature();
037
038 String methodName = signature.getName();
039
040 Object[] arguments = proceedingJoinPoint.getArgs();
041
042 if (methodName.equals(_START_WORKFLOW_INSTANCE_METHOD_NAME)) {
043 String workflowDefinitionName = (String)arguments[3];
044 Integer workflowDefinitionVersion = (Integer)arguments[4];
045
046 String className = WorkflowDefinition.class.getName();
047 String key = _encodeKey(
048 workflowDefinitionName, workflowDefinitionVersion);
049
050 if (LockLocalServiceUtil.isLocked(className, key)) {
051 throw new WorkflowException(
052 "Workflow definition name " + workflowDefinitionName +
053 " and version " + workflowDefinitionVersion +
054 " is being undeployed");
055 }
056
057 return proceedingJoinPoint.proceed();
058 }
059 else if (!methodName.equals(
060 _UNDEPLOY_WORKFLOW_DEFINITION_METHOD_NAME)) {
061
062 return proceedingJoinPoint.proceed();
063 }
064
065 long userId = (Long)arguments[1];
066 String name = (String)arguments[2];
067 Integer version = (Integer)arguments[3];
068
069 String className = WorkflowDefinition.class.getName();
070 String key = _encodeKey(name, version);
071
072 if (LockLocalServiceUtil.isLocked(className, key)) {
073 throw new WorkflowException(
074 "Workflow definition name " + name + " and version " + version +
075 " is being undeployed");
076 }
077
078 try {
079 LockLocalServiceUtil.lock(
080 userId, className, key, String.valueOf(userId), false,
081 Time.HOUR);
082
083 return proceedingJoinPoint.proceed();
084 }
085 finally {
086 LockLocalServiceUtil.unlock(className, key);
087 }
088 }
089
090 private String _encodeKey(String name, int version) {
091 return name.concat(StringPool.POUND).concat(
092 StringUtil.toHexString(version));
093 }
094
095 private static final String _START_WORKFLOW_INSTANCE_METHOD_NAME =
096 "startWorkflowInstance";
097
098 private static final String _UNDEPLOY_WORKFLOW_DEFINITION_METHOD_NAME =
099 "undeployWorkflowDefinition";
100
101 }