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.rmi.Remote;
21  import java.rmi.RemoteException;
22  
23  import ch.elca.el4j.util.interfaceenrichment.EnrichmentDecorator;
24  import ch.elca.el4j.util.interfaceenrichment.MethodDescriptor;
25  
26  
27  /**
28   * This interface decorator adds to a given interface rmi needs.
29   *
30   * @svnLink $Revision: 3883 $;$Date: 2009-08-04 15:35:01 +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/RmiEnrichmentDecorator.java $
31   *
32   * @author Martin Zeltner (MZE)
33   */
34  public class RmiEnrichmentDecorator implements EnrichmentDecorator {
35  	/**
36  	 * Interface which the given interface has to extend.
37  	 */
38  	private static final Class REMOTE_CLASS = Remote.class;
39  	
40  	/**
41  	 * Exception which every method must be able to throw.
42  	 */
43  	private static final Class REMOTE_EXCEPTION_CLASS = RemoteException.class;
44  	
45  
46  	/**
47  	 * {@inheritDoc}
48  	 */
49  	public String changedInterfaceName(String originalInterfaceName) {
50  		return originalInterfaceName + "Rmi";
51  	}
52  
53  	/**
54  	 * {@inheritDoc}
55  	 */
56  	public Class[] changedExtendedInterface(Class[] extendedInterfaces) {
57  		Class[] extendedInterfacesNew;
58  		if (extendedInterfaces == null || extendedInterfaces.length == 0) {
59  			extendedInterfacesNew = new Class[1];
60  			extendedInterfacesNew[0] = REMOTE_CLASS;
61  		} else {
62  			extendedInterfacesNew = new Class[extendedInterfaces.length + 1];
63  			for (int i = 0; i < extendedInterfaces.length; i++) {
64  				if (REMOTE_CLASS.isAssignableFrom(extendedInterfaces[i])) {
65  					/**
66  					 * If the needed interface does already exists, return.
67  					 */
68  					return extendedInterfaces;
69  				}
70  				extendedInterfacesNew[i] = extendedInterfaces[i];
71  			}
72  			extendedInterfacesNew[extendedInterfacesNew.length - 1]
73  				= REMOTE_CLASS;
74  		}
75  		return extendedInterfacesNew;
76  	}
77  
78  	/**
79  	 * {@inheritDoc}
80  	 */
81  	public MethodDescriptor changedMethodSignature(MethodDescriptor method) {
82  		Class[] thrownExceptions = method.getThrownExceptions();
83  		
84  		Class[] thrownExceptionsNew;
85  		if (thrownExceptions == null || thrownExceptions.length == 0) {
86  			thrownExceptionsNew = new Class[1];
87  			thrownExceptionsNew[0] = REMOTE_EXCEPTION_CLASS;
88  		} else {
89  			thrownExceptionsNew = new Class[thrownExceptions.length + 1];
90  			for (int i = 0; i < thrownExceptions.length; i++) {
91  				if (thrownExceptions[i].isAssignableFrom(
92  					REMOTE_EXCEPTION_CLASS)) {
93  					/**
94  					 * If the needed exception does already exists, return.
95  					 */
96  					return method;
97  				}
98  				thrownExceptionsNew[i] = thrownExceptions[i];
99  			}
100 			thrownExceptionsNew[thrownExceptionsNew.length - 1]
101 				= REMOTE_EXCEPTION_CLASS;
102 		}
103 		
104 		method.setThrownExceptions(thrownExceptionsNew);
105 		return method;
106 	}
107 }