001    /**
002     * 
003     */
004    package org.wdssii.core;
005    
006    import java.io.File;
007    import java.io.FileInputStream;
008    import java.io.InputStream;
009    import java.util.Set;
010    import java.util.zip.GZIPInputStream;
011    
012    import javax.xml.parsers.SAXParser;
013    import javax.xml.parsers.SAXParserFactory;
014    
015    import org.apache.commons.logging.Log;
016    import org.apache.commons.logging.LogFactory;
017    import org.wdssii.core.fam.FamIndexHelper;
018    import org.wdssii.core.fam.FamIndexHelperFactory;
019    import org.wdssii.core.fam.FamIndexHelperInotifyImpl;
020    import org.wdssii.core.fam.FamIndexHelperLsImpl;
021    
022    /**
023     * Index that reads a code_index.fam listing
024     * 
025     * @author lakshman
026     * 
027     */
028    public class FamIndex extends Index {
029            private static Log log = LogFactory.getLog(FamIndex.class);
030    
031            private boolean initComplete = false;
032            private final File indexDir;
033            private final FamIndexHelper helper;
034            private final SAXParser saxParser;
035    
036            /** meant for prototype factory use only. */
037            public FamIndex() {
038                    super(null, null);
039                    indexDir = null;
040                    helper = null;
041                    saxParser = null;
042            }
043    
044            public void update() {
045                    if (!initComplete) {
046                            return;
047                    }
048                    File[] files = helper.getNewFiles();
049                    for (File file : files) {
050                            addRecord(file);
051                    }
052            }
053            
054            public FamIndex(File path, Set<IndexRecordListener> listeners)
055                            throws DataUnavailableException {
056                    super(path.getParent(), listeners);
057                    this.indexDir = path;
058                    try {
059                            if (log.isInfoEnabled()) {
060                                    log.info("Reading records from " + path.getAbsolutePath()
061                                                    + " indexLocation=" + getIndexLocation());
062                            }
063                            helper = FamIndexHelperFactory.newHelper();
064                            saxParser = SAXParserFactory.newInstance().newSAXParser();
065                            // get files in directory
066                            File[] files = helper.getInitialFiles(indexDir.getAbsolutePath());
067                            for (File file : files) {
068                                    addRecord(file);
069                            }
070                    } catch (Exception e) {
071                            log.error("Failed to load index from " + path, e);
072                            throw new DataUnavailableException(e);
073                    }
074                    // avoid update() being called before we are complete
075                    initComplete = true;
076            }
077    
078            private void addRecord(File file) {
079                    InputStream is = null;
080                    try {
081                            is = new FileInputStream(file);
082                            if (file.getAbsolutePath().endsWith(".gz")) {
083                                    is = new GZIPInputStream(is);
084                            }
085                            saxParser.parse(is, new SAXIndexHandler(this));
086                    } catch (Exception e) {
087                            log.warn("Unable to read " + file, e);
088                    } finally {
089                            if (is != null) {
090                                    try {
091                                            is.close();
092                                    } catch (Exception e2) {
093                                            // ok
094                                    }
095                            }
096                    }
097            }
098    
099            @Override
100            public Index newInstance(String path, Set<IndexRecordListener> listeners)
101                            throws DataUnavailableException {
102                    return new FamIndex(new File(path), listeners);
103            }
104    }