001/* 002 * Licensed to DuraSpace under one or more contributor license agreements. 003 * See the NOTICE file distributed with this work for additional information 004 * regarding copyright ownership. 005 * 006 * DuraSpace licenses this file to you under the Apache License, 007 * Version 2.0 (the "License"); you may not use this file except in 008 * compliance with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019package org.fcrepo.config; 020 021import javax.sql.DataSource; 022 023import org.flywaydb.core.Flyway; 024import org.springframework.beans.factory.FactoryBean; 025 026/** 027 * Factory to generate a Flyway instance for Fedora. 028 * @author whikloj 029 */ 030public class FlywayFactory implements FactoryBean<Flyway> { 031 032 private DataSource dataSource; 033 034 private String databaseType; 035 036 /** 037 * Static constructor 038 * @return a new FlywayFactory instance. 039 */ 040 public static FlywayFactory create() { 041 return new FlywayFactory(); 042 } 043 044 @Override 045 public Flyway getObject() throws Exception { 046 if (dataSource == null) { 047 throw new IllegalStateException("Cannot get flyway instance without a configured datasource."); 048 } 049 final var fly = Flyway.configure().dataSource(dataSource) 050 .locations("classpath:sql/" + (databaseType == null ? "h2" : databaseType)).load(); 051 fly.migrate(); 052 return fly; 053 } 054 055 @Override 056 public Class<?> getObjectType() { 057 return Flyway.class; 058 } 059 060 /** 061 * Set the datasource for use with Flyway. 062 * @param source the data source. 063 * @return this factory 064 */ 065 public FlywayFactory setDataSource(final DataSource source) { 066 dataSource = source; 067 return this; 068 } 069 070 /** 071 * Set the type of database to pick the correct schema files. 072 * @param type database type 073 * @return this factory 074 */ 075 public FlywayFactory setDatabaseType(final String type) { 076 databaseType = type; 077 return this; 078 } 079}