001    /**
002     * 
003     */
004    package org.wdssii.webindex.servlet;
005    
006    import java.io.File;
007    import java.util.ArrayList;
008    import java.util.Date;
009    import java.util.List;
010    
011    import org.apache.commons.logging.Log;
012    import org.apache.commons.logging.LogFactory;
013    
014    /**
015     * Gets all the possible WebIndex sources available on the system.
016     * 
017     * @author lakshman
018     *
019     */
020    public class SourcesListingDAO {
021            // injected parameters
022            private long updateIntervalInMilliSeconds = 60*1000;
023            private WebIndexFactory indexFactory;
024            
025            private String[] allSources = null;
026            private long lastUpdated = 0;
027            private final Log log = LogFactory.getLog(SourcesListingDAO.class);
028            
029            public long getUpdateIntervalInMilliSeconds() {
030                    return updateIntervalInMilliSeconds;
031            }
032    
033            public void setUpdateIntervalInMilliSeconds(long updateIntervalInMilliSeconds) {
034                    this.updateIntervalInMilliSeconds = updateIntervalInMilliSeconds;
035            }
036    
037            public WebIndexFactory getIndexFactory() {
038                    return indexFactory;
039            }
040    
041            public void setIndexFactory(WebIndexFactory indexFactory) {
042                    this.indexFactory = indexFactory;
043            }
044    
045            /** All the sources available on the system. */
046            public String[] getAllSources(){
047                    // do we need to update?
048                    long now = new Date().getTime();
049                    if ( allSources != null && (now - lastUpdated) < updateIntervalInMilliSeconds ){
050                            return allSources;
051                    }
052                    lastUpdated = now;
053    
054                    // all subdirectories of the sourcePrefix that have a code_index.fam subdirectory
055                    List<String> sources = new ArrayList<String>();
056                    File file = new File(indexFactory.getSourcePrefix());
057                    
058                    String[] subdirs = file.list();
059                    for (String subdir : subdirs){
060                            File indexDir = new File( indexFactory.getIndexDir(subdir) );
061                            if (log.isDebugEnabled()){
062                                    log.debug("Checking for " + indexDir);
063                            }
064                            if (indexDir.exists()){
065                                    sources.add(subdir);
066                            }
067                    }
068    
069                    // update allSources and return
070                    allSources = sources.toArray(new String[0]);
071                    return allSources;
072            }
073    }