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.binding.BindingFactoryManager;
22 import org.apache.cxf.endpoint.Server;
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.ksb.messaging.RESTServiceDefinition;
30 import org.kuali.rice.ksb.messaging.ServiceDefinition;
31 import org.kuali.rice.ksb.messaging.ServiceInfo;
32 import org.kuali.rice.ksb.service.KSBContextServiceLocator;
33
34
35
36
37
38
39 public class RESTServiceExporter extends AbstractWebServiceExporter implements ServiceExporter {
40
41 private static final Logger LOG = Logger.getLogger(RESTServiceExporter.class);
42
43 private ServiceInfo serviceInfo;
44 private KSBContextServiceLocator serviceLocator;
45
46 public RESTServiceExporter(ServiceInfo serviceInfo) {
47 this(serviceInfo, null);
48 }
49
50 public RESTServiceExporter(ServiceInfo serviceInfo, KSBContextServiceLocator serviceLocator) {
51 super(serviceInfo, serviceLocator);
52 }
53
54
55
56
57
58
59
60 @Override
61 public void publishService(ServiceDefinition serviceDef, Object serviceImpl, String address) throws Exception{
62 RESTServiceDefinition restServiceDef = (RESTServiceDefinition)serviceDef;
63
64 LOG.info("Creating JAXRSService " + (getServiceInfo().getQname()));
65 JAXRSServerFactoryBean svrFactory = new JAXRSServerFactoryBean();
66 svrFactory.setBus(getCXFBus());
67
68 List<Object> resources = restServiceDef.getResources();
69 if (resources != null && !resources.isEmpty()) {
70 svrFactory.setServiceBeans(resources);
71 } else {
72 Class<?> resourceClass = this.getClass().getClassLoader().loadClass(restServiceDef.getResourceClass());
73 svrFactory.setResourceClasses(resourceClass);
74 svrFactory.setResourceProvider(resourceClass, new SingletonResourceProvider(serviceImpl));
75 }
76
77 svrFactory.setServiceName(getServiceInfo().getQname());
78 svrFactory.setAddress(address);
79 svrFactory.setExtensionMappings(restServiceDef.getExtensionMappings());
80 svrFactory.setLanguageMappings(restServiceDef.getLanguageMappings());
81
82 List<Object> providers = restServiceDef.getProviders();
83 if (providers != null)
84 svrFactory.setProviders(providers);
85
86 BindingFactoryManager bindingFactoryManager = getCXFBus().getExtension(BindingFactoryManager.class);
87 JAXRSBindingFactory bindingFactory = new JAXRSBindingFactory();
88 bindingFactory.setBus(getCXFBus());
89 bindingFactoryManager.registerBindingFactory(JAXRSBindingFactory.JAXRS_BINDING_ID, bindingFactory);
90
91
92 if (LOG.isDebugEnabled()) {
93 svrFactory.getInInterceptors().add(new LoggingInInterceptor());
94 }
95
96 if (LOG.isDebugEnabled()) {
97 svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
98 }
99
100
101 svrFactory.setPublishedEndpointUrl(getServiceInfo().getActualEndpointUrl());
102 Server server = svrFactory.create();
103 }
104
105 }