001/**
002 * Copyright 2005-2017 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.ksb.messaging.serviceconnectors;
017
018import java.net.URL;
019
020import org.kuali.rice.core.api.security.credentials.CredentialsSource;
021import org.kuali.rice.ksb.api.bus.ServiceConfiguration;
022import org.kuali.rice.ksb.messaging.BusClientFailureProxy;
023import org.kuali.rice.ksb.messaging.bam.BAMClientProxy;
024import org.springframework.util.Assert;
025
026
027/**
028 * Abstract implementation of the ServiceConnector that holds the ServiceInfo
029 * and the CredentialsSource as well as providing convenience proxy methods.
030 * 
031 * @author Kuali Rice Team (rice.collab@kuali.org)
032 * @since 0.9
033 * 
034 */
035public abstract class AbstractServiceConnector implements ServiceConnector {
036
037        /**
038         * Maintains the information about the service.  This should never be null.
039         */
040        private ServiceConfiguration serviceConfiguration;
041        private URL alternateEndpointUrl;
042
043        /**
044         * Maintains the credentials needed by the service.  This may be null.
045         */
046        private CredentialsSource credentialsSource;
047
048        public AbstractServiceConnector(final ServiceConfiguration serviceConfiguration) {
049                this(serviceConfiguration, null);
050        }
051        
052        public AbstractServiceConnector(final ServiceConfiguration serviceConfiguration, URL alternateEndpointUrl) {
053                Assert.notNull(serviceConfiguration, "serviceConfiguration cannot be null");
054                this.serviceConfiguration = serviceConfiguration;
055                this.alternateEndpointUrl = alternateEndpointUrl;
056        }
057        
058        public URL getActualEndpointUrl() {
059                if (alternateEndpointUrl != null) {
060            return alternateEndpointUrl;
061        }
062        return getServiceConfiguration().getEndpointUrl();
063        }
064
065        public ServiceConfiguration getServiceConfiguration() {
066                return this.serviceConfiguration;
067        }
068
069        public void setCredentialsSource(final CredentialsSource credentialsSource) {
070                this.credentialsSource = credentialsSource;
071        }
072
073        protected CredentialsSource getCredentialsSource() {
074                return this.credentialsSource;
075        }
076
077        protected Object getServiceProxyWithFailureMode(final Object service,
078                        final ServiceConfiguration serviceConfiguration) {
079                Object bamWrappedClientProxy = BAMClientProxy.wrap(service, serviceConfiguration);
080        if (!serviceConfiguration.isQueue()) {
081            return bamWrappedClientProxy;
082        }
083                return BusClientFailureProxy.wrap(bamWrappedClientProxy, serviceConfiguration);
084        }
085}