1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.ksb.messaging.serviceconnectors; |
17 | |
|
18 | |
import java.util.Map; |
19 | |
|
20 | |
import org.apache.cxf.binding.BindingFactoryManager; |
21 | |
import org.apache.cxf.interceptor.LoggingInInterceptor; |
22 | |
import org.apache.cxf.interceptor.LoggingOutInterceptor; |
23 | |
import org.apache.cxf.jaxrs.JAXRSBindingFactory; |
24 | |
import org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean; |
25 | |
import org.apache.log4j.Logger; |
26 | |
import org.kuali.rice.core.exception.RiceRuntimeException; |
27 | |
import org.kuali.rice.core.security.credentials.CredentialsSource; |
28 | |
import org.kuali.rice.ksb.messaging.BusClientFailureProxy; |
29 | |
import org.kuali.rice.ksb.messaging.RESTServiceDefinition; |
30 | |
import org.kuali.rice.ksb.messaging.ServiceInfo; |
31 | |
import org.kuali.rice.ksb.messaging.bam.BAMClientProxy; |
32 | |
import org.kuali.rice.ksb.security.soap.CredentialsOutHandler; |
33 | |
import org.kuali.rice.ksb.service.KSBServiceLocator; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public class ResourceFacadeImpl implements ResourceFacade { |
42 | |
|
43 | 0 | private static final Logger LOG = Logger.getLogger(ResourceFacadeImpl.class); |
44 | |
|
45 | |
private final ServiceInfo serviceInfo; |
46 | |
private CredentialsSource credentialsSource; |
47 | |
|
48 | 0 | public ResourceFacadeImpl(final ServiceInfo serviceInfo) { |
49 | 0 | this.serviceInfo = serviceInfo; |
50 | 0 | } |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
public <R> R getResource(Class<R> resourceClass) { |
58 | 0 | if (resourceClass == null) throw new IllegalArgumentException("resourceClass argument must not be null"); |
59 | |
|
60 | 0 | RESTServiceDefinition restServiceDefinition = (RESTServiceDefinition) serviceInfo.getServiceDefinition(); |
61 | |
|
62 | 0 | if (!restServiceDefinition.hasClass(resourceClass.getName())) { |
63 | 0 | throw new IllegalArgumentException("Service " + serviceInfo.getServiceName() + |
64 | |
" does not contain an implementation of type " + resourceClass.getName()); |
65 | |
} |
66 | |
|
67 | 0 | return getServiceProxy(resourceClass); |
68 | |
} |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
public <R> R getResource(String resourceName) { |
76 | 0 | RESTServiceDefinition restServiceDefinition = (RESTServiceDefinition) serviceInfo.getServiceDefinition(); |
77 | |
|
78 | 0 | String resourceClassName = null; |
79 | |
|
80 | 0 | Map<String, String> resourceToClassNameMap = restServiceDefinition.getResourceToClassNameMap(); |
81 | |
|
82 | 0 | if (resourceName != null && resourceToClassNameMap != null) |
83 | 0 | resourceClassName = resourceToClassNameMap.get(resourceName); |
84 | |
else |
85 | 0 | resourceClassName = restServiceDefinition.getResourceClass(); |
86 | |
|
87 | 0 | if (resourceClassName == null) |
88 | 0 | throw new RiceRuntimeException("No resource class name was found for the specified resourceName: " + resourceName); |
89 | |
|
90 | 0 | Class<?> resourceClass = null; |
91 | |
|
92 | |
try { |
93 | 0 | resourceClass = Class.forName(resourceClassName); |
94 | 0 | } catch (ClassNotFoundException e) { |
95 | 0 | throw new RiceRuntimeException("Configured resource class " + resourceClassName + |
96 | |
" in service " + serviceInfo.getServiceName() + " is not loadable", e); |
97 | 0 | } |
98 | |
|
99 | 0 | return (R)getServiceProxy(resourceClass); |
100 | |
} |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
private <R> R getServiceProxy(Class<R> resourceClass) { |
109 | |
JAXRSClientFactoryBean clientFactory; |
110 | |
|
111 | 0 | clientFactory = new JAXRSClientFactoryBean(); |
112 | 0 | clientFactory.setBus(KSBServiceLocator.getCXFBus()); |
113 | |
|
114 | 0 | clientFactory.setResourceClass(resourceClass); |
115 | 0 | clientFactory.setAddress(serviceInfo.getActualEndpointUrl()); |
116 | 0 | BindingFactoryManager bindingFactoryManager = KSBServiceLocator.getCXFBus().getExtension(BindingFactoryManager.class); |
117 | 0 | JAXRSBindingFactory bindingFactory = new JAXRSBindingFactory(); |
118 | 0 | bindingFactory.setBus(KSBServiceLocator.getCXFBus()); |
119 | |
|
120 | 0 | bindingFactoryManager.registerBindingFactory(JAXRSBindingFactory.JAXRS_BINDING_ID, bindingFactory); |
121 | |
|
122 | |
|
123 | 0 | if (LOG.isDebugEnabled()) { |
124 | 0 | clientFactory.getOutInterceptors().add(new LoggingOutInterceptor()); |
125 | |
} |
126 | |
|
127 | 0 | if (getCredentialsSource() != null) { |
128 | 0 | clientFactory.getOutInterceptors().add(new CredentialsOutHandler(getCredentialsSource(), serviceInfo)); |
129 | |
} |
130 | |
|
131 | 0 | if (LOG.isDebugEnabled()) { |
132 | 0 | clientFactory.getInInterceptors().add(new LoggingInInterceptor()); |
133 | |
} |
134 | |
|
135 | 0 | Object service = clientFactory.create(); |
136 | 0 | return (R)getServiceProxyWithFailureMode(service, serviceInfo); |
137 | |
} |
138 | |
|
139 | |
public boolean isSingleResourceService() { |
140 | 0 | RESTServiceDefinition restServiceDefinition = (RESTServiceDefinition) serviceInfo.getServiceDefinition(); |
141 | 0 | return restServiceDefinition.getResourceToClassNameMap() == null; |
142 | |
} |
143 | |
|
144 | |
public void setCredentialsSource(final CredentialsSource credentialsSource) { |
145 | 0 | this.credentialsSource = credentialsSource; |
146 | 0 | } |
147 | |
|
148 | |
protected CredentialsSource getCredentialsSource() { |
149 | 0 | return this.credentialsSource; |
150 | |
} |
151 | |
|
152 | |
protected Object getServiceProxyWithFailureMode(final Object service, |
153 | |
final ServiceInfo serviceInfo) { |
154 | 0 | Object bamWrappedClientProxy = BAMClientProxy |
155 | |
.wrap(service, serviceInfo); |
156 | 0 | return BusClientFailureProxy.wrap(bamWrappedClientProxy, serviceInfo); |
157 | |
} |
158 | |
|
159 | |
} |