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.services.remoting;
19  
20  import java.util.Map;
21  
22  import ch.elca.el4j.util.interfaceenrichment.EnrichmentDecorator;
23  import ch.elca.el4j.util.interfaceenrichment.InterfaceEnricher;
24  import ch.elca.el4j.util.interfaceenrichment.MethodDescriptor;
25  
26  
27  /**
28   * This interface decorator adds a map with assembled implicit context to each
29   * method of the given interface.
30   *
31   * @svnLink $Revision: 3873 $;$Date: 2009-08-04 13:59:45 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/remoting_core/src/main/java/ch/elca/el4j/services/remoting/ContextEnrichmentDecorator.java $
32   *
33   * @author Martin Zeltner (MZE)
34   */
35  public class ContextEnrichmentDecorator implements EnrichmentDecorator {
36  	/**
37  	 * Parameter type which has to added.
38  	 */
39  	private static final Class CONTEXT_CLASS = Map.class;
40  	
41  	/**
42  	 * This is the name of the context parameter.
43  	 */
44  	private static final String CONTEXT_PARAMETER_NAME = "contextMap";
45  
46  	/**
47  	 * {@inheritDoc}
48  	 */
49  	public String changedInterfaceName(String originalInterfaceName) {
50  		return originalInterfaceName + "WithContext";
51  	}
52  
53  	/**
54  	 * {@inheritDoc}
55  	 */
56  	public Class[] changedExtendedInterface(Class[] extendedInterfaces) {
57  		InterfaceEnricher interfaceIndirector = new InterfaceEnricher();
58  		ClassLoader cl = Thread.currentThread().getContextClassLoader();
59  		
60  		Class[] changedInterfaces = new Class[extendedInterfaces.length];
61  		for (int i = 0; i < extendedInterfaces.length; i++) {
62  			changedInterfaces[i] = interfaceIndirector.createShadowInterfaceAndLoadItDirectly(
63  				extendedInterfaces[i], this, cl);
64  		}
65  		
66  		return changedInterfaces;
67  	}
68  
69  	/**
70  	 * {@inheritDoc}
71  	 */
72  	public MethodDescriptor changedMethodSignature(MethodDescriptor method) {
73  		Class[] methodParameterTypes = method.getParameterTypes();
74  		Class[] methodParameterTypesWithContext = null;
75  		if (methodParameterTypes == null || methodParameterTypes.length == 0) {
76  			methodParameterTypesWithContext = new Class[] {CONTEXT_CLASS};
77  			
78  		} else {
79  			methodParameterTypesWithContext
80  				= new Class[methodParameterTypes.length + 1];
81  			for (int i = 0; i < methodParameterTypes.length; i++) {
82  				methodParameterTypesWithContext[i] = methodParameterTypes[i];
83  			}
84  			methodParameterTypesWithContext[
85  				methodParameterTypesWithContext.length - 1] = CONTEXT_CLASS;
86  		}
87  		method.setParameterTypes(methodParameterTypesWithContext);
88  		
89  		String[] methodParameterNames = method.getParameterNames();
90  		/**
91  		 * If the original method no method or has at minimum one parameter and
92  		 * the parameter names are given from the original method, than add the
93  		 * new parameter name.
94  		 */
95  		if ((methodParameterNames != null
96  			&& methodParameterNames.length == methodParameterTypes.length)
97  			|| methodParameterTypesWithContext.length == 1) {
98  			String[] methodParameterNamesWithContext = null;
99  			if (methodParameterTypesWithContext.length == 1) {
100 				methodParameterNamesWithContext
101 					= new String[] {CONTEXT_PARAMETER_NAME};
102 			} else {
103 				methodParameterNamesWithContext
104 					= new String[methodParameterNames.length + 1];
105 				for (int i = 0; i < methodParameterNames.length; i++) {
106 					methodParameterNamesWithContext[i]
107 						= methodParameterNames[i];
108 				}
109 				methodParameterNamesWithContext[
110 					methodParameterNamesWithContext.length - 1]
111 					= CONTEXT_PARAMETER_NAME;
112 			}
113 			method.setParameterNames(methodParameterNamesWithContext);
114 		}
115 		
116 		return method;
117 	}
118 }