source: ccr/trunk/nhin-vista/projects/NHINC/Current/Product/Production/Gateway/SubscriptionRepository/src/gov/hhs/fha/nhinc/subscription/repository/manager/DataSaver.java@ 507

Last change on this file since 507 was 507, checked in by George Lilly, 15 years ago

NHIN gateway and adaptor for use on linux with VistA EHR and RPMS

File size: 3.9 KB
Line 
1package gov.hhs.fha.nhinc.subscription.repository.manager;
2
3import gov.hhs.fha.nhinc.subscription.repository.data.SubscriptionRecordList;
4import java.beans.XMLDecoder;
5import java.beans.XMLEncoder;
6import java.io.File;
7import java.io.FileInputStream;
8import java.io.FileNotFoundException;
9import java.io.FileOutputStream;
10import java.io.IOException;
11import org.apache.commons.logging.Log;
12import org.apache.commons.logging.LogFactory;
13
14/**
15 * Performs file operations for the subscription repository
16 *
17 * @author dunnek
18 */
19class 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}
Note: See TracBrowser for help on using the repository browser.