| ServiceHookAdvice.java |
1 /**
2 * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 package com.liferay.portal.spring.aop;
24
25 import com.liferay.portal.kernel.log.Log;
26 import com.liferay.portal.kernel.log.LogFactoryUtil;
27 import com.liferay.portal.kernel.util.InitialThreadLocal;
28
29 import java.lang.reflect.InvocationTargetException;
30 import java.lang.reflect.Method;
31
32 import java.util.Map;
33 import java.util.concurrent.ConcurrentHashMap;
34
35 import org.aspectj.lang.ProceedingJoinPoint;
36 import org.aspectj.lang.reflect.MethodSignature;
37
38 /**
39 * <a href="ServiceHookAdvice.java.html"><b><i>View Source</i></b></a>
40 *
41 * @author Brian Wing Shun Chan
42 */
43 public class ServiceHookAdvice {
44
45 public static Object getService(String className) {
46 return _services.get(className);
47 }
48
49 public static void setService(String className, Object service) {
50 if (_log.isDebugEnabled()) {
51 if (service == null) {
52 _log.debug("Remove service hook " + className);
53 }
54 else {
55 _log.debug(
56 "Add service hook " + className + " " +
57 service.getClass().getName());
58 }
59 }
60
61 if (service == null) {
62 _services.remove(className);
63 }
64 else {
65 _services.put(className, service);
66 }
67 }
68
69 public Object invoke(ProceedingJoinPoint proceedingJoinPoint)
70 throws Throwable {
71
72 if (_immediatelyProceed.get()) {
73
74 // If the thread local variable to immedately proceed is set to
75 // true, then immediately proceed to prevent an infinite loop
76
77 _immediatelyProceed.set(Boolean.FALSE);
78
79 return proceedingJoinPoint.proceed();
80 }
81
82 MethodSignature methodSignature =
83 (MethodSignature)proceedingJoinPoint.getSignature();
84
85 String className = methodSignature.getDeclaringTypeName();
86
87 Object service = _services.get(className);
88
89 if (service == null) {
90 return proceedingJoinPoint.proceed();
91 }
92
93 _immediatelyProceed.set(Boolean.TRUE);
94
95 try {
96 Method method = methodSignature.getMethod();
97
98 return method.invoke(service, proceedingJoinPoint.getArgs());
99 }
100 catch (InvocationTargetException ite) {
101 throw ite.getTargetException();
102 }
103 finally {
104 _immediatelyProceed.remove();
105 }
106 }
107
108 private static Log _log = LogFactoryUtil.getLog(ServiceHookAdvice.class);
109
110 private static ThreadLocal<Boolean> _immediatelyProceed =
111 new InitialThreadLocal<Boolean>(Boolean.FALSE);
112 private static Map<String, Object> _services =
113 new ConcurrentHashMap<String, Object>();
114
115 }