Coverage Report - org.kuali.rice.ksb.messaging.serviceexporters.AbstractWebServiceExporter
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractWebServiceExporter
0%
0/34
0%
0/12
2.5
 
 1  
 /*
 2  
  * Copyright 2007-2010 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.ksb.messaging.serviceexporters;
 17  
 
 18  
 import java.util.List;
 19  
 
 20  
 import org.apache.cxf.Bus;
 21  
 import org.apache.cxf.endpoint.Server;
 22  
 import org.apache.cxf.endpoint.ServerRegistry;
 23  
 import org.apache.log4j.Logger;
 24  
 import org.kuali.rice.ksb.messaging.ServerSideRemotedServiceHolder;
 25  
 import org.kuali.rice.ksb.messaging.ServiceDefinition;
 26  
 import org.kuali.rice.ksb.messaging.ServiceInfo;
 27  
 import org.kuali.rice.ksb.messaging.bam.BAMServerProxy;
 28  
 import org.kuali.rice.ksb.messaging.servlet.CXFServletControllerAdapter;
 29  
 import org.kuali.rice.ksb.service.KSBContextServiceLocator;
 30  
 import org.kuali.rice.ksb.service.KSBServiceLocator;
 31  
 
 32  
 /**
 33  
  * Abstract ServiceExporter for web services 
 34  
  * 
 35  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 36  
  *
 37  
  */
 38  
 public abstract class AbstractWebServiceExporter {
 39  
 
 40  0
     static final Logger LOG = Logger.getLogger(AbstractWebServiceExporter.class);
 41  
     
 42  
     protected ServiceInfo serviceInfo;
 43  
 
 44  
     public abstract void publishService(ServiceDefinition serviceDef, Object serviceImpl, String address) throws Exception;
 45  
 
 46  
     protected KSBContextServiceLocator serviceLocator;
 47  
 
 48  0
     public AbstractWebServiceExporter(ServiceInfo serviceInfo, KSBContextServiceLocator serviceLocator) {
 49  0
         this.serviceInfo = serviceInfo;
 50  0
         this.serviceLocator = serviceLocator;
 51  0
     }
 52  
     
 53  
     public ServerSideRemotedServiceHolder getServiceExporter(Object serviceImpl) {
 54  
             try {                        
 55  0
                     ServiceDefinition serviceDef = getServiceInfo().getServiceDefinition();
 56  
                     
 57  0
                     String serviceAddress = getServiceAddress(serviceDef);
 58  
                     
 59  
                     //Publish the CXF service if it hasn't already been published
 60  0
                     if (!(isServicePublished(serviceAddress))){
 61  0
                             publishService(serviceDef, serviceImpl, serviceAddress);
 62  
                     }
 63  
                     
 64  
                     //Create a CXF mvc controller for this service
 65  0
                     CXFServletControllerAdapter cxfController = new CXFServletControllerAdapter(this.getServiceInfo());
 66  
                     
 67  0
                     return new ServerSideRemotedServiceHolder(BAMServerProxy.wrap(cxfController, this.getServiceInfo()), this.serviceInfo.getServiceDefinition().getService(), getServiceInfo());
 68  0
             } catch (Exception e) {
 69  0
                     throw new RuntimeException(e);
 70  
             }
 71  
     }
 72  
 
 73  
     /**
 74  
      * @return the address where the service is (or will be) published
 75  
      */
 76  
     protected String getServiceAddress(ServiceDefinition serviceDef) {
 77  
         //Determine endpoint address to publish service on
 78  0
         String serviceAddress = serviceDef.getServicePath();
 79  0
         if (("/").equals(serviceAddress)){
 80  0
                 serviceAddress = serviceAddress + getServiceInfo().getQname().getLocalPart();
 81  
         } else {
 82  0
                 serviceAddress = serviceAddress + "/" + getServiceInfo().getQname().getLocalPart();
 83  
         }
 84  0
         return serviceAddress;
 85  
     }
 86  
 
 87  
     /** 
 88  
      * This determines if the service has already been published on the CXF bus.
 89  
      * 
 90  
      * @return true if cxf server exists for this service.
 91  
      */
 92  
     protected boolean isServicePublished(String serviceAddress) {
 93  
             
 94  0
             ServerRegistry serverRegistry = getCXFServerRegistry();
 95  0
             List<Server> servers = serverRegistry.getServers();
 96  
             
 97  0
             for (Server server:servers){                
 98  0
                     String endpointAddress = server.getEndpoint().getEndpointInfo().getAddress();
 99  
     
 100  0
                     if (endpointAddress.equals(serviceAddress)){
 101  0
                             LOG.info("Service already published on CXF, not republishing: " + serviceAddress);
 102  0
                             return true;
 103  
                     }                
 104  0
             }
 105  
             
 106  0
             return false;
 107  
     }
 108  
 
 109  
     public ServiceInfo getServiceInfo() {
 110  0
             return this.serviceInfo;
 111  
     }
 112  
 
 113  
     protected Bus getCXFBus() {
 114  0
             if (this.serviceLocator != null) {
 115  0
                     return serviceLocator.getCXFBus();
 116  
             }
 117  0
             return KSBServiceLocator.getCXFBus();
 118  
     }
 119  
 
 120  
     protected ServerRegistry getCXFServerRegistry() {
 121  0
             if (this.serviceLocator != null) {
 122  0
                     return serviceLocator.getCXFServerRegistry();
 123  
             }
 124  0
             return KSBServiceLocator.getCXFServerRegistry();
 125  
     }
 126  
 
 127  
 }