/*
 * Copyright 2013-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.cloudfoundry.test.support.application;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import reactor.core.publisher.Flux;

import javax.annotation.PostConstruct;
import java.time.Duration;
import java.util.List;

@Configuration
class ApplicationConfiguration {

    @Autowired
    private List<Application> applications;

    @PostConstruct
    void push() {
        Flux
            .fromIterable(this.applications)
            .flatMap(Application::push)
            .doOnSubscribe(s -> registerShutdownHook())
            .then()
            .block(Duration.ofMinutes(15));
    }

    private void registerShutdownHook() {
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            Flux
                .fromIterable(this.applications)
                .flatMap(Application::delete)
                .then()
                .block(Duration.ofMinutes(15));
        }));
    }

}

@@ -867,16 +867,52 @@
 struct;%0A
+import javax.annotation.PreDestroy;%0A
 import j
@@ -1069,28 +1069,25 @@
 %0A%0A    @P
-ostConstruct
+reDestroy
 %0A    voi
@@ -1088,20 +1088,22 @@
    void 
-push
+delete
 () %7B%0A   
@@ -1195,68 +1195,14 @@
 on::
-push)%0A            .doOnSubscribe(s -%3E registerShutdownHook()
+delete
 )%0A  
@@ -1278,121 +1278,49 @@
     
-private void registerShutdownHook() %7B%0A        Runtime.getRuntime().addShutdownHook(new Thread(() -%3E %7B%0A   
+@PostConstruct%0A    void push() %7B%0A
         
  Flu
@@ -1311,34 +1311,29 @@
 ) %7B%0A        
- 
 Flux%0A
-    
             
@@ -1369,36 +1369,32 @@
 ns)%0A            
-    
 .flatMap(Applica
@@ -1395,36 +1395,30 @@
 pplication::
-delete)%0A    
+push)%0A
             
@@ -1417,36 +1417,32 @@
         .then()%0A
-    
             .blo
@@ -1473,21 +1473,8 @@
 ));%0A
-        %7D));%0A
     

Improve Shutdown Deletion