001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.cocoon.components.search.components.impl;
017:
018: import java.io.IOException;
019: import java.util.ArrayList;
020: import java.util.List;
021:
022: import org.apache.avalon.excalibur.pool.Recyclable;
023: import org.apache.avalon.framework.activity.Disposable;
024: import org.apache.avalon.framework.logger.AbstractLogEnabled;
025: import org.apache.cocoon.ProcessingException;
026: import org.apache.cocoon.components.search.components.Searcher;
027: import org.apache.lucene.search.Hits;
028: import org.apache.lucene.search.Query;
029: import org.apache.lucene.search.Sort;
030: import org.apache.lucene.search.SortField;
031: import org.apache.lucene.store.Directory;
032:
033: /**
034: * @author Nicolas Maisonneuve
035: *
036: */
037: abstract class AbstractSearcher extends AbstractLogEnabled implements
038: Searcher, Disposable, Recyclable {
039: /**
040: * Lucene Directory
041: */
042: protected List directories = new ArrayList();
043:
044: /**
045: * Lucene SortField
046: */
047: protected SortField sortfield;
048:
049: /**
050: * Lucene Searcher
051: */
052: protected org.apache.lucene.search.Searcher luceneSearcher;
053:
054: /*
055: * (non-Javadoc)
056: *
057: * @see org.apache.cocoon.components.search.components.Searcher#addDirectory(org.apache.lucene.store.Directory)
058: */
059: public void addDirectory(Directory directory) {
060: directories.add(directory);
061: }
062:
063: /*
064: * (non-Javadoc)
065: *
066: * @see org.apache.cocoon.components.search.components.Searcher#search(org.apache.lucene.search.Query)
067: */
068: public Hits search(Query query) throws ProcessingException {
069: try {
070: getLuceneSearcher();
071:
072: if (sortfield == null) {
073: return luceneSearcher.search(query);
074: } else {
075: return luceneSearcher
076: .search(query, new Sort(sortfield));
077: }
078: } catch (IOException e) {
079: throw new ProcessingException(e);
080: }
081:
082: }
083:
084: protected abstract void getLuceneSearcher() throws IOException;
085:
086: /*
087: * (non-Javadoc)
088: *
089: * @see org.apache.cocoon.components.search.components.Searcher#setSortField(java.lang.String,
090: * boolean)
091: */
092: public void setSortField(String field, boolean reverse) {
093: sortfield = new SortField(field, reverse);
094: }
095:
096: /*
097: * (non-Javadoc)
098: *
099: * @see org.apache.avalon.framework.activity.Disposable#dispose()
100: */
101: public void dispose() {
102: recycle();
103: }
104:
105: /*
106: * (non-Javadoc)
107: *
108: * @see org.apache.avalon.excalibur.pool.Recyclable#recycle()
109: */
110: public void recycle() {
111: try {
112: directories.clear();
113: sortfield = null;
114: luceneSearcher.close();
115: } catch (IOException ex) {
116: this .getLogger().error("release error", ex);
117: }
118:
119: }
120:
121: }
|