-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericConfig.java
More file actions
73 lines (61 loc) · 2.24 KB
/
Copy pathGenericConfig.java
File metadata and controls
73 lines (61 loc) · 2.24 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
package test;
import java.io.BufferedReader;
import java.io.FileReader;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;
public class GenericConfig implements Config {
private String configFile;
private List<ParallelAgent> agents = new ArrayList<>();
public void setConfFile(String configFile) {
this.configFile = configFile;
}
@Override
public void create() {
try (BufferedReader reader = new BufferedReader(new FileReader(configFile))) {
List<String> lines = new ArrayList<>();
String line;
while ((line = reader.readLine()) != null) {
lines.add(line.trim());
}
// Validate config file structure
if (lines.size() % 3 != 0) {
System.err.println("Invalid config file format");
return;
}
// Process agents
for (int i = 0; i < lines.size(); i += 3) {
String className = lines.get(i);
String[] subs = lines.get(i + 1).split(",");
String[] pubs = lines.get(i + 2).split(",");
// Remove empty strings from subs and pubs
subs = subs[0].isEmpty() ? new String[0] : subs;
pubs = pubs[0].isEmpty() ? new String[0] : pubs;
// Load class dynamically
Class<?> agentClass = Class.forName(className);
Constructor<?> constructor = agentClass.getConstructor(String[].class, String[].class);
Agent agent = (Agent) constructor.newInstance((Object) subs, (Object) pubs);
// Wrap with ParallelAgent
ParallelAgent parallelAgent = new ParallelAgent(agent);
agents.add(parallelAgent);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@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();
}
}