1 16 package org.apache.cocoon.samples; 17 18 import java.io.File ; 19 import java.io.IOException ; 20 import java.net.MalformedURLException ; 21 import java.net.URL ; 22 23 import org.apache.avalon.framework.context.Context; 24 import org.apache.avalon.framework.context.ContextException; 25 import org.apache.avalon.framework.context.Contextualizable; 26 import org.apache.avalon.framework.service.ServiceException; 27 import org.apache.avalon.framework.service.ServiceManager; 28 import org.apache.avalon.framework.service.Serviceable; 29 import org.apache.cocoon.Constants; 30 import org.apache.cocoon.ProcessingException; 31 import org.apache.cocoon.components.search.LuceneCocoonHelper; 32 import org.apache.cocoon.components.search.LuceneCocoonIndexer; 33 import org.apache.lucene.analysis.Analyzer; 34 import org.apache.lucene.store.Directory; 35 36 41 public class LuceneUtil 42 implements Contextualizable, Serviceable { 43 44 private File workDir; 45 private ServiceManager manager; 46 47 50 public void contextualize(Context context) throws ContextException { 51 this.workDir = (File ) context.get(Constants.CONTEXT_WORK_DIR); 52 } 53 54 57 public void service(ServiceManager manager) throws ServiceException { 58 this.manager = manager; 59 } 60 61 public void createIndex(String baseURL, boolean create) 62 throws ProcessingException { 63 LuceneCocoonIndexer lcii = null; 64 Analyzer analyzer = LuceneCocoonHelper.getAnalyzer( "org.apache.lucene.analysis.standard.StandardAnalyzer" ); 65 66 try { 67 68 lcii = (LuceneCocoonIndexer)this.manager.lookup( LuceneCocoonIndexer.ROLE ); 69 Directory directory = LuceneCocoonHelper.getDirectory( new File ( workDir, "index" ), create ); 70 lcii.setAnalyzer( analyzer ); 71 URL base_url = new URL ( baseURL ); 72 lcii.index( directory, create, base_url ); 73 } catch (MalformedURLException mue) { 74 throw new ProcessingException( "MalformedURLException in createIndex()!", mue ); 75 } catch (IOException ioe) { 76 throw new ProcessingException( "IOException in createIndex()!", ioe ); 78 } catch (ServiceException ce) { 79 throw new ProcessingException( "ServiceException in createIndex()!", ce ); 81 } finally { 82 this.manager.release( lcii ); 83 } 84 } 85 86 } 87 | Popular Tags |