-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVulnerabilityScriptRunner.java
120 lines (104 loc) · 2.76 KB
/
VulnerabilityScriptRunner.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
/**
* @author Manjeer
*
* 2. Let's say I have a database of scripts. Each script has an
* arbitrary number of dependencies. The dependencies are expressed as a
* list of scriptIds that need to be executed before a given script.
* There are no circular dependencies. I want to come up with an
* execution plan so that I can run all of the scripts in a sane order.
* Below is the script representation.
*
*/
class VulnerabilityScript {
private final int scriptId;
private final List<Integer> dependencies;
public VulnerabilityScript(int scriptId, List<Integer> dependencies) {
this.scriptId = scriptId;
this.dependencies = dependencies;
}
public int getScriptId() {
return scriptId;
}
public List<Integer> getDependencies() {
return dependencies;
}
}
public class VulnerabilityScriptRunner {
/**
* @param args
* Main method
*/
public static void main(String[] args) {
new VulnerabilityScriptRunner().generatePlan(VulnerabilityScriptRunner.getScriptById(10))
.forEach(x -> System.out.println(x.getScriptId()));
}
/**
* @param script
* @return
*
* Plan generator
*/
public List<VulnerabilityScript> generatePlan(VulnerabilityScript script) {
if (script == null) {
return null;
}
return generatePlan(script.getScriptId(), script);
}
/**
* @param id
* @param script
* @return
*
* Plan generator (private)
*/
private List<VulnerabilityScript> generatePlan(Integer id, VulnerabilityScript script) {
if (script == null) {
script = VulnerabilityScriptRunner.getScriptById(id);
}
if (script == null) {
return null;
}
List<VulnerabilityScript> plan = new ArrayList<>();
if (script.getDependencies() != null && script.getDependencies().size() > 0) {
for (Integer scriptId : script.getDependencies()) {
List<VulnerabilityScript> subPlan = generatePlan(scriptId, null);
if (subPlan != null && subPlan.size() > 0) {
plan.addAll(subPlan);
}
}
}
plan.add(script);
return plan;
}
private static Set<Integer> dup = new HashSet<>();
/**
* @param id
* @return
*
* Need to write script to get Object from DB. The following is just a
* random code
*/
private static VulnerabilityScript getScriptById(Integer id) {
Random r = new Random();
List<Integer> list = new ArrayList<>();
if (r.nextInt(100) % 2 == 0) {
int rand = r.nextInt(100);
if (!dup.contains(rand)) {
list.add(rand);
dup.add(rand);
}
rand = r.nextInt(100);
if (!dup.contains(rand)) {
list.add(rand);
dup.add(rand);
}
list.add(r.nextInt(100));
}
return new VulnerabilityScript(id, list);
}
}