1 23 package org.apache.slide.index; 24 import java.io.CharArrayReader ; 25 import java.io.IOException ; 26 import java.util.Hashtable ; 27 import org.apache.lucene.analysis.standard.StandardAnalyzer; 28 import org.apache.lucene.index.IndexWriter; 29 import org.apache.slide.common.XAServiceBase; 30 import org.apache.slide.common.NamespaceAccessToken; 31 import org.apache.slide.common.ServiceAccessException; 32 import org.apache.slide.common.ServiceConnectionFailedException; 33 import org.apache.slide.common.ServiceDisconnectionFailedException; 34 import org.apache.slide.common.ServiceInitializationFailedException; 35 import org.apache.slide.common.ServiceParameterErrorException; 36 import org.apache.slide.common.ServiceParameterMissingException; 37 import org.apache.slide.common.ServiceResetFailedException; 38 import org.apache.slide.common.Uri; 39 import org.apache.slide.content.NodeRevisionContent; 40 import org.apache.slide.content.NodeRevisionDescriptor; 41 import org.apache.slide.content.NodeRevisionNumber; 42 import org.apache.slide.search.IndexException; 43 import org.apache.slide.search.basic.IBasicExpressionFactory; 44 import org.apache.slide.store.IndexStore; 45 46 47 52 public class SampleTxtContainsIndexer extends XAServiceBase implements IndexStore 53 { 54 private static final String INDEX_PATH = "indexpath"; 55 56 private String indexpath; 57 58 66 public void initialize(NamespaceAccessToken token) 67 throws ServiceInitializationFailedException 68 { 69 indexpath = token.getNamespaceConfig().getParameter (INDEX_PATH); 70 IndexWriter indexWriter = null; 71 try 72 { 73 indexWriter = new IndexWriter(indexpath, new StandardAnalyzer(), false); 74 } 75 catch (IOException e) 77 { 78 try 79 { 80 indexWriter = new IndexWriter(indexpath, new StandardAnalyzer(), true); 82 } 83 catch (IOException ex) 84 { 85 throw new ServiceInitializationFailedException (this, ex); 86 } 87 } 88 89 try 90 { 91 indexWriter.close(); 92 } 93 catch (IOException e) 94 { 95 throw new ServiceInitializationFailedException (this, e); 96 } 97 } 98 99 105 public IBasicExpressionFactory getBasicExpressionFactory() 106 { 107 return new BasicExpressionFactoryTxtContainsSample (indexpath); 108 } 109 110 private boolean started = false; 111 112 113 119 synchronized public void createIndex (Uri uri, 120 NodeRevisionDescriptor revisionDescriptor, 121 NodeRevisionContent revisionContent) 122 throws IndexException 123 { 124 try 125 { 126 LuceneIndexer indexer = new LuceneIndexer (indexpath); 127 indexer.index (uri.toString(), 128 new CharArrayReader (revisionContent.getContent())); 129 } 130 catch (Exception e) 131 { 132 throw new IndexException (e); 133 } 134 } 136 137 147 synchronized public void updateIndex(Uri uri, 148 NodeRevisionDescriptor revisionDescriptor, 149 NodeRevisionContent revisionContent) 150 throws IndexException 151 { 152 try 153 { 154 LuceneIndexer indexer = new LuceneIndexer (indexpath); 155 indexer.removeIndex (uri.toString()); 156 indexer.index (uri.toString(), 157 new CharArrayReader (revisionContent.getContent())); 158 } 159 catch (Exception e) 160 { 161 throw new IndexException (e); 162 } 163 } 164 165 171 synchronized public void dropIndex(Uri uri, NodeRevisionNumber number) 172 throws IndexException 173 { 174 try 175 { 176 LuceneIndexer indexer = new LuceneIndexer (indexpath); 177 indexer.removeIndex (uri.toString()); 178 } 179 catch (Exception e) 180 { 181 throw new IndexException (e); 182 } 183 } 184 185 190 public void connect() throws ServiceConnectionFailedException 191 { 192 System.out.println("SampleIndexer: connect"); 193 started = true; 194 } 195 196 202 public boolean isConnected() throws ServiceAccessException 203 { 204 return started; 206 } 207 208 221 public void setParameters (Hashtable parameters) throws ServiceParameterErrorException, ServiceParameterMissingException 222 { 223 indexpath = (String )parameters.get (INDEX_PATH); 224 if (indexpath == null || indexpath.length() == 0) 225 throw new ServiceParameterMissingException (this, INDEX_PATH); 226 } 227 228 233 public void disconnect() throws ServiceDisconnectionFailedException 234 { 235 System.out.println("SampleIndexer: disconnect"); 236 started = false; 237 } 238 239 244 public void reset() throws ServiceResetFailedException 245 { 246 System.out.println("SampleIndexer: reset"); 247 } 248 } 249 250 | Popular Tags |