[507] | 1 | package gov.hhs.fha.nhinc.subscription.repository.manager;
|
---|
| 2 |
|
---|
| 3 | import gov.hhs.fha.nhinc.subscription.repository.data.SubscriptionRecordList;
|
---|
| 4 | import java.beans.XMLDecoder;
|
---|
| 5 | import java.beans.XMLEncoder;
|
---|
| 6 | import java.io.File;
|
---|
| 7 | import java.io.FileInputStream;
|
---|
| 8 | import java.io.FileNotFoundException;
|
---|
| 9 | import java.io.FileOutputStream;
|
---|
| 10 | import java.io.IOException;
|
---|
| 11 | import org.apache.commons.logging.Log;
|
---|
| 12 | import org.apache.commons.logging.LogFactory;
|
---|
| 13 |
|
---|
| 14 | /**
|
---|
| 15 | * Performs file operations for the subscription repository
|
---|
| 16 | *
|
---|
| 17 | * @author dunnek
|
---|
| 18 | */
|
---|
| 19 | class DataSaver
|
---|
| 20 | {
|
---|
| 21 | private static Log log = LogFactory.getLog(DataSaver.class);
|
---|
| 22 |
|
---|
| 23 | /**
|
---|
| 24 | * Save a subscription list using the provided file name
|
---|
| 25 | *
|
---|
| 26 | * @param list Subscription list
|
---|
| 27 | * @param file File name to use when saving the file
|
---|
| 28 | */
|
---|
| 29 | public void saveList(SubscriptionRecordList list, String file)
|
---|
| 30 | {
|
---|
| 31 | log.info("Saving " + list.size() + " items(s)");
|
---|
| 32 |
|
---|
| 33 | // Create output stream.
|
---|
| 34 | log.info("Filename=" + file);
|
---|
| 35 |
|
---|
| 36 | FileOutputStream fos;
|
---|
| 37 |
|
---|
| 38 | try
|
---|
| 39 | {
|
---|
| 40 | fos = new FileOutputStream(file);
|
---|
| 41 |
|
---|
| 42 | try
|
---|
| 43 | {
|
---|
| 44 | // Create XML encoder.
|
---|
| 45 | XMLEncoder xenc = new XMLEncoder(fos);
|
---|
| 46 | try
|
---|
| 47 | {
|
---|
| 48 | // Write object.
|
---|
| 49 | xenc.writeObject(list);
|
---|
| 50 | xenc.flush();
|
---|
| 51 | }
|
---|
| 52 | finally
|
---|
| 53 | {
|
---|
| 54 | xenc.close();
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 | finally
|
---|
| 58 | {
|
---|
| 59 | try
|
---|
| 60 | {
|
---|
| 61 | fos.close();
|
---|
| 62 | }
|
---|
| 63 | catch (IOException ex)
|
---|
| 64 | {
|
---|
| 65 | log.info("Could not close " + file + ": " + ex.getMessage());
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 | catch (FileNotFoundException ex)
|
---|
| 70 | {
|
---|
| 71 | log.error("Error accessing storage " + file + ": " + ex.getMessage());
|
---|
| 72 | }
|
---|
| 73 | log.info("Save complete");
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | /**
|
---|
| 77 | * Load a subscription list from the provided file name
|
---|
| 78 | *
|
---|
| 79 | * @param fileName Name of the file containing the subscription list
|
---|
| 80 | * @return Subscription list
|
---|
| 81 | */
|
---|
| 82 | public SubscriptionRecordList loadList(String fileName)
|
---|
| 83 | {
|
---|
| 84 | log.info("Loading list");
|
---|
| 85 |
|
---|
| 86 | SubscriptionRecordList subscriptionListlist = new SubscriptionRecordList();
|
---|
| 87 | File file;
|
---|
| 88 |
|
---|
| 89 | // Create input stream.
|
---|
| 90 | log.info("Filename=" + fileName);
|
---|
| 91 | log.info("user.dir: " + System.getProperty("user.dir"));
|
---|
| 92 |
|
---|
| 93 | file = new File(fileName);
|
---|
| 94 | XMLDecoder xdec = null;
|
---|
| 95 | FileInputStream fis = null;
|
---|
| 96 |
|
---|
| 97 | try
|
---|
| 98 | {
|
---|
| 99 | if (!file.exists())
|
---|
| 100 | {
|
---|
| 101 | //fileName does not exist, so create it
|
---|
| 102 | //for testing purposes
|
---|
| 103 | //for testing purposes
|
---|
| 104 | file.createNewFile();
|
---|
| 105 | saveList(new SubscriptionRecordList(), fileName);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | fis = new FileInputStream(fileName);
|
---|
| 109 |
|
---|
| 110 | // Create XML decode.
|
---|
| 111 | xdec = new XMLDecoder(fis);
|
---|
| 112 |
|
---|
| 113 | // Write object.
|
---|
| 114 | log.info("Loading object");
|
---|
| 115 | Object obj = xdec.readObject();
|
---|
| 116 | if (obj instanceof SubscriptionRecordList)
|
---|
| 117 | {
|
---|
| 118 | subscriptionListlist = (SubscriptionRecordList) obj;
|
---|
| 119 | }
|
---|
| 120 | else
|
---|
| 121 | {
|
---|
| 122 | log.debug("Loaded object was not of expected type - SubscriptionListlist - default used");
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | }
|
---|
| 126 | catch (IOException ex)
|
---|
| 127 | {
|
---|
| 128 | log.error("Error accessing storage " + fileName + ": " + ex.getMessage());
|
---|
| 129 | }
|
---|
| 130 | finally
|
---|
| 131 | {
|
---|
| 132 | try
|
---|
| 133 | {
|
---|
| 134 | xdec.close();
|
---|
| 135 | fis.close();
|
---|
| 136 | }
|
---|
| 137 | catch (IOException ex)
|
---|
| 138 | {
|
---|
| 139 | log.info("Unable to close streams: " + ex.getMessage());
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | log.info("Loaded " + subscriptionListlist.size() + " subscription(s)");
|
---|
| 144 | return subscriptionListlist;
|
---|
| 145 | }
|
---|
| 146 | }
|
---|