-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericConfig.java
More file actions
99 lines (88 loc) · 2.73 KB
/
Copy pathGenericConfig.java
File metadata and controls
99 lines (88 loc) · 2.73 KB
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
package test;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class GenericConfig implements Config {
private String confFile;
private final List<ParallelAgent> agents = new ArrayList<>();
public void setConfFile(String confFile) {
this.confFile = confFile;
}
@Override
public void create() {
if (confFile == null) {
return;
}
List<String> lines;
try {
lines = Files.readAllLines(Paths.get(confFile));
} catch (IOException e) {
return;
}
if (lines.size() % 3 != 0) {
return;
}
for (int i = 0; i < lines.size(); i += 3) {
String className = lines.get(i).trim();
String[] subs = splitTopics(lines.get(i + 1));
String[] pubs = splitTopics(lines.get(i + 2));
Agent agent = instantiateAgent(className, subs, pubs);
if (agent == null) {
continue;
}
ParallelAgent pa = new ParallelAgent(agent, 10);
replaceAgentInTopics(agent, pa);
agents.add(pa);
}
}
private String[] splitTopics(String line) {
if (line == null || line.trim().isEmpty()) {
return new String[0];
}
String[] raw = line.split(",");
for (int i = 0; i < raw.length; i++) {
raw[i] = raw[i].trim();
}
return raw;
}
private Agent instantiateAgent(String className, String[] subs, String[] pubs) {
try {
Class<?> cls = Class.forName(className);
return (Agent) cls.getConstructor(String[].class, String[].class)
.newInstance((Object) subs, (Object) pubs);
} catch (Exception e) {
return null;
}
}
private void replaceAgentInTopics(Agent original, Agent replacement) {
for (Topic topic : TopicManagerSingleton.get().getTopics()) {
replaceInList(topic.getSubscribers(), original, replacement);
replaceInList(topic.getPublishers(), original, replacement);
}
}
private void replaceInList(List<Agent> list, Agent original, Agent replacement) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i) == original) {
list.set(i, replacement);
}
}
}
@Override
public String getName() {
return "Generic Config";
}
@Override
public int getVersion() {
return 1;
}
@Override
public void close() {
for (ParallelAgent agent : agents) {
agent.close();
}
agents.clear();
}
}