001    /*
002     * Copyright 2011 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     */
016    package org.kuali.kfs.module.external.kc.service;
017    
018    import java.net.MalformedURLException;
019    import java.net.URL;
020    import java.util.List;
021    
022    import javax.xml.namespace.QName;
023    import javax.xml.ws.Service;
024    
025    import org.apache.log4j.Logger;
026    import org.kuali.kfs.sys.KFSConstants;
027    import org.kuali.kfs.sys.context.SpringContext;
028    import org.kuali.rice.kns.service.KNSServiceLocator;
029    import org.kuali.rice.ksb.messaging.ServiceInfo;
030    import org.kuali.rice.ksb.messaging.service.ServiceRegistry;
031    
032    public abstract class KfsService extends Service {
033        protected static final Logger LOG = Logger.getLogger(KfsService.class);
034        
035        protected KfsService(URL wsdlDocumentLocation, QName serviceName) {
036            super(wsdlDocumentLocation, serviceName);
037        }
038     
039        public static String getWebServiceServerName() {
040           return  KNSServiceLocator.getKualiConfigurationService().getPropertyString(KFSConstants.KC_APPLICATION_URL_KEY);
041        }
042        
043        protected static URL getWsdl(QName qname) throws MalformedURLException {
044            URL url = null;
045            String webServiceServer =  getWebServiceServerName();
046    
047            //FIXME KC needs to get the services exposed and working on the KSB for this to work
048            if (webServiceServer == null) {
049                // look for service on the KSB registry
050                ServiceRegistry serviceRegistry = SpringContext.getBean(ServiceRegistry.class);
051                List<ServiceInfo> wsdlServices = serviceRegistry.fetchActiveByName(qname);
052                if (wsdlServices.size() > 0 ) {
053                    ServiceInfo serviceInfo = wsdlServices.get(0);
054                    String wsdlName = serviceInfo.getActualEndpointUrl() + "?wsdl";
055                    url = new URL(wsdlName);
056                }
057            } else {
058                url  = new URL(webServiceServer + "/remoting/" +  qname.getLocalPart() + "?wsdl");
059            }  
060            return url;
061        }
062        
063        public abstract URL getWsdl() throws MalformedURLException;
064    }