1 package net.mograsim.machine;
3 import java.util.Arrays;
4 import java.util.Collections;
5 import java.util.HashMap;
6 import java.util.HashSet;
8 import java.util.Objects;
11 import org.eclipse.core.runtime.CoreException;
12 import org.eclipse.core.runtime.IConfigurationElement;
13 import org.eclipse.core.runtime.IExtension;
14 import org.eclipse.core.runtime.IExtensionPoint;
15 import org.eclipse.core.runtime.IExtensionRegistry;
16 import org.eclipse.core.runtime.IRegistryEventListener;
17 import org.eclipse.core.runtime.Platform;
19 public class MachineRegistry
22 private static final String MACHINE_EXT_ID = "net.mograsim.machine.machine_definition";
24 private static final Map<String, MachineDefinition> installedMachines = Collections.synchronizedMap(new HashMap<>());
25 private static final Set<MachineRegistryListener> listeners = Collections.synchronizedSet(new HashSet<>());
27 private static void reload()
29 installedMachines.clear();
30 IExtensionRegistry registry = Platform.getExtensionRegistry();
31 System.out.println(Arrays.toString(registry.getExtensionPoints("net.mograsim.machine")));
32 IConfigurationElement[] config = registry.getConfigurationElementsFor(MACHINE_EXT_ID);
35 for (IConfigurationElement e : config)
37 System.out.println(e.getNamespaceIdentifier());
38 System.out.println(Arrays.toString(e.getAttributeNames()));
39 final Object o = e.createExecutableExtension("class");
40 final String id = e.getAttribute("unique_id");
41 if (o instanceof MachineDefinition)
43 System.out.println("Found " + id);
44 MachineDefinition md = (MachineDefinition) o;
45 if (Objects.equals(id, md.getId()))
46 installedMachines.put(id, md);
48 System.err.println("Machine definition ids to not match: " + id + " and " + md.getId());
51 System.err.println("Invalid machine definition: " + o + "(id=" + id + "");
55 catch (CoreException ex)
57 System.out.println(ex.getMessage());
62 public static void initialize()
65 Platform.getExtensionRegistry().addListener(new IRegistryEventListener()
69 public void removed(IExtensionPoint[] extensionPoints)
75 public void removed(IExtension[] extensions)
81 public void added(IExtensionPoint[] extensionPoints)
87 public void added(IExtension[] extensions)
94 public static Map<String, MachineDefinition> getInstalledMachines()
96 return Collections.unmodifiableMap(installedMachines);
99 public static MachineDefinition getMachine(String id)
101 return installedMachines.get(id);
104 private static void notifyListeners()
106 Map<String, MachineDefinition> unmodMachines = getInstalledMachines();
107 listeners.forEach(l -> l.onReload(unmodMachines));
110 public static void addMachineRegistryListener(MachineRegistryListener listener)
112 listeners.add(listener);
115 public static void removeMachineRegistryListener(MachineRegistryListener listener)
117 listeners.remove(listener);
121 public interface MachineRegistryListener
123 void onReload(Map<String, MachineDefinition> installedMachines);