| 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.api.bus.ServiceDefinition; |
| 25 | |
import org.kuali.rice.ksb.messaging.bam.BAMServerProxy; |
| 26 | |
import org.kuali.rice.ksb.messaging.servlet.CXFServletControllerAdapter; |
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
public abstract class AbstractWebServiceExporter implements ServiceExporter { |
| 35 | |
|
| 36 | 0 | static final Logger LOG = Logger.getLogger(AbstractWebServiceExporter.class); |
| 37 | |
|
| 38 | |
private final ServiceDefinition serviceDefinition; |
| 39 | |
private final Bus cxfBus; |
| 40 | |
private final ServerRegistry cxfServerRegistry; |
| 41 | |
|
| 42 | |
protected abstract void publishService(ServiceDefinition serviceDefinition, Object serviceImpl, String address) throws Exception; |
| 43 | |
|
| 44 | 0 | public AbstractWebServiceExporter(ServiceDefinition serviceDefinition, Bus cxfBus, ServerRegistry cxfServerRegistry) { |
| 45 | 0 | this.serviceDefinition = serviceDefinition; |
| 46 | 0 | this.cxfBus = cxfBus; |
| 47 | 0 | this.cxfServerRegistry = cxfServerRegistry; |
| 48 | 0 | } |
| 49 | |
|
| 50 | |
@Override |
| 51 | |
public Object exportService(ServiceDefinition serviceDefinition) { |
| 52 | |
try { |
| 53 | 0 | String serviceAddress = getServiceAddress(serviceDefinition); |
| 54 | |
|
| 55 | |
|
| 56 | 0 | if (!(isServicePublished(serviceAddress))){ |
| 57 | 0 | publishService(serviceDefinition, serviceDefinition.getService(), serviceAddress); |
| 58 | |
} |
| 59 | |
|
| 60 | |
|
| 61 | 0 | CXFServletControllerAdapter cxfController = new CXFServletControllerAdapter(); |
| 62 | |
|
| 63 | 0 | return BAMServerProxy.wrap(cxfController, serviceDefinition); |
| 64 | 0 | } catch (Exception e) { |
| 65 | 0 | throw new RuntimeException(e); |
| 66 | |
} |
| 67 | |
|
| 68 | |
} |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
protected String getServiceAddress(ServiceDefinition serviceDefinition) { |
| 74 | |
|
| 75 | 0 | String serviceAddress = serviceDefinition.getServicePath(); |
| 76 | 0 | if (("/").equals(serviceAddress)){ |
| 77 | 0 | serviceAddress = serviceAddress + serviceDefinition.getServiceName().getLocalPart(); |
| 78 | |
} else { |
| 79 | 0 | serviceAddress = serviceAddress + "/" + serviceDefinition.getServiceName().getLocalPart(); |
| 80 | |
} |
| 81 | 0 | return serviceAddress; |
| 82 | |
} |
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
protected boolean isServicePublished(String serviceAddress) { |
| 90 | |
|
| 91 | 0 | ServerRegistry serverRegistry = getCXFServerRegistry(); |
| 92 | 0 | List<Server> servers = serverRegistry.getServers(); |
| 93 | |
|
| 94 | 0 | for (Server server:servers){ |
| 95 | 0 | String endpointAddress = server.getEndpoint().getEndpointInfo().getAddress(); |
| 96 | |
|
| 97 | 0 | if (endpointAddress.equals(serviceAddress)){ |
| 98 | 0 | LOG.info("Service already published on CXF, not republishing: " + serviceAddress); |
| 99 | 0 | return true; |
| 100 | |
} |
| 101 | 0 | } |
| 102 | |
|
| 103 | 0 | return false; |
| 104 | |
} |
| 105 | |
|
| 106 | |
protected ServiceDefinition getServiceDefinition() { |
| 107 | 0 | return this.serviceDefinition; |
| 108 | |
} |
| 109 | |
|
| 110 | |
protected Bus getCXFBus() { |
| 111 | 0 | return this.cxfBus; |
| 112 | |
} |
| 113 | |
|
| 114 | |
protected ServerRegistry getCXFServerRegistry() { |
| 115 | 0 | return this.cxfServerRegistry; |
| 116 | |
} |
| 117 | |
|
| 118 | |
} |