1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.workflow;
16  
17  import com.liferay.portal.kernel.util.StringPool;
18  import com.liferay.portal.kernel.util.Time;
19  import com.liferay.portal.kernel.workflow.WorkflowDefinition;
20  import com.liferay.portal.kernel.workflow.WorkflowException;
21  import com.liferay.portal.service.LockLocalServiceUtil;
22  
23  import org.aspectj.lang.ProceedingJoinPoint;
24  
25  /**
26   * <a href="WorkflowLockingAdvice.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Shuyang Zhou
29   * @author Brian Wing Shun Chan
30   */
31  public class WorkflowLockingAdvice {
32  
33      public Object invoke(ProceedingJoinPoint proceedingJoinPoint)
34          throws Throwable {
35  
36          String methodName = proceedingJoinPoint.getSignature().getName();
37          Object[] arguments = proceedingJoinPoint.getArgs();
38  
39          if (methodName.equals(_START_WORKFLOW_INSTANCE_METHOD_NAME)) {
40              String workflowDefinitionName = (String)arguments[2];
41              Integer workflowDefinitionVersion = (Integer)arguments[3];
42  
43              String className = WorkflowDefinition.class.getName();
44              String key = _encodeKey(
45                  workflowDefinitionName, workflowDefinitionVersion);
46  
47              if (LockLocalServiceUtil.isLocked(className, key)) {
48                  throw new WorkflowException(
49                      "Workflow definition name " + workflowDefinitionName +
50                          " and version " + workflowDefinitionVersion +
51                              " is being undeployed");
52              }
53  
54              return proceedingJoinPoint.proceed();
55          }
56          else if (!methodName.equals(
57                      _UNDEPLOY_WORKFLOW_DEFINITION_METHOD_NAME)) {
58  
59              return proceedingJoinPoint.proceed();
60          }
61  
62          long userId = (Long)arguments[1];
63          String name = (String)arguments[2];
64          Integer version = (Integer)arguments[3];
65  
66          String className = WorkflowDefinition.class.getName();
67          String key = _encodeKey(name, version);
68  
69          if (LockLocalServiceUtil.isLocked(className, key)) {
70              throw new WorkflowException(
71                  "Workflow definition name " + name + " and version " + version +
72                      " is being undeployed");
73          }
74  
75          try {
76              LockLocalServiceUtil.lock(
77                  userId, className, key, String.valueOf(userId), false,
78                  Time.HOUR);
79  
80              return proceedingJoinPoint.proceed();
81          }
82          finally {
83              LockLocalServiceUtil.unlock(className, key);
84          }
85      }
86  
87      private String _encodeKey(String name, int version) {
88          return name.concat(StringPool.POUND).concat(String.valueOf(version));
89      }
90  
91      private static final String _START_WORKFLOW_INSTANCE_METHOD_NAME =
92          "startWorkflowInstance";
93  
94      private static final String _UNDEPLOY_WORKFLOW_DEFINITION_METHOD_NAME =
95          "undeployWorkflowDefinition";
96  
97  }