1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ws.server.endpoint.mapping;
18
19 import java.lang.reflect.Method;
20 import java.util.HashMap;
21 import java.util.Map;
22
23 import org.springframework.aop.support.AopUtils;
24 import org.springframework.beans.BeansException;
25 import org.springframework.context.ApplicationContextException;
26 import org.springframework.util.Assert;
27 import org.springframework.util.ClassUtils;
28 import org.springframework.util.ReflectionUtils;
29 import org.springframework.util.StringUtils;
30 import org.springframework.ws.context.MessageContext;
31 import org.springframework.ws.server.endpoint.MethodEndpoint;
32
33
34
35
36
37
38
39
40
41
42
43 public abstract class AbstractMethodEndpointMapping extends AbstractEndpointMapping {
44
45
46 private final Map endpointMap = new HashMap();
47
48
49
50
51
52
53
54
55 protected Object getEndpointInternal(MessageContext messageContext) throws Exception {
56 String key = getLookupKeyForMessage(messageContext);
57 if (!StringUtils.hasLength(key)) {
58 return null;
59 }
60 if (logger.isDebugEnabled()) {
61 logger.debug("Looking up endpoint for [" + key + "]");
62 }
63 return lookupEndpoint(key);
64 }
65
66
67
68
69
70
71 protected abstract String getLookupKeyForMessage(MessageContext messageContext) throws Exception;
72
73
74
75
76
77
78
79 protected MethodEndpoint lookupEndpoint(String key) {
80 return (MethodEndpoint) endpointMap.get(key);
81 }
82
83
84
85
86
87
88
89
90 protected void registerEndpoint(String key, MethodEndpoint endpoint) throws BeansException {
91 Object mappedEndpoint = endpointMap.get(key);
92 if (mappedEndpoint != null) {
93 throw new ApplicationContextException("Cannot map endpoint [" + endpoint + "] on registration key [" + key +
94 "]: there's already endpoint [" + mappedEndpoint + "] mapped");
95 }
96 if (endpoint == null) {
97 throw new ApplicationContextException("Could not find endpoint for key [" + key + "]");
98 }
99 endpointMap.put(key, endpoint);
100 if (logger.isDebugEnabled()) {
101 logger.debug("Mapped key [" + key + "] onto endpoint [" + endpoint + "]");
102 }
103 }
104
105
106
107
108
109
110
111
112 protected void registerMethods(final Object endpoint) {
113 Assert.notNull(endpoint, "'endpoint' must not be null");
114 Class endpointClass = getEndpointClass(endpoint);
115 ReflectionUtils.doWithMethods(endpointClass, new ReflectionUtils.MethodCallback() {
116
117 public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
118 String key = getLookupKeyForMethod(method);
119 if (StringUtils.hasLength(key)) {
120 registerEndpoint(key, new MethodEndpoint(endpoint, method));
121 }
122 }
123 });
124 }
125
126
127
128
129
130
131
132
133 protected void registerMethods(final String beanName) {
134 Assert.hasText(beanName, "'beanName' must not be empty");
135 Class endpointClass = getApplicationContext().getType(beanName);
136 ReflectionUtils.doWithMethods(endpointClass, new ReflectionUtils.MethodCallback() {
137
138 public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
139 String key = getLookupKeyForMethod(method);
140 if (StringUtils.hasLength(key)) {
141 registerEndpoint(key, new MethodEndpoint(beanName, getApplicationContext(), method));
142 }
143 }
144
145 });
146 }
147
148
149
150
151
152
153
154
155 protected String getLookupKeyForMethod(Method method) {
156 return null;
157 }
158
159
160
161
162
163
164
165
166
167 protected Class getEndpointClass(Object endpoint) {
168 if (AopUtils.isJdkDynamicProxy(endpoint)) {
169 throw new IllegalArgumentException(ClassUtils.getShortName(getClass()) +
170 " does not work with JDK Dynamic Proxies. " +
171 "Please use CGLIB proxies, by setting proxy-target-class=\"true\" on the aop:aspectj-autoproxy " +
172 "or aop:config element.");
173 }
174 return AopUtils.getTargetClass(endpoint);
175 }
176 }