1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.cluster;
16  
17  import com.liferay.portal.kernel.cluster.ClusterExecutorUtil;
18  import com.liferay.portal.kernel.cluster.ClusterRequest;
19  import com.liferay.portal.kernel.cluster.Clusterable;
20  import com.liferay.portal.kernel.util.MethodHandler;
21  import com.liferay.portal.kernel.util.MethodTargetClassKey;
22  import com.liferay.portal.spring.aop.AnnotationChainableMethodAdvice;
23  
24  import java.lang.annotation.Annotation;
25  import java.lang.reflect.Method;
26  
27  import org.aopalliance.intercept.MethodInvocation;
28  
29  /**
30   * <a href="ClusterableAdvice.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Shuyang Zhou
33   */
34  public class ClusterableAdvice
35      extends AnnotationChainableMethodAdvice<Clusterable> {
36  
37      public void afterReturning(MethodInvocation methodInvocation, Object result)
38          throws Throwable {
39  
40          if (!ClusterInvokeThreadLocal.isEnabled()) {
41              return;
42          }
43  
44          MethodTargetClassKey methodTargetClassKey = buildMethodTargetClassKey(
45              methodInvocation);
46  
47          Clusterable clusterable = findAnnotation(methodTargetClassKey);
48  
49          if (clusterable == _nullClusterable) {
50              return;
51          }
52  
53          Method method = methodTargetClassKey.getMethod();
54  
55          Method utilClassMethod = _getUtilClassMethod(method);
56  
57          MethodHandler methodHandler = new MethodHandler(
58              utilClassMethod, methodInvocation.getArguments());
59  
60          ClusterRequest clusterRequest = ClusterRequest.createMulticastRequest(
61              methodHandler, true);
62  
63          clusterRequest.setServletContextName(_servletContextName);
64  
65          ClusterExecutorUtil.execute(clusterRequest);
66      }
67  
68      public Clusterable getNullAnnotation() {
69          return _nullClusterable;
70      }
71  
72      public void setServletContextName(String servletContextName) {
73          _servletContextName = servletContextName;
74      }
75  
76      private Method _getUtilClassMethod(Method method) throws Exception {
77          Thread currentThread = Thread.currentThread();
78  
79          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
80  
81          Class<?> declaringClass = method.getDeclaringClass();
82  
83          String utilClassName = declaringClass.getName().concat("Util");
84  
85          Class<?> utilClass = contextClassLoader.loadClass(utilClassName);
86  
87          return utilClass.getMethod(
88              method.getName(), method.getParameterTypes());
89      }
90  
91      private static Clusterable _nullClusterable =
92          new Clusterable() {
93  
94              public Class<? extends Annotation> annotationType() {
95                  return Clusterable.class;
96              }
97  
98          };
99  
100     private String _servletContextName;
101 
102 }