1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package ch.elca.el4j.services.remoting.protocol;
18
19 import java.lang.reflect.Method;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Map;
23
24 import javax.xml.ws.BindingProvider;
25 import javax.xml.ws.Service;
26 import javax.xml.ws.handler.Handler;
27 import javax.xml.ws.handler.HandlerResolver;
28 import javax.xml.ws.handler.PortInfo;
29
30 import org.apache.commons.collections.CollectionUtils;
31 import org.jvnet.jax_ws_commons.spring.SpringService;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import com.sun.xml.ws.client.sei.SEIStub;
36 import com.sun.xml.ws.transport.http.servlet.SpringBinding;
37
38 import ch.elca.el4j.services.monitoring.notification.CoreNotificationHelper;
39 import ch.elca.el4j.services.remoting.AbstractRemotingBase;
40 import ch.elca.el4j.services.remoting.ProtocolSpecificConfiguration;
41 import ch.elca.el4j.services.remoting.RemotingProxyFactoryBean;
42 import ch.elca.el4j.services.remoting.RemotingServiceExporter;
43 import ch.elca.el4j.util.codingsupport.AopHelper;
44
45
46
47
48
49
50
51
52
53
54 public class Jaxws extends AbstractInetSocketAddressWebProtocol {
55
56
57
58 protected static final Logger s_logger = LoggerFactory.getLogger(Jaxws.class);
59
60
61
62
63 @SuppressWarnings("unchecked")
64 private List<Handler> handlers = new ArrayList<Handler>();
65
66
67 @SuppressWarnings("unchecked")
68 @Override
69 public Object createExporterBean(RemotingServiceExporter exporterBean,
70 Class serviceInterfaceWithContext, Object serviceProxy) {
71
72 Object bean = exporterBean.getApplicationContext().getBean(
73 exporterBean.getService());
74
75 SpringService service = new SpringService();
76 service.setBean(bean);
77
78
79
80 if (AopHelper.isAopProxy(bean)) {
81 service.setImpl(AopHelper.getTargetClass(bean));
82 }
83
84
85 adaptExporterService(service);
86
87 SpringBinding binding = new SpringBinding();
88 binding.setUrl("/" + exporterBean.getServiceName());
89 try {
90 binding.setService(service.getObject());
91 } catch (Exception e) {
92 CoreNotificationHelper.notifyMisconfiguration(
93 "Could not create JAX-WS binding for " + bean, e);
94 }
95
96 return binding;
97 }
98
99
100 @SuppressWarnings("unchecked")
101 @Override
102 public Object createProxyBean(RemotingProxyFactoryBean proxyBean,
103 Class serviceInterfaceWithContext) {
104 Object createdProxy = null;
105
106 Class serviceInterface = proxyBean.getServiceInterface();
107 String serviceName = serviceInterface.getName();
108
109 try {
110 Class wsServiceClass;
111 String portName;
112
113 ProtocolSpecificConfiguration cfg
114 = proxyBean.getProtocolSpecificConfiguration();
115 if (cfg != null && cfg instanceof JaxwsProtocolConfiguration) {
116 JaxwsProtocolConfiguration jaxWsConfig
117 = (JaxwsProtocolConfiguration) cfg;
118
119
120 wsServiceClass = jaxWsConfig.getServiceImplementation();
121 portName = "get" + serviceInterface.getSimpleName();
122 } else {
123
124 wsServiceClass = Class.forName(serviceName + "Service");
125 portName = "get" + serviceInterface.getSimpleName() + "Port";
126 }
127
128 Service clientService = (Service) wsServiceClass.newInstance();
129
130
131 adaptProxyService(clientService);
132
133 Method portGetter = wsServiceClass.getMethod(portName);
134 Object port = portGetter.invoke(clientService);
135
136
137 BindingProvider bindingProvider = (BindingProvider) port;
138 Map<String, Object> context = bindingProvider.getRequestContext();
139
140 context.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, generateUrl(proxyBean));
141
142 createdProxy = port;
143 } catch (Exception e) {
144 CoreNotificationHelper.notifyMisconfiguration(
145 "Could not create JAX-WS binding for "
146 + serviceInterface, e);
147 }
148 return createdProxy;
149 }
150
151
152
153
154
155 public boolean getProtocolSpecificContextPassing() {
156 return true;
157 }
158
159
160 @Override
161 public String generateUrl(AbstractRemotingBase remoteBase) {
162 StringBuffer sb = new StringBuffer();
163 sb.append(getServiceProtocol());
164 sb.append("://");
165 sb.append(getServiceHost());
166 sb.append(":");
167 sb.append(getServicePort());
168 sb.append("/");
169 sb.append(getContextPath());
170 sb.append("/");
171 sb.append(remoteBase.getServiceName());
172 return sb.toString();
173 }
174
175
176 @SuppressWarnings("unchecked")
177 @Override
178 public Class getExporterObjectType() {
179 return SpringBinding.class;
180 }
181
182
183 @SuppressWarnings("unchecked")
184 @Override
185 public Class getProxyObjectType() {
186 return SEIStub.class;
187 }
188
189
190
191
192
193 protected void adaptProxyService(Service service) {
194 if (CollectionUtils.isNotEmpty(handlers)) {
195 service.setHandlerResolver(new HandlerResolver() {
196 @SuppressWarnings("unchecked")
197 public List<Handler> getHandlerChain(PortInfo portInfo) {
198 List<Handler> list = new ArrayList<Handler>(handlers.size());
199 list.addAll(handlers);
200 return list;
201 }
202 });
203 }
204 }
205
206
207
208
209
210 protected void adaptExporterService(SpringService service) {
211 if (CollectionUtils.isNotEmpty(handlers)) {
212 service.setHandlers(handlers);
213 }
214 }
215
216
217
218
219
220 @SuppressWarnings("unchecked")
221 public List<Handler> getHandlers() {
222 return handlers;
223 }
224
225
226
227
228
229 @SuppressWarnings("unchecked")
230 public void setHandlers(List<Handler> handlers) {
231 this.handlers = handlers;
232 }
233 }