1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ws.server.endpoint;
18
19 import java.lang.reflect.InvocationTargetException;
20 import java.lang.reflect.Method;
21
22 import org.springframework.beans.factory.BeanFactory;
23 import org.springframework.core.JdkVersion;
24 import org.springframework.util.Assert;
25
26
27
28
29
30
31
32
33
34 public final class MethodEndpoint {
35
36 private final Object bean;
37
38 private final Method method;
39
40 private final BeanFactory beanFactory;
41
42
43
44
45
46
47
48 public MethodEndpoint(Object bean, Method method) {
49 Assert.notNull(bean, "bean must not be null");
50 Assert.notNull(method, "method must not be null");
51 this.bean = bean;
52 this.method = method;
53 this.beanFactory = null;
54 }
55
56
57
58
59
60
61
62
63
64 public MethodEndpoint(Object bean, String methodName, Class[] parameterTypes) throws NoSuchMethodException {
65 Assert.notNull(bean, "bean must not be null");
66 Assert.notNull(methodName, "method must not be null");
67 this.bean = bean;
68 this.method = bean.getClass().getMethod(methodName, parameterTypes);
69 this.beanFactory = null;
70 }
71
72
73
74
75
76
77
78
79
80 public MethodEndpoint(String beanName, BeanFactory beanFactory, Method method) {
81 Assert.hasText(beanName, "'beanName' must not be null");
82 Assert.notNull(beanFactory, "'beanFactory' must not be null");
83 Assert.notNull(method, "'method' must not be null");
84 Assert.isTrue(beanFactory.containsBean(beanName),
85 "Bean factory [" + beanFactory + "] does not contain bean " + "with name [" + beanName + "]");
86 this.bean = beanName;
87 this.beanFactory = beanFactory;
88 this.method = method;
89 }
90
91
92 public Object getBean() {
93 return this.bean;
94 }
95
96
97 public Method getMethod() {
98 return this.method;
99 }
100
101
102
103
104
105
106
107
108 public Object invoke(Object[] args) throws Exception {
109 Object endpoint = bean;
110 if (endpoint instanceof String) {
111 String endpointName = (String) endpoint;
112 endpoint = beanFactory.getBean(endpointName);
113 }
114 try {
115 return this.method.invoke(endpoint, args);
116 }
117 catch (InvocationTargetException ex) {
118 handleInvocationTargetException(ex);
119 throw new IllegalStateException("Unexpected exception thrown by method - " +
120 ex.getTargetException().getClass().getName() + ": " + ex.getTargetException().getMessage());
121 }
122 }
123
124 private void handleInvocationTargetException(InvocationTargetException ex) throws Exception {
125 if (ex.getTargetException() instanceof RuntimeException) {
126 throw (RuntimeException) ex.getTargetException();
127 }
128 if (ex.getTargetException() instanceof Error) {
129 throw (Error) ex.getTargetException();
130 }
131 if (ex.getTargetException() instanceof Exception) {
132 throw (Exception) ex.getTargetException();
133 }
134
135 }
136
137 public boolean equals(Object o) {
138 if (this == o) {
139 return true;
140 }
141 if (o != null && o instanceof MethodEndpoint) {
142 MethodEndpoint other = (MethodEndpoint) o;
143 return this.bean.equals(other.bean) && this.method.equals(other.method);
144 }
145 return false;
146 }
147
148 public int hashCode() {
149 return 31 * this.bean.hashCode() + this.method.hashCode();
150 }
151
152 public String toString() {
153 if (JdkVersion.getMajorJavaVersion() <= JdkVersion.JAVA_14) {
154 return this.method.toString();
155 }
156 else {
157 return GenericToStringProvider.toString(method);
158 }
159 }
160
161
162 private static class GenericToStringProvider {
163
164 public static String toString(Method method) {
165 return method.toGenericString();
166 }
167 }
168 }