001    
002    package org.wdssii.webindex.servlet;
003    
004    import java.util.HashMap;
005    import java.util.Map;
006    
007    import org.apache.commons.logging.Log;
008    import org.apache.commons.logging.LogFactory;
009    import org.springframework.beans.BeansException;
010    import org.springframework.beans.factory.BeanFactory;
011    import org.springframework.beans.factory.BeanFactoryAware;
012    
013    
014    public class WebIndexFactory implements BeanFactoryAware {
015            private final Log log = LogFactory.getLog(getClass());
016            // can not be thread-local because otherwise recordNo on the caches will depend on when the thread instance was created
017            private Map<String, WebIndexDAO> daoCache = new HashMap<String, WebIndexDAO>();
018            private BeanFactory beanFactory;
019            
020            private String sourcePrefix = "";
021            private String sourceSuffix = "";
022            
023            public String getSourcePrefix() {
024                    return sourcePrefix;
025            }
026    
027            public void setSourcePrefix(String sourcePrefix) {
028                    this.sourcePrefix = sourcePrefix;
029                    if (sourcePrefix.charAt(sourcePrefix.length()-1) != '/'){
030                            this.sourcePrefix += '/';
031                    }
032            }
033    
034            public String getSourceSuffix() {
035                    return sourceSuffix;
036            }
037    
038            public void setSourceSuffix(String sourceSuffix) {
039                    this.sourceSuffix = sourceSuffix;
040            }
041    
042            public String getIndexDir(String source)
043            {
044                    return (getSourcePrefix() + source + getSourceSuffix());
045            }
046            
047            private WebIndexDAO createNewDAOInstance(String source)
048            {
049                    String indexLocation = getIndexDir(source);
050                    if ( log.isInfoEnabled() ){
051                            log.info("Creating new WebIndexDAO for " + source + " from " + indexLocation);
052                    }
053                    // gets new instance each time
054                    WebIndexDAO dao = (WebIndexDAO) beanFactory.getBean("webIndexDAO", WebIndexDAO.class);
055                    dao.setSource(source);
056                    dao.setIndexDir(indexLocation);
057                    // add it to the cache, so that next time it is reused
058                    daoCache.put(source, dao);
059                    return dao;
060            }
061            
062    
063            public synchronized WebIndexDAO getDAO(String source){
064                    WebIndexDAO dao = daoCache.get(source);
065                    if ( dao == null ){
066                            return createNewDAOInstance(source);
067                    }
068                    return dao;
069            }
070    
071            @Override
072            public void setBeanFactory(BeanFactory arg0) throws BeansException {
073                    beanFactory = arg0;
074            }
075            
076    }