001: /*
002: * Copyright 2006-2007 The Scriptella Project Team.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package scriptella.driver.lucene;
017:
018: import org.apache.lucene.index.IndexReader;
019: import scriptella.driver.text.TextProviderException;
020: import scriptella.spi.*;
021: import scriptella.util.IOUtils;
022:
023: import java.io.IOException;
024: import java.io.Reader;
025: import java.net.URL;
026: import java.util.Set;
027: import java.util.TreeSet;
028:
029: /**
030: * Represents a connection to a Lucene index.
031: * <p>For configuration details and examples see <a href="package-summary.html">overview page</a>.
032: *
033: * @author Kirill Volgin
034: * @version 1.0
035: */
036: public class LuceneConnection extends AbstractConnection {
037:
038: private static final String[] EMPTY_ARRAY = new String[] {};
039:
040: private URL url;
041: private Set<String> fields;
042: private static final String DEFAULT_FIELD = "contents";
043: private Boolean useMultiFieldQueryParser;
044: private Boolean useLowercaseExpandedTerms;
045:
046: /**
047: * Instantiates a new connection to Lucene Query.
048: * @param parameters connection parameters.
049: */
050: public LuceneConnection(ConnectionParameters parameters) {
051: super (Driver.DIALECT_IDENTIFIER, parameters);
052: url = parameters.getResolvedUrl();
053: parseFields((String) parameters.getProperty("fields"));
054: useMultiFieldQueryParser = parameters.getBooleanProperty(
055: "useMultiFieldQueryParser", false);
056: useLowercaseExpandedTerms = parameters.getBooleanProperty(
057: "useLowercaseExpandedTerms", true);
058: }
059:
060: public void executeScript(Resource scriptContent,
061: ParametersCallback parametersCallback)
062: throws ProviderException {
063: throw new UnsupportedOperationException(
064: "Script execution is not supported yet");
065: }
066:
067: /**
068: * Executes a query specified by its content.
069: * <p/>
070: *
071: * @param queryContent query content.
072: * @param parametersCallback callback to get parameter values.
073: * @param queryCallback callback to call for each result set element produced by this query.
074: * @see #executeScript(scriptella.spi.Resource, scriptella.spi.ParametersCallback)
075: */
076: public synchronized void executeQuery(Resource queryContent,
077: ParametersCallback parametersCallback,
078: QueryCallback queryCallback) throws ProviderException {
079: LuceneQuery query = null;
080: Reader r;
081: try {
082: r = queryContent.open();
083: } catch (IOException e) {
084: throw new TextProviderException(
085: "Cannot open a query for reading", e);
086: }
087: try {
088: query = new LuceneQuery(url.getFile(), parametersCallback,
089: queryCallback);
090: query.execute(r, fields, useMultiFieldQueryParser,
091: useLowercaseExpandedTerms);
092: } finally {
093: IOUtils.closeSilently(query);
094: }
095: }
096:
097: /**
098: * Closes the connection and releases all related resources.
099: */
100: public void close() throws ProviderException {
101: }
102:
103: /**
104: * Parses given string to find default Lucene fields for search query
105: * @param s given string
106: */
107: private void parseFields(String s) {
108: if (s == null) {
109: fields = new TreeSet<String>();
110: fields.add(DEFAULT_FIELD);
111: } else {
112: String[] strings = (' ' + s + ' ').split(",");
113: fields = new TreeSet<String>();
114: for (int i = 0; i < strings.length; i++) {
115: strings[i] = strings[i].trim();
116: if ("".equals(strings[i])) {
117: fields.add(DEFAULT_FIELD); //default value
118: } else if ("*".contains(strings[i])) {
119: try {
120: IndexReader ir = IndexReader
121: .open(url.getFile());
122: fields
123: .addAll(ir
124: .getFieldNames(IndexReader.FieldOption.INDEXED));
125: } catch (IOException e) {
126: throw new LuceneProviderException(
127: "Failed to open lucene index.", e);
128: }
129: } else {
130: fields.add(strings[i]);
131: }
132:
133: }
134: }
135: }
136:
137: }
|