[java]代码库
package com.geostar.poi.support.lucene;
import java.io.File;
import java.io.IOException;
import org.apache.log4j.Logger;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;
import com.geostar.poi.support.POIConfig;
public class POILuceneManager implements LuceneManager{
private IndexWriter indexWriter=null;
private IndexReader indexReader=null;
//------lock 1
private Object lock_w=new Object();
//------lock 2
private Object lock_r=new Object();
private Logger logger=Logger.getLogger(POILuceneManager.class);
@Override
public IndexWriter getIndexWriter() throws CorruptIndexException, LockObtainFailedException, IOException {
synchronized(lock_w){
if(indexWriter==null){
System.out.println("创建对象");
if(IndexWriter.isLocked(FSDirectory.open(new File(POIConfig.DEFULT_POI_LUCENNE_INDEX_PATH)))){
IndexWriter.unlock(FSDirectory.open(new File(POIConfig.DEFULT_POI_LUCENNE_INDEX_PATH)));
};
Analyzer any=new StandardAnalyzer(Version.LUCENE_CURRENT);
indexWriter=new IndexWriter(FSDirectory.open(new File(POIConfig.DEFULT_POI_LUCENNE_INDEX_PATH)),any,MaxFieldLength.UNLIMITED );
};
}
return indexWriter;
}
@Override
public IndexReader getIndexReader() throws CorruptIndexException, IOException {
synchronized (lock_r) {
if(indexReader==null){
indexReader=IndexReader.open(FSDirectory.open(new File(POIConfig.DEFULT_POI_LUCENNE_INDEX_PATH)));
};
}
return indexReader;
}
@Override
public void closeIndexWriter() throws IOException {
// TODO Auto-generated method stub
//synchronized (lock_w) {
if(this.indexWriter!=null){
this.indexWriter.close();
};
//}
}
@Override
public void closeIndexReader() throws IOException {
// TODO Auto-generated method stub
//synchronized (lock_r) {
if(this.indexReader!=null){
this.indexReader.close();
};
//}
}
@Override
public void closeAll() throws IOException {
// TODO Auto-generated method stub
this.closeIndexReader();
this.closeIndexWriter();
}
}