1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.id.uuid.state;
19
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.util.HashSet;
23 import java.util.Set;
24
25 import javax.xml.parsers.SAXParser;
26 import javax.xml.parsers.SAXParserFactory;
27
28 import org.xml.sax.Attributes;
29 import org.xml.sax.SAXException;
30 import org.xml.sax.helpers.DefaultHandler;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 public class ReadOnlyResourceStateImpl implements State {
46
47
48 static long synchronizeInterval = Long.MAX_VALUE;
49
50
51 static HashSet nodes = new HashSet();
52
53
54
55
56
57 public static final String CONFIG_FILENAME_KEY = "org.apache.commons.id.uuid.config.resource.filename";
58
59
60
61
62 public ReadOnlyResourceStateImpl() {
63 super();
64 }
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96 public void load() throws Exception {
97
98 String resourceName = System.getProperty(CONFIG_FILENAME_KEY);
99 if (resourceName == null) {
100 throw new IllegalStateException("No value set for system property: "
101 + CONFIG_FILENAME_KEY);
102 }
103
104
105 InputStream in = null;
106 try {
107 in = ClassLoader.getSystemResourceAsStream(resourceName);
108 if (in == null) {
109 throw new IllegalStateException(resourceName +
110 " loaded as system resource is null");
111 }
112
113 parse(in);
114 } finally {
115 if (in != null) {
116 try {
117 in.close();
118 } catch (IOException ioe) {
119
120 }
121 }
122 }
123 }
124
125
126
127
128 public long getSynchInterval() {
129
130 return Long.MAX_VALUE;
131 }
132
133
134
135
136 public Set getNodes() {
137 return nodes;
138 }
139
140
141
142
143 public void store(Set nodeSet) throws IOException {
144
145 return;
146 }
147
148
149
150
151 public void store(Set nodeSet, long timestamp) {
152
153 return;
154 }
155
156
157
158
159
160
161
162 protected void parse(InputStream in) throws Exception {
163 DefaultHandler handler = new StateConfigHandler();
164 SAXParserFactory saxFactory = SAXParserFactory.newInstance();
165 saxFactory.setValidating(true);
166 SAXParser parser = saxFactory.newSAXParser();
167 parser.parse(in, handler);
168 }
169
170
171
172
173
174 class StateConfigHandler extends DefaultHandler {
175
176 static final short UUID_STATE_TAG = 1;
177
178 static final String UUID_STATE_TAG_STR = "uuidstate";
179
180 static final String SYNCH_INTERVAL_STR = "synchinterval";
181
182 static final short NODE_TAG = 2;
183
184 static final String NODE_TAG_STR = "node";
185
186 static final String ATTR_ID_STR = "id";
187
188 static final String ATTR_CLOCKSEQ_STR = "clocksequence";
189
190 static final String ATTR_LASTIMESTAMP_STR = "timestamp";
191
192
193
194
195
196 public void startElement(
197 String namespaceURI,
198 String simpleName,
199 String qualifiedName,
200 Attributes attributes)
201 throws SAXException {
202
203 short currentTag = 0;
204
205 String element = simpleName;
206 if ("".equals(simpleName)) {
207 element = qualifiedName;
208 }
209 if (element.equalsIgnoreCase(UUID_STATE_TAG_STR)) {
210 currentTag = UUID_STATE_TAG;
211 } else if (element.equalsIgnoreCase(NODE_TAG_STR)) {
212 currentTag = NODE_TAG;
213 }
214
215 if (attributes != null) {
216 switch (currentTag) {
217 case 1 :
218 processBodyTag(attributes);
219 break;
220 case 2 :
221 processNodeTag(attributes);
222 break;
223 default :
224 break;
225 }
226 }
227 }
228
229
230
231
232
233
234 private void processBodyTag(Attributes attributes) {
235 for (int i = 0; i < attributes.getLength(); i++) {
236 String attributeName = attributes.getLocalName(i);
237 if ("".equals(attributeName)) {
238 attributeName = attributes.getQName(i);
239 }
240 String attributeValue = attributes.getValue(i);
241 if (attributeName.equalsIgnoreCase(SYNCH_INTERVAL_STR)) {
242 try {
243 synchronizeInterval = Long.parseLong(attributeValue);
244 } catch (NumberFormatException nfe) {
245 synchronizeInterval = 0;
246 }
247 }
248 }
249 }
250
251
252
253
254
255
256 private void processNodeTag(Attributes attributes) {
257 byte[] node = null;
258 long lastTS = 0;
259 short lastClockSeq = 0;
260 for (int i = 0; i < attributes.getLength(); i++) {
261 String attributeName = attributes.getLocalName(i);
262 if ("".equals(attributeName)) {
263 attributeName = attributes.getQName(i);
264 }
265 String attributeValue = attributes.getValue(i);
266
267 if (attributeName.equalsIgnoreCase(ATTR_ID_STR)) {
268 node = StateHelper.decodeMACAddress(attributeValue);
269 } else if (attributeName.equalsIgnoreCase(ATTR_CLOCKSEQ_STR)) {
270 try {
271 lastClockSeq = Short.parseShort(attributeValue);
272 } catch (NumberFormatException nfe) {
273 lastClockSeq = 0;
274 }
275 } else if ( attributeName.equalsIgnoreCase(ATTR_LASTIMESTAMP_STR)) {
276 try {
277 lastTS = Long.parseLong(attributeValue);
278 } catch (NumberFormatException nfe) {
279 lastTS = 0;
280 }
281 }
282 }
283 if (node != null) {
284 if (lastClockSeq != 0) {
285 nodes.add(new Node(node, lastTS, lastClockSeq));
286 } else {
287 nodes.add(new Node(node));
288 }
289 }
290 }
291 }
292
293 }