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.spring.aop;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.InitialThreadLocal;
20  
21  import java.lang.reflect.InvocationTargetException;
22  import java.lang.reflect.Method;
23  
24  import java.util.Map;
25  import java.util.concurrent.ConcurrentHashMap;
26  
27  import org.aspectj.lang.ProceedingJoinPoint;
28  import org.aspectj.lang.reflect.MethodSignature;
29  
30  /**
31   * <a href="ServiceHookAdvice.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   */
35  public class ServiceHookAdvice {
36  
37      public static Object getService(String className) {
38          return _services.get(className);
39      }
40  
41      public static void setService(String className, Object service) {
42          if (_log.isDebugEnabled()) {
43              if (service == null) {
44                  _log.debug("Remove service hook " + className);
45              }
46              else {
47                  _log.debug(
48                      "Add service hook " + className + " " +
49                          service.getClass().getName());
50              }
51          }
52  
53          if (service == null) {
54              _services.remove(className);
55          }
56          else {
57              _services.put(className, service);
58          }
59      }
60  
61      public Object invoke(ProceedingJoinPoint proceedingJoinPoint)
62          throws Throwable {
63  
64          if (_immediatelyProceed.get()) {
65  
66              // If the thread local variable to immedately proceed is set to
67              // true, then immediately proceed to prevent an infinite loop
68  
69              _immediatelyProceed.set(Boolean.FALSE);
70  
71              return proceedingJoinPoint.proceed();
72          }
73  
74          MethodSignature methodSignature =
75              (MethodSignature)proceedingJoinPoint.getSignature();
76  
77          String className = methodSignature.getDeclaringTypeName();
78  
79          Object service = _services.get(className);
80  
81          if (service == null) {
82              return proceedingJoinPoint.proceed();
83          }
84  
85          _immediatelyProceed.set(Boolean.TRUE);
86  
87          try {
88              Method method = methodSignature.getMethod();
89  
90              return method.invoke(service, proceedingJoinPoint.getArgs());
91          }
92          catch (InvocationTargetException ite) {
93              throw ite.getTargetException();
94          }
95          finally {
96              _immediatelyProceed.remove();
97          }
98      }
99  
100     private static Log _log = LogFactoryUtil.getLog(ServiceHookAdvice.class);
101 
102     private static ThreadLocal<Boolean> _immediatelyProceed =
103         new InitialThreadLocal<Boolean>(false);
104     private static Map<String, Object> _services =
105         new ConcurrentHashMap<String, Object>();
106 
107 }