View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  /*
17   * Created on 2005/09/01
18   *
19   * TODO To change the template for this generated file go to
20   * Window - Preferences - Java - Code Style - Code Templates
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  		// String url = null;
66  		try {
67  			indexpath = ResourceUtils.getFileName(file.toURL());
68  		} catch (MalformedURLException e2) {
69  			// TODO Auto-generated catch block
70  			e2.printStackTrace();
71  		}
72  
73  		
74  
75  		IndexWriter indexWriter = null;
76  		try {
77  			indexWriter = new IndexWriter(indexpath, new CJKAnalyzer(),
78  					false);
79  		}
80  		// will fail, if not yet exists
81  		catch (IOException e) {
82  			try {
83  				// create index
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 		// index(revisionContent, uri);
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 		// System.out.println("isConnected");
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 }