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.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   * @deprecated
35   */
36  public class ServiceHookAdvice {
37  
38      public static Object getService(String className) {
39          return _services.get(className);
40      }
41  
42      public static void setService(String className, Object service) {
43          if (_log.isDebugEnabled()) {
44              if (service == null) {
45                  _log.debug("Remove service hook " + className);
46              }
47              else {
48                  _log.debug(
49                      "Add service hook " + className + " " +
50                          service.getClass().getName());
51              }
52          }
53  
54          if (service == null) {
55              _services.remove(className);
56          }
57          else {
58              _services.put(className, service);
59          }
60      }
61  
62      public Object invoke(ProceedingJoinPoint proceedingJoinPoint)
63          throws Throwable {
64  
65          if (_immediatelyProceed.get()) {
66  
67              // If the thread local variable to immedately proceed is set to
68              // true, then immediately proceed to prevent an infinite loop
69  
70              _immediatelyProceed.set(Boolean.FALSE);
71  
72              return proceedingJoinPoint.proceed();
73          }
74  
75          MethodSignature methodSignature =
76              (MethodSignature)proceedingJoinPoint.getSignature();
77  
78          String className = methodSignature.getDeclaringTypeName();
79  
80          Object service = _services.get(className);
81  
82          if (service == null) {
83              return proceedingJoinPoint.proceed();
84          }
85  
86          _immediatelyProceed.set(Boolean.TRUE);
87  
88          try {
89              Method method = methodSignature.getMethod();
90  
91              return method.invoke(service, proceedingJoinPoint.getArgs());
92          }
93          catch (InvocationTargetException ite) {
94              throw ite.getTargetException();
95          }
96          finally {
97              _immediatelyProceed.remove();
98          }
99      }
100 
101     private static Log _log = LogFactoryUtil.getLog(ServiceHookAdvice.class);
102 
103     private static ThreadLocal<Boolean> _immediatelyProceed =
104         new InitialThreadLocal<Boolean>(false);
105     private static Map<String, Object> _services =
106         new ConcurrentHashMap<String, Object>();
107 
108 }