1 |
26600
|
sandro.lab
|
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 |
30053
|
michele.ar
|
|
68 |
|
|
public int countRunningWfs() {
|
69 |
|
|
int count = 0;
|
70 |
|
|
|
71 |
|
|
for (Entry<String, GraphProcess> e : procs.entrySet()) {
|
72 |
|
|
final GraphProcess proc = e.getValue();
|
73 |
|
|
|
74 |
|
|
if (!proc.isComplete() && !proc.isCanceled()) {
|
75 |
|
|
count++;
|
76 |
|
|
}
|
77 |
|
|
}
|
78 |
|
|
|
79 |
|
|
return count;
|
80 |
|
|
}
|
81 |
26600
|
sandro.lab
|
|
82 |
|
|
private void removeOldestProcess() {
|
83 |
|
|
Date oldDate = new Date();
|
84 |
|
|
String oldId = null;
|
85 |
|
|
|
86 |
|
|
for (Entry<String, GraphProcess> e : procs.entrySet()) {
|
87 |
|
|
final GraphProcess proc = e.getValue();
|
88 |
|
|
|
89 |
|
|
if (proc.isComplete() || proc.isCanceled()) {
|
90 |
|
|
final Date date = ProcessUtils.calculateLastActivityDate(proc);
|
91 |
|
|
if (date.before(oldDate)) {
|
92 |
|
|
oldDate = date;
|
93 |
|
|
oldId = e.getKey();
|
94 |
|
|
}
|
95 |
|
|
}
|
96 |
|
|
}
|
97 |
|
|
|
98 |
|
|
if (oldId != null) {
|
99 |
|
|
unregisterProcess(oldId);
|
100 |
|
|
}
|
101 |
|
|
|
102 |
|
|
}
|
103 |
|
|
|
104 |
|
|
public void unregisterProcess(final String identifier) {
|
105 |
|
|
final GraphProcess process = findProcess(identifier); // NOPMD
|
106 |
|
|
procs.remove(identifier);
|
107 |
|
|
|
108 |
|
|
for (final Collection<GraphProcess> processes : byResource.values()) {
|
109 |
|
|
processes.remove(process);
|
110 |
|
|
}
|
111 |
|
|
}
|
112 |
|
|
|
113 |
|
|
public Collection<String> listIdentifiers() {
|
114 |
|
|
return procs.keySet();
|
115 |
|
|
}
|
116 |
|
|
|
117 |
|
|
public Map<String, Collection<GraphProcess>> getByResource() {
|
118 |
|
|
return byResource;
|
119 |
|
|
}
|
120 |
|
|
|
121 |
|
|
public int getMaxSize() {
|
122 |
|
|
return maxSize;
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
@Required
|
126 |
|
|
public void setMaxSize(int maxSize) {
|
127 |
|
|
this.maxSize = maxSize;
|
128 |
|
|
}
|
129 |
|
|
|
130 |
|
|
}
|