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.net.MalformedURLException; |
19 | |
import java.net.URL; |
20 | |
import java.util.List; |
21 | |
import java.util.regex.Matcher; |
22 | |
import java.util.regex.Pattern; |
23 | |
|
24 | |
import org.apache.commons.lang.StringUtils; |
25 | |
import org.apache.log4j.Logger; |
26 | |
import org.kuali.rice.core.api.config.property.Config; |
27 | |
import org.kuali.rice.core.api.config.property.ConfigContext; |
28 | |
import org.kuali.rice.core.api.exception.RiceRuntimeException; |
29 | |
import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader; |
30 | |
import org.kuali.rice.core.api.security.credentials.CredentialsSource; |
31 | |
import org.kuali.rice.core.api.security.credentials.CredentialsSourceFactory; |
32 | |
import org.kuali.rice.ksb.api.bus.ServiceConfiguration; |
33 | |
import org.kuali.rice.ksb.api.bus.ServiceDefinition; |
34 | |
import org.kuali.rice.ksb.api.bus.support.JavaServiceConfiguration; |
35 | |
import org.kuali.rice.ksb.api.bus.support.RestServiceConfiguration; |
36 | |
import org.kuali.rice.ksb.api.bus.support.SoapServiceConfiguration; |
37 | |
import org.kuali.rice.ksb.messaging.AlternateEndpoint; |
38 | |
import org.kuali.rice.ksb.messaging.AlternateEndpointLocation; |
39 | |
import org.kuali.rice.ksb.util.KSBConstants; |
40 | |
import org.springframework.cache.CacheManager; |
41 | |
import org.springframework.cache.interceptor.CacheProxy; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | 0 | public class ServiceConnectorFactory { |
59 | |
|
60 | 0 | private static final Logger LOG = Logger.getLogger(ServiceConnectorFactory.class); |
61 | |
|
62 | |
public static ServiceConnector getServiceConnector( |
63 | |
final ServiceConfiguration serviceConfiguration) { |
64 | 0 | final CredentialsSourceFactory credentialsSourceFactory = (CredentialsSourceFactory) ConfigContext |
65 | |
.getCurrentContextConfig().getObjects() |
66 | |
.get(Config.CREDENTIALS_SOURCE_FACTORY); |
67 | 0 | final CredentialsSource credentialsSource = credentialsSourceFactory != null ? credentialsSourceFactory |
68 | |
.getCredentialsForType(serviceConfiguration.getCredentialsType()) : null; |
69 | 0 | ServiceConnector serviceConnector = null; |
70 | |
|
71 | 0 | if (serviceConfiguration.getCredentialsType() != null && credentialsSource == null) { |
72 | 0 | throw new RiceRuntimeException("Service requires credentials but no factory or CredentialsSource could be located."); |
73 | |
} |
74 | |
|
75 | 0 | String alternateEndpoint = determineAlternateEndpoint(serviceConfiguration); |
76 | 0 | URL alternateEndpointUrl = null; |
77 | 0 | if (!StringUtils.isBlank(alternateEndpoint)) { |
78 | |
try { |
79 | 0 | alternateEndpointUrl = new URL(alternateEndpoint); |
80 | 0 | } catch (MalformedURLException e) { |
81 | 0 | throw new IllegalStateException("Encountered invalid alternate endpoint url: " + alternateEndpoint, e); |
82 | 0 | } |
83 | |
} |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | 0 | if (ConfigContext.getCurrentContextConfig().getDevMode()) { |
90 | 0 | serviceConnector = new BusLocalConnector(serviceConfiguration); |
91 | 0 | } else if (serviceConfiguration instanceof JavaServiceConfiguration) { |
92 | 0 | serviceConnector = new HttpInvokerConnector((JavaServiceConfiguration) serviceConfiguration, alternateEndpointUrl); |
93 | 0 | } else if (serviceConfiguration instanceof SoapServiceConfiguration) { |
94 | 0 | serviceConnector = new SOAPConnector((SoapServiceConfiguration) serviceConfiguration, alternateEndpointUrl); |
95 | 0 | } else if (serviceConfiguration instanceof RestServiceConfiguration) { |
96 | 0 | serviceConnector = new RESTConnector((RestServiceConfiguration) serviceConfiguration, alternateEndpointUrl); |
97 | |
} |
98 | |
|
99 | 0 | if (serviceConnector == null) { |
100 | 0 | throw new RiceRuntimeException("Don't support service type of " + serviceConfiguration); |
101 | |
} |
102 | 0 | serviceConnector.setCredentialsSource(credentialsSource); |
103 | 0 | if (StringUtils.isNotBlank(serviceConfiguration.getCacheManager())) { |
104 | 0 | CacheManager c = GlobalResourceLoader.getService(serviceConfiguration.getCacheManager()); |
105 | 0 | if (c != null) { |
106 | 0 | return new CacheServiceConnector(serviceConnector, c); |
107 | |
} |
108 | |
} |
109 | |
|
110 | 0 | return serviceConnector; |
111 | |
} |
112 | |
|
113 | 0 | private static class CacheServiceConnector implements ServiceConnector { |
114 | |
private final ServiceConnector connector; |
115 | |
private final Object o; |
116 | 0 | private CacheServiceConnector(ServiceConnector connector, CacheManager cacheManager) { |
117 | 0 | this.connector = connector; |
118 | 0 | this.o = CacheProxy.createCacheProxy(this.connector.getService(), cacheManager); |
119 | 0 | } |
120 | |
|
121 | |
@Override |
122 | |
public Object getService() { |
123 | 0 | return o; |
124 | |
} |
125 | |
|
126 | |
@Override |
127 | |
public void setCredentialsSource(CredentialsSource credentialsSource) { |
128 | 0 | connector.setCredentialsSource(credentialsSource); |
129 | 0 | } |
130 | |
} |
131 | |
|
132 | |
public static String determineAlternateEndpoint(ServiceConfiguration serviceConfiguration) { |
133 | 0 | String alternateEndpointUrl = null; |
134 | 0 | List<AlternateEndpointLocation> alternateEndpointLocations = (List<AlternateEndpointLocation>) ConfigContext.getCurrentContextConfig().getObject(KSBConstants.Config.KSB_ALTERNATE_ENDPOINT_LOCATIONS); |
135 | 0 | if (alternateEndpointLocations != null) { |
136 | 0 | for (AlternateEndpointLocation alternateEndpointLocation : alternateEndpointLocations) { |
137 | 0 | if (Pattern.matches(".*" + alternateEndpointLocation.getEndpointHostReplacementPattern() + ".*", serviceConfiguration.getEndpointUrl().toExternalForm())) { |
138 | 0 | Pattern myPattern = Pattern.compile(alternateEndpointLocation.getEndpointHostReplacementPattern()); |
139 | 0 | Matcher myMatcher = myPattern.matcher(serviceConfiguration.getEndpointUrl().toExternalForm()); |
140 | 0 | String alternateEndpoint = myMatcher.replaceFirst(alternateEndpointLocation.getEndpointHostReplacementValue()); |
141 | 0 | if ( LOG.isInfoEnabled() ) { |
142 | 0 | LOG.info("Found an alternate url host value (" |
143 | |
+ alternateEndpointLocation.getEndpointHostReplacementValue() + ") for endpoint: " |
144 | |
+ serviceConfiguration.getEndpointUrl() + " -> instead using: " + alternateEndpoint); |
145 | |
} |
146 | 0 | alternateEndpointUrl = alternateEndpoint; |
147 | 0 | break; |
148 | |
} |
149 | |
} |
150 | |
} |
151 | 0 | List<AlternateEndpoint> alternateEndpoints = (List<AlternateEndpoint>) ConfigContext.getCurrentContextConfig().getObject(KSBConstants.Config.KSB_ALTERNATE_ENDPOINTS); |
152 | 0 | if (alternateEndpoints != null) { |
153 | 0 | for (AlternateEndpoint alternateEndpoint : alternateEndpoints) { |
154 | 0 | if (Pattern.matches(alternateEndpoint.getEndpointUrlPattern(), serviceConfiguration.getEndpointUrl().toExternalForm())) { |
155 | 0 | if ( LOG.isInfoEnabled() ) { |
156 | 0 | LOG.info("Found an alternate url for endpoint: " + serviceConfiguration.getEndpointUrl() + " -> instead using: " + alternateEndpoint.getActualEndpoint()); |
157 | |
} |
158 | 0 | alternateEndpointUrl = alternateEndpoint.getActualEndpoint(); |
159 | 0 | break; |
160 | |
} |
161 | |
} |
162 | |
} |
163 | 0 | return alternateEndpointUrl; |
164 | |
} |
165 | |
|
166 | |
} |