001/** 002 * Copyright 2005-2014 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.serviceexporters; 017 018import java.util.List; 019 020import org.apache.cxf.Bus; 021import org.apache.cxf.endpoint.Server; 022import org.apache.cxf.endpoint.ServerRegistry; 023import org.apache.log4j.Logger; 024import org.kuali.rice.ksb.api.bus.ServiceDefinition; 025import org.kuali.rice.ksb.messaging.bam.BAMServerProxy; 026import org.kuali.rice.ksb.messaging.servlet.CXFServletControllerAdapter; 027 028/** 029 * Abstract ServiceExporter for web services 030 * 031 * @author Kuali Rice Team (rice.collab@kuali.org) 032 * 033 */ 034public abstract class AbstractWebServiceExporter implements ServiceExporter { 035 036 static final Logger LOG = Logger.getLogger(AbstractWebServiceExporter.class); 037 038 private final ServiceDefinition serviceDefinition; 039 private final Bus cxfBus; 040 041 protected abstract void publishService(ServiceDefinition serviceDefinition, Object serviceImpl, String address) throws Exception; 042 043 public AbstractWebServiceExporter(ServiceDefinition serviceDefinition, Bus cxfBus) { 044 this.serviceDefinition = serviceDefinition; 045 this.cxfBus = cxfBus; 046 } 047 048 @Override 049 public Object exportService(ServiceDefinition serviceDefinition) { 050 try { 051 String serviceAddress = getServiceAddress(serviceDefinition); 052 053 //Publish the CXF service if it hasn't already been published 054 if (!(isServicePublished(serviceAddress))){ 055 publishService(serviceDefinition, serviceDefinition.getService(), serviceAddress); 056 } 057 058 //Create a CXF mvc controller for this service 059 CXFServletControllerAdapter cxfController = new CXFServletControllerAdapter(); 060 061 return BAMServerProxy.wrap(cxfController, serviceDefinition); 062 } catch (Exception e) { 063 throw new RuntimeException(e); 064 } 065 066 } 067 068 /** 069 * @return the address where the service is (or will be) published 070 */ 071 protected String getServiceAddress(ServiceDefinition serviceDefinition) { 072 //Determine endpoint address to publish service on 073 String serviceAddress = serviceDefinition.getServicePath(); 074 if (("/").equals(serviceAddress)){ 075 serviceAddress = serviceAddress + serviceDefinition.getServiceName().getLocalPart(); 076 } else { 077 serviceAddress = serviceAddress + "/" + serviceDefinition.getServiceName().getLocalPart(); 078 } 079 return serviceAddress; 080 } 081 082 /** 083 * This determines if the service has already been published on the CXF bus. 084 * 085 * @return true if cxf server exists for this service. 086 */ 087 protected boolean isServicePublished(String serviceAddress) { 088 089 ServerRegistry serverRegistry = getCXFServerRegistry(); 090 List<Server> servers = serverRegistry.getServers(); 091 092 for (Server server:servers){ 093 String endpointAddress = server.getEndpoint().getEndpointInfo().getAddress(); 094 if (endpointAddress.contains(serviceAddress)){ 095 LOG.info("Service already published on CXF, not republishing: " + serviceAddress); 096 return true; 097 } 098 } 099 100 return false; 101 } 102 103 protected ServiceDefinition getServiceDefinition() { 104 return this.serviceDefinition; 105 } 106 107 protected Bus getCXFBus() { 108 return this.cxfBus; 109 } 110 111 protected ServerRegistry getCXFServerRegistry() { 112 return getCXFBus().getExtension(ServerRegistry.class); 113 } 114 115}