001    package ca.uhn.hl7v2.util;
002    
003    import java.io.*;
004    
005    /**
006     * Creates unique message IDs.  IDs are stored in a file called <hapi.home>/id_file for persistence
007     * across JVM sessions.  Note that if one day you run the JVM with a new working directory,
008     * you must move or copy id_file into this directory so that new ID numbers will begin
009     * with the last one used, rather than starting over again.
010     * @author Neal Acharya
011     */
012    public class MessageIDGenerator {
013        
014        private static MessageIDGenerator messageIdGenerator;
015        private long id;
016        private FileWriter fileW;
017        private static final String idFile = Home.getHomeDirectory().getAbsolutePath() + "/id_file";
018        
019        /**
020         * Constructor
021         * Creates an instance of the class
022         * Its reads an id (longint#) from an external file, if one is not present then the external file
023         * is created and initialized to zero.
024         * This id is stored into the private field of id.
025         */
026        private  MessageIDGenerator() throws IOException {
027            /*check external file for the last value unique id value generated by
028            this class*/
029            try{
030                // We should check to see if the external file for storing the unique ids exists
031                File extFile = new File(idFile);
032                if (extFile.createNewFile()== true){
033                    /*there was no existing file so a new one has been created with createNewFile method.  The
034                    file is stored at  <hapi.home>/id_file.txt */
035                    // We can simply initialize the private id field to zero
036                    id = 0;
037                    
038                }//end if
039                else{
040                    /*The file does exist which is why we received false from the
041                    createNewFile method. We should now try to read from this file*/
042                    FileReader fileR = new FileReader(idFile);
043                    char[] charArray = new char[100];
044                    int e = fileR.read(charArray);
045                    if (e <= 0){
046                        /*We know the file exists but it has no value stored in it. So at this point we can simply initialize the
047                        private id field to zero*/
048                        id = 0;
049                    }//end if
050                    else{
051                        /* Here we know that the file exists and has a stored value. we should read this value and set the
052                        private id field to it*/
053                        String idStr = String.valueOf(charArray);
054                        String idStrTrim = idStr.trim();
055                        id = Long.parseLong(idStrTrim);
056                    }//end else
057                    //Fix for bug 1100881:  Close the file after writing.
058                    fileR.close();
059                }//end else
060            }//end try
061            catch (FileNotFoundException e){
062                System.out.println(e);
063            }//end catch
064        }//end constructor code
065        
066        /**
067         * Synchronized method used to return the single (static) instance of the class
068         */
069        public static synchronized MessageIDGenerator getInstance() throws IOException {
070            if (messageIdGenerator == null)
071                messageIdGenerator = new MessageIDGenerator();
072            return messageIdGenerator;
073        }//end method
074        
075        /**
076         * Synchronized method used to return the incremented id value
077         */
078        public synchronized String getNewID()throws IOException{
079            //increment the private field
080            id = id + 1;
081            //create an instance of the Filewriter Object pointing to "C:\\extfiles\\Idfile.txt"
082            fileW = new FileWriter(idFile, false);
083            //write the id value to the file
084            String idStr = String.valueOf(id);
085            fileW.write(idStr);
086            fileW.flush();
087            fileW.close();
088            return String.valueOf(id);
089        }//end method
090        
091    }