001/** 002The contents of this file are subject to the Mozilla Public License Version 1.1 003(the "License"); you may not use this file except in compliance with the License. 004You may obtain a copy of the License at http://www.mozilla.org/MPL/ 005Software distributed under the License is distributed on an "AS IS" basis, 006WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 007specific language governing rights and limitations under the License. 008 009The Original Code is "FileBasedGenerator.java". Description: 010"Replacement for the legagy MessageIDGenerator class" 011 012The Initial Developer of the Original Code is University Health Network. Copyright (C) 0132001. All Rights Reserved. 014 015Contributor(s): ______________________________________. 016 017Alternatively, the contents of this file may be used under the terms of the 018GNU General Public License (the "GPL"), in which case the provisions of the GPL are 019applicable instead of those above. If you wish to allow use of your version of this 020file only under the terms of the GPL and not to allow others to use your version 021of this file under the MPL, indicate your decision by deleting the provisions above 022and replace them with the notice and other provisions required by the GPL License. 023If you do not delete the provisions above, a recipient may use your version of 024this file under either the MPL or the GPL. 025 */ 026package ca.uhn.hl7v2.util.idgenerator; 027 028import java.io.BufferedReader; 029import java.io.File; 030import java.io.FileInputStream; 031import java.io.FileOutputStream; 032import java.io.IOException; 033import java.io.InputStreamReader; 034import java.io.PrintWriter; 035import java.util.concurrent.locks.ReentrantLock; 036 037import org.slf4j.Logger; 038import org.slf4j.LoggerFactory; 039 040import ca.uhn.hl7v2.util.Home; 041 042/** 043 * Replacement for {@link ca.uhn.hl7v2.util.MessageIDGenerator}. You should not use this class 044 * directly, however, but wrap it into a {@link DelegatingHiLoGenerator} generator. Its primary 045 * improvement over {@link ca.uhn.hl7v2.util.MessageIDGenerator} is that you can set path and file 046 * name. 047 * <p> 048 * Reading and writing to the file is thread-safe, however, you should not use the same file from 049 * different Java processes because no read/write locks are being checked. 050 */ 051public class FileBasedGenerator extends InMemoryIDGenerator { 052 053 private static final Logger LOG = LoggerFactory.getLogger(FileBasedGenerator.class.getName()); 054 055 private String directory = Home.getHomeDirectory().getAbsolutePath(); 056 private String fileName = "id_file"; 057 private boolean neverFail = true; 058 private boolean used = false; 059 private boolean minimizeReads = false; 060 private ReentrantLock lock = new ReentrantLock(); 061 062 public FileBasedGenerator() { 063 this(1L); 064 } 065 066 FileBasedGenerator(long increment) { 067 super(increment); 068 } 069 070 public String getID() throws IOException { 071 try { 072 lock.lock(); 073 074 // If ID is 0, read initial value from file if possible 075 if (!minimizeReads || !used) { 076 long readInitialValue = readInitialValue(getFilePath()); 077 if (readInitialValue >= 0) { 078 set(readInitialValue); 079 } 080 used = true; 081 } 082 083 String id = super.getID(); 084 // The id held in the file is always <increment> larger so that 085 // the ID is still unique after a restart. 086 writeNextValue(Long.parseLong(id) + getIncrement()); 087 return id; 088 } finally { 089 lock.unlock(); 090 } 091 } 092 093 094 private void writeNextValue(long id) throws IOException { 095 PrintWriter pw = null; 096 try { 097 pw = new PrintWriter(new FileOutputStream(getFilePath(), false)); 098 pw.println(Long.toString(id)); 099 } catch (IOException e) { 100 if (neverFail) { 101 LOG.warn("Could not write ID to file {}, going to use internal ID generator. {}", 102 getFilePath(), e.getMessage()); 103 return; 104 } 105 throw e; 106 } finally { 107 if (pw != null) 108 pw.close(); 109 } 110 } 111 112 private long readInitialValue(String path) throws IOException { 113 BufferedReader br = null; 114 String id = null; 115 try { 116 br = new BufferedReader(new InputStreamReader(new FileInputStream(path))); 117 id = br.readLine(); 118 return Long.parseLong(id); 119 } catch (IOException e) { 120 LOG.info("Could not read ID file {} ", path); 121 if (!neverFail) { 122 throw e; 123 } 124 return -1; 125 } catch (NumberFormatException e) { 126 LOG.info("ID {} read from file is not a number", id); 127 return -1; 128 } finally { 129 if (br != null) 130 try { 131 br.close(); 132 } catch (IOException e) { 133 } 134 } 135 } 136 137 private String getFilePath() { 138 return new File(directory, fileName).getAbsolutePath(); 139 } 140 141 public void setDirectory(String directory) { 142 try { 143 lock.lock(); 144 this.directory = directory; 145 } finally { 146 lock.unlock(); 147 } 148 } 149 150 public void setFileName(String fileName) { 151 try { 152 lock.lock(); 153 this.fileName = fileName; 154 } finally { 155 lock.unlock(); 156 } 157 } 158 159 /** 160 * If set to <code>true</code> (default is <code>false</code>) the generator 161 * minimizes the number of disk reads by caching the last read value. This means 162 * one less disk read per X number of IDs generated, but also means that multiple 163 * instances of this generator may clobber each other's values. 164 */ 165 public void setMinimizeReads(boolean theMinimizeReads) { 166 minimizeReads = theMinimizeReads; 167 } 168 169 /** 170 * If set to <code>false</code> (default is <code>true</code>), 171 * retrieving a new ID may fail if the ID file in the home 172 * directory can not be written/read. If set to true, failures 173 * will be ignored, which means that IDs may be repeated after 174 * a JVM restart. 175 */ 176 public void setNeverFail(boolean neverFail) { 177 this.neverFail = neverFail; 178 } 179 180 public void reset() { 181 try { 182 lock.lock(); 183 super.reset(); 184 writeNextValue(0l); 185 } catch (IOException e) { 186 throw new IllegalStateException("Cannot initialize persistent ID generator", e); 187 } finally { 188 lock.unlock(); 189 } 190 } 191 192}