01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.samples;
18:
19: import java.io.File;
20: import java.io.IOException;
21: import java.net.MalformedURLException;
22: import java.net.URL;
23:
24: import org.apache.avalon.framework.context.Context;
25: import org.apache.avalon.framework.context.ContextException;
26: import org.apache.avalon.framework.context.Contextualizable;
27: import org.apache.avalon.framework.service.ServiceException;
28: import org.apache.avalon.framework.service.ServiceManager;
29: import org.apache.avalon.framework.service.Serviceable;
30: import org.apache.cocoon.Constants;
31: import org.apache.cocoon.ProcessingException;
32: import org.apache.cocoon.components.search.LuceneCocoonHelper;
33: import org.apache.cocoon.components.search.LuceneCocoonIndexer;
34: import org.apache.lucene.analysis.Analyzer;
35: import org.apache.lucene.store.Directory;
36:
37: /**
38: * This is a sample helper class that can be used from flow to
39: * create an index.
40: * @version $Id: LuceneUtil.java 433543 2006-08-22 06:22:54Z crossley $
41: */
42: public class LuceneUtil implements Contextualizable, Serviceable {
43:
44: private File 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) 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 baseURL, boolean create)
62: throws ProcessingException {
63: LuceneCocoonIndexer lcii = null;
64: Analyzer analyzer = LuceneCocoonHelper
65: .getAnalyzer("org.apache.lucene.analysis.standard.StandardAnalyzer");
66:
67: try {
68:
69: lcii = (LuceneCocoonIndexer) this .manager
70: .lookup(LuceneCocoonIndexer.ROLE);
71: Directory directory = LuceneCocoonHelper.getDirectory(
72: new File(workDir, "index"), create);
73: lcii.setAnalyzer(analyzer);
74: URL base_url = new URL(baseURL);
75: lcii.index(directory, create, base_url);
76: } catch (MalformedURLException mue) {
77: throw new ProcessingException(
78: "MalformedURLException in createIndex()!", mue);
79: } catch (IOException ioe) {
80: // ignore ??
81: throw new ProcessingException(
82: "IOException in createIndex()!", ioe);
83: } catch (ServiceException ce) {
84: // ignore ??
85: throw new ProcessingException(
86: "ServiceException in createIndex()!", ce);
87: } finally {
88: this.manager.release(lcii);
89: }
90: }
91:
92: }
|