KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > index > SampleTxtContainsIndexer


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/stores/org/apache/slide/index/SampleTxtContainsIndexer.java,v 1.3 2004/07/11 10:12:50 unico Exp $
3  * $Revision: 1.3 $
4  * $Date: 2004/07/11 10:12:50 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2004 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23 package org.apache.slide.index;
24 import java.io.CharArrayReader JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.Hashtable JavaDoc;
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 /**
48  * Take this as a starting point for your own Indexer implementation.
49  *
50  * @version $Revision: 1.3 $
51  */

52 public class SampleTxtContainsIndexer extends XAServiceBase implements IndexStore
53 {
54     private static final String JavaDoc INDEX_PATH = "indexpath";
55     
56     private String JavaDoc indexpath;
57     
58     /**
59      * Create Index, if not yet done.
60      *
61      * @param token a NamespaceAccessToken
62      *
63      * @throws ServiceInitializationFailedException
64      *
65      */

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         // will fail, if not yet exists
76
catch (IOException JavaDoc e)
77         {
78             try
79             {
80                 // create index
81
indexWriter = new IndexWriter(indexpath, new StandardAnalyzer(), true);
82             }
83             catch (IOException JavaDoc ex)
84             {
85                 throw new ServiceInitializationFailedException (this, ex);
86             }
87         }
88         
89         try
90         {
91             indexWriter.close();
92         }
93         catch (IOException JavaDoc e)
94         {
95             throw new ServiceInitializationFailedException (this, e);
96         }
97     }
98     
99     /**
100      * Method getFactory
101      *
102      * @return an IBasicExpressionFactory
103      *
104      */

105     public IBasicExpressionFactory getBasicExpressionFactory()
106     {
107         return new BasicExpressionFactoryTxtContainsSample (indexpath);
108     }
109     
110     private boolean started = false;
111     
112     
113     /**
114      * Index an object content.
115      *
116      * @param uri Uri
117      * @exception IndexException Error accessing the Data Source
118      */

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 JavaDoc (revisionContent.getContent()));
129         }
130         catch (Exception JavaDoc e)
131         {
132             throw new IndexException (e);
133         }
134         //index(revisionContent, uri);
135
}
136     
137     /**
138      * Method updateIndex
139      *
140      * @param uri an Uri
141      * @param revisionDescriptor a NodeRevisionDescriptor
142      * @param revisionContent a NodeRevisionContent
143      *
144      * @throws IndexException
145      *
146      */

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 JavaDoc (revisionContent.getContent()));
158         }
159         catch (Exception JavaDoc e)
160         {
161             throw new IndexException (e);
162         }
163     }
164         
165     /**
166      * Drop an object revision from the index.
167      *
168      * @param uri Uri
169      * @exception ServiceAccessException Error accessing the Data Source
170      */

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 JavaDoc e)
180         {
181             throw new IndexException (e);
182         }
183     }
184         
185     /**
186      * Connects to the underlying data source (if any is needed).
187      *
188      * @exception ServiceConnectionFailedException Connection failed
189      */

190     public void connect() throws ServiceConnectionFailedException
191     {
192         System.out.println("SampleIndexer: connect");
193         started = true;
194     }
195     
196     /**
197      * This function tells whether or not the service is connected.
198      *
199      * @return boolean true if we are connected
200      * @exception ServiceAccessException Service access error
201      */

202     public boolean isConnected() throws ServiceAccessException
203     {
204         // System.out.println("isConnected");
205
return started;
206     }
207     
208     /**
209      * Initializes the service with a set of parameters. Those could be :
210      * <li>User name, login info
211      * <li>Host name on which to connect
212      * <li>Remote port
213      * <li>JDBC driver whoich is to be used :-)
214      * <li>Anything else ...
215      *
216      * @param parameters Hashtable containing the parameters' names
217      * and associated values
218      * @exception ServiceParameterErrorException Incorrect service parameter
219      * @exception ServiceParameterMissingException Service parameter missing
220      */

221     public void setParameters (Hashtable JavaDoc parameters) throws ServiceParameterErrorException, ServiceParameterMissingException
222     {
223         indexpath = (String JavaDoc)parameters.get (INDEX_PATH);
224         if (indexpath == null || indexpath.length() == 0)
225             throw new ServiceParameterMissingException (this, INDEX_PATH);
226     }
227     
228     /**
229      * Disconnects from the underlying data source.
230      *
231      * @exception ServiceDisconnectionFailedException Disconnection failed
232      */

233     public void disconnect() throws ServiceDisconnectionFailedException
234     {
235         System.out.println("SampleIndexer: disconnect");
236         started = false;
237     }
238     
239     /**
240      * Deletes service underlying data source, if possible (and meaningful).
241      *
242      * @exception ServiceResetFailedException Reset failed
243      */

244     public void reset() throws ServiceResetFailedException
245     {
246         System.out.println("SampleIndexer: reset");
247     }
248 }
249
250
Popular Tags