diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index e0c881fbdfb..2dc2c525c6b 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -545,6 +545,10 @@ Bug Fixes (Drew Farris via koji) * SOLR-1973: Empty fields in XML update messages confuse DataImportHandler. (koji) + +* SOLR-2221: Use StrUtils.parseBool() to get values of boolean options in DIH. + true/on/yes (for TRUE) and false/off/no (for FALSE) can be used for sub-options + (debug, verbose, synchronous, commit, clean, optimize) for full/delta-import commands. (koji) Other Changes ---------------------- diff --git a/solr/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/DataImportHandler.java b/solr/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/DataImportHandler.java index 946206a5f7b..e813e4e3460 100644 --- a/solr/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/DataImportHandler.java +++ b/solr/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/DataImportHandler.java @@ -26,6 +26,7 @@ import org.apache.solr.common.params.UpdateParams; import org.apache.solr.common.util.ContentStreamBase; import org.apache.solr.common.util.NamedList; import org.apache.solr.common.util.ContentStream; +import org.apache.solr.common.util.StrUtils; import org.apache.solr.core.SolrConfig; import org.apache.solr.core.SolrCore; import org.apache.solr.core.SolrResourceLoader; @@ -101,9 +102,7 @@ public class DataImportHandler extends RequestHandlerBase implements myName = myName.replaceAll("/","_") ; } } - String debug = (String) initArgs.get(ENABLE_DEBUG); - if (debug != null && "no".equals(debug)) - debugEnabled = false; + debugEnabled = StrUtils.parseBool((String)initArgs.get(ENABLE_DEBUG), true); NamedList defaults = (NamedList) initArgs.get("defaults"); if (defaults != null) { String configLoc = (String) defaults.get("config"); diff --git a/solr/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/DataImporter.java b/solr/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/DataImporter.java index e522a051441..6d9206d43ed 100644 --- a/solr/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/DataImporter.java +++ b/solr/contrib/dataimporthandler/src/main/java/org/apache/solr/handler/dataimport/DataImporter.java @@ -22,6 +22,7 @@ import org.apache.solr.core.SolrCore; import org.apache.solr.schema.IndexSchema; import org.apache.solr.schema.SchemaField; import org.apache.solr.common.util.ContentStream; +import org.apache.solr.common.util.StrUtils; import static org.apache.solr.handler.dataimport.DataImportHandlerException.wrapAndThrow; import static org.apache.solr.handler.dataimport.DataImportHandlerException.SEVERE; @@ -498,29 +499,28 @@ public class DataImporter { if (requestParams.containsKey("command")) command = (String) requestParams.get("command"); - if ("on".equals(requestParams.get("debug"))) { + if (StrUtils.parseBool((String)requestParams.get("debug"),false)) { debug = true; rows = 10; // Set default values suitable for debug mode commit = false; clean = false; - verbose = "true".equals(requestParams.get("verbose")) - || "on".equals(requestParams.get("verbose")); + verbose = StrUtils.parseBool((String)requestParams.get("verbose"),false); } - syncMode = "true".equals(requestParams.get("synchronous")); + syncMode = StrUtils.parseBool((String)requestParams.get("synchronous"),false); if (DELTA_IMPORT_CMD.equals(command) || IMPORT_CMD.equals(command)) { clean = false; } if (requestParams.containsKey("commit")) - commit = Boolean.parseBoolean((String) requestParams.get("commit")); + commit = StrUtils.parseBool((String) requestParams.get("commit"),true); if (requestParams.containsKey("start")) start = Integer.parseInt((String) requestParams.get("start")); if (requestParams.containsKey("rows")) rows = Integer.parseInt((String) requestParams.get("rows")); if (requestParams.containsKey("clean")) - clean = Boolean.parseBoolean((String) requestParams.get("clean")); + clean = StrUtils.parseBool((String) requestParams.get("clean"),true); if (requestParams.containsKey("optimize")) { - optimize = Boolean.parseBoolean((String) requestParams.get("optimize")); + optimize = StrUtils.parseBool((String) requestParams.get("optimize"),true); if (optimize) commit = true; } diff --git a/solr/src/common/org/apache/solr/common/util/StrUtils.java b/solr/src/common/org/apache/solr/common/util/StrUtils.java index 80b7c61c991..7848358f1b6 100644 --- a/solr/src/common/org/apache/solr/common/util/StrUtils.java +++ b/solr/src/common/org/apache/solr/common/util/StrUtils.java @@ -238,6 +238,24 @@ public class StrUtils { throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, "invalid boolean value: "+s ); } + /** + * {@link NullPointerException} and {@link SolrException} free version of {@link #parseBool(String)} + * @param s + * @param def + * @return + */ + public static boolean parseBool(String s, boolean def) { + if( s != null ) { + if( s.startsWith("true") || s.startsWith("on") || s.startsWith("yes") ) { + return true; + } + if( s.startsWith("false") || s.startsWith("off") || s.equals("no") ) { + return false; + } + } + return def; + } + /** * URLEncodes a value, replacing only enough chars so that * the URL may be unambiguously pasted back into a browser.