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.binding.BindingFactoryManager; |
22 | |
import org.apache.cxf.endpoint.ServerRegistry; |
23 | |
import org.apache.cxf.interceptor.LoggingInInterceptor; |
24 | |
import org.apache.cxf.interceptor.LoggingOutInterceptor; |
25 | |
import org.apache.cxf.jaxrs.JAXRSBindingFactory; |
26 | |
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean; |
27 | |
import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider; |
28 | |
import org.apache.log4j.Logger; |
29 | |
import org.kuali.rice.core.api.exception.RiceRuntimeException; |
30 | |
import org.kuali.rice.ksb.api.bus.ServiceDefinition; |
31 | |
import org.kuali.rice.ksb.api.bus.support.RestServiceDefinition; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
public class RESTServiceExporter extends AbstractWebServiceExporter implements ServiceExporter { |
39 | |
|
40 | 0 | private static final Logger LOG = Logger.getLogger(RESTServiceExporter.class); |
41 | |
|
42 | |
public RESTServiceExporter(RestServiceDefinition serviceDefinition, Bus cxfBus, ServerRegistry cxfServerRegistry) { |
43 | 0 | super(serviceDefinition, cxfBus, cxfServerRegistry); |
44 | 0 | } |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
@Override |
53 | |
public void publishService(ServiceDefinition serviceDefinition, Object serviceImpl, String address) { |
54 | 0 | RestServiceDefinition restServiceDef = (RestServiceDefinition)serviceDefinition; |
55 | |
|
56 | 0 | LOG.info("Creating JAXRSService " + restServiceDef.getServiceName()); |
57 | 0 | JAXRSServerFactoryBean svrFactory = new JAXRSServerFactoryBean(); |
58 | 0 | svrFactory.setBus(getCXFBus()); |
59 | |
|
60 | 0 | List<Object> resources = restServiceDef.getResources(); |
61 | 0 | if (resources != null && !resources.isEmpty()) { |
62 | 0 | svrFactory.setServiceBeans(resources); |
63 | |
} else { |
64 | |
try { |
65 | 0 | Class<?> resourceClass = this.getClass().getClassLoader().loadClass(restServiceDef.getResourceClass()); |
66 | 0 | svrFactory.setResourceClasses(resourceClass); |
67 | 0 | svrFactory.setResourceProvider(resourceClass, new SingletonResourceProvider(serviceImpl)); |
68 | 0 | } catch (ClassNotFoundException e) { |
69 | 0 | throw new RiceRuntimeException("Failed to publish the service because resource class could not be loaded: " + restServiceDef.getResourceClass(), e); |
70 | 0 | } |
71 | |
} |
72 | |
|
73 | 0 | svrFactory.setServiceName(restServiceDef.getServiceName()); |
74 | 0 | svrFactory.setAddress(address); |
75 | 0 | svrFactory.setExtensionMappings(restServiceDef.getExtensionMappings()); |
76 | 0 | svrFactory.setLanguageMappings(restServiceDef.getLanguageMappings()); |
77 | |
|
78 | 0 | List<Object> providers = restServiceDef.getProviders(); |
79 | 0 | if (providers != null) { |
80 | 0 | svrFactory.setProviders(providers); |
81 | |
} |
82 | |
|
83 | 0 | BindingFactoryManager bindingFactoryManager = getCXFBus().getExtension(BindingFactoryManager.class); |
84 | 0 | JAXRSBindingFactory bindingFactory = new JAXRSBindingFactory(); |
85 | 0 | bindingFactory.setBus(getCXFBus()); |
86 | 0 | bindingFactoryManager.registerBindingFactory(JAXRSBindingFactory.JAXRS_BINDING_ID, bindingFactory); |
87 | |
|
88 | |
|
89 | 0 | if (LOG.isDebugEnabled()) { |
90 | 0 | svrFactory.getInInterceptors().add(new LoggingInInterceptor()); |
91 | |
} |
92 | |
|
93 | 0 | if (LOG.isDebugEnabled()) { |
94 | 0 | svrFactory.getOutInterceptors().add(new LoggingOutInterceptor()); |
95 | |
} |
96 | |
|
97 | |
|
98 | 0 | svrFactory.setPublishedEndpointUrl(restServiceDef.getEndpointUrl().toExternalForm()); |
99 | 0 | svrFactory.create(); |
100 | 0 | } |
101 | |
|
102 | |
} |