001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.monitoring.spring;
019    
020    import org.aopalliance.aop.Advice;
021    import org.apache.commons.monitoring.aop.DefaultMonitorNameExtractor;
022    import org.apache.commons.monitoring.aop.MonitorNameExtractor;
023    import org.springframework.aop.Advisor;
024    import org.springframework.aop.Pointcut;
025    import org.springframework.aop.PointcutAdvisor;
026    import org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator;
027    import org.springframework.aop.support.DefaultPointcutAdvisor;
028    
029    import java.util.ArrayList;
030    import java.util.List;
031    
032    /**
033     * Creates monitored proxies for beans that match a pointcut.
034     *
035     * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
036     */
037    public class PointcutMonitoringAutoProxyCreator extends AbstractAdvisorAutoProxyCreator {
038        private MonitorNameExtractor monitorNameExtractor = DefaultMonitorNameExtractor.INSTANCE;
039        private Pointcut pointcut;
040    
041        @Override
042        protected List<Advisor> findCandidateAdvisors() {
043            final AopaliancePerformanceInterceptor interceptor = new AopaliancePerformanceInterceptor();
044            interceptor.setMonitorNameExtractor(monitorNameExtractor);
045    
046            final PointcutAdvisor adivsor = createPointcutAdvisor(interceptor);
047    
048            final List<Advisor> adivisors = new ArrayList<Advisor>(1);
049            adivisors.add(adivsor);
050            return adivisors;
051        }
052    
053        protected PointcutAdvisor createPointcutAdvisor(Advice advice) {
054            return new DefaultPointcutAdvisor(pointcut, advice);
055        }
056    
057        public void setMonitorNameExtractor(final MonitorNameExtractor monitorNameExtractor) {
058            this.monitorNameExtractor = monitorNameExtractor;
059        }
060    
061        public void setPointcut(final Pointcut pointcut) {
062            this.pointcut = pointcut;
063        }
064    
065        public MonitorNameExtractor getMonitorNameExtractor() {
066            return monitorNameExtractor;
067        }
068    }