001package fr.ifremer.adagio.core.action; 002 003/* 004 * #%L 005 * SIH-Adagio :: Shared 006 * $Id:$ 007 * $HeadURL:$ 008 * %% 009 * Copyright (C) 2012 - 2014 Ifremer 010 * %% 011 * This program is free software: you can redistribute it and/or modify 012 * it under the terms of the GNU Affero General Public License as published by 013 * the Free Software Foundation, either version 3 of the License, or 014 * (at your option) any later version. 015 * 016 * This program is distributed in the hope that it will be useful, 017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 019 * GNU General Public License for more details. 020 * 021 * You should have received a copy of the GNU Affero General Public License 022 * along with this program. If not, see <http://www.gnu.org/licenses/>. 023 * #L% 024 */ 025 026import java.io.File; 027import java.io.IOException; 028 029import org.apache.commons.lang3.StringUtils; 030import org.apache.commons.logging.Log; 031import org.apache.commons.logging.LogFactory; 032import org.nuiton.util.Version; 033 034import fr.ifremer.adagio.core.AdagioTechnicalException; 035import fr.ifremer.adagio.core.config.AdagioConfiguration; 036import fr.ifremer.adagio.core.dao.technical.DaoUtils; 037import fr.ifremer.adagio.core.service.DatabaseSchemaService; 038import fr.ifremer.adagio.core.service.ServiceLocator; 039 040public class DatabaseStatusAction { 041 /* Logger */ 042 private static final Log log = LogFactory.getLog(DatabaseStatusAction.class); 043 044 public void run() { 045 AdagioConfiguration config = AdagioConfiguration.getInstance(); 046 047 if (log.isInfoEnabled()) { 048 log.info("Starting database status report..."); 049 boolean isFileDatabase = DaoUtils.isFileDatabase(AdagioConfiguration.getInstance().getJdbcDriver()); 050 if (isFileDatabase) { 051 log.info(String.format(" Database directory: %s", AdagioConfiguration.getInstance().getDbDirectory())); 052 } 053 log.info(String.format(" JDBC Driver: %s", AdagioConfiguration.getInstance().getJdbcDriver())); 054 log.info(String.format(" JDBC URL: %s", AdagioConfiguration.getInstance().getJdbcURL())); 055 log.info(String.format(" JDBC Username: %s", AdagioConfiguration.getInstance().getJdbcUsername())); 056 String jdbcCatalog = config.getJdbcCatalog(); 057 if (StringUtils.isNotBlank(jdbcCatalog)) { 058 log.info(String.format(" JDBC Catalog: %s", jdbcCatalog)); 059 } 060 String jdbcSchema = config.getJdbcSchema(); 061 if (StringUtils.isNotBlank(jdbcSchema)) { 062 log.info(String.format(" JDBC Schema: %s", jdbcSchema)); 063 } 064 } 065 066 boolean isValidConnection = DaoUtils.isValidConnectionProperties(AdagioConfiguration.getInstance().getJdbcDriver(), 067 AdagioConfiguration.getInstance().getJdbcURL(), 068 AdagioConfiguration.getInstance().getJdbcUsername(), 069 AdagioConfiguration.getInstance().getJdbcPassword()); 070 071 if (!isValidConnection) { 072 log.warn("Connection error : could not check database status."); 073 return; 074 } 075 DatabaseSchemaService databaseSchemaService = ServiceLocator.instance().getService("databaseSchemaService", DatabaseSchemaService.class); 076 077 // Check if database is well loaded 078 if (!databaseSchemaService.isDbLoaded()) { 079 log.warn("Database not start ! Could not check database status."); 080 return; 081 } 082 083 try { 084 Version actualDbVersion = databaseSchemaService.getDbVersion(); 085 if (actualDbVersion != null) { 086 log.info("Database schema version is: " + actualDbVersion.toString()); 087 } 088 089 Version expectedDbVersion = databaseSchemaService.getApplicationVersion(); 090 log.info("Database schema version AFTER an update should be: " + expectedDbVersion.toString()); 091 } catch (AdagioTechnicalException e) { 092 log.error("Error while getting versions.", e); 093 } 094 095 File outputFile = config.getLiquibaseOutputFile(); 096 097 try { 098 log.info("Launching status report generation..."); 099 databaseSchemaService.generateStatusReport(outputFile); 100 if (outputFile != null) { 101 log.info(String.format("Database status report successfullly generated at %s", outputFile)); 102 } 103 else { 104 log.info("Database status report successfullly generated."); 105 } 106 } catch (AdagioTechnicalException e) { 107 log.error("Error while writing database status report.", e); 108 } catch (IOException e) { 109 log.error("Error while writing database status report.", e); 110 } 111 } 112 113}