001 package ca.uhn.hl7v2.conf.store; 002 003 import java.io.*; 004 import ca.uhn.hl7v2.conf.ProfileException; 005 import ca.uhn.log.*; 006 007 /** 008 * Stores profiles in a local directory. Profiles are stored as text 009 * in files named ID.xml (where ID is the profile ID). 010 * @author Bryan Tripp 011 */ 012 public class FileProfileStore implements ProfileStore { 013 014 private File root; 015 private static final HapiLog log = HapiLogFactory.getHapiLog(FileProfileStore.class); 016 017 /** Creates a new instance of FileProfileStore */ 018 public FileProfileStore(String file) { 019 root = new File(file); 020 if (!root.isDirectory()) 021 if (!root.mkdirs()) 022 throw new IllegalArgumentException(file + " is not a directory"); 023 } 024 025 /** 026 * Retrieves profile from persistent storage (by ID). Returns null 027 * if the profile isn't found. 028 */ 029 public String getProfile(String ID) throws IOException { 030 String profile = null; 031 032 File source = new File(getFileName(ID)); 033 if (source.isFile()) { 034 BufferedReader in = new BufferedReader(new FileReader(source)); 035 char[] buf = new char[(int) source.length()]; 036 int check = in.read(buf, 0, buf.length); 037 in.close(); 038 if (check != buf.length) 039 throw new IOException("Only read " + check + " of " + buf.length 040 + " bytes of file " + source.getAbsolutePath()); 041 profile = new String(buf); 042 } 043 log.info("Got profile " + ID + ": \r\n" + profile); 044 return profile; 045 } 046 047 /** 048 * Stores profile in persistent storage with given ID. 049 */ 050 public void persistProfile(String ID, String profile) throws IOException { 051 File dest = new File(getFileName(ID)); 052 BufferedWriter out = new BufferedWriter(new FileWriter(dest)); 053 out.write(profile); 054 out.flush(); 055 out.close(); 056 } 057 058 private String getFileName(String ID) { 059 return root.getAbsolutePath() + "/" + ID + ".xml"; 060 } 061 }