Author: sannegrinovero
Date: 2008-06-17 09:03:13 -0400 (Tue, 17 Jun 2008)
New Revision: 14772
Added:
search/trunk/src/java/org/hibernate/search/backend/configuration/ConfigurationParseHelper.java
search/trunk/src/test/org/hibernate/search/test/configuration/ConfigurationParseHelperTest.java
Modified:
search/trunk/src/java/org/hibernate/search/backend/configuration/IndexWriterSetting.java
search/trunk/src/java/org/hibernate/search/backend/impl/BatchedQueueingProcessor.java
search/trunk/src/java/org/hibernate/search/filter/MRUFilterCachingStrategy.java
search/trunk/src/java/org/hibernate/search/store/DirectoryProviderFactory.java
search/trunk/src/java/org/hibernate/search/store/optimization/IncrementalOptimizerStrategy.java
Log:
HSEARCH-217 : Proper errors on parsing of all numeric configuration parameters
Added:
search/trunk/src/java/org/hibernate/search/backend/configuration/ConfigurationParseHelper.java
===================================================================
---
search/trunk/src/java/org/hibernate/search/backend/configuration/ConfigurationParseHelper.java
(rev 0)
+++
search/trunk/src/java/org/hibernate/search/backend/configuration/ConfigurationParseHelper.java 2008-06-17
13:03:13 UTC (rev 14772)
@@ -0,0 +1,69 @@
+package org.hibernate.search.backend.configuration;
+
+import java.util.Properties;
+
+import org.hibernate.annotations.common.util.StringHelper;
+import org.hibernate.search.SearchException;
+
+/**
+ * Helper class to avoid managing NumberFormatException and similar code
+ * and ensure consistent error messages across Configuration parsing problems.
+ *
+ * @author Sanne Grinovero
+ */
+public abstract class ConfigurationParseHelper {
+
+ /**
+ * Parses a String to get an int value.
+ * @param value A string containing an int value to parse
+ * @param errorMsgOnParseFailure message being wrapped in a SearchException if value is
null or not correct.
+ * @return the parsed value
+ * @throws SearchException both for null values and for Strings not containing a valid
int.
+ */
+ public static final int parseInt(String value, String errorMsgOnParseFailure) {
+ if ( value == null ) {
+ throw new SearchException( errorMsgOnParseFailure );
+ }
+ else {
+ try {
+ return Integer.parseInt( value.trim() );
+ } catch (NumberFormatException nfe) {
+ throw new SearchException( errorMsgOnParseFailure, nfe );
+ }
+ }
+ }
+
+ /**
+ * In case value is null or an empty string the defValue is returned
+ * @param value
+ * @param defValue
+ * @param errorMsgOnParseFailure
+ * @return the converted int.
+ * @throws SearchException if value can't be parsed.
+ */
+ public static final int parseInt(String value, int defValue, String
errorMsgOnParseFailure) {
+ if ( StringHelper.isEmpty( value ) ) {
+ return defValue;
+ }
+ else {
+ return parseInt( value, errorMsgOnParseFailure );
+ }
+ }
+
+ /**
+ * Looks for a numeric value in the Properties, returning
+ * defValue if not found or if an empty string is found.
+ * When the key the value is found but not in valid format
+ * a standard error message is generated.
+ * @param cfg
+ * @param key
+ * @param defValue
+ * @return the converted int.
+ * @throws SearchException for invalid format.
+ */
+ public static final int getIntValue(Properties cfg, String key, int defValue) {
+ String propValue = cfg.getProperty( key );
+ return parseInt( propValue, defValue, "Unable to parse " + key + ":
" + propValue );
+ }
+
+}
Modified:
search/trunk/src/java/org/hibernate/search/backend/configuration/IndexWriterSetting.java
===================================================================
---
search/trunk/src/java/org/hibernate/search/backend/configuration/IndexWriterSetting.java 2008-06-16
17:15:32 UTC (rev 14771)
+++
search/trunk/src/java/org/hibernate/search/backend/configuration/IndexWriterSetting.java 2008-06-17
13:03:13 UTC (rev 14772)
@@ -94,11 +94,8 @@
* @throws SearchException for unrecognized values
*/
public Integer parseVal(String value) {
- try {
- return Integer.valueOf( value );
- } catch (NumberFormatException ne) {
- throw new SearchException( "Invalid value for " + cfgKey + ": " +
value );
- }
+ return ConfigurationParseHelper.parseInt( value,
+ "Invalid value for " + cfgKey + ": " + value );
}
}
Modified:
search/trunk/src/java/org/hibernate/search/backend/impl/BatchedQueueingProcessor.java
===================================================================
---
search/trunk/src/java/org/hibernate/search/backend/impl/BatchedQueueingProcessor.java 2008-06-16
17:15:32 UTC (rev 14771)
+++
search/trunk/src/java/org/hibernate/search/backend/impl/BatchedQueueingProcessor.java 2008-06-17
13:03:13 UTC (rev 14772)
@@ -20,6 +20,7 @@
import org.hibernate.search.backend.Work;
import org.hibernate.search.backend.WorkType;
import org.hibernate.search.backend.WorkQueue;
+import org.hibernate.search.backend.configuration.ConfigurationParseHelper;
import org.hibernate.search.backend.impl.jms.JMSBackendQueueProcessorFactory;
import org.hibernate.search.backend.impl.lucene.LuceneBackendQueueProcessorFactory;
import org.hibernate.search.engine.DocumentBuilder;
@@ -50,16 +51,12 @@
this.sync = !"async".equalsIgnoreCase( properties.getProperty(
Environment.WORKER_EXECUTION ) );
//default to a simple asynchronous operation
- int min = Integer.parseInt(
- properties.getProperty( Environment.WORKER_THREADPOOL_SIZE, "1" ).trim()
- );
+ int min = ConfigurationParseHelper.getIntValue( properties,
Environment.WORKER_THREADPOOL_SIZE, 1 );
//no queue limit
- int queueSize = Integer.parseInt(
- properties.getProperty( Environment.WORKER_WORKQUEUE_SIZE, Integer.toString(
Integer.MAX_VALUE ) ).trim()
- );
- batchSize = Integer.parseInt(
- properties.getProperty( Environment.WORKER_BATCHSIZE, "0" ).trim()
- );
+ int queueSize = ConfigurationParseHelper.getIntValue( properties,
Environment.WORKER_WORKQUEUE_SIZE, Integer.MAX_VALUE );
+
+ batchSize = ConfigurationParseHelper.getIntValue( properties,
Environment.WORKER_BATCHSIZE, 0 );
+
if ( !sync ) {
/**
* choose min = max with a sizable queue to be able to
Modified: search/trunk/src/java/org/hibernate/search/filter/MRUFilterCachingStrategy.java
===================================================================
---
search/trunk/src/java/org/hibernate/search/filter/MRUFilterCachingStrategy.java 2008-06-16
17:15:32 UTC (rev 14771)
+++
search/trunk/src/java/org/hibernate/search/filter/MRUFilterCachingStrategy.java 2008-06-17
13:03:13 UTC (rev 14772)
@@ -5,7 +5,7 @@
import org.apache.lucene.search.Filter;
import org.hibernate.search.Environment;
-import org.hibernate.search.SearchException;
+import org.hibernate.search.backend.configuration.ConfigurationParseHelper;
import org.hibernate.util.SoftLimitMRUCache;
/**
@@ -16,23 +16,12 @@
* @author Emmanuel Bernard
*/
public class MRUFilterCachingStrategy implements FilterCachingStrategy {
- private static final String DEFAULT_SIZE = "128";
+ private static final int DEFAULT_SIZE = 128;
private SoftLimitMRUCache cache;
private static final String SIZE = Environment.FILTER_CACHING_STRATEGY +
".size";
public void initialize(Properties properties) {
- int size;
- try {
- size = Integer.parseInt(
- properties.getProperty( SIZE, DEFAULT_SIZE )
- );
- }
- catch (NumberFormatException nfe) {
- throw new SearchException(
- "Unable to parse " + SIZE + ": " + properties.getProperty( SIZE,
DEFAULT_SIZE ), nfe
- );
- }
-
+ int size = ConfigurationParseHelper.getIntValue( properties, SIZE, DEFAULT_SIZE );
cache = new SoftLimitMRUCache( size );
}
Modified: search/trunk/src/java/org/hibernate/search/store/DirectoryProviderFactory.java
===================================================================
---
search/trunk/src/java/org/hibernate/search/store/DirectoryProviderFactory.java 2008-06-16
17:15:32 UTC (rev 14771)
+++
search/trunk/src/java/org/hibernate/search/store/DirectoryProviderFactory.java 2008-06-17
13:03:13 UTC (rev 14772)
@@ -13,6 +13,7 @@
import org.hibernate.search.SearchException;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.backend.LuceneIndexingParameters;
+import org.hibernate.search.backend.configuration.ConfigurationParseHelper;
import org.hibernate.search.backend.configuration.MaskedProperty;
import org.hibernate.search.engine.SearchFactoryImplementor;
import org.hibernate.search.impl.SearchFactoryImpl;
@@ -208,14 +209,7 @@
return new Properties[] { directoryLocalProperties };
} else {
// count shards
- int shardsCount;
- {
- try {
- shardsCount = Integer.parseInt( shardsCountValue );
- } catch (NumberFormatException e) {
- throw new SearchException( shardsCountValue + " is not a number", e);
- }
- }
+ int shardsCount = ConfigurationParseHelper.parseInt( shardsCountValue,
shardsCountValue + " is not a number" );
// create shard-specific Props
Properties[] shardLocalProperties = new Properties[shardsCount];
for ( int i = 0; i < shardsCount; i++ ) {
Modified:
search/trunk/src/java/org/hibernate/search/store/optimization/IncrementalOptimizerStrategy.java
===================================================================
---
search/trunk/src/java/org/hibernate/search/store/optimization/IncrementalOptimizerStrategy.java 2008-06-16
17:15:32 UTC (rev 14771)
+++
search/trunk/src/java/org/hibernate/search/store/optimization/IncrementalOptimizerStrategy.java 2008-06-17
13:03:13 UTC (rev 14772)
@@ -6,9 +6,9 @@
import org.hibernate.search.engine.SearchFactoryImplementor;
import org.hibernate.search.backend.Workspace;
+import org.hibernate.search.backend.configuration.ConfigurationParseHelper;
import org.hibernate.search.store.DirectoryProvider;
import org.hibernate.search.SearchException;
-import org.hibernate.annotations.common.util.StringHelper;
import org.apache.lucene.index.IndexWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -28,15 +28,8 @@
public void initialize(DirectoryProvider directoryProvider, Properties indexProperties,
SearchFactoryImplementor searchFactoryImplementor) {
this.directoryProvider = directoryProvider;
- String maxString = indexProperties.getProperty(
"optimizer.operation_limit.max" );
- if ( StringHelper.isNotEmpty( maxString ) ) {
- operationMax = Integer.parseInt( maxString );
- }
-
- maxString = indexProperties.getProperty( "optimizer.transaction_limit.max"
);
- if ( StringHelper.isNotEmpty( maxString ) ) {
- transactionMax = Integer.parseInt( maxString );
- }
+ operationMax = ConfigurationParseHelper.getIntValue( indexProperties,
"optimizer.operation_limit.max", -1 );
+ transactionMax = ConfigurationParseHelper.getIntValue( indexProperties,
"optimizer.transaction_limit.max", -1 );
}
public void optimizationForced() {
Added:
search/trunk/src/test/org/hibernate/search/test/configuration/ConfigurationParseHelperTest.java
===================================================================
---
search/trunk/src/test/org/hibernate/search/test/configuration/ConfigurationParseHelperTest.java
(rev 0)
+++
search/trunk/src/test/org/hibernate/search/test/configuration/ConfigurationParseHelperTest.java 2008-06-17
13:03:13 UTC (rev 14772)
@@ -0,0 +1,34 @@
+package org.hibernate.search.test.configuration;
+
+import java.util.Properties;
+
+import junit.framework.TestCase;
+
+import org.hibernate.search.SearchException;
+import org.hibernate.search.backend.configuration.ConfigurationParseHelper;
+
+/**
+ * @author Sanne Grinovero
+ */
+public class ConfigurationParseHelperTest extends TestCase {
+
+ public void testIntegerParsers(){
+ assertEquals( 0, ConfigurationParseHelper.parseInt( " 0 ", "not
important") );
+ assertEquals( 8, ConfigurationParseHelper.parseInt( null, 8, null ) );
+ assertEquals( 56, ConfigurationParseHelper.parseInt( "56", 8, null ) );
+ Properties props = new Properties();
+ props.setProperty( "value1", "58" );
+ assertEquals( 58, ConfigurationParseHelper.getIntValue( props, "value1", 8 )
);
+ assertEquals( 8, ConfigurationParseHelper.getIntValue( props, "value2", 8 )
);
+ props.setProperty( "value2", "nand" );
+ boolean exceptionLaunched;
+ try {
+ ConfigurationParseHelper.getIntValue( props, "value2", 8 );
+ exceptionLaunched = false;
+ } catch (SearchException e) {
+ exceptionLaunched = true;
+ }
+ assertTrue( exceptionLaunched );
+ }
+
+}