001/* 002 * Copyright 2012-2018 the original author or authors. 003 * 004 * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0 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 017package org.springframework.boot.liquibase; 018 019import liquibase.servicelocator.CustomResolverServiceLocator; 020import liquibase.servicelocator.ServiceLocator; 021import org.apache.commons.logging.Log; 022import org.apache.commons.logging.LogFactory; 023 024import org.springframework.boot.context.event.ApplicationStartingEvent; 025import org.springframework.context.ApplicationListener; 026import org.springframework.util.ClassUtils; 027 028/** 029 * {@link ApplicationListener} that replaces the liquibase {@link ServiceLocator} with a 030 * version that works with Spring Boot executable archives. 031 * 032 * @author Phillip Webb 033 * @author Dave Syer 034 */ 035public class LiquibaseServiceLocatorApplicationListener 036 implements ApplicationListener<ApplicationStartingEvent> { 037 038 private static final Log logger = LogFactory 039 .getLog(LiquibaseServiceLocatorApplicationListener.class); 040 041 @Override 042 public void onApplicationEvent(ApplicationStartingEvent event) { 043 if (ClassUtils.isPresent("liquibase.servicelocator.CustomResolverServiceLocator", 044 event.getSpringApplication().getClassLoader())) { 045 new LiquibasePresent().replaceServiceLocator(); 046 } 047 } 048 049 /** 050 * Inner class to prevent class not found issues. 051 */ 052 private static class LiquibasePresent { 053 054 public void replaceServiceLocator() { 055 CustomResolverServiceLocator customResolverServiceLocator = new CustomResolverServiceLocator( 056 new SpringPackageScanClassResolver(logger)); 057 ServiceLocator.setInstance(customResolverServiceLocator); 058 } 059 060 } 061 062}