001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.workflow;
016    
017    import com.liferay.portal.kernel.util.StringPool;
018    import com.liferay.portal.kernel.util.Time;
019    import com.liferay.portal.kernel.workflow.WorkflowDefinition;
020    import com.liferay.portal.kernel.workflow.WorkflowException;
021    import com.liferay.portal.service.LockLocalServiceUtil;
022    
023    import org.aspectj.lang.ProceedingJoinPoint;
024    
025    /**
026     * @author Shuyang Zhou
027     * @author Brian Wing Shun Chan
028     */
029    public class WorkflowLockingAdvice {
030    
031            public Object invoke(ProceedingJoinPoint proceedingJoinPoint)
032                    throws Throwable {
033    
034                    String methodName = proceedingJoinPoint.getSignature().getName();
035                    Object[] arguments = proceedingJoinPoint.getArgs();
036    
037                    if (methodName.equals(_START_WORKFLOW_INSTANCE_METHOD_NAME)) {
038                            String workflowDefinitionName = (String)arguments[3];
039                            Integer workflowDefinitionVersion = (Integer)arguments[4];
040    
041                            String className = WorkflowDefinition.class.getName();
042                            String key = _encodeKey(
043                                    workflowDefinitionName, workflowDefinitionVersion);
044    
045                            if (LockLocalServiceUtil.isLocked(className, key)) {
046                                    throw new WorkflowException(
047                                            "Workflow definition name " + workflowDefinitionName +
048                                                    " and version " + workflowDefinitionVersion +
049                                                            " is being undeployed");
050                            }
051    
052                            return proceedingJoinPoint.proceed();
053                    }
054                    else if (!methodName.equals(
055                                            _UNDEPLOY_WORKFLOW_DEFINITION_METHOD_NAME)) {
056    
057                            return proceedingJoinPoint.proceed();
058                    }
059    
060                    long userId = (Long)arguments[1];
061                    String name = (String)arguments[2];
062                    Integer version = (Integer)arguments[3];
063    
064                    String className = WorkflowDefinition.class.getName();
065                    String key = _encodeKey(name, version);
066    
067                    if (LockLocalServiceUtil.isLocked(className, key)) {
068                            throw new WorkflowException(
069                                    "Workflow definition name " + name + " and version " + version +
070                                            " is being undeployed");
071                    }
072    
073                    try {
074                            LockLocalServiceUtil.lock(
075                                    userId, className, key, String.valueOf(userId), false,
076                                    Time.HOUR);
077    
078                            return proceedingJoinPoint.proceed();
079                    }
080                    finally {
081                            LockLocalServiceUtil.unlock(className, key);
082                    }
083            }
084    
085            private String _encodeKey(String name, int version) {
086                    return name.concat(StringPool.POUND).concat(String.valueOf(version));
087            }
088    
089            private static final String _START_WORKFLOW_INSTANCE_METHOD_NAME =
090                    "startWorkflowInstance";
091    
092            private static final String _UNDEPLOY_WORKFLOW_DEFINITION_METHOD_NAME =
093                    "undeployWorkflowDefinition";
094    
095    }