001: /*
002: * Copyright 2003-2005 Michael Franken, Zilverline.
003: *
004: * The contents of this file, or the files included with this file, are subject to
005: * the current version of ZILVERLINE Collaborative Source License for the
006: * Zilverline Search Engine (the "License"); You may not use this file except in
007: * compliance with the License.
008: *
009: * You may obtain a copy of the License at
010: *
011: * http://www.zilverline.org.
012: *
013: * See the License for the rights, obligations and
014: * limitations governing use of the contents of the file.
015: *
016: * The Original and Upgraded Code is the Zilverline Search Engine. The developer of
017: * the Original and Upgraded Code is Michael Franken. Michael Franken owns the
018: * copyrights in the portions it created. All Rights Reserved.
019: *
020: */
021:
022: package org.zilverline.dao.xstream;
023:
024: import java.io.File;
025: import java.io.FileReader;
026: import java.io.FileWriter;
027: import java.io.IOException;
028:
029: import com.thoughtworks.xstream.converters.ConversionException;
030:
031: import org.zilverline.dao.DAOException;
032: import org.zilverline.dao.SearchServiceDAO;
033: import org.zilverline.service.SearchServiceImpl;
034:
035: /**
036: * Implementation of DAO interface using XStream support.
037: *
038: * @author Michael Franken
039: * @version $Revision: 1.10 $
040: */
041: public class SearchServiceXStreamDAOImpl extends AbstractXStreamDAOImpl
042: implements SearchServiceDAO {
043: /**
044: * Create a DAO for persisting SearchService.
045: */
046: public SearchServiceXStreamDAOImpl() {
047: xstream.alias("searchService", SearchServiceImpl.class);
048: filename = "searchService.xml";
049: }
050:
051: /**
052: * Save the SearchService to the datastore.
053: *
054: * @param service the SearchService
055: * @throws DAOException if the service can not be stored
056: */
057: public final void store(final SearchServiceImpl service)
058: throws DAOException {
059: try {
060: writer = new FileWriter(new File(getBaseDir(), filename));
061: xstream.toXML(service, writer);
062: } catch (IOException e) {
063: throw new DAOException("Error saving SearchService", e);
064: } finally {
065: if (writer != null) {
066: try {
067: writer.close();
068: } catch (IOException e1) {
069: throw new DAOException("Error closing file writer",
070: e1);
071: }
072: }
073: }
074: }
075:
076: /**
077: * Retrieve the SearchService from store.
078: *
079: * @return SearchService the service
080: */
081: public final SearchServiceImpl load() {
082: SearchServiceImpl service = null;
083: try {
084: reader = new FileReader(new File(getBaseDir(), filename));
085: service = (SearchServiceImpl) xstream.fromXML(reader);
086: } catch (ConversionException e) {
087: log
088: .warn(
089: "Inconsistent SearchService data found. Possibly old version. Ignoring old data.",
090: e);
091: } catch (IOException e) {
092: log
093: .warn(
094: "No SearchService data found. Possibly first time zilverline runs.",
095: e);
096: } catch (Exception e) {
097: log.warn("Something went wrong, but the show must go on.",
098: e);
099: } finally {
100: if (reader != null) {
101: try {
102: reader.close();
103: } catch (IOException e1) {
104: log.warn("Can not close file reader", e1);
105: }
106: }
107: }
108: return service;
109: }
110: }
|