View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  
18  package ch.elca.el4j.tests.util.metadata.annotations;
19  
20  import java.lang.annotation.Annotation;
21  import java.lang.reflect.Method;
22  import java.util.Collection;
23  
24  import org.aopalliance.intercept.MethodInterceptor;
25  import org.aopalliance.intercept.MethodInvocation;
26  import org.springframework.aop.support.AopUtils;
27  import org.springframework.util.Assert;
28  
29  import ch.elca.el4j.services.monitoring.notification.CoreNotificationHelper;
30  import ch.elca.el4j.util.metadata.GenericMetaDataSource;
31  import ch.elca.el4j.util.metadata.MetaDataSourceAware;
32  
33  /**
34   * The interceptor to be invoked if an ExampleAnnotation is set.
35   *
36   * @svnLink $Revision: 3874 $;$Date: 2009-08-04 14:25:40 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/core/src/test/java/ch/elca/el4j/tests/util/metadata/annotations/ExampleInterceptor.java $
37   *
38   * @author Martin Zeltner (MZE)
39   */
40  public class ExampleInterceptor
41  	implements MethodInterceptor, MetaDataSourceAware {
42  
43  	/**
44  	 * The annotation source.
45  	 */
46  	private GenericMetaDataSource m_metaDataSource;
47  
48  	/**
49  	 * {@inheritDoc}
50  	 */
51  	@SuppressWarnings("unchecked")
52  	public Object invoke(MethodInvocation methodInvocation) throws Throwable {
53  		Method invokedMethod = methodInvocation.getMethod();
54  		Class targetClass = null;
55  		if (!AopUtils.isAopProxy(methodInvocation.getThis())) {
56  			targetClass = methodInvocation.getThis().getClass();
57  		}
58  		
59  		Collection<Annotation> c = m_metaDataSource.getMetaData(
60  			invokedMethod, targetClass);
61  
62  		Assert.isTrue(c != null && !c.isEmpty());
63  		Annotation annotation = c.iterator().next();
64  		int factor = -1;
65  		if (annotation instanceof ExampleAnnotationOne) {
66  			factor = ((ExampleAnnotationOne) annotation).factor();
67  		} else if (annotation instanceof ExampleAnnotationTwo) {
68  			ExampleAnnotationTwo eat = (ExampleAnnotationTwo) annotation;
69  			factor = eat.factor() * ExampleAnnotationTwo.CONSTANT_FACTOR;
70  		} else {
71  			CoreNotificationHelper.notifyMisconfiguration(
72  				"There is no annotated annotation of type "
73  				+ "ExampleAnnotation declared at method "
74  				+ methodInvocation.getMethod());
75  		}
76  
77  		Object[] param = methodInvocation.getArguments();
78  		param[0] = Integer.valueOf(factor);
79  
80  		// This is an around advice.
81  		// Invoke the next interceptor in the chain.
82  		// This will normally result in a target object being invoked.
83  		return methodInvocation.proceed();
84  	}
85  
86  	/**
87  	 * {@inheritDoc}
88  	 */
89  	public void setMetaDataSource(GenericMetaDataSource metaDataSource) {
90  		m_metaDataSource = metaDataSource;
91  	}
92  }