001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * Created on 24/07/2007 12:23:05
040: *
041: * The JForum Project
042: * http://www.jforum.net
043: */
044: package net.jforum.search;
045:
046: import java.io.IOException;
047:
048: import net.jforum.entities.Post;
049: import net.jforum.exceptions.ForumException;
050: import net.jforum.util.preferences.ConfigKeys;
051: import net.jforum.util.preferences.SystemGlobals;
052:
053: import org.apache.lucene.analysis.Analyzer;
054: import org.apache.lucene.index.IndexReader;
055:
056: /**
057: * @author Rafael Steil
058: * @version $Id: LuceneManager.java,v 1.16 2007/09/09 22:53:35 rafaelsteil Exp $
059: */
060: public class LuceneManager implements SearchManager {
061: private LuceneSearch search;
062: private LuceneSettings settings;
063: private LuceneIndexer indexer;
064:
065: /**
066: * @see net.jforum.search.SearchManager#init()
067: */
068: public void init() {
069: try {
070: Analyzer analyzer = (Analyzer) Class.forName(
071: SystemGlobals.getValue(ConfigKeys.LUCENE_ANALYZER))
072: .newInstance();
073:
074: this .settings = new LuceneSettings(analyzer);
075:
076: this .settings.useFSDirectory(SystemGlobals
077: .getValue(ConfigKeys.LUCENE_INDEX_WRITE_PATH));
078:
079: this .removeLockFile();
080:
081: this .indexer = new LuceneIndexer(this .settings);
082:
083: this .search = new LuceneSearch(this .settings,
084: new LuceneContentCollector(this .settings));
085:
086: this .indexer.watchNewDocuDocumentAdded(this .search);
087:
088: SystemGlobals.setObjectValue(ConfigKeys.LUCENE_SETTINGS,
089: this .settings);
090: }
091:
092: catch (Exception e) {
093: throw new ForumException(e);
094: }
095: }
096:
097: public LuceneSearch luceneSearch() {
098: return this .search;
099: }
100:
101: public LuceneIndexer luceneIndexer() {
102: return this .indexer;
103: }
104:
105: public void removeLockFile() {
106: try {
107: if (IndexReader.isLocked(this .settings.directory())) {
108: IndexReader.unlock(this .settings.directory());
109: }
110: } catch (IOException e) {
111: throw new ForumException(e);
112: }
113: }
114:
115: /**
116: * @see net.jforum.search.SearchManager#create(net.jforum.entities.Post)
117: */
118: public void create(Post post) {
119: this .indexer.create(post);
120: }
121:
122: /**
123: * @see net.jforum.search.SearchManager#update(net.jforum.entities.Post)
124: */
125: public void update(Post post) {
126: this .indexer.update(post);
127: }
128:
129: /**
130: * @see net.jforum.search.SearchManager#search(net.jforum.search.SearchArgs)
131: */
132: public SearchResult search(SearchArgs args) {
133: return this .search.search(args);
134: }
135:
136: /**
137: * @see net.jforum.search.SearchManager#delete(net.jforum.entities.Post)
138: */
139: public void delete(Post p) {
140: this.indexer.delete(p);
141: }
142: }
|