You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by da...@apache.org on 2018/11/02 11:33:41 UTC
[20/25] lucene-solr:jira/gradle: Adding dataimporthandler-extras
module
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/d7c03684/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/FileDataSource.java
----------------------------------------------------------------------
diff --git a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/FileDataSource.java b/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/FileDataSource.java
deleted file mode 100644
index 920472e..0000000
--- a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/FileDataSource.java
+++ /dev/null
@@ -1,153 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.solr.handler.dataimport;
-
-import java.io.*;
-import java.lang.invoke.MethodHandles;
-import java.nio.charset.StandardCharsets;
-import java.util.Properties;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import static org.apache.solr.handler.dataimport.DataImportHandlerException.wrapAndThrow;
-import static org.apache.solr.handler.dataimport.DataImportHandlerException.SEVERE;
-
-/**
- * <p>
- * A {@link DataSource} which reads from local files
- * </p>
- * <p>
- * The file is read with the default platform encoding. It can be overriden by
- * specifying the encoding in solrconfig.xml
- * </p>
- * <p>
- * Refer to <a
- * href="http://wiki.apache.org/solr/DataImportHandler">http://wiki.apache.org/solr/DataImportHandler</a>
- * for more details.
- * </p>
- * <p>
- * <b>This API is experimental and may change in the future.</b>
- *
- * @since solr 1.3
- */
-public class FileDataSource extends DataSource<Reader> {
- public static final String BASE_PATH = "basePath";
-
- /**
- * The basePath for this data source
- */
- protected String basePath;
-
- /**
- * The encoding using which the given file should be read
- */
- protected String encoding = null;
-
- private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
-
- @Override
- public void init(Context context, Properties initProps) {
- basePath = initProps.getProperty(BASE_PATH);
- if (initProps.get(URLDataSource.ENCODING) != null)
- encoding = initProps.getProperty(URLDataSource.ENCODING);
- }
-
- /**
- * <p>
- * Returns a reader for the given file.
- * </p>
- * <p>
- * If the given file is not absolute, we try to construct an absolute path
- * using basePath configuration. If that fails, then the relative path is
- * tried. If file is not found a RuntimeException is thrown.
- * </p>
- * <p>
- * <b>It is the responsibility of the calling method to properly close the
- * returned Reader</b>
- * </p>
- */
- @Override
- public Reader getData(String query) {
- File f = getFile(basePath,query);
- try {
- return openStream(f);
- } catch (Exception e) {
- wrapAndThrow(SEVERE,e,"Unable to open File : "+f.getAbsolutePath());
- return null;
- }
- }
-
- static File getFile(String basePath, String query) {
- try {
- File file = new File(query);
-
- // If it's not an absolute path, try relative from basePath.
- if (!file.isAbsolute()) {
- // Resolve and correct basePath.
- File basePathFile;
- if (basePath == null) {
- basePathFile = new File(".").getAbsoluteFile();
- log.warn("FileDataSource.basePath is empty. " +
- "Resolving to: " + basePathFile.getAbsolutePath());
- } else {
- basePathFile = new File(basePath);
- if (!basePathFile.isAbsolute()) {
- basePathFile = basePathFile.getAbsoluteFile();
- log.warn("FileDataSource.basePath is not absolute. Resolving to: "
- + basePathFile.getAbsolutePath());
- }
- }
-
- file = new File(basePathFile, query).getAbsoluteFile();
- }
-
- if (file.isFile() && file.canRead()) {
- log.debug("Accessing File: " + file.getAbsolutePath());
- return file;
- } else {
- throw new FileNotFoundException("Could not find file: " + query +
- " (resolved to: " + file.getAbsolutePath());
- }
- } catch (FileNotFoundException e) {
- throw new RuntimeException(e);
- }
- }
-
- /**
- * Open a {@link java.io.Reader} for the given file name
- *
- * @param file a {@link java.io.File} instance
- * @return a Reader on the given file
- * @throws FileNotFoundException if the File does not exist
- * @throws UnsupportedEncodingException if the encoding is unsupported
- * @since solr 1.4
- */
- protected Reader openStream(File file) throws FileNotFoundException,
- UnsupportedEncodingException {
- if (encoding == null) {
- return new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8);
- } else {
- return new InputStreamReader(new FileInputStream(file), encoding);
- }
- }
-
- @Override
- public void close() {
-
- }
-}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/d7c03684/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/FileListEntityProcessor.java
----------------------------------------------------------------------
diff --git a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/FileListEntityProcessor.java b/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/FileListEntityProcessor.java
deleted file mode 100644
index a03354f..0000000
--- a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/FileListEntityProcessor.java
+++ /dev/null
@@ -1,305 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.solr.handler.dataimport;
-
-import java.io.File;
-import java.io.FilenameFilter;
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Locale;
-import java.util.Map;
-import java.util.TimeZone;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-import org.apache.solr.util.DateMathParser;
-
-/**
- * <p>
- * An {@link EntityProcessor} instance which can stream file names found in a given base
- * directory matching patterns and returning rows containing file information.
- * </p>
- * <p>
- * It supports querying a give base directory by matching:
- * <ul>
- * <li>regular expressions to file names</li>
- * <li>excluding certain files based on regular expression</li>
- * <li>last modification date (newer or older than a given date or time)</li>
- * <li>size (bigger or smaller than size given in bytes)</li>
- * <li>recursively iterating through sub-directories</li>
- * </ul>
- * Its output can be used along with {@link FileDataSource} to read from files in file
- * systems.
- * <p>
- * Refer to <a
- * href="http://wiki.apache.org/solr/DataImportHandler">http://wiki.apache.org/solr/DataImportHandler</a>
- * for more details.
- * </p>
- * <p>
- * <b>This API is experimental and may change in the future.</b>
- *
- * @since solr 1.3
- * @see Pattern
- */
-public class FileListEntityProcessor extends EntityProcessorBase {
- /**
- * A regex pattern to identify files given in data-config.xml after resolving any variables
- */
- protected String fileName;
-
- /**
- * The baseDir given in data-config.xml after resolving any variables
- */
- protected String baseDir;
-
- /**
- * A Regex pattern of excluded file names as given in data-config.xml after resolving any variables
- */
- protected String excludes;
-
- /**
- * The newerThan given in data-config as a {@link java.util.Date}
- * <p>
- * <b>Note: </b> This variable is resolved just-in-time in the {@link #nextRow()} method.
- * </p>
- */
- protected Date newerThan;
-
- /**
- * The newerThan given in data-config as a {@link java.util.Date}
- */
- protected Date olderThan;
-
- /**
- * The biggerThan given in data-config as a long value
- * <p>
- * <b>Note: </b> This variable is resolved just-in-time in the {@link #nextRow()} method.
- * </p>
- */
- protected long biggerThan = -1;
-
- /**
- * The smallerThan given in data-config as a long value
- * <p>
- * <b>Note: </b> This variable is resolved just-in-time in the {@link #nextRow()} method.
- * </p>
- */
- protected long smallerThan = -1;
-
- /**
- * The recursive given in data-config. Default value is false.
- */
- protected boolean recursive = false;
-
- private Pattern fileNamePattern, excludesPattern;
-
- @Override
- public void init(Context context) {
- super.init(context);
- fileName = context.getEntityAttribute(FILE_NAME);
- if (fileName != null) {
- fileName = context.replaceTokens(fileName);
- fileNamePattern = Pattern.compile(fileName);
- }
- baseDir = context.getEntityAttribute(BASE_DIR);
- if (baseDir == null)
- throw new DataImportHandlerException(DataImportHandlerException.SEVERE,
- "'baseDir' is a required attribute");
- baseDir = context.replaceTokens(baseDir);
- File dir = new File(baseDir);
- if (!dir.isDirectory())
- throw new DataImportHandlerException(DataImportHandlerException.SEVERE,
- "'baseDir' value: " + baseDir + " is not a directory");
-
- String r = context.getEntityAttribute(RECURSIVE);
- if (r != null)
- recursive = Boolean.parseBoolean(r);
- excludes = context.getEntityAttribute(EXCLUDES);
- if (excludes != null) {
- excludes = context.replaceTokens(excludes);
- excludesPattern = Pattern.compile(excludes);
- }
- }
-
- /**
- * Get the Date object corresponding to the given string.
- *
- * @param dateStr the date string. It can be a DateMath string or it may have a evaluator function
- * @return a Date instance corresponding to the input string
- */
- private Date getDate(String dateStr) {
- if (dateStr == null)
- return null;
-
- Matcher m = PLACE_HOLDER_PATTERN.matcher(dateStr);
- if (m.find()) {
- Object o = context.resolve(m.group(1));
- if (o instanceof Date) return (Date)o;
- dateStr = (String) o;
- } else {
- dateStr = context.replaceTokens(dateStr);
- }
- m = Evaluator.IN_SINGLE_QUOTES.matcher(dateStr);
- if (m.find()) {
- String expr = m.group(1);
- //TODO refactor DateMathParser.parseMath a bit to have a static method for this logic.
- if (expr.startsWith("NOW")) {
- expr = expr.substring("NOW".length());
- }
- try {
- // DWS TODO: is this TimeZone the right default for us? Deserves explanation if so.
- return new DateMathParser(TimeZone.getDefault()).parseMath(expr);
- } catch (ParseException exp) {
- throw new DataImportHandlerException(DataImportHandlerException.SEVERE,
- "Invalid expression for date", exp);
- }
- }
- try {
- return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ROOT).parse(dateStr);
- } catch (ParseException exp) {
- throw new DataImportHandlerException(DataImportHandlerException.SEVERE,
- "Invalid expression for date", exp);
- }
- }
-
- /**
- * Get the Long value for the given string after resolving any evaluator or variable.
- *
- * @param sizeStr the size as a string
- * @return the Long value corresponding to the given string
- */
- private Long getSize(String sizeStr) {
- if (sizeStr == null)
- return null;
-
- Matcher m = PLACE_HOLDER_PATTERN.matcher(sizeStr);
- if (m.find()) {
- Object o = context.resolve(m.group(1));
- if (o instanceof Number) {
- Number number = (Number) o;
- return number.longValue();
- }
- sizeStr = (String) o;
- } else {
- sizeStr = context.replaceTokens(sizeStr);
- }
-
- return Long.parseLong(sizeStr);
- }
-
- @Override
- public Map<String, Object> nextRow() {
- if (rowIterator != null)
- return getNext();
- List<Map<String, Object>> fileDetails = new ArrayList<>();
- File dir = new File(baseDir);
-
- String dateStr = context.getEntityAttribute(NEWER_THAN);
- newerThan = getDate(dateStr);
- dateStr = context.getEntityAttribute(OLDER_THAN);
- olderThan = getDate(dateStr);
- String biggerThanStr = context.getEntityAttribute(BIGGER_THAN);
- if (biggerThanStr != null)
- biggerThan = getSize(biggerThanStr);
- String smallerThanStr = context.getEntityAttribute(SMALLER_THAN);
- if (smallerThanStr != null)
- smallerThan = getSize(smallerThanStr);
-
- getFolderFiles(dir, fileDetails);
- rowIterator = fileDetails.iterator();
- return getNext();
- }
-
- private void getFolderFiles(File dir, final List<Map<String, Object>> fileDetails) {
- // Fetch an array of file objects that pass the filter, however the
- // returned array is never populated; accept() always returns false.
- // Rather we make use of the fileDetails array which is populated as
- // a side affect of the accept method.
- dir.list(new FilenameFilter() {
- @Override
- public boolean accept(File dir, String name) {
- File fileObj = new File(dir, name);
- if (fileObj.isDirectory()) {
- if (recursive) getFolderFiles(fileObj, fileDetails);
- } else if (fileNamePattern == null) {
- addDetails(fileDetails, dir, name);
- } else if (fileNamePattern.matcher(name).find()) {
- if (excludesPattern != null && excludesPattern.matcher(name).find())
- return false;
- addDetails(fileDetails, dir, name);
- }
- return false;
- }
- });
- }
-
- private void addDetails(List<Map<String, Object>> files, File dir, String name) {
- Map<String, Object> details = new HashMap<>();
- File aFile = new File(dir, name);
- if (aFile.isDirectory()) return;
- long sz = aFile.length();
- Date lastModified = new Date(aFile.lastModified());
- if (biggerThan != -1 && sz <= biggerThan)
- return;
- if (smallerThan != -1 && sz >= smallerThan)
- return;
- if (olderThan != null && lastModified.after(olderThan))
- return;
- if (newerThan != null && lastModified.before(newerThan))
- return;
- details.put(DIR, dir.getAbsolutePath());
- details.put(FILE, name);
- details.put(ABSOLUTE_FILE, aFile.getAbsolutePath());
- details.put(SIZE, sz);
- details.put(LAST_MODIFIED, lastModified);
- files.add(details);
- }
-
- public static final Pattern PLACE_HOLDER_PATTERN = Pattern
- .compile("\\$\\{(.*?)\\}");
-
- public static final String DIR = "fileDir";
-
- public static final String FILE = "file";
-
- public static final String ABSOLUTE_FILE = "fileAbsolutePath";
-
- public static final String SIZE = "fileSize";
-
- public static final String LAST_MODIFIED = "fileLastModified";
-
- public static final String FILE_NAME = "fileName";
-
- public static final String BASE_DIR = "baseDir";
-
- public static final String EXCLUDES = "excludes";
-
- public static final String NEWER_THAN = "newerThan";
-
- public static final String OLDER_THAN = "olderThan";
-
- public static final String BIGGER_THAN = "biggerThan";
-
- public static final String SMALLER_THAN = "smallerThan";
-
- public static final String RECURSIVE = "recursive";
-
-}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/d7c03684/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/HTMLStripTransformer.java
----------------------------------------------------------------------
diff --git a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/HTMLStripTransformer.java b/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/HTMLStripTransformer.java
deleted file mode 100644
index e62c329..0000000
--- a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/HTMLStripTransformer.java
+++ /dev/null
@@ -1,95 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.solr.handler.dataimport;
-
-import org.apache.lucene.analysis.charfilter.HTMLStripCharFilter;
-
-import java.io.IOException;
-import java.io.StringReader;
-import java.io.BufferedReader;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-
-/**
- * A {@link Transformer} implementation which strip off HTML tags using {@link HTMLStripCharFilter} This is useful
- * in case you don't need this HTML anyway.
- *
- * @see HTMLStripCharFilter
- * @since solr 1.4
- */
-public class HTMLStripTransformer extends Transformer {
-
- @Override
- @SuppressWarnings("unchecked")
- public Object transformRow(Map<String, Object> row, Context context) {
- List<Map<String, String>> fields = context.getAllEntityFields();
- for (Map<String, String> field : fields) {
- String col = field.get(DataImporter.COLUMN);
- String splitHTML = context.replaceTokens(field.get(STRIP_HTML));
- if (!TRUE.equals(splitHTML))
- continue;
- Object tmpVal = row.get(col);
- if (tmpVal == null)
- continue;
-
- if (tmpVal instanceof List) {
- List<String> inputs = (List<String>) tmpVal;
- List results = new ArrayList();
- for (String input : inputs) {
- if (input == null)
- continue;
- Object o = stripHTML(input, col);
- if (o != null)
- results.add(o);
- }
- row.put(col, results);
- } else {
- String value = tmpVal.toString();
- Object o = stripHTML(value, col);
- if (o != null)
- row.put(col, o);
- }
- }
- return row;
- }
-
- private Object stripHTML(String value, String column) {
- StringBuilder out = new StringBuilder();
- StringReader strReader = new StringReader(value);
- try {
- HTMLStripCharFilter html = new HTMLStripCharFilter(strReader.markSupported() ? strReader : new BufferedReader(strReader));
- char[] cbuf = new char[1024 * 10];
- while (true) {
- int count = html.read(cbuf);
- if (count == -1)
- break; // end of stream mark is -1
- if (count > 0)
- out.append(cbuf, 0, count);
- }
- html.close();
- } catch (IOException e) {
- throw new DataImportHandlerException(DataImportHandlerException.SEVERE,
- "Failed stripping HTML for column: " + column, e);
- }
- return out.toString();
- }
-
- public static final String STRIP_HTML = "stripHTML";
-
- public static final String TRUE = "true";
-}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/d7c03684/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/JdbcDataSource.java
----------------------------------------------------------------------
diff --git a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/JdbcDataSource.java b/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/JdbcDataSource.java
deleted file mode 100644
index a8eed55..0000000
--- a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/JdbcDataSource.java
+++ /dev/null
@@ -1,593 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.solr.handler.dataimport;
-
-import static java.nio.charset.StandardCharsets.UTF_8;
-import static org.apache.solr.handler.dataimport.DataImportHandlerException.wrapAndThrow;
-import static org.apache.solr.handler.dataimport.DataImportHandlerException.SEVERE;
-
-import org.apache.solr.common.SolrException;
-import org.apache.solr.util.CryptoKeys;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
-
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.Reader;
-import java.lang.invoke.MethodHandles;
-import java.math.BigDecimal;
-import java.math.BigInteger;
-import java.sql.*;
-import java.util.*;
-import java.util.concurrent.Callable;
-import java.util.concurrent.TimeUnit;
-
-/**
- * <p> A DataSource implementation which can fetch data using JDBC. </p> <p> Refer to <a
- * href="http://wiki.apache.org/solr/DataImportHandler">http://wiki.apache.org/solr/DataImportHandler</a> for more
- * details. </p>
- * <p>
- * <b>This API is experimental and may change in the future.</b>
- *
- * @since solr 1.3
- */
-public class JdbcDataSource extends
- DataSource<Iterator<Map<String, Object>>> {
- private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
-
- protected Callable<Connection> factory;
-
- private long connLastUsed = 0;
-
- private Connection conn;
-
- private ResultSetIterator resultSetIterator;
-
- private Map<String, Integer> fieldNameVsType = new HashMap<>();
-
- private boolean convertType = false;
-
- private int batchSize = FETCH_SIZE;
-
- private int maxRows = 0;
-
- @Override
- public void init(Context context, Properties initProps) {
- resolveVariables(context, initProps);
- initProps = decryptPwd(context, initProps);
- Object o = initProps.get(CONVERT_TYPE);
- if (o != null)
- convertType = Boolean.parseBoolean(o.toString());
-
- factory = createConnectionFactory(context, initProps);
-
- String bsz = initProps.getProperty("batchSize");
- if (bsz != null) {
- bsz = context.replaceTokens(bsz);
- try {
- batchSize = Integer.parseInt(bsz);
- if (batchSize == -1)
- batchSize = Integer.MIN_VALUE;
- } catch (NumberFormatException e) {
- log.warn("Invalid batch size: " + bsz);
- }
- }
-
- for (Map<String, String> map : context.getAllEntityFields()) {
- String n = map.get(DataImporter.COLUMN);
- String t = map.get(DataImporter.TYPE);
- if ("sint".equals(t) || "integer".equals(t))
- fieldNameVsType.put(n, Types.INTEGER);
- else if ("slong".equals(t) || "long".equals(t))
- fieldNameVsType.put(n, Types.BIGINT);
- else if ("float".equals(t) || "sfloat".equals(t))
- fieldNameVsType.put(n, Types.FLOAT);
- else if ("double".equals(t) || "sdouble".equals(t))
- fieldNameVsType.put(n, Types.DOUBLE);
- else if ("date".equals(t))
- fieldNameVsType.put(n, Types.DATE);
- else if ("boolean".equals(t))
- fieldNameVsType.put(n, Types.BOOLEAN);
- else if ("binary".equals(t))
- fieldNameVsType.put(n, Types.BLOB);
- else
- fieldNameVsType.put(n, Types.VARCHAR);
- }
- }
-
- private Properties decryptPwd(Context context, Properties initProps) {
- String encryptionKey = initProps.getProperty("encryptKeyFile");
- if (initProps.getProperty("password") != null && encryptionKey != null) {
- // this means the password is encrypted and use the file to decode it
- try {
- try (Reader fr = new InputStreamReader(new FileInputStream(encryptionKey), UTF_8)) {
- char[] chars = new char[100];//max 100 char password
- int len = fr.read(chars);
- if (len < 6)
- throw new DataImportHandlerException(SEVERE, "There should be a password of length 6 atleast " + encryptionKey);
- Properties props = new Properties();
- props.putAll(initProps);
- String password = null;
- try {
- password = CryptoKeys.decodeAES(initProps.getProperty("password"), new String(chars, 0, len)).trim();
- } catch (SolrException se) {
- throw new DataImportHandlerException(SEVERE, "Error decoding password", se.getCause());
- }
- props.put("password", password);
- initProps = props;
- }
- } catch (IOException e) {
- throw new DataImportHandlerException(SEVERE, "Could not load encryptKeyFile " + encryptionKey);
- }
- }
- return initProps;
- }
-
- protected Callable<Connection> createConnectionFactory(final Context context,
- final Properties initProps) {
-// final VariableResolver resolver = context.getVariableResolver();
- final String jndiName = initProps.getProperty(JNDI_NAME);
- final String url = initProps.getProperty(URL);
- final String driver = initProps.getProperty(DRIVER);
-
- if (url == null && jndiName == null)
- throw new DataImportHandlerException(SEVERE,
- "JDBC URL or JNDI name has to be specified");
-
- if (driver != null) {
- try {
- DocBuilder.loadClass(driver, context.getSolrCore());
- } catch (ClassNotFoundException e) {
- wrapAndThrow(SEVERE, e, "Could not load driver: " + driver);
- }
- } else {
- if(jndiName == null){
- throw new DataImportHandlerException(SEVERE, "One of driver or jndiName must be specified in the data source");
- }
- }
-
- String s = initProps.getProperty("maxRows");
- if (s != null) {
- maxRows = Integer.parseInt(s);
- }
-
- return factory = new Callable<Connection>() {
- @Override
- public Connection call() throws Exception {
- log.info("Creating a connection for entity "
- + context.getEntityAttribute(DataImporter.NAME) + " with URL: "
- + url);
- long start = System.nanoTime();
- Connection c = null;
-
- if (jndiName != null) {
- c = getFromJndi(initProps, jndiName);
- } else if (url != null) {
- try {
- c = DriverManager.getConnection(url, initProps);
- } catch (SQLException e) {
- // DriverManager does not allow you to use a driver which is not loaded through
- // the class loader of the class which is trying to make the connection.
- // This is a workaround for cases where the user puts the driver jar in the
- // solr.home/lib or solr.home/core/lib directories.
- Driver d = (Driver) DocBuilder.loadClass(driver, context.getSolrCore()).newInstance();
- c = d.connect(url, initProps);
- }
- }
- if (c != null) {
- try {
- initializeConnection(c, initProps);
- } catch (SQLException e) {
- try {
- c.close();
- } catch (SQLException e2) {
- log.warn("Exception closing connection during cleanup", e2);
- }
-
- throw new DataImportHandlerException(SEVERE, "Exception initializing SQL connection", e);
- }
- }
- log.info("Time taken for getConnection(): "
- + TimeUnit.MILLISECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS));
- return c;
- }
-
- private void initializeConnection(Connection c, final Properties initProps)
- throws SQLException {
- if (Boolean.parseBoolean(initProps.getProperty("readOnly"))) {
- c.setReadOnly(true);
- // Add other sane defaults
- c.setAutoCommit(true);
- c.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
- c.setHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT);
- }
- if (!Boolean.parseBoolean(initProps.getProperty("autoCommit"))) {
- c.setAutoCommit(false);
- }
- String transactionIsolation = initProps.getProperty("transactionIsolation");
- if ("TRANSACTION_READ_UNCOMMITTED".equals(transactionIsolation)) {
- c.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
- } else if ("TRANSACTION_READ_COMMITTED".equals(transactionIsolation)) {
- c.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
- } else if ("TRANSACTION_REPEATABLE_READ".equals(transactionIsolation)) {
- c.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
- } else if ("TRANSACTION_SERIALIZABLE".equals(transactionIsolation)) {
- c.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
- } else if ("TRANSACTION_NONE".equals(transactionIsolation)) {
- c.setTransactionIsolation(Connection.TRANSACTION_NONE);
- }
- String holdability = initProps.getProperty("holdability");
- if ("CLOSE_CURSORS_AT_COMMIT".equals(holdability)) {
- c.setHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT);
- } else if ("HOLD_CURSORS_OVER_COMMIT".equals(holdability)) {
- c.setHoldability(ResultSet.HOLD_CURSORS_OVER_COMMIT);
- }
- }
-
- private Connection getFromJndi(final Properties initProps, final String jndiName) throws NamingException,
- SQLException {
-
- Connection c = null;
- InitialContext ctx = new InitialContext();
- Object jndival = ctx.lookup(jndiName);
- if (jndival instanceof javax.sql.DataSource) {
- javax.sql.DataSource dataSource = (javax.sql.DataSource) jndival;
- String user = (String) initProps.get("user");
- String pass = (String) initProps.get("password");
- if(user == null || user.trim().equals("")){
- c = dataSource.getConnection();
- } else {
- c = dataSource.getConnection(user, pass);
- }
- } else {
- throw new DataImportHandlerException(SEVERE,
- "the jndi name : '"+jndiName +"' is not a valid javax.sql.DataSource");
- }
- return c;
- }
- };
- }
-
- private void resolveVariables(Context ctx, Properties initProps) {
- for (Map.Entry<Object, Object> entry : initProps.entrySet()) {
- if (entry.getValue() != null) {
- entry.setValue(ctx.replaceTokens((String) entry.getValue()));
- }
- }
- }
-
- @Override
- public Iterator<Map<String, Object>> getData(String query) {
- if (resultSetIterator != null) {
- resultSetIterator.close();
- resultSetIterator = null;
- }
- resultSetIterator = createResultSetIterator(query);
- return resultSetIterator.getIterator();
- }
-
- protected ResultSetIterator createResultSetIterator(String query) {
- return new ResultSetIterator(query);
- }
-
- private void logError(String msg, Exception e) {
- log.warn(msg, e);
- }
-
- protected List<String> readFieldNames(ResultSetMetaData metaData)
- throws SQLException {
- List<String> colNames = new ArrayList<>();
- int count = metaData.getColumnCount();
- for (int i = 0; i < count; i++) {
- colNames.add(metaData.getColumnLabel(i + 1));
- }
- return colNames;
- }
-
- protected class ResultSetIterator {
- private ResultSet resultSet;
-
- private Statement stmt = null;
-
- private List<String> colNames;
-
- private Iterator<Map<String, Object>> rSetIterator;
-
- public ResultSetIterator(String query) {
-
- try {
- Connection c = getConnection();
- stmt = createStatement(c, batchSize, maxRows);
- log.debug("Executing SQL: " + query);
- long start = System.nanoTime();
- resultSet = executeStatement(stmt, query);
- log.trace("Time taken for sql :"
- + TimeUnit.MILLISECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS));
- setColNames(resultSet);
- } catch (Exception e) {
- close();
- wrapAndThrow(SEVERE, e, "Unable to execute query: " + query);
- return;
- }
- if (resultSet == null) {
- close();
- rSetIterator = new ArrayList<Map<String, Object>>().iterator();
- return;
- }
-
- rSetIterator = createIterator(convertType, fieldNameVsType);
- }
-
-
- protected Statement createStatement(final Connection c, final int batchSize, final int maxRows)
- throws SQLException {
- Statement statement = c.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
- statement.setFetchSize(batchSize);
- statement.setMaxRows(maxRows);
- return statement;
- }
-
- protected ResultSet executeStatement(Statement statement, String query) throws SQLException {
- boolean resultSetReturned = statement.execute(query);
- return getNextResultSet(resultSetReturned, statement);
- }
-
- protected ResultSet getNextResultSet(final boolean initialResultSetAvailable, final Statement statement) throws SQLException {
- boolean resultSetAvailable = initialResultSetAvailable;
- while (!resultSetAvailable && statement.getUpdateCount() != -1) {
- resultSetAvailable = statement.getMoreResults();
- }
- if (resultSetAvailable) {
- return statement.getResultSet();
- }
- return null;
- }
-
- protected void setColNames(final ResultSet resultSet) throws SQLException {
- if (resultSet != null) {
- colNames = readFieldNames(resultSet.getMetaData());
- } else {
- colNames = Collections.emptyList();
- }
- }
-
- protected Iterator<Map<String,Object>> createIterator(final boolean convertType,
- final Map<String,Integer> fieldNameVsType) {
- return new Iterator<Map<String,Object>>() {
- @Override
- public boolean hasNext() {
- return hasnext();
- }
-
- @Override
- public Map<String,Object> next() {
- return getARow(convertType, fieldNameVsType);
- }
-
- @Override
- public void remove() {/* do nothing */
- }
- };
- }
-
-
-
- protected Map<String,Object> getARow(boolean convertType, Map<String,Integer> fieldNameVsType) {
- if (getResultSet() == null)
- return null;
- Map<String, Object> result = new HashMap<>();
- for (String colName : getColNames()) {
- try {
- if (!convertType) {
- // Use underlying database's type information except for BigDecimal and BigInteger
- // which cannot be serialized by JavaBin/XML. See SOLR-6165
- Object value = getResultSet().getObject(colName);
- if (value instanceof BigDecimal || value instanceof BigInteger) {
- result.put(colName, value.toString());
- } else {
- result.put(colName, value);
- }
- continue;
- }
-
- Integer type = fieldNameVsType.get(colName);
- if (type == null)
- type = Types.VARCHAR;
- switch (type) {
- case Types.INTEGER:
- result.put(colName, getResultSet().getInt(colName));
- break;
- case Types.FLOAT:
- result.put(colName, getResultSet().getFloat(colName));
- break;
- case Types.BIGINT:
- result.put(colName, getResultSet().getLong(colName));
- break;
- case Types.DOUBLE:
- result.put(colName, getResultSet().getDouble(colName));
- break;
- case Types.DATE:
- result.put(colName, getResultSet().getTimestamp(colName));
- break;
- case Types.BOOLEAN:
- result.put(colName, getResultSet().getBoolean(colName));
- break;
- case Types.BLOB:
- result.put(colName, getResultSet().getBytes(colName));
- break;
- default:
- result.put(colName, getResultSet().getString(colName));
- break;
- }
- } catch (SQLException e) {
- logError("Error reading data ", e);
- wrapAndThrow(SEVERE, e, "Error reading data from database");
- }
- }
- return result;
- }
-
- protected boolean hasnext() {
- if (getResultSet() == null) {
- close();
- return false;
- }
- try {
- if (getResultSet().next()) {
- return true;
- } else {
- closeResultSet();
- setResultSet(getNextResultSet(getStatement().getMoreResults(), getStatement()));
- setColNames(getResultSet());
- return hasnext();
- }
- } catch (SQLException e) {
- close();
- wrapAndThrow(SEVERE,e);
- return false;
- }
- }
-
- protected void close() {
- closeResultSet();
- try {
- if (getStatement() != null)
- getStatement().close();
- } catch (Exception e) {
- logError("Exception while closing statement", e);
- } finally {
- setStatement(null);
- }
- }
-
- protected void closeResultSet() {
- try {
- if (getResultSet() != null) {
- getResultSet().close();
- }
- } catch (Exception e) {
- logError("Exception while closing result set", e);
- } finally {
- setResultSet(null);
- }
- }
-
- protected final Iterator<Map<String,Object>> getIterator() {
- return rSetIterator;
- }
-
-
- protected final Statement getStatement() {
- return stmt;
- }
-
- protected final void setStatement(Statement stmt) {
- this.stmt = stmt;
- }
-
- protected final ResultSet getResultSet() {
- return resultSet;
- }
-
- protected final void setResultSet(ResultSet resultSet) {
- this.resultSet = resultSet;
- }
-
- protected final List<String> getColNames() {
- return colNames;
- }
-
- protected final void setColNames(List<String> colNames) {
- this.colNames = colNames;
- }
-
- }
-
- protected Connection getConnection() throws Exception {
- long currTime = System.nanoTime();
- if (currTime - connLastUsed > CONN_TIME_OUT) {
- synchronized (this) {
- Connection tmpConn = factory.call();
- closeConnection();
- connLastUsed = System.nanoTime();
- return conn = tmpConn;
- }
-
- } else {
- connLastUsed = currTime;
- return conn;
- }
- }
-
- @Override
- protected void finalize() throws Throwable {
- try {
- if(!isClosed){
- log.error("JdbcDataSource was not closed prior to finalize(), indicates a bug -- POSSIBLE RESOURCE LEAK!!!");
- close();
- }
- } finally {
- super.finalize();
- }
- }
-
- private boolean isClosed = false;
-
- @Override
- public void close() {
- if (resultSetIterator != null) {
- resultSetIterator.close();
- }
- try {
- closeConnection();
- } finally {
- isClosed = true;
- }
- }
-
- private void closeConnection() {
- try {
- if (conn != null) {
- try {
- //SOLR-2045
- conn.commit();
- } catch(Exception ex) {
- //ignore.
- }
- conn.close();
- }
- } catch (Exception e) {
- log.error("Ignoring Error when closing connection", e);
- }
- }
-
- private static final long CONN_TIME_OUT = TimeUnit.NANOSECONDS.convert(10, TimeUnit.SECONDS);
-
- private static final int FETCH_SIZE = 500;
-
- public static final String URL = "url";
-
- public static final String JNDI_NAME = "jndiName";
-
- public static final String DRIVER = "driver";
-
- public static final String CONVERT_TYPE = "convertType";
-}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/d7c03684/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/LineEntityProcessor.java
----------------------------------------------------------------------
diff --git a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/LineEntityProcessor.java b/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/LineEntityProcessor.java
deleted file mode 100644
index 0940cbd..0000000
--- a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/LineEntityProcessor.java
+++ /dev/null
@@ -1,164 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.solr.handler.dataimport;
-
-import java.io.*;
-import java.util.*;
-import java.util.regex.Pattern;
-
-import org.apache.commons.io.IOUtils;
-
-
-/**
- * <p>
- * An {@link EntityProcessor} instance which can stream lines of text read from a
- * datasource. Options allow lines to be explicitly skipped or included in the index.
- * </p>
- * <p>
- * Attribute summary
- * <ul>
- * <li>url is the required location of the input file. If this value is
- * relative, it assumed to be relative to baseLoc.</li>
- * <li>acceptLineRegex is an optional attribute that if present discards any
- * line which does not match the regExp.</li>
- * <li>skipLineRegex is an optional attribute that is applied after any
- * acceptLineRegex and discards any line which matches this regExp.</li>
- * </ul>
- * <p>
- * Although envisioned for reading lines from a file or url, LineEntityProcessor may also be useful
- * for dealing with change lists, where each line contains filenames which can be used by subsequent entities
- * to parse content from those files.
- * <p>
- * Refer to <a
- * href="http://wiki.apache.org/solr/DataImportHandler">http://wiki.apache.org/solr/DataImportHandler</a>
- * for more details.
- * </p>
- * <p>
- * <b>This API is experimental and may change in the future.</b>
- *
- * @since solr 1.4
- * @see Pattern
- */
-public class LineEntityProcessor extends EntityProcessorBase {
- private Pattern acceptLineRegex, skipLineRegex;
- private String url;
- private BufferedReader reader;
-
- /**
- * Parses each of the entity attributes.
- */
- @Override
- public void init(Context context) {
- super.init(context);
- String s;
-
- // init a regex to locate files from the input we want to index
- s = context.getResolvedEntityAttribute(ACCEPT_LINE_REGEX);
- if (s != null) {
- acceptLineRegex = Pattern.compile(s);
- }
-
- // init a regex to locate files from the input to be skipped
- s = context.getResolvedEntityAttribute(SKIP_LINE_REGEX);
- if (s != null) {
- skipLineRegex = Pattern.compile(s);
- }
-
- // the FileName is required.
- url = context.getResolvedEntityAttribute(URL);
- if (url == null) throw
- new DataImportHandlerException(DataImportHandlerException.SEVERE,
- "'"+ URL +"' is a required attribute");
- }
-
-
- /**
- * Reads lines from the url till it finds a lines that matches the
- * optional acceptLineRegex and does not match the optional skipLineRegex.
- *
- * @return A row containing a minimum of one field "rawLine" or null to signal
- * end of file. The rawLine is the as line as returned by readLine()
- * from the url. However transformers can be used to create as
- * many other fields as required.
- */
- @Override
- public Map<String, Object> nextRow() {
- if (reader == null) {
- reader = new BufferedReader((Reader) context.getDataSource().getData(url));
- }
-
- String line;
-
- while ( true ) {
- // read a line from the input file
- try {
- line = reader.readLine();
- }
- catch (IOException exp) {
- throw new DataImportHandlerException(DataImportHandlerException.SEVERE,
- "Problem reading from input", exp);
- }
-
- // end of input
- if (line == null) {
- closeResources();
- return null;
- }
-
- // First scan whole line to see if we want it
- if (acceptLineRegex != null && ! acceptLineRegex.matcher(line).find()) continue;
- if (skipLineRegex != null && skipLineRegex.matcher(line).find()) continue;
- // Contruct the 'row' of fields
- Map<String, Object> row = new HashMap<>();
- row.put("rawLine", line);
- return row;
- }
- }
-
- public void closeResources() {
- if (reader != null) {
- IOUtils.closeQuietly(reader);
- }
- reader= null;
- }
-
- @Override
- public void destroy() {
- closeResources();
- super.destroy();
- }
-
- /**
- * Holds the name of entity attribute that will be parsed to obtain
- * the filename containing the changelist.
- */
- public static final String URL = "url";
-
- /**
- * Holds the name of entity attribute that will be parsed to obtain
- * the pattern to be used when checking to see if a line should
- * be returned.
- */
- public static final String ACCEPT_LINE_REGEX = "acceptLineRegex";
-
- /**
- * Holds the name of entity attribute that will be parsed to obtain
- * the pattern to be used when checking to see if a line should
- * be ignored.
- */
- public static final String SKIP_LINE_REGEX = "skipLineRegex";
-}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/d7c03684/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/LogTransformer.java
----------------------------------------------------------------------
diff --git a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/LogTransformer.java b/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/LogTransformer.java
deleted file mode 100644
index 66c525e..0000000
--- a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/LogTransformer.java
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.solr.handler.dataimport;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.lang.invoke.MethodHandles;
-import java.util.Map;
-
-/**
- * A {@link Transformer} implementation which logs messages in a given template format.
- * <p>
- * Refer to <a href="http://wiki.apache.org/solr/DataImportHandler">http://wiki.apache.org/solr/DataImportHandler</a>
- * for more details.
- * <p>
- * <b>This API is experimental and may change in the future.</b>
- *
- * @since solr 1.4
- */
-public class LogTransformer extends Transformer {
- private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
-
- @Override
- public Object transformRow(Map<String, Object> row, Context ctx) {
- String expr = ctx.getEntityAttribute(LOG_TEMPLATE);
- String level = ctx.replaceTokens(ctx.getEntityAttribute(LOG_LEVEL));
-
- if (expr == null || level == null) return row;
-
- if ("info".equals(level)) {
- if (log.isInfoEnabled())
- log.info(ctx.replaceTokens(expr));
- } else if ("trace".equals(level)) {
- if (log.isTraceEnabled())
- log.trace(ctx.replaceTokens(expr));
- } else if ("warn".equals(level)) {
- if (log.isWarnEnabled())
- log.warn(ctx.replaceTokens(expr));
- } else if ("error".equals(level)) {
- if (log.isErrorEnabled())
- log.error(ctx.replaceTokens(expr));
- } else if ("debug".equals(level)) {
- if (log.isDebugEnabled())
- log.debug(ctx.replaceTokens(expr));
- }
-
- return row;
- }
-
- public static final String LOG_TEMPLATE = "logTemplate";
- public static final String LOG_LEVEL = "logLevel";
-}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/d7c03684/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/MockDataSource.java
----------------------------------------------------------------------
diff --git a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/MockDataSource.java b/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/MockDataSource.java
deleted file mode 100644
index 8989ea2..0000000
--- a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/MockDataSource.java
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.solr.handler.dataimport;
-
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.Properties;
-
-/**
- * <p>
- * A mock DataSource implementation which can be used for testing.
- * </p>
- * <p>
- * <b>This API is experimental and may change in the future.</b>
- *
- * @since solr 1.3
- */
-public class MockDataSource extends
- DataSource<Iterator<Map<String, Object>>> {
-
- private static Map<String, Iterator<Map<String, Object>>> cache = new HashMap<>();
-
- public static void setIterator(String query,
- Iterator<Map<String, Object>> iter) {
- cache.put(query, iter);
- }
-
- public static void clearCache() {
- cache.clear();
- }
-
- @Override
- public void init(Context context, Properties initProps) {
- }
-
- @Override
- public Iterator<Map<String, Object>> getData(String query) {
- return cache.get(query);
- }
-
- @Override
- public void close() {
- cache.clear();
-
- }
-}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/d7c03684/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/NumberFormatTransformer.java
----------------------------------------------------------------------
diff --git a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/NumberFormatTransformer.java b/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/NumberFormatTransformer.java
deleted file mode 100644
index 349b14e..0000000
--- a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/NumberFormatTransformer.java
+++ /dev/null
@@ -1,133 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.solr.handler.dataimport;
-import java.text.NumberFormat;
-import java.text.ParseException;
-import java.text.ParsePosition;
-import java.util.ArrayList;
-import java.util.IllformedLocaleException;
-import java.util.List;
-import java.util.Locale;
-import java.util.Map;
-
-/**
- * <p>
- * A {@link Transformer} instance which can extract numbers out of strings. It uses
- * {@link NumberFormat} class to parse strings and supports
- * Number, Integer, Currency and Percent styles as supported by
- * {@link NumberFormat} with configurable locales.
- * </p>
- * <p>
- * Refer to <a
- * href="http://wiki.apache.org/solr/DataImportHandler">http://wiki.apache.org/solr/DataImportHandler</a>
- * for more details.
- * </p>
- * <p>
- * <b>This API is experimental and may change in the future.</b>
- *
- * @since solr 1.3
- */
-public class NumberFormatTransformer extends Transformer {
-
- @Override
- @SuppressWarnings("unchecked")
- public Object transformRow(Map<String, Object> row, Context context) {
- for (Map<String, String> fld : context.getAllEntityFields()) {
- String style = context.replaceTokens(fld.get(FORMAT_STYLE));
- if (style != null) {
- String column = fld.get(DataImporter.COLUMN);
- String srcCol = fld.get(RegexTransformer.SRC_COL_NAME);
- String localeStr = context.replaceTokens(fld.get(LOCALE));
- if (srcCol == null)
- srcCol = column;
- Locale locale = Locale.ROOT;
- if (localeStr != null) {
- try {
- locale = new Locale.Builder().setLanguageTag(localeStr).build();
- } catch (IllformedLocaleException e) {
- throw new DataImportHandlerException(DataImportHandlerException.SEVERE,
- "Invalid Locale '" + localeStr + "' specified for field: " + fld, e);
- }
- }
-
- Object val = row.get(srcCol);
- String styleSmall = style.toLowerCase(Locale.ROOT);
-
- if (val instanceof List) {
- List<String> inputs = (List) val;
- List results = new ArrayList();
- for (String input : inputs) {
- try {
- results.add(process(input, styleSmall, locale));
- } catch (ParseException e) {
- throw new DataImportHandlerException(
- DataImportHandlerException.SEVERE,
- "Failed to apply NumberFormat on column: " + column, e);
- }
- }
- row.put(column, results);
- } else {
- if (val == null || val.toString().trim().equals(""))
- continue;
- try {
- row.put(column, process(val.toString(), styleSmall, locale));
- } catch (ParseException e) {
- throw new DataImportHandlerException(
- DataImportHandlerException.SEVERE,
- "Failed to apply NumberFormat on column: " + column, e);
- }
- }
- }
- }
- return row;
- }
-
- private Number process(String val, String style, Locale locale) throws ParseException {
- if (INTEGER.equals(style)) {
- return parseNumber(val, NumberFormat.getIntegerInstance(locale));
- } else if (NUMBER.equals(style)) {
- return parseNumber(val, NumberFormat.getNumberInstance(locale));
- } else if (CURRENCY.equals(style)) {
- return parseNumber(val, NumberFormat.getCurrencyInstance(locale));
- } else if (PERCENT.equals(style)) {
- return parseNumber(val, NumberFormat.getPercentInstance(locale));
- }
-
- return null;
- }
-
- private Number parseNumber(String val, NumberFormat numFormat) throws ParseException {
- ParsePosition parsePos = new ParsePosition(0);
- Number num = numFormat.parse(val, parsePos);
- if (parsePos.getIndex() != val.length()) {
- throw new ParseException("illegal number format", parsePos.getIndex());
- }
- return num;
- }
-
- public static final String FORMAT_STYLE = "formatStyle";
-
- public static final String LOCALE = "locale";
-
- public static final String NUMBER = "number";
-
- public static final String PERCENT = "percent";
-
- public static final String INTEGER = "integer";
-
- public static final String CURRENCY = "currency";
-}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/d7c03684/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/PlainTextEntityProcessor.java
----------------------------------------------------------------------
diff --git a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/PlainTextEntityProcessor.java b/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/PlainTextEntityProcessor.java
deleted file mode 100644
index c75608c..0000000
--- a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/PlainTextEntityProcessor.java
+++ /dev/null
@@ -1,77 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.solr.handler.dataimport;
-
-import static org.apache.solr.handler.dataimport.DataImportHandlerException.SEVERE;
-import static org.apache.solr.handler.dataimport.DataImportHandlerException.wrapAndThrow;
-import static org.apache.solr.handler.dataimport.XPathEntityProcessor.URL;
-import org.apache.commons.io.IOUtils;
-
-import java.io.IOException;
-import java.io.Reader;
-import java.io.StringWriter;
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- * <p>An implementation of {@link EntityProcessor} which reads data from a url/file and give out a row which contains one String
- * value. The name of the field is 'plainText'.
- *
- * @since solr 1.4
- */
-public class PlainTextEntityProcessor extends EntityProcessorBase {
- private boolean ended = false;
-
- @Override
- public void init(Context context) {
- super.init(context);
- ended = false;
- }
-
- @Override
- public Map<String, Object> nextRow() {
- if (ended) return null;
- DataSource<Reader> ds = context.getDataSource();
- String url = context.replaceTokens(context.getEntityAttribute(URL));
- Reader r = null;
- try {
- r = ds.getData(url);
- } catch (Exception e) {
- wrapAndThrow(SEVERE, e, "Exception reading url : " + url);
- }
- StringWriter sw = new StringWriter();
- char[] buf = new char[1024];
- while (true) {
- int len = 0;
- try {
- len = r.read(buf);
- } catch (IOException e) {
- IOUtils.closeQuietly(r);
- wrapAndThrow(SEVERE, e, "Exception reading url : " + url);
- }
- if (len <= 0) break;
- sw.append(new String(buf, 0, len));
- }
- Map<String, Object> row = new HashMap<>();
- row.put(PLAIN_TEXT, sw.toString());
- ended = true;
- IOUtils.closeQuietly(r);
- return row;
- }
-
- public static final String PLAIN_TEXT = "plainText";
-}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/d7c03684/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/RegexTransformer.java
----------------------------------------------------------------------
diff --git a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/RegexTransformer.java b/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/RegexTransformer.java
deleted file mode 100644
index 7a919de..0000000
--- a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/RegexTransformer.java
+++ /dev/null
@@ -1,201 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.solr.handler.dataimport;
-
-import java.lang.invoke.MethodHandles;
-import java.util.*;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-/**
- * <p>
- * A {@link Transformer} implementation which uses Regular Expressions to extract, split
- * and replace data in fields.
- * </p>
- * <p>
- * Refer to <a
- * href="http://wiki.apache.org/solr/DataImportHandler">http://wiki.apache.org/solr/DataImportHandler</a>
- * for more details.
- * </p>
- * <p>
- * <b>This API is experimental and may change in the future.</b>
- *
- * @since solr 1.3
- * @see Pattern
- */
-public class RegexTransformer extends Transformer {
- private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
-
- @Override
- @SuppressWarnings("unchecked")
- public Map<String, Object> transformRow(Map<String, Object> row,
- Context ctx) {
- List<Map<String, String>> fields = ctx.getAllEntityFields();
- for (Map<String, String> field : fields) {
- String col = field.get(DataImporter.COLUMN);
- String reStr = ctx.replaceTokens(field.get(REGEX));
- String splitBy = ctx.replaceTokens(field.get(SPLIT_BY));
- String replaceWith = ctx.replaceTokens(field.get(REPLACE_WITH));
- String groupNames = ctx.replaceTokens(field.get(GROUP_NAMES));
- if (reStr != null || splitBy != null) {
- String srcColName = field.get(SRC_COL_NAME);
- if (srcColName == null) {
- srcColName = col;
- }
- Object tmpVal = row.get(srcColName);
- if (tmpVal == null)
- continue;
-
- if (tmpVal instanceof List) {
- List<String> inputs = (List<String>) tmpVal;
- List results = new ArrayList();
- Map<String,List> otherVars= null;
- for (String input : inputs) {
- Object o = process(col, reStr, splitBy, replaceWith, input, groupNames);
- if (o != null){
- if (o instanceof Map) {
- Map map = (Map) o;
- for (Object e : map.entrySet()) {
- Map.Entry<String ,Object> entry = (Map.Entry<String, Object>) e;
- List l = results;
- if(!col.equals(entry.getKey())){
- if(otherVars == null) otherVars = new HashMap<>();
- l = otherVars.get(entry.getKey());
- if(l == null){
- l = new ArrayList();
- otherVars.put(entry.getKey(), l);
- }
- }
- if (entry.getValue() instanceof Collection) {
- l.addAll((Collection) entry.getValue());
- } else {
- l.add(entry.getValue());
- }
- }
- } else {
- if (o instanceof Collection) {
- results.addAll((Collection) o);
- } else {
- results.add(o);
- }
- }
- }
- }
- row.put(col, results);
- if(otherVars != null) row.putAll(otherVars);
- } else {
- String value = tmpVal.toString();
- Object o = process(col, reStr, splitBy, replaceWith, value, groupNames);
- if (o != null){
- if (o instanceof Map) {
- row.putAll((Map) o);
- } else{
- row.put(col, o);
- }
- }
- }
- }
- }
- return row;
- }
-
- private Object process(String col, String reStr, String splitBy,
- String replaceWith, String value, String groupNames) {
- if (splitBy != null) {
- return readBySplit(splitBy, value);
- } else if (replaceWith != null) {
- Pattern p = getPattern(reStr);
- Matcher m = p.matcher(value);
- return m.find() ? m.replaceAll(replaceWith) : value;
- } else {
- return readfromRegExp(reStr, value, col, groupNames);
- }
- }
-
- @SuppressWarnings("unchecked")
- private List<String> readBySplit(String splitBy, String value) {
- String[] vals = value.split(splitBy);
- List<String> l = new ArrayList<>();
- l.addAll(Arrays.asList(vals));
- return l;
- }
-
- @SuppressWarnings("unchecked")
- private Object readfromRegExp(String reStr, String value, String columnName, String gNames) {
- String[] groupNames = null;
- if(gNames != null && gNames.trim().length() >0){
- groupNames = gNames.split(",");
- }
- Pattern regexp = getPattern(reStr);
- Matcher m = regexp.matcher(value);
- if (m.find() && m.groupCount() > 0) {
- if (m.groupCount() > 1) {
- List l = null;
- Map<String ,String > map = null;
- if(groupNames == null){
- l = new ArrayList();
- } else {
- map = new HashMap<>();
- }
- for (int i = 1; i <= m.groupCount(); i++) {
- try {
- if(l != null){
- l.add(m.group(i));
- } else if (map != null ){
- if(i <= groupNames.length){
- String nameOfGroup = groupNames[i-1];
- if(nameOfGroup != null && nameOfGroup.trim().length() >0){
- map.put(nameOfGroup, m.group(i));
- }
- }
- }
- } catch (Exception e) {
- log.warn("Parsing failed for field : " + columnName, e);
- }
- }
- return l == null ? map: l;
- } else {
- return m.group(1);
- }
- }
-
- return null;
- }
-
- private Pattern getPattern(String reStr) {
- Pattern result = PATTERN_CACHE.get(reStr);
- if (result == null) {
- PATTERN_CACHE.put(reStr, result = Pattern.compile(reStr));
- }
- return result;
- }
-
- private HashMap<String, Pattern> PATTERN_CACHE = new HashMap<>();
-
- public static final String REGEX = "regex";
-
- public static final String REPLACE_WITH = "replaceWith";
-
- public static final String SPLIT_BY = "splitBy";
-
- public static final String SRC_COL_NAME = "sourceColName";
-
- public static final String GROUP_NAMES = "groupNames";
-
-}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/d7c03684/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/RequestInfo.java
----------------------------------------------------------------------
diff --git a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/RequestInfo.java b/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/RequestInfo.java
deleted file mode 100644
index d3f1a56..0000000
--- a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/RequestInfo.java
+++ /dev/null
@@ -1,177 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.solr.handler.dataimport;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import org.apache.solr.common.util.ContentStream;
-import org.apache.solr.common.util.StrUtils;
-import org.apache.solr.request.SolrQueryRequest;
-
-public class RequestInfo {
- private final String command;
- private final boolean debug;
- private final boolean syncMode;
- private final boolean commit;
- private final boolean optimize;
- private final int start;
- private final long rows;
- private final boolean clean;
- private final List<String> entitiesToRun;
- private final Map<String,Object> rawParams;
- private final String configFile;
- private final String dataConfig;
- private final SolrQueryRequest request;
-
- //TODO: find a different home for these two...
- private final ContentStream contentStream;
- private final DebugInfo debugInfo;
-
- public RequestInfo(SolrQueryRequest request, Map<String,Object> requestParams, ContentStream stream) {
- this.request = request;
- this.contentStream = stream;
- if (requestParams.containsKey("command")) {
- command = (String) requestParams.get("command");
- } else {
- command = null;
- }
- boolean debugMode = StrUtils.parseBool((String) requestParams.get("debug"), false);
- if (debugMode) {
- debug = true;
- debugInfo = new DebugInfo(requestParams);
- } else {
- debug = false;
- debugInfo = null;
- }
- if (requestParams.containsKey("clean")) {
- clean = StrUtils.parseBool( (String) requestParams.get("clean"), true);
- } else if (DataImporter.DELTA_IMPORT_CMD.equals(command) || DataImporter.IMPORT_CMD.equals(command)) {
- clean = false;
- } else {
- clean = debug ? false : true;
- }
- optimize = StrUtils.parseBool((String) requestParams.get("optimize"), false);
- if(optimize) {
- commit = true;
- } else {
- commit = StrUtils.parseBool( (String) requestParams.get("commit"), (debug ? false : true));
- }
- if (requestParams.containsKey("rows")) {
- rows = Integer.parseInt((String) requestParams.get("rows"));
- } else {
- rows = debug ? 10 : Long.MAX_VALUE;
- }
-
- if (requestParams.containsKey("start")) {
- start = Integer.parseInt((String) requestParams.get("start"));
- } else {
- start = 0;
- }
- syncMode = StrUtils.parseBool((String) requestParams.get("synchronous"), false);
-
- Object o = requestParams.get("entity");
- List<String> modifiableEntities = null;
- if(o != null) {
- if (o instanceof String) {
- modifiableEntities = new ArrayList<>();
- modifiableEntities.add((String) o);
- } else if (o instanceof List<?>) {
- @SuppressWarnings("unchecked")
- List<String> modifiableEntities1 = new ArrayList<>((List<String>) o);
- modifiableEntities = modifiableEntities1;
- }
- entitiesToRun = Collections.unmodifiableList(modifiableEntities);
- } else {
- entitiesToRun = null;
- }
- String configFileParam = (String) requestParams.get("config");
- configFile = configFileParam;
- String dataConfigParam = (String) requestParams.get("dataConfig");
- if (dataConfigParam != null && dataConfigParam.trim().length() == 0) {
- // Empty data-config param is not valid, change it to null
- dataConfigParam = null;
- }
- dataConfig = dataConfigParam;
- this.rawParams = Collections.unmodifiableMap(new HashMap<>(requestParams));
- }
-
- public String getCommand() {
- return command;
- }
-
- public boolean isDebug() {
- return debug;
- }
-
- public boolean isSyncMode() {
- return syncMode;
- }
-
- public boolean isCommit() {
- return commit;
- }
-
- public boolean isOptimize() {
- return optimize;
- }
-
- public int getStart() {
- return start;
- }
-
- public long getRows() {
- return rows;
- }
-
- public boolean isClean() {
- return clean;
- }
- /**
- * Returns null if we are to run all entities, otherwise just run the entities named in the list.
- */
- public List<String> getEntitiesToRun() {
- return entitiesToRun;
- }
-
- public String getDataConfig() {
- return dataConfig;
- }
-
- public Map<String,Object> getRawParams() {
- return rawParams;
- }
-
- public ContentStream getContentStream() {
- return contentStream;
- }
-
- public DebugInfo getDebugInfo() {
- return debugInfo;
- }
-
- public String getConfigFile() {
- return configFile;
- }
-
- public SolrQueryRequest getRequest() {
- return request;
- }
-}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/d7c03684/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/ScriptTransformer.java
----------------------------------------------------------------------
diff --git a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/ScriptTransformer.java b/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/ScriptTransformer.java
deleted file mode 100644
index 165d76d..0000000
--- a/solr/contrib/dataimporthandler/src/java/org/apache/solr/handler/dataimport/ScriptTransformer.java
+++ /dev/null
@@ -1,102 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.solr.handler.dataimport;
-
-import static org.apache.solr.handler.dataimport.DataImportHandlerException.wrapAndThrow;
-import static org.apache.solr.handler.dataimport.DataImportHandlerException.SEVERE;
-
-import java.util.Map;
-
-import javax.script.Invocable;
-import javax.script.ScriptEngine;
-import javax.script.ScriptEngineManager;
-import javax.script.ScriptException;
-
-/**
- * <p>
- * A {@link Transformer} instance capable of executing functions written in scripting
- * languages as a {@link Transformer} instance.
- * </p>
- * <p>
- * Refer to <a
- * href="http://wiki.apache.org/solr/DataImportHandler">http://wiki.apache.org/solr/DataImportHandler</a>
- * for more details.
- * </p>
- * <p>
- * <b>This API is experimental and may change in the future.</b>
- *
- * @since solr 1.3
- */
-public class ScriptTransformer extends Transformer {
- private Invocable engine;
- private String functionName;
-
- @Override
- public Object transformRow(Map<String, Object> row, Context context) {
- try {
- if (engine == null)
- initEngine(context);
- if (engine == null)
- return row;
- return engine.invokeFunction(functionName, new Object[]{row, context});
- } catch (DataImportHandlerException e) {
- throw e;
- } catch (Exception e) {
- wrapAndThrow(SEVERE,e, "Error invoking script for entity " + context.getEntityAttribute("name"));
- }
- //will not reach here
- return null;
- }
-
- private void initEngine(Context context) {
- String scriptText = context.getScript();
- String scriptLang = context.getScriptLanguage();
- if (scriptText == null) {
- throw new DataImportHandlerException(SEVERE,
- "<script> tag is not present under <dataConfig>");
- }
- ScriptEngineManager scriptEngineMgr = new ScriptEngineManager();
- ScriptEngine scriptEngine = scriptEngineMgr.getEngineByName(scriptLang);
- if (scriptEngine == null) {
- throw new DataImportHandlerException(SEVERE,
- "Cannot load Script Engine for language: " + scriptLang);
- }
- if (scriptEngine instanceof Invocable) {
- engine = (Invocable) scriptEngine;
- } else {
- throw new DataImportHandlerException(SEVERE,
- "The installed ScriptEngine for: " + scriptLang
- + " does not implement Invocable. Class is "
- + scriptEngine.getClass().getName());
- }
- try {
- scriptEngine.eval(scriptText);
- } catch (ScriptException e) {
- wrapAndThrow(SEVERE, e, "'eval' failed with language: " + scriptLang
- + " and script: \n" + scriptText);
- }
- }
-
- public void setFunctionName(String methodName) {
- this.functionName = methodName;
- }
-
- public String getFunctionName() {
- return functionName;
- }
-
-}