1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package com.isenshi.util.extlib;
23
24 import java.io.ByteArrayInputStream;
25 import java.io.CharArrayReader;
26 import java.io.File;
27 import java.io.IOException;
28 import java.io.InputStreamReader;
29 import java.net.MalformedURLException;
30
31 import org.apache.lucene.analysis.cjk.CJKAnalyzer;
32 import org.apache.lucene.index.IndexWriter;
33 import org.apache.slide.common.NamespaceAccessToken;
34 import org.apache.slide.common.ServiceAccessException;
35 import org.apache.slide.common.ServiceConnectionFailedException;
36 import org.apache.slide.common.ServiceDisconnectionFailedException;
37 import org.apache.slide.common.ServiceInitializationFailedException;
38 import org.apache.slide.common.ServiceResetFailedException;
39 import org.apache.slide.common.Uri;
40 import org.apache.slide.common.XAServiceBase;
41 import org.apache.slide.content.NodeRevisionContent;
42 import org.apache.slide.content.NodeRevisionDescriptor;
43 import org.apache.slide.content.NodeRevisionNumber;
44 import org.apache.slide.search.IndexException;
45 import org.apache.slide.search.basic.IBasicExpressionFactory;
46 import org.apache.slide.store.IndexStore;
47
48 import com.isenshi.util.ResourceUtils;
49
50 /***
51 * @author nishioka
52 */
53 public class CJKTxtContainsIndexer extends XAServiceBase implements IndexStore {
54
55 private String indexpath;
56
57 private static final String INDEX_PATH = "indexpath";
58
59 public void initialize(NamespaceAccessToken token)
60 throws ServiceInitializationFailedException {
61 indexpath = token.getNamespaceConfig().getParameter(
62 INDEX_PATH);
63
64 File file = ResourceUtils.getFile("index/");
65
66 try {
67 indexpath = ResourceUtils.getFileName(file.toURL());
68 } catch (MalformedURLException e2) {
69
70 e2.printStackTrace();
71 }
72
73
74
75 IndexWriter indexWriter = null;
76 try {
77 indexWriter = new IndexWriter(indexpath, new CJKAnalyzer(),
78 false);
79 }
80
81 catch (IOException e) {
82 try {
83
84 indexWriter = new IndexWriter(indexpath, new CJKAnalyzer(),
85 true);
86 } catch (IOException ex) {
87 throw new ServiceInitializationFailedException(this, ex);
88 }
89 }
90
91 try {
92 indexWriter.close();
93 } catch (IOException e) {
94 throw new ServiceInitializationFailedException(this, e);
95 }
96 }
97
98 /***
99 * Method getFactory
100 *
101 * @return an IBasicExpressionFactory
102 *
103 */
104 public IBasicExpressionFactory getBasicExpressionFactory() {
105 return new BasicExpressionFactoryTxtContainsCJK(indexpath);
106 }
107
108 private boolean started = false;
109
110 /***
111 * Index an object content.
112 *
113 * @param uri
114 * Uri
115 * @exception IndexException
116 * Error accessing the Data Source
117 */
118 synchronized public void createIndex(Uri uri,
119 NodeRevisionDescriptor revisionDescriptor,
120 NodeRevisionContent revisionContent) throws IndexException {
121 try {
122
123 CJKLuceneIndexer indexer = new CJKLuceneIndexer(indexpath);
124 indexer.index(uri.toString(), new InputStreamReader(new ByteArrayInputStream(revisionContent.getContentBytes()), "Windows-31J"));
125 } catch (Exception e) {
126 throw new IndexException(e);
127 }
128
129 }
130
131 /***
132 * Method updateIndex
133 *
134 * @param uri
135 * an Uri
136 * @param revisionDescriptor
137 * a NodeRevisionDescriptor
138 * @param revisionContent
139 * a NodeRevisionContent
140 *
141 * @throws IndexException
142 *
143 */
144 synchronized public void updateIndex(Uri uri,
145 NodeRevisionDescriptor revisionDescriptor,
146 NodeRevisionContent revisionContent) throws IndexException {
147 try {
148 CJKLuceneIndexer indexer = new CJKLuceneIndexer(indexpath);
149 indexer.removeIndex(uri.toString());
150 indexer.index(uri.toString(), new CharArrayReader(revisionContent
151 .getContent()));
152 } catch (Exception e) {
153 throw new IndexException(e);
154 }
155 }
156
157 /***
158 * Drop an object revision from the index.
159 *
160 * @param uri
161 * Uri
162 * @exception ServiceAccessException
163 * Error accessing the Data Source
164 */
165 synchronized public void dropIndex(Uri uri, NodeRevisionNumber number)
166 throws IndexException {
167 try {
168 CJKLuceneIndexer indexer = new CJKLuceneIndexer(indexpath);
169 indexer.removeIndex(uri.toString());
170 } catch (Exception e) {
171 throw new IndexException(e);
172 }
173 }
174
175 /***
176 * Connects to the underlying data source (if any is needed).
177 *
178 * @exception ServiceConnectionFailedException
179 * Connection failed
180 */
181 public void connect() throws ServiceConnectionFailedException {
182 System.out.println("SampleIndexer: connect");
183 started = true;
184 }
185
186 /***
187 * This function tells whether or not the service is connected.
188 *
189 * @return boolean true if we are connected
190 * @exception ServiceAccessException
191 * Service access error
192 */
193 public boolean isConnected() throws ServiceAccessException {
194
195 return started;
196 }
197
198 /***
199 * Disconnects from the underlying data source.
200 *
201 * @exception ServiceDisconnectionFailedException
202 * Disconnection failed
203 */
204 public void disconnect() throws ServiceDisconnectionFailedException {
205 System.out.println("SampleIndexer: disconnect");
206 started = false;
207 }
208
209 /***
210 * Deletes service underlying data source, if possible (and meaningful).
211 *
212 * @exception ServiceResetFailedException
213 * Reset failed
214 */
215 public void reset() throws ServiceResetFailedException {
216 System.out.println("SampleIndexer: reset");
217 }
218 }