1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.zilverline.dao.xstream;
23
24 import java.io.File;
25 import java.io.FileReader;
26 import java.io.FileWriter;
27 import java.io.IOException;
28
29 import com.thoughtworks.xstream.converters.ConversionException;
30
31 import org.zilverline.dao.DAOException;
32 import org.zilverline.dao.IndexServiceDAO;
33 import org.zilverline.service.IndexServiceImpl;
34
35 /***
36 * Implementation of DAO interface using XStream support.
37 *
38 * @author Michael Franken
39 */
40 public class IndexServiceXStreamDAOImpl extends AbstractXStreamDAOImpl implements IndexServiceDAO {
41 /***
42 * Create a DAO for persisting IndexService.
43 */
44 public IndexServiceXStreamDAOImpl() {
45 xstream.alias("IndexService", IndexServiceImpl.class);
46 filename = "indexService.xml";
47 }
48
49 /***
50 * Save the IndexService to the datastore.
51 *
52 * @param service the IndexService
53 * @throws DAOException if the service can not be stored
54 */
55 public final void store(IndexServiceImpl service) throws DAOException {
56 try {
57 writer = new FileWriter(new File(getBaseDir(), filename));
58 xstream.toXML(service, writer);
59 }
60 catch (IOException e) {
61 throw new DAOException("Error saving IndexService", e);
62 }
63 finally {
64 if (writer != null) {
65 try {
66 writer.close();
67 }
68 catch (IOException e1) {
69 throw new DAOException("Error closing file writer", e1);
70 }
71 }
72 }
73 }
74
75 /***
76 * Retrieve the IndexService from store.
77 *
78 * @return IndexService the service
79 */
80 public final IndexServiceImpl load() {
81 IndexServiceImpl service = null;
82 try {
83 reader = new FileReader(new File(getBaseDir(), filename));
84 service = (IndexServiceImpl) xstream.fromXML(reader);
85 }
86 catch (ConversionException e) {
87 log.warn("Inconsistent IndexService data found. Possibly old version. Ignoring old data.", e);
88 }
89 catch (IOException e) {
90 log.warn("No IndexService data found. Possibly first time zilverline runs.", e);
91 }
92 catch (Exception e) {
93 log.warn("Something went wrong, but the show must go on.", e);
94 }
95 finally {
96 if (reader != null) {
97 try {
98 reader.close();
99 }
100 catch (IOException e1) {
101 log.warn("Can not close file reader", e1);
102 }
103 }
104 }
105 return service;
106 }
107 }