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