001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.monitoring.statistics.service;
016    
017    import com.liferay.portal.kernel.monitoring.MonitoringProcessor;
018    import com.liferay.portal.kernel.monitoring.RequestStatus;
019    import com.liferay.portal.kernel.monitoring.statistics.DataSampleThreadLocal;
020    import com.liferay.portal.kernel.util.AutoResetThreadLocal;
021    import com.liferay.portal.monitoring.jmx.MethodSignature;
022    import com.liferay.portal.spring.aop.ChainableMethodAdvice;
023    
024    import java.lang.reflect.Method;
025    
026    import java.util.HashSet;
027    import java.util.Set;
028    
029    import org.aopalliance.intercept.MethodInvocation;
030    
031    /**
032     * @author Michael C. Han
033     */
034    public class ServiceMonitorAdvice extends ChainableMethodAdvice {
035    
036            /**
037             * @deprecated As of 6.1.0
038             */
039            public static ServiceMonitorAdvice getInstance() {
040                    return new ServiceMonitorAdvice();
041            }
042    
043            public static boolean isActive() {
044                    return _active;
045            }
046    
047            public void addMonitoredClass(String className) {
048                    _monitoredClasses.add(className);
049            }
050    
051            public void addMonitoredMethod(
052                    String className, String methodName, String[] parameterTypes) {
053    
054                    MethodSignature methodSignature = new MethodSignature(
055                            className, methodName, parameterTypes);
056    
057                    _monitoredMethods.add(methodSignature);
058            }
059    
060            @Override
061            public void afterReturning(MethodInvocation methodInvocation, Object result)
062                    throws Throwable {
063    
064                    ServiceRequestDataSample serviceRequestDataSample =
065                            _serviceRequestDataSampleThreadLocal.get();
066    
067                    if (serviceRequestDataSample != null) {
068                            serviceRequestDataSample.capture(RequestStatus.SUCCESS);
069                    }
070            }
071    
072            @Override
073            public void afterThrowing(
074                            MethodInvocation methodInvocation, Throwable throwable)
075                    throws Throwable {
076    
077                    ServiceRequestDataSample serviceRequestDataSample =
078                            _serviceRequestDataSampleThreadLocal.get();
079    
080                    if (serviceRequestDataSample != null) {
081                            serviceRequestDataSample.capture(RequestStatus.ERROR);
082                    }
083            }
084    
085            @Override
086            public Object before(MethodInvocation methodInvocation) throws Throwable {
087                    if (!_active) {
088                            serviceBeanAopCacheManager.removeMethodInterceptor(
089                                    methodInvocation, this);
090    
091                            return null;
092                    }
093    
094                    Object thisObject = methodInvocation.getThis();
095    
096                    Class<?> clazz = thisObject.getClass();
097    
098                    Class<?>[] interfaces = clazz.getInterfaces();
099    
100                    for (int i = 0; i < interfaces.length; i++) {
101                            if (interfaces[i].isAssignableFrom(MonitoringProcessor.class)) {
102                                    return null;
103                            }
104                    }
105    
106                    if (!_permissiveMode && !isMonitored(methodInvocation)) {
107                            return null;
108                    }
109    
110                    ServiceRequestDataSample serviceRequestDataSample =
111                            new ServiceRequestDataSample(methodInvocation);
112    
113                    serviceRequestDataSample.prepare();
114    
115                    _serviceRequestDataSampleThreadLocal.set(serviceRequestDataSample);
116    
117                    DataSampleThreadLocal.initialize();
118    
119                    return null;
120            }
121    
122            @Override
123            public void duringFinally(MethodInvocation methodInvocation) {
124                    ServiceRequestDataSample serviceRequestDataSample =
125                            _serviceRequestDataSampleThreadLocal.get();
126    
127                    if (serviceRequestDataSample != null) {
128                            _serviceRequestDataSampleThreadLocal.remove();
129    
130                            DataSampleThreadLocal.addDataSample(serviceRequestDataSample);
131                    }
132            }
133    
134            public Set<String> getMonitoredClasses() {
135                    return _monitoredClasses;
136            }
137    
138            public Set<MethodSignature> getMonitoredMethods() {
139                    return _monitoredMethods;
140            }
141    
142            public boolean isPermissiveMode() {
143                    return _permissiveMode;
144            }
145    
146            public void setActive(boolean active) {
147                    if (active && !_active) {
148                            serviceBeanAopCacheManager.reset();
149                    }
150    
151                    _active = active;
152            }
153    
154            public void setMonitoredClasses(Set<String> monitoredClasses) {
155                    _monitoredClasses = monitoredClasses;
156            }
157    
158            public void setMonitoredMethods(Set<MethodSignature> monitoredMethods) {
159                    _monitoredMethods = monitoredMethods;
160            }
161    
162            /**
163             * @deprecated As of 6.2.0
164             */
165            public void setMonitoringDestinationName(String monitoringDestinationName) {
166            }
167    
168            public void setPermissiveMode(boolean permissiveMode) {
169                    _permissiveMode = permissiveMode;
170            }
171    
172            protected boolean isMonitored(MethodInvocation methodInvocation) {
173                    Method method = methodInvocation.getMethod();
174    
175                    Class<?> declaringClass = method.getDeclaringClass();
176    
177                    String className = declaringClass.getName();
178    
179                    if (_monitoredClasses.contains(className)) {
180                            return true;
181                    }
182    
183                    MethodSignature methodSignature = new MethodSignature(method);
184    
185                    if (_monitoredMethods.contains(methodSignature)) {
186                            return true;
187                    }
188    
189                    return false;
190            }
191    
192            private static boolean _active;
193            private static Set<String> _monitoredClasses = new HashSet<String>();
194            private static Set<MethodSignature> _monitoredMethods =
195                    new HashSet<MethodSignature>();
196            private static boolean _permissiveMode;
197            private static ThreadLocal<ServiceRequestDataSample>
198                    _serviceRequestDataSampleThreadLocal =
199                            new AutoResetThreadLocal<ServiceRequestDataSample>(
200                                    ServiceRequestDataSample.class +
201                                            "._serviceRequestDataSampleThreadLocal");
202    
203    }