Coverage Report - ca.uhn.hl7v2.conf.store.FileProfileStore
 
Classes in this File Line Coverage Branch Coverage Complexity
FileProfileStore
82%
28/34
42%
6/14
3.5
 
 1  
 package ca.uhn.hl7v2.conf.store;
 2  
 
 3  
 import java.io.BufferedReader;
 4  
 import java.io.BufferedWriter;
 5  
 import java.io.File;
 6  
 import java.io.FileReader;
 7  
 import java.io.FileWriter;
 8  
 import java.io.IOException;
 9  
 
 10  
 import org.slf4j.Logger;
 11  
 import org.slf4j.LoggerFactory;
 12  
 
 13  
 /**
 14  
  * Stores profiles in a local directory.  Profiles are stored as text
 15  
  * in files named ID.xml (where ID is the profile ID).
 16  
  * 
 17  
  * @author Bryan Tripp
 18  
  */
 19  
 public class FileProfileStore implements ProfileStore {
 20  
     
 21  
     private File root;
 22  1
     private static final Logger log = LoggerFactory.getLogger(FileProfileStore.class);
 23  
     
 24  
     /** Creates a new instance of FileProfileStore */
 25  1
     public FileProfileStore(String theFile) {
 26  1
         root = new File(theFile);
 27  1
         if (root.exists()) {
 28  0
                 if (!root.isDirectory()) {
 29  0
                     log.warn("Profile store is not a directory (won't be able to retrieve any profiles): {}", theFile);
 30  
                 }
 31  
         } else {
 32  1
                 log.debug("Profile store directory doesn't exist: {}", theFile);
 33  
         }
 34  1
     }
 35  
     
 36  
     /**
 37  
      * Retrieves profile from persistent storage (by ID).  Returns null
 38  
      * if the profile isn't found.
 39  
      */
 40  
     public String getProfile(String theID) throws IOException {
 41  6
         String profile = null;
 42  
         
 43  6
         String fileName = getFileName(theID);
 44  6
                 File source = new File(fileName);
 45  6
         if (!source.exists()) {
 46  0
                 log.debug("File for profile {} doesn't exist: {}", theID, fileName);
 47  6
         } else if (source.isFile()) {
 48  6
             BufferedReader in = new BufferedReader(new FileReader(source));
 49  6
             char[] buf = new char[(int) source.length()];
 50  6
             int check = in.read(buf, 0, buf.length);
 51  6
             in.close();
 52  6
             if (check != buf.length)
 53  0
                 throw new IOException("Only read " + check + " of " + buf.length
 54  
                 + " bytes of file " + source.getAbsolutePath());
 55  6
             profile = new String(buf);
 56  6
             log.debug("Got profile {}: \r\n {}", theID, profile);
 57  
         }
 58  6
         return profile;
 59  
     }
 60  
     
 61  
     /**
 62  
      * Stores profile in persistent storage with given ID.
 63  
      */
 64  
     public void persistProfile(String ID, String profile) throws IOException {
 65  1
         if (!root.exists()) {
 66  0
                 throw new IOException("Can't persist profile. Directory doesn't exist: " + root.getAbsolutePath());
 67  
         }
 68  1
         if (!root.isDirectory()) {
 69  0
                 throw new IOException("Can't persist profile. Not a directory: " + root.getAbsolutePath());
 70  
         }
 71  
             
 72  1
             File dest = new File(getFileName(ID));
 73  1
         BufferedWriter out = new BufferedWriter(new FileWriter(dest));
 74  1
         out.write(profile);
 75  1
         out.flush();
 76  1
         out.close();
 77  1
     }
 78  
     
 79  
     private String getFileName(String ID) {
 80  7
         return root.getAbsolutePath() + "/" + ID + ".xml";
 81  
     }
 82  
 }