1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package ca.uhn.hl7v2.parser;
24
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.Map;
30 import java.util.Properties;
31
32 import ca.uhn.hl7v2.HL7Exception;
33 import ca.uhn.hl7v2.Version;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37
38
39
40
41
42
43
44
45
46
47
48
49 @SuppressWarnings("serial")
50 public abstract class AbstractModelClassFactory implements ModelClassFactory {
51
52 protected static final String DEFAULT_EVENT_MAP_DIRECTORY = "ca/uhn/hl7v2/parser/eventmap/";
53 private static final Logger LOG = LoggerFactory.getLogger(AbstractModelClassFactory.class);
54
55 private String eventMapDirectory = DEFAULT_EVENT_MAP_DIRECTORY;
56 private Map<Version, Map<String, String>> eventMap;
57
58
59
60
61
62 public String getEventMapDirectory() {
63 return eventMapDirectory;
64 }
65
66
67
68
69 public void setEventMapDirectory(String eventMapPrefix) {
70 this.eventMapDirectory = eventMapPrefix;
71 }
72
73
74
75
76
77 public String getMessageStructureForEvent(String name, Version version) throws HL7Exception {
78 Map<String, String> p = getEventMapForVersion(version);
79 if (p == null) {
80
81
82 LOG.debug("No event map found for version " + version);
83 return name;
84
85
86
87 } else {
88 return p.get(name);
89 }
90 }
91
92
93
94
95
96
97
98
99
100
101
102 public Map<String, String> getEventMapForVersion(Version version) throws HL7Exception {
103 return getEventMap().get(version);
104 }
105
106
107
108
109
110
111
112
113
114 synchronized Map<Version, Map<String, String>> getEventMap() throws HL7Exception {
115 if (eventMap == null) {
116 try {
117 eventMap = loadMessageStructures();
118 } catch (IOException e) {
119 throw new HL7Exception("Could not load event map", e);
120 }
121 }
122 return eventMap;
123 }
124
125
126
127
128
129
130
131 protected Map<Version, Map<String, String>> loadMessageStructures() throws IOException {
132 Map<Version, Map<String, String>> map = new HashMap<>();
133 for (Version v : Version.values()) {
134 String resource = getEventMapDirectory() + v.getVersion() + ".properties";
135 InputStream in = getResource(resource);
136 if (in != null) {
137 try {
138 Properties structures = new Properties();
139 structures.load(in);
140
141 Map<String, String> structureMap = new HashMap<>();
142 for(Map.Entry<Object, Object> next : structures.entrySet()) {
143 structureMap.put((String)next.getKey(), (String)next.getValue());
144 }
145
146 map.put(v, Collections.unmodifiableMap(structureMap));
147 } finally {
148 in.close();
149 }
150 }
151 }
152 return map;
153 }
154
155 private InputStream getResource(String resource) {
156 InputStream in = null;
157 ClassLoader loader = Thread.currentThread().getContextClassLoader();
158 if (loader != null) {
159 in = loader.getResourceAsStream(resource);
160 }
161 if (in == null) {
162 loader = AbstractModelClassFactory.class.getClassLoader();
163 if (loader != null) {
164 in = loader.getResourceAsStream(resource);
165 }
166 }
167 if (in == null) {
168 in = ClassLoader.getSystemResourceAsStream(resource);
169 }
170 return in;
171 }
172
173 }