Project

General

Profile

1
package eu.dnetlib.functionality.modular.ui.utils;
2

    
3
import java.util.List;
4

    
5
import org.apache.commons.logging.Log;
6
import org.apache.commons.logging.LogFactory;
7
import org.springframework.beans.factory.annotation.Autowired;
8

    
9
import com.google.common.collect.Lists;
10

    
11
import eu.dnetlib.enabling.common.Stoppable;
12
import eu.dnetlib.enabling.common.StoppableDetails;
13
import eu.dnetlib.enabling.common.StoppableDetails.StopStatus;
14

    
15
public class ShutdownUtils {
16

    
17
	@Autowired(required=false)
18
	private List<Stoppable> stoppables = Lists.newArrayList();
19
		
20
	private StopStatus status = StopStatus.RUNNING;
21
	
22
	private static final Log log = LogFactory.getLog(ShutdownUtils.class);
23
	
24
	public  List<StoppableDetails> listStoppableDetails() {
25
		final List<StoppableDetails> list = Lists.newArrayList();
26
		
27
		for (Stoppable s : stoppables) {
28
			list.add(s.getStopDetails());
29
		}
30
		
31
		return list;
32
	}
33
	
34
	public void stopAll() {
35
		this.status = StopStatus.STOPPING;
36
		
37
		for (Stoppable s : stoppables) {
38
			final StoppableDetails info = s.getStopDetails();
39
			if (info.getStatus() == StopStatus.RUNNING) {
40
				log.info("Stopping " + info.getName());
41
				s.stop();
42
			}
43
		}
44
	}
45
	
46
	public StopStatus currentStatus() {
47
		if (status == StopStatus.STOPPING) {
48
			int running = 0;
49
			for (Stoppable s : stoppables) {
50
				final StoppableDetails info = s.getStopDetails();
51
				if (info.getStatus() != StopStatus.STOPPED) {
52
					running++;
53
				}
54
			}
55
			if (running == 0) {
56
				status = StopStatus.STOPPED;
57
			}
58
		}
59
		return status;
60
	}
61
}
    (1-1/1)