Added id to machine definition (was only set in the extension point)
[Mograsim.git] / plugins / net.mograsim.machine / src / net / mograsim / machine / MachineRegistry.java
1 package net.mograsim.machine;
2
3 import java.util.Arrays;
4 import java.util.Collections;
5 import java.util.HashMap;
6 import java.util.Map;
7 import org.eclipse.core.runtime.CoreException;
8 import org.eclipse.core.runtime.IConfigurationElement;
9 import org.eclipse.core.runtime.IExtension;
10 import org.eclipse.core.runtime.IExtensionPoint;
11 import org.eclipse.core.runtime.IExtensionRegistry;
12 import org.eclipse.core.runtime.IRegistryEventListener;
13 import org.eclipse.core.runtime.Platform;
14
15 public class MachineRegistry
16 {
17         private static final String MACHINE_EXT_ID = "net.mograsim.machine.machine_definition";
18
19         private static final Map<String, MachineDefinition> installedMachines = new HashMap<>();
20
21         private static void reload()
22         {
23                 installedMachines.clear();
24                 IExtensionRegistry registry = Platform.getExtensionRegistry();
25                 System.out.println(Arrays.toString(registry.getExtensionPoints("net.mograsim.machine")));
26                 IConfigurationElement[] config = registry.getConfigurationElementsFor(MACHINE_EXT_ID);
27                 try
28                 {
29                         for (IConfigurationElement e : config)
30                         {
31                                 System.out.println(e.getNamespaceIdentifier());
32                                 System.out.println(Arrays.toString(e.getAttributeNames()));
33                                 final Object o = e.createExecutableExtension("class");
34                                 final String id = e.getAttribute("unique_id");
35                                 if (o instanceof MachineDefinition)
36                                 {
37                                         System.out.println("Found " + id);
38                                         MachineDefinition md = (MachineDefinition) o;
39                                         if (Objects.equals(id, md.getId()))
40                                                 installedMachines.put(id, md);
41                                         else
42                                                 System.err.println("Machine definition ids to not match: " + id + " and " + md.getId());
43                                 } else
44                                 {
45                                         System.err.println("Invalid machine definition: " + o + "(id=" + id + "");
46                                 }
47                         }
48                 }
49                 catch (CoreException ex)
50                 {
51                         System.out.println(ex.getMessage());
52                 }
53         }
54
55         public static void initialize()
56         {
57                 reload();
58                 Platform.getExtensionRegistry().addListener(new IRegistryEventListener()
59                 {
60
61                         @Override
62                         public void removed(IExtensionPoint[] extensionPoints)
63                         {
64                                 // nothing?
65                         }
66
67                         @Override
68                         public void removed(IExtension[] extensions)
69                         {
70                                 reload();
71                         }
72
73                         @Override
74                         public void added(IExtensionPoint[] extensionPoints)
75                         {
76                                 // nothing?
77                         }
78
79                         @Override
80                         public void added(IExtension[] extensions)
81                         {
82                                 reload();
83                         }
84                 }, MACHINE_EXT_ID);
85         }
86
87         public static Map<String, MachineDefinition> getInstalledMachines()
88         {
89                 return Collections.unmodifiableMap(installedMachines);
90         }
91
92         public static MachineDefinition getMachine(String id)
93         {
94                 return installedMachines.get(id);
95         }
96 }