Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ReadWriteFileStateImpl |
|
| 3.0;3 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | * contributor license agreements. See the NOTICE file distributed with | |
4 | * this work for additional information regarding copyright ownership. | |
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
6 | * (the "License"); you may not use this file except in compliance with | |
7 | * the License. You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | ||
18 | package org.apache.commons.id.uuid.state; | |
19 | ||
20 | import java.io.File; | |
21 | import java.io.FileWriter; | |
22 | import java.io.IOException; | |
23 | import java.net.URL; | |
24 | import java.util.Iterator; | |
25 | import java.util.Set; | |
26 | ||
27 | /** | |
28 | * <p>The <code>ReadWriteFileStateImpl</code> is an implementation of the | |
29 | * <code>State</code> interface. This implementation provides better guarantees | |
30 | * that no duplicate UUID's will be generated since the last time stamp, and | |
31 | * last clock sequence are stored to a persistent file. | |
32 | * | |
33 | * @author Commons-Id team | |
34 | * @version $Id: ReadWriteFileStateImpl.java 480488 2006-11-29 08:57:26Z bayard $ | |
35 | */ | |
36 | 3 | public class ReadWriteFileStateImpl extends ReadOnlyResourceStateImpl implements State { |
37 | ||
38 | /** | |
39 | * Persists the UUID generator state to file. | |
40 | * | |
41 | * @see org.apache.commons.id.uuid.state.State#store(java.util.Set) | |
42 | */ | |
43 | public void store(Set nodes) throws IOException { | |
44 | 5 | writeXML(genXML(nodes)); |
45 | 5 | } |
46 | ||
47 | /** | |
48 | * Returns an XML string of the node Set. | |
49 | * | |
50 | * @param nodes the Set to create xml for. | |
51 | * @return an XML string of the node Set. | |
52 | * @throws IOException an IOException. | |
53 | */ | |
54 | private String genXML(Set nodes) throws IOException { | |
55 | 5 | Iterator it = nodes.iterator(); |
56 | 5 | StringBuffer buf = new StringBuffer(1024); |
57 | 5 | buf.append(StateHelper.XML_DOC_START); |
58 | 5 | buf.append(getSynchInterval()); |
59 | 5 | buf.append(StateHelper.XML_DOC_START_END); |
60 | 15 | while (it.hasNext()) { |
61 | 10 | Node n = (Node) it.next(); |
62 | 10 | buf.append(StateHelper.XML_NODE_TAG_START); |
63 | 10 | buf.append(StateHelper.encodeMACAddress(n.getNodeIdentifier())); |
64 | 10 | buf.append(StateHelper.XML_NODE_TAG_AFTER_ID); |
65 | 10 | buf.append(n.getClockSequence()); |
66 | 10 | buf.append(StateHelper.XML_NODE_TAG_AFTER_CSEQ); |
67 | 10 | buf.append(n.getLastTimestamp()); |
68 | 10 | buf.append(StateHelper.XML_NODE_TAG_END); |
69 | 10 | } |
70 | 5 | buf.append(StateHelper.XML_DOC_END); |
71 | 5 | return buf.toString(); |
72 | } | |
73 | ||
74 | /** | |
75 | * Returns an XML string of the node Set using a predetermined last time stamp for all <code>Node</code>s. | |
76 | * | |
77 | * @param nodes the Set to create xml for. | |
78 | * @param timestamp the timestamp to write for all nodes. | |
79 | * @return an XML string of the node Set. | |
80 | * @throws IOException an IOException. | |
81 | */ | |
82 | private String genXML(Set nodes, long timestamp) throws IOException { | |
83 | 0 | Iterator it = nodes.iterator(); |
84 | 0 | StringBuffer buf = new StringBuffer(1024); |
85 | 0 | buf.append(StateHelper.XML_DOC_START); |
86 | 0 | buf.append(getSynchInterval()); |
87 | 0 | buf.append(StateHelper.XML_DOC_START_END); |
88 | 0 | while (it.hasNext()) { |
89 | 0 | Node n = (Node) it.next(); |
90 | 0 | buf.append(StateHelper.XML_NODE_TAG_START); |
91 | 0 | buf.append(StateHelper.encodeMACAddress(n.getNodeIdentifier())); |
92 | 0 | buf.append(StateHelper.XML_NODE_TAG_AFTER_ID); |
93 | 0 | buf.append(n.getClockSequence()); |
94 | 0 | buf.append(StateHelper.XML_NODE_TAG_AFTER_CSEQ); |
95 | 0 | buf.append(timestamp); |
96 | 0 | buf.append(StateHelper.XML_NODE_TAG_END); |
97 | 0 | } |
98 | 0 | buf.append(StateHelper.XML_DOC_END); |
99 | 0 | return buf.toString(); |
100 | } | |
101 | ||
102 | /** | |
103 | * <p>Writes the XML String to the file system.</p> | |
104 | * | |
105 | * @param xml the xml string to write. | |
106 | */ | |
107 | private void writeXML(String xml) { | |
108 | 5 | String resourceName = System.getProperty(CONFIG_FILENAME_KEY); |
109 | 5 | if (resourceName == null) { |
110 | 0 | return; |
111 | } else { | |
112 | 5 | URL rUrl = ClassLoader.getSystemResource(resourceName); |
113 | 5 | if (rUrl != null) { |
114 | 5 | File file = new File(rUrl.getFile()); |
115 | 5 | if (file != null && file.canWrite()) { |
116 | 5 | FileWriter fw = null; |
117 | try { | |
118 | 5 | fw = new FileWriter(file); |
119 | 5 | fw.write(xml); |
120 | 5 | fw.close(); |
121 | 0 | } catch (IOException ioe) { |
122 | //@TODO log it? | |
123 | } finally { | |
124 | 0 | try { |
125 | 5 | fw.close(); |
126 | 0 | } catch (IOException ioee) { |
127 | ; //Nothing to do. | |
128 | 5 | } |
129 | 5 | fw = null; |
130 | 5 | file = null; |
131 | 5 | } |
132 | } | |
133 | } | |
134 | } | |
135 | 5 | } |
136 | } |