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                    return null;
118            }
119    
120            @Override
121            public void duringFinally(MethodInvocation methodInvocation) {
122                    ServiceRequestDataSample serviceRequestDataSample =
123                            _serviceRequestDataSampleThreadLocal.get();
124    
125                    if (serviceRequestDataSample != null) {
126                            _serviceRequestDataSampleThreadLocal.remove();
127    
128                            DataSampleThreadLocal.addDataSample(serviceRequestDataSample);
129                    }
130            }
131    
132            public Set<String> getMonitoredClasses() {
133                    return _monitoredClasses;
134            }
135    
136            public Set<MethodSignature> getMonitoredMethods() {
137                    return _monitoredMethods;
138            }
139    
140            public boolean isPermissiveMode() {
141                    return _permissiveMode;
142            }
143    
144            public void setActive(boolean active) {
145                    if (active && !_active) {
146                            serviceBeanAopCacheManager.reset();
147                    }
148    
149                    _active = active;
150            }
151    
152            public void setMonitoredClasses(Set<String> monitoredClasses) {
153                    _monitoredClasses = monitoredClasses;
154            }
155    
156            public void setMonitoredMethods(Set<MethodSignature> monitoredMethods) {
157                    _monitoredMethods = monitoredMethods;
158            }
159    
160            /**
161             * @deprecated As of 6.2.0
162             */
163            public void setMonitoringDestinationName(String monitoringDestinationName) {
164            }
165    
166            public void setPermissiveMode(boolean permissiveMode) {
167                    _permissiveMode = permissiveMode;
168            }
169    
170            protected boolean isMonitored(MethodInvocation methodInvocation) {
171                    Method method = methodInvocation.getMethod();
172    
173                    Class<?> declaringClass = method.getDeclaringClass();
174    
175                    String className = declaringClass.getName();
176    
177                    if (_monitoredClasses.contains(className)) {
178                            return true;
179                    }
180    
181                    MethodSignature methodSignature = new MethodSignature(method);
182    
183                    if (_monitoredMethods.contains(methodSignature)) {
184                            return true;
185                    }
186    
187                    return false;
188            }
189    
190            private static boolean _active;
191            private static Set<String> _monitoredClasses = new HashSet<String>();
192            private static Set<MethodSignature> _monitoredMethods =
193                    new HashSet<MethodSignature>();
194            private static boolean _permissiveMode;
195            private static ThreadLocal<ServiceRequestDataSample>
196                    _serviceRequestDataSampleThreadLocal =
197                            new AutoResetThreadLocal<ServiceRequestDataSample>(
198                                    ServiceRequestDataSample.class +
199                                            "._serviceRequestDataSampleThreadLocal");
200    
201    }