KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > driver > lucene > LuceneConnection


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
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, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package scriptella.driver.lucene;
17
18 import org.apache.lucene.index.IndexReader;
19 import scriptella.driver.text.TextProviderException;
20 import scriptella.spi.*;
21 import scriptella.util.IOUtils;
22
23 import java.io.IOException JavaDoc;
24 import java.io.Reader JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.TreeSet JavaDoc;
28
29 /**
30  * Represents a connection to a Lucene index.
31  * <p>For configuration details and examples see <a HREF="package-summary.html">overview page</a>.
32  *
33  * @author Kirill Volgin
34  * @version 1.0
35  */

36 public class LuceneConnection extends AbstractConnection {
37
38     private static final String JavaDoc[] EMPTY_ARRAY = new String JavaDoc[] {};
39
40     private URL JavaDoc url;
41     private Set JavaDoc<String JavaDoc> fields;
42     private static final String JavaDoc DEFAULT_FIELD = "contents";
43     private Boolean JavaDoc useMultiFieldQueryParser;
44     private Boolean JavaDoc useLowercaseExpandedTerms;
45
46     /**
47      * Instantiates a new connection to Lucene Query.
48      * @param parameters connection parameters.
49      */

50     public LuceneConnection(ConnectionParameters parameters) {
51         super(Driver.DIALECT_IDENTIFIER, parameters);
52         url = parameters.getResolvedUrl();
53         parseFields((String JavaDoc)parameters.getProperty("fields"));
54         useMultiFieldQueryParser = parameters.getBooleanProperty("useMultiFieldQueryParser", false);
55         useLowercaseExpandedTerms = parameters.getBooleanProperty("useLowercaseExpandedTerms", true);
56     }
57
58
59
60     public void executeScript(Resource scriptContent, ParametersCallback parametersCallback) throws ProviderException {
61         throw new UnsupportedOperationException JavaDoc("Script execution is not supported yet");
62     }
63
64     /**
65      * Executes a query specified by its content.
66      * <p/>
67      *
68      * @param queryContent query content.
69      * @param parametersCallback callback to get parameter values.
70      * @param queryCallback callback to call for each result set element produced by this query.
71      * @see #executeScript(scriptella.spi.Resource, scriptella.spi.ParametersCallback)
72      */

73     public synchronized void executeQuery(Resource queryContent, ParametersCallback parametersCallback, QueryCallback queryCallback) throws ProviderException{
74         LuceneQuery query = null;
75         Reader r;
76         try {
77             r = queryContent.open();
78         } catch (IOException JavaDoc e) {
79             throw new TextProviderException("Cannot open a query for reading", e);
80         }
81         try {
82             query = new LuceneQuery(url.getFile(), parametersCallback, queryCallback);
83             query.execute(r, fields, useMultiFieldQueryParser, useLowercaseExpandedTerms);
84         } finally {
85             IOUtils.closeSilently(query);
86         }
87     }
88
89     /**
90      * Closes the connection and releases all related resources.
91      */

92     public void close() throws ProviderException {
93     }
94
95     /**
96      * Parses given string to find default Lucene fields for search query
97      * @param s given string
98      */

99     private void parseFields(String JavaDoc s) {
100         if (s == null) {
101             fields = new TreeSet JavaDoc<String JavaDoc>();
102             fields.add(DEFAULT_FIELD);
103         } else {
104             String JavaDoc[] strings = (' '+s+' ').split(",");
105             fields = new TreeSet JavaDoc<String JavaDoc>();
106             for (int i = 0; i < strings.length; i++) {
107                 strings[i]=strings[i].trim();
108                 if ("".equals(strings[i])) {
109                     fields.add(DEFAULT_FIELD); //default value
110
} else if ("*".contains(strings[i])) {
111                     try {
112                         IndexReader ir = IndexReader.open(url.getFile());
113                         fields.addAll(ir.getFieldNames(IndexReader.FieldOption.INDEXED));
114                     } catch (IOException JavaDoc e) {
115                         throw new LuceneProviderException("Failed to open lucene index.",e);
116                     }
117                 } else {
118                     fields.add(strings[i]);
119                 }
120
121             }
122         }
123     }
124
125 }
126
Popular Tags