1 package ca.uhn.hl7v2.util;
2
3 import java.io.File;
4 import java.io.FileNotFoundException;
5 import java.io.FileReader;
6 import java.io.FileWriter;
7 import java.io.IOException;
8
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
12 import ca.uhn.hl7v2.util.idgenerator.FileBasedHiLoGenerator;
13
14 /**
15 * <p>
16 * Creates unique message IDs. IDs are stored in a file called {@link Home#getHomeDirectory() hapi.home}/id_file for persistence
17 * across JVM sessions. Note that if one day you run the JVM with a new working directory,
18 * you must move or copy id_file into this directory so that new ID numbers will begin
19 * with the last one used, rather than starting over again.
20 * </p>
21 * <p>
22 * Note that as of HAPI 2.0, by default this class will not fail even if the id_file can
23 * not be read/written. In this case, HAPI will try to fail gracefully by simply generating
24 * a numeric sequence starting at zero. This behaviour can be overwritten using
25 * {@link #NEVER_FAIL_PROPERTY}
26 * </p>
27 * Also consider using {@link FileBasedHiLoGenerator} which provides better performance
28 *
29 *
30 * @author Neal Acharya
31 * @deprecated use one of the IDGenerator implementations
32 */
33 public class MessageIDGenerator {
34
35 private static final Logger ourLog = LoggerFactory.getLogger(MessageIDGenerator.class.getName());
36 private static MessageIDGenerator messageIdGenerator;
37
38 /**
39 * Contains the complete path to the default ID file, which is a plain text file containing
40 * the number corresponding to the last generated ID
41 */
42 public final static String DEFAULT_ID_FILE = Home.getHomeDirectory().getAbsolutePath() + "/id_file";
43
44 /**
45 * System property key which indicates that this class should never fail. If this
46 * system property is set to false (default is true), as in the following code:<br>
47 * <code>System.setProperty(MessageIDGenerator.NEVER_FAIL_PROPERTY, Boolean.FALSE.toString());</code><br>
48 * this class will fail if the underlying disk file can not be
49 * read or written. This means you are roughly guaranteed a unique
50 * ID number between JVM sessions (barring the file getting lost or corrupted).
51 */
52 public static final String NEVER_FAIL_PROPERTY = MessageIDGenerator.class.getName() + "_NEVER_FAIL_PROPERTY";
53
54 private long id;
55 private FileWriter fileW;
56
57 /**
58 * Constructor
59 * Creates an instance of the class
60 * Its reads an id (longint#) from an external file, if one is not present then the external file
61 * is created and initialized to zero.
62 * This id is stored into the private field of id.
63 */
64 private MessageIDGenerator() throws IOException {
65 initialize();
66 }//end constructor code
67
68
69 /**
70 * Force the generator to re-load the ID file and initialize itself.
71 *
72 * This method is mostly provided as a convenience to unit tests, and does
73 * not normally need to be called.
74 */
75 void initialize() throws IOException {
76 id = 0;
77
78 /*check external file for the last value unique id value generated by
79 this class*/
80 try{
81 // We should check to see if the external file for storing the unique ids exists
82 File extFile = new File(DEFAULT_ID_FILE);
83 if (extFile.createNewFile()){
84 /*there was no existing file so a new one has been created with createNewFile method. The
85 file is stored at <hapi.home>/id_file.txt */
86 // We can simply initialize the private id field to zero
87 id = 0;
88
89 }//end if
90 else{
91 /*The file does exist which is why we received false from the
92 createNewFile method. We should now try to read from this file*/
93 FileReader fileR = new FileReader(DEFAULT_ID_FILE);
94 char[] charArray = new char[100];
95 int e = fileR.read(charArray);
96 if (e <= 0){
97 /*We know the file exists but it has no value stored in it. So at this point we can simply initialize the
98 private id field to zero*/
99 id = 0;
100 }//end if
101 else{
102 /* Here we know that the file exists and has a stored value. we should read this value and set the
103 private id field to it*/
104 String idStr = String.valueOf(charArray);
105 String idStrTrim = idStr.trim();
106
107 try {
108 id = Long.parseLong(idStrTrim);
109 } catch (NumberFormatException nfe) {
110 ourLog.warn("Failed to parse message ID file value \"" + idStrTrim + "\". Defaulting to 0.");
111 }
112
113 }//end else
114 //Fix for bug 1100881: Close the file after writing.
115 fileR.close();
116 }//end else
117 } catch (FileNotFoundException e) {
118 ourLog.error("Failed to locate message ID file. Message was: {}", e.getMessage());
119 } catch (IOException e) {
120 System.getProperty(NEVER_FAIL_PROPERTY, Boolean.TRUE.toString());
121 throw e;
122 }
123 }
124
125 /**
126 * Synchronized method used to return the single (static) instance of the class
127 */
128 public static synchronized MessageIDGenerator getInstance() throws IOException {
129 if (messageIdGenerator == null)
130 messageIdGenerator = new MessageIDGenerator();
131 return messageIdGenerator;
132 }//end method
133
134 /**
135 * Synchronized method used to return the incremented id value
136 */
137 public synchronized String getNewID() throws IOException{
138 try {
139 //increment the private field
140 id = id + 1;
141 //write the id value to the file
142 String idStr = String.valueOf(id);
143
144 //create an instance of the Filewriter Object pointing to "C:\\extfiles\\Idfile.txt"
145 fileW = new FileWriter(DEFAULT_ID_FILE, false);
146 fileW.write(idStr);
147 fileW.flush();
148 fileW.close();
149 } catch (FileNotFoundException e) {
150 System.getProperty(NEVER_FAIL_PROPERTY, Boolean.TRUE.toString());
151 } catch (IOException e) {
152 System.getProperty(NEVER_FAIL_PROPERTY, Boolean.TRUE.toString());
153 throw e;
154 }
155 return String.valueOf(id);
156 }//end method
157
158 }