KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > samples > LuceneUtil


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.samples;
17
18 import java.io.File JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.net.MalformedURLException JavaDoc;
21 import java.net.URL JavaDoc;
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 /**
37  * This is a sample helper class that can be used from flow to
38  * create an index.
39  * @version $Id: LuceneUtil.java 155049 2005-02-23 19:02:34Z cziegeler $
40  */

41 public class LuceneUtil
42     implements Contextualizable, Serviceable {
43
44     private File JavaDoc workDir;
45     private ServiceManager manager;
46
47     /* (non-Javadoc)
48      * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
49      */

50     public void contextualize(Context context) throws ContextException {
51         this.workDir = (File JavaDoc) context.get(Constants.CONTEXT_WORK_DIR);
52     }
53     
54     /* (non-Javadoc)
55      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
56      */

57     public void service(ServiceManager manager) throws ServiceException {
58         this.manager = manager;
59     }
60
61     public void createIndex(String JavaDoc 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 JavaDoc( workDir, "index" ), create );
70             lcii.setAnalyzer( analyzer );
71             URL JavaDoc base_url = new URL JavaDoc( baseURL );
72             lcii.index( directory, create, base_url );
73         } catch (MalformedURLException JavaDoc mue) {
74             throw new ProcessingException( "MalformedURLException in createIndex()!", mue );
75         } catch (IOException JavaDoc ioe) {
76             // ignore ??
77
throw new ProcessingException( "IOException in createIndex()!", ioe );
78         } catch (ServiceException ce) {
79             // ignore ??
80
throw new ProcessingException( "ServiceException in createIndex()!", ce );
81         } finally {
82             this.manager.release( lcii );
83         }
84     }
85     
86 }
87
Popular Tags