/*
 * Copyright 2016 EPAM Systems
 *
 *
 * This file is part of EPAM Report Portal.
 * https://github.com/epam/ReportPortal
 *
 * Report Portal is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Report Portal is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Report Portal.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.epam.ta.reportportal.core.configs;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.Environment;

import com.epam.ta.reportportal.config.MongodbConfiguration;
import com.github.mongobee.Mongobee;
import com.mongodb.MongoClient;

/**
 * Mongo Migration tool configs
 *
 * @author Andrei Varabyeu
 */
@Configuration
public class MigrationConfiguration {
	@Autowired
	private MongodbConfiguration.MongoProperies mongoProperties;
	@Autowired
	private Environment environment;

	@Bean
	@Autowired
	@Profile({ "!unittest" })
	public Mongobee mongobee(MongoClient mongoClient) {
		Mongobee runner = new Mongobee(mongoClient);
		runner.setDbName(mongoProperties.getDbName());
		runner.setChangeLogsScanPackage("com.epam.ta.reportportal.migration");
		runner.setSpringEnvironment(environment);
		return runner;
	}
}

@@ -822,16 +822,313 @@
 nfigs;%0A%0A
+import com.epam.ta.reportportal.commons.accessible.Accessible;%0Aimport com.epam.ta.reportportal.config.MongodbConfiguration;%0Aimport com.github.mongobee.Mongobee;%0Aimport com.github.mongobee.dao.ChangeEntryDao;%0Aimport com.mongodb.MongoClient;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import o
@@ -1404,139 +1404,8 @@
 t;%0A%0A
-import com.epam.ta.reportportal.config.MongodbConfiguration;%0Aimport com.github.mongobee.Mongobee;%0Aimport com.mongodb.MongoClient;%0A%0A
 /**%0A
@@ -1523,17 +1523,117 @@
 ation %7B%0A
-%09
+%0A    private static final Logger LOGGER = LoggerFactory.getLogger(MongodbConfiguration.class);%0A%0A%0A    
 @Autowir
@@ -1627,33 +1627,36 @@
 %0A    @Autowired%0A
-%09
+    
 private MongodbC
@@ -1700,17 +1700,20 @@
 erties;%0A
-%09
+    
 @Autowir
@@ -1715,17 +1715,20 @@
 towired%0A
-%09
+    
 private 
@@ -1757,16 +1757,22 @@
 t;%0A%0A
-%09
+    
 @Bean%0A
-%09
+    
 @Aut
@@ -1778,17 +1778,20 @@
 towired%0A
-%09
+    
 @Profile
@@ -1792,17 +1792,16 @@
 rofile(%7B
- 
 %22!unitte
@@ -1807,13 +1807,15 @@
 est%22
- 
 %7D)%0A
-%09
+    
 publ
@@ -1862,18 +1862,24 @@
 ient) %7B%0A
-%09%09
+        
 Mongobee
@@ -1915,18 +1915,24 @@
 lient);%0A
-%09%09
+        
 runner.s
@@ -1970,18 +1970,24 @@
 ame());%0A
-%09%09
+        
 runner.s
@@ -2049,18 +2049,24 @@
 tion%22);%0A
-%09%09
+        
 runner.s
@@ -2103,26 +2103,737 @@
 t);%0A
-%09%09return runner;%0A%09
+%0A%0A        Runtime.getRuntime().addShutdownHook(new Thread(() -%3E %7B%0A            try %7B%0A                LOGGER.info(%22Making sure mongobee lock is removed...%22);%0A                ChangeEntryDao dao = (ChangeEntryDao) Accessible.on(runner).field(Mongobee.class.getDeclaredField(%22dao%22)).getValue();%0A                if (dao.isProccessLockHeld()) %7B%0A                    LOGGER.warn(%22Mongobee lock is NOT removed. Removing...%22);%0A                    dao.releaseProcessLock();%0A                %7D%0A                LOGGER.info(%22Mongobee lock has been removed%22);%0A            %7D catch (Exception ignored) %7B%0A                LOGGER.error(%22Cannot make sure mongobee lock has been removed%22, ignored);%0A            %7D%0A        %7D));%0A        return runner;%0A    
 %7D%0A%7D%0A

add shutdown hook for mongobee