1 | package de.uka.ipd.sdq.prototype.framework; |
2 | import java.util.Collection; |
3 | import java.util.HashMap; |
4 | import java.util.LinkedList; |
5 | import java.util.Map; |
6 | import java.util.Map.Entry; |
7 | |
8 | /** |
9 | * Manages the sets of container ID, name and allocated components |
10 | * |
11 | * @author zolynski |
12 | */ |
13 | public abstract class AbstractAllocationStorage |
14 | { |
15 | protected static AbstractAllocationStorage singleton; |
16 | |
17 | private static Map<String, String> containerIdToName = new HashMap<String, String>(); |
18 | private static HashMap<String, Collection<Class<?>>> containerIdToComponents = new HashMap<String, Collection<Class<?>>>(); |
19 | |
20 | private static String activeContainer; |
21 | private static boolean localMode = false; |
22 | |
23 | /** |
24 | * Stores a tuple of container ID, name and one component |
25 | * |
26 | * @param containerId key, can be used multiple times |
27 | * @param containerName value |
28 | * @param component value |
29 | */ |
30 | protected static void saveContainerComponent(String containerId, String containerName, Class<?> component) |
31 | { |
32 | if (!containerIdToName.containsKey(containerId)) { |
33 | containerIdToName.put(containerId, containerName); |
34 | containerIdToComponents.put(containerId, new LinkedList<Class<?>>()); |
35 | } |
36 | |
37 | containerIdToComponents.get(containerId).add(component); |
38 | } |
39 | |
40 | public static String getContainerName(String containerId) |
41 | { |
42 | return containerIdToName.get(containerId); |
43 | } |
44 | |
45 | /** |
46 | * FIXME: Possible source of errors if two container have the same name? |
47 | * |
48 | * @param container |
49 | * @return |
50 | */ |
51 | public static String getContainerID(String container) |
52 | { |
53 | for (Entry<String, String> entry : containerIdToName.entrySet()) { |
54 | if (entry.getValue().equals(container)) { |
55 | return entry.getKey(); |
56 | } |
57 | } |
58 | return null; |
59 | } |
60 | |
61 | /** |
62 | * Returns a set of all container ids |
63 | * |
64 | * @return Set of container ids |
65 | */ |
66 | public static Collection<String> getContainerIds() |
67 | { |
68 | return containerIdToName.keySet(); |
69 | } |
70 | |
71 | /** |
72 | * Returns a set of all container names |
73 | * |
74 | * @return Set of container names |
75 | */ |
76 | public static Collection<String> getContainerNames() |
77 | { |
78 | return containerIdToName.values(); |
79 | } |
80 | |
81 | /** |
82 | * Returns a collection of components to a given container id |
83 | * |
84 | * @param containerId |
85 | * @return collection of components |
86 | */ |
87 | public static Collection<Class<?>> getComponents(String containerId) |
88 | { |
89 | return containerIdToComponents.get(containerId); |
90 | } |
91 | |
92 | protected abstract void initContainerTemplate(); |
93 | |
94 | public static void initContainer() { |
95 | singleton.initContainerTemplate(); |
96 | } |
97 | |
98 | public static void initSingleton(AbstractAllocationStorage instance) { |
99 | singleton = instance; |
100 | } |
101 | |
102 | public static void setActiveContainer(String containerId) { |
103 | activeContainer = containerId; |
104 | } |
105 | |
106 | /** |
107 | * Returns the container of this current component. |
108 | * FIXME: Also returns the FIRST container if local mode is active, since |
109 | * no container has been (and can not be) chosen for this hardware node. |
110 | * @return |
111 | */ |
112 | public static String getActiveContainer() { |
113 | if (!localMode) { |
114 | return activeContainer; |
115 | } else { |
116 | return containerIdToName.entrySet().iterator().next().getKey(); |
117 | } |
118 | } |
119 | |
120 | public static void setLocalMode(boolean localMode) { |
121 | AbstractAllocationStorage.localMode = localMode; |
122 | } |
123 | |
124 | public static boolean isLocalMode() { |
125 | return localMode; |
126 | } |
127 | |
128 | |
129 | |
130 | } |