001    /**
002     * Copyright (c) 2000-2013 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.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    /**
028     * @author Shuyang Zhou
029     * @author Brian Wing Shun Chan
030     */
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    }