Project

General

Profile

1
package eu.dnetlib.msro.workflows.sarasvati.registry;
2

    
3
import java.util.ArrayList;
4
import java.util.Collection;
5
import java.util.Date;
6
import java.util.HashMap;
7
import java.util.Map;
8
import java.util.Map.Entry;
9

    
10
import org.apache.commons.logging.Log;
11
import org.apache.commons.logging.LogFactory;
12
import org.springframework.beans.factory.annotation.Required;
13

    
14
import com.google.common.collect.BiMap;
15
import com.google.common.collect.HashBiMap;
16
import com.googlecode.sarasvati.GraphProcess;
17

    
18
import eu.dnetlib.msro.workflows.util.ProcessUtils;
19

    
20
public class GraphProcessRegistry {
21
	private static final Log log = LogFactory.getLog(GraphProcessRegistry.class); // NOPMD by marko on 11/24/08 5:02 PM
22

    
23
	private BiMap<String, GraphProcess> procs = HashBiMap.create();
24
	private Map<String, Collection<GraphProcess>> byResource = new HashMap<String, Collection<GraphProcess>>();
25

    
26
	private int maxSize;
27
	
28
	public GraphProcess findProcess(final String identifier) {
29
		return procs.get(identifier);
30
	}
31

    
32
	public Collection<GraphProcess> findProcessesByResource(final String identifier) {
33
		synchronized (this) {
34
			final Collection<GraphProcess> res = byResource.get(identifier);
35
			if (res == null) {
36
				return new ArrayList<GraphProcess>();
37
			}
38
			return res;
39
		}
40
	}
41

    
42
	public String associateProcessWithResource(final GraphProcess process, final String identifier) {
43
		registerProcess(process);
44
		synchronized (this) {
45
			final Collection<GraphProcess> processes = findProcessesByResource(identifier);
46
			processes.add(process);
47
			byResource.put(identifier, processes);
48
		}
49
		return identifier;
50
	}
51

    
52
	public String registerProcess(final GraphProcess process) {
53
		if (procs.containsValue(process)) {
54
			return procs.inverse().get(process);
55
		}
56
		final String id = ProcessUtils.generateProcessId();
57
		
58
		if (procs.size() >= maxSize) {
59
			removeOldestProcess();
60
		}
61
		
62
		procs.put(id, process);
63
		log.info("Registered proc " + process.getGraph().getName() + " with id " + id);
64
		
65
		return id;
66
	}
67

    
68
	private void removeOldestProcess() {
69
		Date oldDate = new Date();
70
		String oldId = null;
71
		
72
		for (Entry<String, GraphProcess> e : procs.entrySet()) {
73
			final GraphProcess proc = e.getValue();
74
			
75
			if (proc.isComplete() || proc.isCanceled()) {
76
				final Date date = ProcessUtils.calculateLastActivityDate(proc);
77
				if (date.before(oldDate)) {
78
					oldDate = date;
79
					oldId = e.getKey();
80
				}
81
			}
82
		}
83
		
84
		if (oldId != null) {
85
			unregisterProcess(oldId);
86
		}
87
		
88
	}
89

    
90
	public void unregisterProcess(final String identifier) {
91
		final GraphProcess process = findProcess(identifier); // NOPMD
92
		procs.remove(identifier);
93

    
94
		for (final Collection<GraphProcess> processes : byResource.values()) {
95
			processes.remove(process);
96
		}
97
	}
98

    
99
	public Collection<String> listIdentifiers() {
100
		return procs.keySet();
101
	}
102
	
103
	public Map<String, Collection<GraphProcess>> getByResource() {
104
		return byResource;
105
	}
106

    
107
	public int getMaxSize() {
108
		return maxSize;
109
	}
110

    
111
	@Required
112
	public void setMaxSize(int maxSize) {
113
		this.maxSize = maxSize;
114
	}
115

    
116
}
    (1-1/1)