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     */
017    package org.apache.servicemix.jbi.audit.lucene;
018    
019    import java.io.File;
020    import java.io.IOException;
021    
022    import org.apache.lucene.analysis.SimpleAnalyzer;
023    import org.apache.lucene.document.Document;
024    import org.apache.lucene.index.IndexReader;
025    import org.apache.lucene.index.IndexWriter;
026    import org.apache.lucene.index.Term;
027    import org.apache.lucene.search.IndexSearcher;
028    import org.apache.lucene.store.Directory;
029    import org.apache.lucene.store.FSDirectory;
030    
031    
032    /**
033     * Utility class for Lucene API.
034     * @author george
035     * @since 2.1
036     * @version $Revision: 2153 $
037     */
038    public class LuceneIndexer {
039        protected Directory directory;
040    
041        private File segmentFile;
042    
043        public LuceneIndexer() {
044        }
045    
046        public Directory getDirectory() {
047            return directory;
048        }
049    
050        public void setDirectory(Directory directory) {
051            this.directory = directory;
052        }
053    
054        public void setDirectoryName(File directoryName) throws IOException {
055            this.segmentFile = new File(directoryName, "segments");
056            this.directory = FSDirectory.getDirectory(directoryName.toString(), !this.segmentFile.exists());
057        }
058    
059        /**
060         * Drop object from Lucene index
061         */
062        protected void remove(String id) throws IOException {
063            synchronized (directory) {
064                IndexReader ir = IndexReader.open(directory);
065                try {
066                    ir.delete(new Term("org.apache.servicemix.exchangeid", id));
067                } finally {
068                    ir.close();
069                }
070            }
071        }
072    
073        protected void remove(String[] ids) throws IOException {
074            if (ids != null && ids.length > 0) {
075                synchronized (directory) {
076                    IndexReader ir = IndexReader.open(directory);
077                    try {
078                        for (int i = 0; i < ids.length; i++) {
079                            ir.delete(new Term("org.apache.servicemix.exchangeid", ids[i]));
080                        }
081                    } finally {
082                        ir.close();
083                    }
084                }
085            }
086        }
087    
088        /**
089         * Add object to Lucene index
090         */
091        public void add(Document lucDoc, String id) throws IOException {
092            synchronized (directory) {
093                IndexWriter writer = new IndexWriter(directory, new SimpleAnalyzer(), !segmentFile.exists());
094                try {
095                    writer.addDocument(lucDoc);
096                } finally {
097                    writer.close();
098                }
099            }
100        }
101    
102        /**
103         * called when an existing document is updated.
104         */
105        public void update(Document lucDoc, String id) throws IOException {
106            remove(id);
107            add(lucDoc, id);
108        }
109    
110        public Object search(LuceneCallback lc) throws IOException {
111            synchronized (directory) {
112                IndexReader ir = IndexReader.open(directory);
113                IndexSearcher is = new IndexSearcher(ir);
114                try {
115                    return lc.doCallback(is);
116                } finally {
117                    is.close();
118                    ir.close();
119                }
120            }
121        }
122    }