1 package net.mograsim.machine;
3 import java.util.Collections;
4 import java.util.HashMap;
5 import java.util.HashSet;
7 import java.util.Objects;
10 import org.eclipse.core.runtime.CoreException;
11 import org.eclipse.core.runtime.IConfigurationElement;
12 import org.eclipse.core.runtime.IExtension;
13 import org.eclipse.core.runtime.IExtensionPoint;
14 import org.eclipse.core.runtime.IExtensionRegistry;
15 import org.eclipse.core.runtime.IRegistryEventListener;
16 import org.eclipse.core.runtime.Platform;
19 * The MachineRegsitry is a static context registry for all {@link MachineDefinition}s known at any point during runtime.
21 * It is updated automatically as mograsim machine definition extension points ({@link #MACHINE_EXT_ID}) are added or removed.
23 * @author Christian Femers
26 public class MachineRegistry
29 public static final String MACHINE_EXT_ID = "net.mograsim.machine.machine_definition";
31 private static final Map<String, MachineDefinition> installedMachines = Collections.synchronizedMap(new HashMap<>());
32 private static final Set<MachineRegistryListener> listeners = Collections.synchronizedSet(new HashSet<>());
34 private static void reload()
36 installedMachines.clear();
37 IExtensionRegistry registry = Platform.getExtensionRegistry();
38 IConfigurationElement[] config = registry.getConfigurationElementsFor(MACHINE_EXT_ID);
41 for (IConfigurationElement e : config)
43 final Object o = e.createExecutableExtension("class");
44 final String id = e.getAttribute("unique_id");
45 if (o instanceof MachineDefinition)
47 MachineDefinition md = (MachineDefinition) o;
48 if (Objects.equals(id, md.getId()))
49 installedMachines.put(id, md);
51 System.err.println("Machine definition ids to not match: " + id + " and " + md.getId());
54 System.err.println("Invalid machine definition: " + o + "(id=" + id + "");
58 catch (CoreException ex)
60 System.err.println("An error occurred reloading the machines:");
66 public static void initialize()
69 Platform.getExtensionRegistry().addListener(new IRegistryEventListener()
73 public void removed(IExtensionPoint[] extensionPoints)
79 public void removed(IExtension[] extensions)
85 public void added(IExtensionPoint[] extensionPoints)
91 public void added(IExtension[] extensions)
98 public static Map<String, MachineDefinition> getInstalledMachines()
100 return Collections.unmodifiableMap(installedMachines);
103 public static MachineDefinition getMachine(String id)
105 return installedMachines.get(id);
108 private static void notifyListeners()
110 Map<String, MachineDefinition> unmodMachines = getInstalledMachines();
111 listeners.forEach(l -> l.onReload(unmodMachines));
114 public static void addMachineRegistryListener(MachineRegistryListener listener)
116 listeners.add(listener);
119 public static void removeMachineRegistryListener(MachineRegistryListener listener)
121 listeners.remove(listener);
125 public interface MachineRegistryListener
127 void onReload(Map<String, MachineDefinition> installedMachines);