KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > kohsuke > stapler > Function


1 package org.kohsuke.stapler;
2
3 import javax.servlet.http.HttpServletRequest JavaDoc;
4 import java.lang.reflect.InvocationTargetException JavaDoc;
5 import java.lang.reflect.Method JavaDoc;
6
7 /**
8  * Abstracts the difference between normal instance methods and
9  * static duck-typed methods.
10  *
11  * @author Kohsuke Kawaguchi
12  */

13 abstract class Function {
14     abstract String JavaDoc getName();
15     abstract Class JavaDoc[] getParameterTypes();
16     abstract Object JavaDoc invoke(HttpServletRequest JavaDoc req, Object JavaDoc o, Object JavaDoc... args) throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc;
17
18     final Function protectBy(Method JavaDoc m) {
19         try {
20             LimitedTo a = m.getAnnotation(LimitedTo.class);
21             if(a==null)
22                 return this; // not protected
23
else
24                 return new ProtectedFunction(this,a.value());
25         } catch (LinkageError JavaDoc e) {
26             // running in JDK 1.4
27
return this;
28         }
29     }
30
31     /**
32      * Normal instance methods.
33      */

34     static final class InstanceFunction extends Function {
35         private final Method JavaDoc m;
36
37         public InstanceFunction(Method JavaDoc m) {
38             this.m = m;
39         }
40
41         public String JavaDoc getName() {
42             return m.getName();
43         }
44
45         public Class JavaDoc[] getParameterTypes() {
46             return m.getParameterTypes();
47         }
48
49         public Object JavaDoc invoke(HttpServletRequest JavaDoc req, Object JavaDoc o, Object JavaDoc... args) throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
50             return m.invoke(o,args);
51         }
52     }
53
54     /**
55      * Static methods on the wrapper type.
56      */

57     static final class StaticFunction extends Function {
58         private final Method JavaDoc m;
59
60         public StaticFunction(Method JavaDoc m) {
61             this.m = m;
62         }
63
64         public String JavaDoc getName() {
65             return m.getName();
66         }
67
68         public Class JavaDoc[] getParameterTypes() {
69             Class JavaDoc[] p = m.getParameterTypes();
70             Class JavaDoc[] r = new Class JavaDoc[p.length-1];
71             System.arraycopy(p,1,r,0,r.length);
72             return r;
73         }
74
75         public Object JavaDoc invoke(HttpServletRequest JavaDoc req, Object JavaDoc o, Object JavaDoc... args) throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
76             Object JavaDoc[] r = new Object JavaDoc[args.length+1];
77             r[0] = o;
78             System.arraycopy(args,0,r,1,args.length);
79             return m.invoke(null,r);
80         }
81     }
82
83     /**
84      * Function that's protected by
85      */

86     static final class ProtectedFunction extends Function {
87         private final String JavaDoc role;
88         private final Function core;
89
90         public ProtectedFunction(Function core, String JavaDoc role) {
91             this.role = role;
92             this.core = core;
93         }
94
95         public String JavaDoc getName() {
96             return core.getName();
97         }
98
99         public Class JavaDoc[] getParameterTypes() {
100             return core.getParameterTypes();
101         }
102
103         public Object JavaDoc invoke(HttpServletRequest JavaDoc req, Object JavaDoc o, Object JavaDoc... args) throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
104             if(req.isUserInRole(role))
105                 return core.invoke(req, o, args);
106             else
107                 throw new IllegalAccessException JavaDoc("Needs to be in role "+role);
108         }
109     }
110 }
111
Popular Tags