View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.monitoring.spring;
19  
20  import org.aopalliance.aop.Advice;
21  import org.apache.commons.monitoring.aop.DefaultMonitorNameExtractor;
22  import org.apache.commons.monitoring.aop.MonitorNameExtractor;
23  import org.springframework.aop.Advisor;
24  import org.springframework.aop.Pointcut;
25  import org.springframework.aop.PointcutAdvisor;
26  import org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator;
27  import org.springframework.aop.support.DefaultPointcutAdvisor;
28  
29  import java.util.ArrayList;
30  import java.util.List;
31  
32  /**
33   * Creates monitored proxies for beans that match a pointcut.
34   *
35   * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
36   */
37  public class PointcutMonitoringAutoProxyCreator extends AbstractAdvisorAutoProxyCreator {
38      private MonitorNameExtractor monitorNameExtractor = DefaultMonitorNameExtractor.INSTANCE;
39      private Pointcut pointcut;
40  
41      @Override
42      protected List<Advisor> findCandidateAdvisors() {
43          final AopaliancePerformanceInterceptor interceptor = new AopaliancePerformanceInterceptor();
44          interceptor.setMonitorNameExtractor(monitorNameExtractor);
45  
46          final PointcutAdvisor adivsor = createPointcutAdvisor(interceptor);
47  
48          final List<Advisor> adivisors = new ArrayList<Advisor>(1);
49          adivisors.add(adivsor);
50          return adivisors;
51      }
52  
53      protected PointcutAdvisor createPointcutAdvisor(Advice advice) {
54          return new DefaultPointcutAdvisor(pointcut, advice);
55      }
56  
57      public void setMonitorNameExtractor(final MonitorNameExtractor monitorNameExtractor) {
58          this.monitorNameExtractor = monitorNameExtractor;
59      }
60  
61      public void setPointcut(final Pointcut pointcut) {
62          this.pointcut = pointcut;
63      }
64  
65      public MonitorNameExtractor getMonitorNameExtractor() {
66          return monitorNameExtractor;
67      }
68  }