001package ca.uhn.hl7v2.conf.store;
002
003import java.io.BufferedReader;
004import java.io.BufferedWriter;
005import java.io.File;
006import java.io.FileReader;
007import java.io.FileWriter;
008import java.io.IOException;
009
010import org.slf4j.Logger;
011import org.slf4j.LoggerFactory;
012
013/**
014 * Stores profiles in a local directory.  Profiles are stored as text
015 * in files named ID.xml (where ID is the profile ID).
016 * 
017 * @author Bryan Tripp
018 */
019public class FileProfileStore implements ProfileStore {
020    
021    private File root;
022    private static final Logger log = LoggerFactory.getLogger(FileProfileStore.class);
023    
024    /** Creates a new instance of FileProfileStore */
025    public FileProfileStore(String theFile) {
026        root = new File(theFile);
027        if (root.exists()) {
028                if (!root.isDirectory()) {
029                    log.warn("Profile store is not a directory (won't be able to retrieve any profiles): {}", theFile);
030                }
031        } else {
032                log.debug("Profile store directory doesn't exist: {}", theFile);
033        }
034    }
035    
036    /**
037     * Retrieves profile from persistent storage (by ID).  Returns null
038     * if the profile isn't found.
039     */
040    public String getProfile(String theID) throws IOException {
041        String profile = null;
042        
043        String fileName = getFileName(theID);
044                File source = new File(fileName);
045        if (!source.exists()) {
046                log.debug("File for profile {} doesn't exist: {}", theID, fileName);
047        } else if (source.isFile()) {
048            BufferedReader in = new BufferedReader(new FileReader(source));
049            char[] buf = new char[(int) source.length()];
050            int check = in.read(buf, 0, buf.length);
051            in.close();
052            if (check != buf.length)
053                throw new IOException("Only read " + check + " of " + buf.length
054                + " bytes of file " + source.getAbsolutePath());
055            profile = new String(buf);
056            log.debug("Got profile {}: \r\n {}", theID, profile);
057        }
058        return profile;
059    }
060    
061    /**
062     * Stores profile in persistent storage with given ID.
063     */
064    public void persistProfile(String ID, String profile) throws IOException {
065        if (!root.exists()) {
066                throw new IOException("Can't persist profile. Directory doesn't exist: " + root.getAbsolutePath());
067        }
068        if (!root.isDirectory()) {
069                throw new IOException("Can't persist profile. Not a directory: " + root.getAbsolutePath());
070        }
071        
072        File dest = new File(getFileName(ID));
073        BufferedWriter out = new BufferedWriter(new FileWriter(dest));
074        out.write(profile);
075        out.flush();
076        out.close();
077    }
078    
079    private String getFileName(String ID) {
080        return root.getAbsolutePath() + "/" + ID + ".xml";
081    }
082}