1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.ksb.messaging.serviceexporters; |
17 | |
|
18 | |
import java.util.List; |
19 | |
|
20 | |
import org.apache.cxf.Bus; |
21 | |
import org.apache.cxf.endpoint.Server; |
22 | |
import org.apache.cxf.endpoint.ServerRegistry; |
23 | |
import org.apache.log4j.Logger; |
24 | |
import org.kuali.rice.ksb.messaging.ServerSideRemotedServiceHolder; |
25 | |
import org.kuali.rice.ksb.messaging.ServiceDefinition; |
26 | |
import org.kuali.rice.ksb.messaging.ServiceInfo; |
27 | |
import org.kuali.rice.ksb.messaging.bam.BAMServerProxy; |
28 | |
import org.kuali.rice.ksb.messaging.servlet.CXFServletControllerAdapter; |
29 | |
import org.kuali.rice.ksb.service.KSBContextServiceLocator; |
30 | |
import org.kuali.rice.ksb.service.KSBServiceLocator; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
public abstract class AbstractWebServiceExporter { |
39 | |
|
40 | 0 | static final Logger LOG = Logger.getLogger(AbstractWebServiceExporter.class); |
41 | |
|
42 | |
protected ServiceInfo serviceInfo; |
43 | |
|
44 | |
public abstract void publishService(ServiceDefinition serviceDef, Object serviceImpl, String address) throws Exception; |
45 | |
|
46 | |
protected KSBContextServiceLocator serviceLocator; |
47 | |
|
48 | 0 | public AbstractWebServiceExporter(ServiceInfo serviceInfo, KSBContextServiceLocator serviceLocator) { |
49 | 0 | this.serviceInfo = serviceInfo; |
50 | 0 | this.serviceLocator = serviceLocator; |
51 | 0 | } |
52 | |
|
53 | |
public ServerSideRemotedServiceHolder getServiceExporter(Object serviceImpl) { |
54 | |
try { |
55 | 0 | ServiceDefinition serviceDef = getServiceInfo().getServiceDefinition(); |
56 | |
|
57 | 0 | String serviceAddress = getServiceAddress(serviceDef); |
58 | |
|
59 | |
|
60 | 0 | if (!(isServicePublished(serviceAddress))){ |
61 | 0 | publishService(serviceDef, serviceImpl, serviceAddress); |
62 | |
} |
63 | |
|
64 | |
|
65 | 0 | CXFServletControllerAdapter cxfController = new CXFServletControllerAdapter(this.getServiceInfo()); |
66 | |
|
67 | 0 | return new ServerSideRemotedServiceHolder(BAMServerProxy.wrap(cxfController, this.getServiceInfo()), this.serviceInfo.getServiceDefinition().getService(), getServiceInfo()); |
68 | 0 | } catch (Exception e) { |
69 | 0 | throw new RuntimeException(e); |
70 | |
} |
71 | |
} |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
protected String getServiceAddress(ServiceDefinition serviceDef) { |
77 | |
|
78 | 0 | String serviceAddress = serviceDef.getServicePath(); |
79 | 0 | if (("/").equals(serviceAddress)){ |
80 | 0 | serviceAddress = serviceAddress + getServiceInfo().getQname().getLocalPart(); |
81 | |
} else { |
82 | 0 | serviceAddress = serviceAddress + "/" + getServiceInfo().getQname().getLocalPart(); |
83 | |
} |
84 | 0 | return serviceAddress; |
85 | |
} |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
protected boolean isServicePublished(String serviceAddress) { |
93 | |
|
94 | 0 | ServerRegistry serverRegistry = getCXFServerRegistry(); |
95 | 0 | List<Server> servers = serverRegistry.getServers(); |
96 | |
|
97 | 0 | for (Server server:servers){ |
98 | 0 | String endpointAddress = server.getEndpoint().getEndpointInfo().getAddress(); |
99 | |
|
100 | 0 | if (endpointAddress.equals(serviceAddress)){ |
101 | 0 | LOG.info("Service already published on CXF, not republishing: " + serviceAddress); |
102 | 0 | return true; |
103 | |
} |
104 | 0 | } |
105 | |
|
106 | 0 | return false; |
107 | |
} |
108 | |
|
109 | |
public ServiceInfo getServiceInfo() { |
110 | 0 | return this.serviceInfo; |
111 | |
} |
112 | |
|
113 | |
protected Bus getCXFBus() { |
114 | 0 | if (this.serviceLocator != null) { |
115 | 0 | return serviceLocator.getCXFBus(); |
116 | |
} |
117 | 0 | return KSBServiceLocator.getCXFBus(); |
118 | |
} |
119 | |
|
120 | |
protected ServerRegistry getCXFServerRegistry() { |
121 | 0 | if (this.serviceLocator != null) { |
122 | 0 | return serviceLocator.getCXFServerRegistry(); |
123 | |
} |
124 | 0 | return KSBServiceLocator.getCXFServerRegistry(); |
125 | |
} |
126 | |
|
127 | |
} |