View Javadoc

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  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          writeXML(genXML(nodes));
45      }
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          Iterator it = nodes.iterator();
56          StringBuffer buf = new StringBuffer(1024);
57          buf.append(StateHelper.XML_DOC_START);
58          buf.append(getSynchInterval());
59          buf.append(StateHelper.XML_DOC_START_END);
60          while (it.hasNext()) {
61              Node n = (Node) it.next();
62              buf.append(StateHelper.XML_NODE_TAG_START);
63              buf.append(StateHelper.encodeMACAddress(n.getNodeIdentifier()));
64              buf.append(StateHelper.XML_NODE_TAG_AFTER_ID);
65              buf.append(n.getClockSequence());
66              buf.append(StateHelper.XML_NODE_TAG_AFTER_CSEQ);
67              buf.append(n.getLastTimestamp());
68              buf.append(StateHelper.XML_NODE_TAG_END);
69          }
70          buf.append(StateHelper.XML_DOC_END);
71          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          Iterator it = nodes.iterator();
84          StringBuffer buf = new StringBuffer(1024);
85          buf.append(StateHelper.XML_DOC_START);
86          buf.append(getSynchInterval());
87          buf.append(StateHelper.XML_DOC_START_END);
88          while (it.hasNext()) {
89              Node n = (Node) it.next();
90              buf.append(StateHelper.XML_NODE_TAG_START);
91              buf.append(StateHelper.encodeMACAddress(n.getNodeIdentifier()));
92              buf.append(StateHelper.XML_NODE_TAG_AFTER_ID);
93              buf.append(n.getClockSequence());
94              buf.append(StateHelper.XML_NODE_TAG_AFTER_CSEQ);
95              buf.append(timestamp);
96              buf.append(StateHelper.XML_NODE_TAG_END);
97          }
98          buf.append(StateHelper.XML_DOC_END);
99          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         String resourceName = System.getProperty(CONFIG_FILENAME_KEY);
109         if (resourceName == null) {
110             return;
111         } else {
112             URL rUrl = ClassLoader.getSystemResource(resourceName);
113             if (rUrl != null) {
114                 File file = new File(rUrl.getFile());
115                 if (file != null && file.canWrite()) {
116                     FileWriter fw = null;
117                     try {
118                         fw = new FileWriter(file);
119                         fw.write(xml);
120                         fw.close();
121                     } catch (IOException ioe) {
122                         //@TODO log it?
123                     } finally {
124                         try {
125                             fw.close();
126                         } catch (IOException ioee) {
127                             ; //Nothing to do.
128                         }
129                         fw = null;
130                         file = null;
131                     }
132                 }
133             }
134         }
135     }
136 }