KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > wrappers > ForwardingWrapper


1 /*
2   Copyright (C) 2001 Renaud Pawlak <renaud@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.wrappers;
19
20 import org.aopalliance.intercept.ConstructorInvocation;
21 import org.aopalliance.intercept.MethodInvocation;
22 import org.objectweb.jac.core.AspectComponent;
23 import org.objectweb.jac.core.Interaction;
24 import org.objectweb.jac.core.Wrapper;
25
26 import java.lang.reflect.InvocationTargetException JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28
29 /**
30  * This wrapper forwards the method calls that arrive on the wrappee
31  * to another object. The wrapped methods should be supported both by
32  * the wrappee and the forwardee. Otherwise, an exception is raised.
33  *
34  * @author <a HREF="http://cedric.cnam.fr/~pawlak/index-english.html">Renaud Pawlak</a>
35  *
36  * @see ForwardingException */

37
38 public class ForwardingWrapper extends Wrapper {
39
40     /**
41      * Construct a new forwarding wrapper.
42      *
43      * @param forwardee the object that receives the forwarded calls */

44
45     public ForwardingWrapper(AspectComponent ac, Object JavaDoc forwardee) {
46         super(ac);
47         this.forwardee = forwardee;
48     }
49
50     /** The forwardee. */
51     protected Object JavaDoc forwardee;
52
53     /**
54      * The getter method for the forwardee.
55      *
56      * @return the object to which the calls are forwarded
57      *
58      * @see #forward(Interaction)
59      */

60
61     public Object JavaDoc getForwardee() {
62         return forwardee;
63     }
64
65     /**
66      * Forwards all the incoming calls to the forwardee.
67      *
68      * <p>The forwardee class must support the wrapped method
69      * prototype. Otherwise, an exception is raised.
70      *
71      * @return the result of the forwarded method call
72      */

73
74     public Object JavaDoc forward(Interaction interaction) throws ForwardingException {
75
76         Object JavaDoc ret = null;
77
78         // System.out.println( "Forwarding " + method() + " to " + forwardee );
79
Method JavaDoc[] methods = forwardee.getClass().getMethods();
80         boolean ok = false;
81         for (int i = 0; i < methods.length; i++) {
82             //System.out.println ( "---> trying " + methods[i].getName() + method_args );
83
if (methods[i].getName().equals(interaction.method)) {
84                 try {
85                     ok = false;
86                     //System.out.println ( "Try to call " + method_name );
87
ret = methods[i].invoke(forwardee, interaction.args);
88                     ok = true;
89                 } catch (IllegalArgumentException JavaDoc e) {
90                 } catch (InvocationTargetException JavaDoc e) {
91                     Throwable JavaDoc t = e.getTargetException();
92                     if (t instanceof RuntimeException JavaDoc) {
93                         throw (RuntimeException JavaDoc) t;
94                     }
95                     e.printStackTrace();
96                     System.exit(1);
97                 } catch (IllegalAccessException JavaDoc e) {
98                     e.printStackTrace();
99                     System.exit(1);
100                 }
101                 if (ok)
102                     break;
103             }
104         }
105         if (!ok) {
106             throw new ForwardingException(
107                 "The forwardee '"
108                     + forwardee
109                     + "' does not support '"
110                     + interaction.method
111                     + "' to forward from '"
112                     + interaction.wrappee
113                     + "'.");
114         }
115
116         return ret;
117     }
118
119     public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
120         return forward((Interaction) invocation);
121     }
122
123     public Object JavaDoc construct(ConstructorInvocation invocation)
124         throws Throwable JavaDoc {
125         throw new Exception JavaDoc("Wrapper "+this+" does not support construction interception.");
126     }
127 }
128
Popular Tags