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.core.ExtractorFactory;
032: import org.zilverline.core.FileSystemCollection;
033: import org.zilverline.core.Handler;
034: import org.zilverline.dao.CollectionManagerDAO;
035: import org.zilverline.dao.DAOException;
036: import org.zilverline.service.CollectionManager;
037: import org.zilverline.service.CollectionManagerImpl;
038:
039: /**
040: * Implementation of CollectionManagerDAO interface using XStream.
041: *
042: * @author Michael Franken
043: * @version $Revision: 1.11 $
044: */
045: public class CollectionManagerXStreamDAOImpl extends
046: AbstractXStreamDAOImpl implements CollectionManagerDAO {
047:
048: /**
049: * Create a DAO for persisting CollectionManager.
050: */
051: public CollectionManagerXStreamDAOImpl() {
052: super ();
053: xstream.alias("collectionManager", CollectionManagerImpl.class);
054: xstream.alias("collection", FileSystemCollection.class);
055: filename = "collectionManager.xml";
056: }
057:
058: /**
059: * Save the CollectionManager to the datastore.
060: *
061: * @param manager the CollectionManager
062: * @throws DAOException if the manager can not be stored
063: */
064: public final void store(final CollectionManager manager)
065: throws DAOException {
066: try {
067: writer = new FileWriter(new File(getBaseDir(), filename));
068: xstream.toXML(manager, writer);
069: } catch (Exception e) {
070: throw new DAOException("Error saving collections", e);
071: } finally {
072: if (writer != null) {
073: try {
074: writer.close();
075: } catch (IOException e1) {
076: throw new DAOException("Error closing file writer",
077: e1);
078: }
079: }
080: }
081: }
082:
083: /**
084: * Retrieve the CollectionManager from store.
085: *
086: * @return CollectionManager the Manager
087: */
088: public final CollectionManager load() {
089: CollectionManager manager = null;
090: try {
091: reader = new FileReader(new File(getBaseDir(), filename));
092: manager = (CollectionManager) xstream.fromXML(reader);
093: log.debug("Reading " + manager.getCollections().size()
094: + " collections from store: "
095: + new File(getBaseDir(), filename));
096: } catch (ConversionException e) {
097: log
098: .warn(
099: "Inconsistent CollectionManager data found. Possibly old version. Ignoring old data.",
100: e);
101: } catch (IOException e) {
102: log
103: .warn(
104: "Can not read collections. Possibly first time zilverline runs.",
105: e);
106: } catch (Exception e) {
107: log.warn("Something went wrong, but the show must go on.",
108: e);
109: } finally {
110: if (reader != null) {
111: try {
112: reader.close();
113: } catch (IOException e1) {
114: log.warn("Can not close file reader", e1);
115: }
116: }
117: manager = checkManagerIntegrity(manager);
118: }
119: return manager;
120: }
121:
122: private CollectionManager checkManagerIntegrity(
123: CollectionManager manager) {
124: if (manager == null) {
125: log
126: .warn("Invalid collectionManager configuration found, resetting to defaults");
127: manager = new CollectionManagerImpl();
128: }
129: if (manager.getFactory() == null
130: || manager.getArchiveHandler() == null
131: || manager.getFactory().getMappings() == null
132: || manager.getFactory().getMimeMappings() == null
133: || manager.getArchiveHandler().getMappings() == null) {
134: log
135: .warn("Invalid collectionManager configuration found, resetting to defaults");
136: manager.setFactory(new ExtractorFactory());
137: manager.setArchiveHandler(new Handler());
138: }
139: return manager;
140: }
141: }
|