001 /** 002 * 003 */ 004 package org.wdssii.core; 005 006 import java.util.ArrayList; 007 import java.util.HashSet; 008 import java.util.List; 009 import java.util.Set; 010 import java.util.Timer; 011 import java.util.TimerTask; 012 013 import org.apache.commons.logging.Log; 014 import org.apache.commons.logging.LogFactory; 015 016 /** 017 * Creates index objects from Index URLs such as xml:/tmp/testindex.xml 018 * 019 * @author Lakshman 020 * @version $Id: IndexFactory.java,v 1.2 2006/12/28 22:23:29 lakshman Exp $ 021 * @see IndexRecord 022 */ 023 public abstract class IndexFactory { 024 private static Log log = LogFactory.getLog(IndexFactory.class); 025 026 private static PrototypeFactory<Index> myFactory = new PrototypeFactory<Index>( 027 "java/Index"); 028 029 private static List<Index> toUpdate = new ArrayList<Index>(); 030 031 private static TimerTask timerTask = new TimerTask() { 032 @Override 033 public void run() { 034 for (Index index : toUpdate) { 035 index.update(); 036 } 037 } 038 }; 039 static { 040 new Timer().schedule(timerTask, 1000 * 30, 1000 * 30); // every 30 041 // seconds 042 } 043 044 /** Creates an index. Add listeners using addIndexRecordListener */ 045 public static Index createIndex(String url) 046 throws IllegalArgumentException, DataUnavailableException { 047 return createIndex(url, new HashSet<IndexRecordListener>()); 048 } 049 050 /** convenience function when only one listener needs to be provided. */ 051 public static Index createIndex(String url, IndexRecordListener listener) 052 throws IllegalArgumentException, DataUnavailableException { 053 Set<IndexRecordListener> listeners = new HashSet<IndexRecordListener>(); 054 listeners.add(listener); 055 return createIndex(url, listeners); 056 } 057 058 /** Creates an index. Existing records are supplied to the list of listeners */ 059 public static Index createIndex(String url, 060 Set<IndexRecordListener> listeners) 061 throws IllegalArgumentException, DataUnavailableException { 062 List<String> pieces = StringUtil.splitOnFirst(url, ':'); 063 if (pieces.size() != 2) { 064 String error = ("Index: no protocol (xml, xmllb, etc.) specified in '" 065 + url + "'"); 066 log.error(error); 067 throw new IllegalArgumentException(error); 068 } 069 070 Index prototype = myFactory.getPrototypeMaster(pieces.get(0)); 071 if (prototype == null) { 072 String error = ("No protocol named " + pieces.get(0)); 073 log.error(error); 074 throw new IllegalArgumentException(error); 075 } 076 077 Index index = prototype.newInstance(pieces.get(1), listeners); 078 toUpdate.add(index); 079 return index; 080 } 081 082 }