You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by gn...@apache.org on 2019/05/13 14:37:19 UTC
[camel] 01/02: [CAMEL-13512] Use unchecked exceptions for property
resolution
This is an automated email from the ASF dual-hosted git repository.
gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git
commit e865e984baf9dc3c40b9450d6ef21be55056dbc0
Author: Guillaume Nodet <gn...@gmail.com>
AuthorDate: Fri May 10 16:16:41 2019 +0200
[CAMEL-13512] Use unchecked exceptions for property resolution
---
.../camel/blueprint/BlueprintPropertiesResolver.java | 2 +-
.../properties/DefaultPropertiesResolver.java | 19 ++++++++++++-------
.../component/properties/PropertiesComponent.java | 12 ++++++------
.../component/properties/PropertiesResolver.java | 4 ++--
.../spi/BridgePropertyPlaceholderConfigurer.java | 2 +-
...CamelSpringPropertyPlaceholderConfigurer3Test.java | 2 +-
.../src/main/java/org/apache/camel/CamelContext.java | 4 ++--
.../org/apache/camel/spi/PropertiesComponent.java | 17 +++++++++--------
.../camel/impl/engine/AbstractCamelContext.java | 2 +-
.../properties/PropertiesComponentDefaultTest.java | 5 ++++-
.../properties/PropertiesComponentRestartTest.java | 2 +-
.../component/properties/PropertiesResolverTest.java | 2 +-
.../org/apache/camel/support/CamelContextHelper.java | 4 ++--
13 files changed, 43 insertions(+), 34 deletions(-)
diff --git a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintPropertiesResolver.java b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintPropertiesResolver.java
index 50c8af8..1e982e9 100644
--- a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintPropertiesResolver.java
+++ b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintPropertiesResolver.java
@@ -42,7 +42,7 @@ public class BlueprintPropertiesResolver implements PropertiesResolver {
}
@Override
- public Properties resolveProperties(CamelContext context, boolean ignoreMissingLocation, List<PropertiesLocation> locations) throws Exception {
+ public Properties resolveProperties(CamelContext context, boolean ignoreMissingLocation, List<PropertiesLocation> locations) {
Properties answer = new Properties();
boolean explicit = false;
diff --git a/components/camel-properties/src/main/java/org/apache/camel/component/properties/DefaultPropertiesResolver.java b/components/camel-properties/src/main/java/org/apache/camel/component/properties/DefaultPropertiesResolver.java
index 8962582..7ac047f 100644
--- a/components/camel-properties/src/main/java/org/apache/camel/component/properties/DefaultPropertiesResolver.java
+++ b/components/camel-properties/src/main/java/org/apache/camel/component/properties/DefaultPropertiesResolver.java
@@ -28,6 +28,7 @@ import java.util.Map;
import java.util.Properties;
import org.apache.camel.CamelContext;
+import org.apache.camel.RuntimeCamelException;
import org.apache.camel.util.IOHelper;
/**
@@ -45,7 +46,7 @@ public class DefaultPropertiesResolver implements PropertiesResolver {
this.propertiesComponent = propertiesComponent;
}
- public Properties resolveProperties(CamelContext context, boolean ignoreMissingLocation, List<PropertiesLocation> locations) throws Exception {
+ public Properties resolveProperties(CamelContext context, boolean ignoreMissingLocation, List<PropertiesLocation> locations) {
Properties answer = new Properties();
Properties prop;
@@ -74,7 +75,7 @@ public class DefaultPropertiesResolver implements PropertiesResolver {
return answer;
}
- protected Properties loadPropertiesFromFilePath(CamelContext context, boolean ignoreMissingLocation, PropertiesLocation location) throws IOException {
+ protected Properties loadPropertiesFromFilePath(CamelContext context, boolean ignoreMissingLocation, PropertiesLocation location) {
Properties answer = new Properties();
String path = location.getPath();
@@ -90,8 +91,10 @@ public class DefaultPropertiesResolver implements PropertiesResolver {
}
} catch (FileNotFoundException e) {
if (!ignoreMissingLocation && !location.isOptional()) {
- throw e;
+ throw RuntimeCamelException.wrapRuntimeCamelException(e);
}
+ } catch (IOException e) {
+ throw RuntimeCamelException.wrapRuntimeCamelException(e);
} finally {
IOHelper.close(reader, is);
}
@@ -99,7 +102,7 @@ public class DefaultPropertiesResolver implements PropertiesResolver {
return answer;
}
- protected Properties loadPropertiesFromClasspath(CamelContext context, boolean ignoreMissingLocation, PropertiesLocation location) throws IOException {
+ protected Properties loadPropertiesFromClasspath(CamelContext context, boolean ignoreMissingLocation, PropertiesLocation location) {
Properties answer = new Properties();
String path = location.getPath();
@@ -107,7 +110,7 @@ public class DefaultPropertiesResolver implements PropertiesResolver {
Reader reader = null;
if (is == null) {
if (!ignoreMissingLocation && !location.isOptional()) {
- throw new FileNotFoundException("Properties file " + path + " not found in classpath");
+ throw RuntimeCamelException.wrapRuntimeCamelException(new FileNotFoundException("Properties file " + path + " not found in classpath"));
}
} else {
try {
@@ -117,6 +120,8 @@ public class DefaultPropertiesResolver implements PropertiesResolver {
} else {
answer.load(is);
}
+ } catch (IOException e) {
+ throw RuntimeCamelException.wrapRuntimeCamelException(e);
} finally {
IOHelper.close(reader, is);
}
@@ -125,7 +130,7 @@ public class DefaultPropertiesResolver implements PropertiesResolver {
}
@SuppressWarnings({"rawtypes", "unchecked"})
- protected Properties loadPropertiesFromRegistry(CamelContext context, boolean ignoreMissingLocation, PropertiesLocation location) throws IOException {
+ protected Properties loadPropertiesFromRegistry(CamelContext context, boolean ignoreMissingLocation, PropertiesLocation location) {
String path = location.getPath();
Properties answer;
try {
@@ -137,7 +142,7 @@ public class DefaultPropertiesResolver implements PropertiesResolver {
answer.putAll(map);
}
if (answer == null && (!ignoreMissingLocation && !location.isOptional())) {
- throw new FileNotFoundException("Properties " + path + " not found in registry");
+ throw RuntimeCamelException.wrapRuntimeCamelException(new FileNotFoundException("Properties " + path + " not found in registry"));
}
return answer != null ? answer : new Properties();
}
diff --git a/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java b/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
index 6d77953..cda1377 100644
--- a/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
+++ b/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
@@ -172,11 +172,11 @@ public class PropertiesComponent extends DefaultComponent implements org.apache.
return answer;
}
- public String parseUri(String uri) throws Exception {
+ public String parseUri(String uri) {
return parseUri(uri, locations);
}
- public String parseUri(String uri, String... locations) throws Exception {
+ public String parseUri(String uri, String... locations) {
return parseUri(
uri,
locations != null
@@ -184,18 +184,18 @@ public class PropertiesComponent extends DefaultComponent implements org.apache.
: Collections.emptyList());
}
- public Properties loadProperties() throws Exception {
+ public Properties loadProperties() {
return doLoadProperties(locations);
}
- public Properties loadProperties(String... locations) throws Exception {
+ public Properties loadProperties(String... locations) {
if (locations != null) {
return doLoadProperties(Arrays.stream(locations).map(PropertiesLocation::new).collect(Collectors.toList()));
}
return new Properties();
}
- protected Properties doLoadProperties(List<PropertiesLocation> paths) throws Exception {
+ protected Properties doLoadProperties(List<PropertiesLocation> paths) {
Properties prop = new Properties();
// use initial properties
@@ -233,7 +233,7 @@ public class PropertiesComponent extends DefaultComponent implements org.apache.
return prop;
}
- protected String parseUri(String uri, List<PropertiesLocation> paths) throws Exception {
+ protected String parseUri(String uri, List<PropertiesLocation> paths) {
Properties prop = doLoadProperties(paths);
// enclose tokens if missing
diff --git a/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesResolver.java b/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesResolver.java
index e93cc0b..aa28883 100644
--- a/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesResolver.java
+++ b/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesResolver.java
@@ -35,7 +35,7 @@ public interface PropertiesResolver {
* @param ignoreMissingLocation ignore silently if the property file is missing
* @param locations location(s) defining the source(s)
* @return the properties
- * @throws Exception is thrown if resolving the properties failed
+ * @throws java.io.IOError is thrown if resolving the properties failed
*/
- Properties resolveProperties(CamelContext context, boolean ignoreMissingLocation, List<PropertiesLocation> locations) throws Exception;
+ Properties resolveProperties(CamelContext context, boolean ignoreMissingLocation, List<PropertiesLocation> locations);
}
diff --git a/components/camel-spring/src/main/java/org/apache/camel/spring/spi/BridgePropertyPlaceholderConfigurer.java b/components/camel-spring/src/main/java/org/apache/camel/spring/spi/BridgePropertyPlaceholderConfigurer.java
index af9852f..0a9d357 100644
--- a/components/camel-spring/src/main/java/org/apache/camel/spring/spi/BridgePropertyPlaceholderConfigurer.java
+++ b/components/camel-spring/src/main/java/org/apache/camel/spring/spi/BridgePropertyPlaceholderConfigurer.java
@@ -132,7 +132,7 @@ public class BridgePropertyPlaceholderConfigurer extends PropertyPlaceholderConf
}
@Override
- public Properties resolveProperties(CamelContext context, boolean ignoreMissingLocation, List<PropertiesLocation> locations) throws Exception {
+ public Properties resolveProperties(CamelContext context, boolean ignoreMissingLocation, List<PropertiesLocation> locations) {
// return the spring properties, if it
Properties answer = new Properties();
for (PropertiesLocation location : locations) {
diff --git a/components/camel-spring/src/test/java/org/apache/camel/component/properties/CamelSpringPropertyPlaceholderConfigurer3Test.java b/components/camel-spring/src/test/java/org/apache/camel/component/properties/CamelSpringPropertyPlaceholderConfigurer3Test.java
index 7e5e628..71e6dbf 100644
--- a/components/camel-spring/src/test/java/org/apache/camel/component/properties/CamelSpringPropertyPlaceholderConfigurer3Test.java
+++ b/components/camel-spring/src/test/java/org/apache/camel/component/properties/CamelSpringPropertyPlaceholderConfigurer3Test.java
@@ -73,7 +73,7 @@ public class CamelSpringPropertyPlaceholderConfigurer3Test extends SpringTestSup
private static class MyBridgePropertyPlaceholderConfigurer extends BridgePropertyPlaceholderConfigurer {
@Override
- public Properties resolveProperties(CamelContext context, boolean ignoreMissingLocation, List<PropertiesLocation> locations) throws Exception {
+ public Properties resolveProperties(CamelContext context, boolean ignoreMissingLocation, List<PropertiesLocation> locations) {
Properties answer = super.resolveProperties(context, ignoreMissingLocation, locations);
// define the additional properties we need to provide so that the uri "direct:{{foo}}" by the "from" clause
diff --git a/core/camel-api/src/main/java/org/apache/camel/CamelContext.java b/core/camel-api/src/main/java/org/apache/camel/CamelContext.java
index 355ff69..a9a5fa9 100644
--- a/core/camel-api/src/main/java/org/apache/camel/CamelContext.java
+++ b/core/camel-api/src/main/java/org/apache/camel/CamelContext.java
@@ -763,9 +763,9 @@ public interface CamelContext extends SuspendableService, RuntimeConfiguration {
*
* @param text the text such as an endpoint uri or the likes
* @return the text with resolved property placeholders
- * @throws Exception is thrown if property placeholders was used and there was an error resolving them
+ * @throws IllegalArgumentException is thrown if property placeholders was used and there was an error resolving them
*/
- String resolvePropertyPlaceholders(String text) throws Exception;
+ String resolvePropertyPlaceholders(String text);
/**
* Returns the configured property placeholder prefix token if and only if the CamelContext has
diff --git a/core/camel-api/src/main/java/org/apache/camel/spi/PropertiesComponent.java b/core/camel-api/src/main/java/org/apache/camel/spi/PropertiesComponent.java
index 07dc1a8..c88f72d 100644
--- a/core/camel-api/src/main/java/org/apache/camel/spi/PropertiesComponent.java
+++ b/core/camel-api/src/main/java/org/apache/camel/spi/PropertiesComponent.java
@@ -16,6 +16,7 @@
*/
package org.apache.camel.spi;
+import java.io.IOError;
import java.util.Properties;
import org.apache.camel.Component;
@@ -46,9 +47,9 @@ public interface PropertiesComponent extends Component {
*
* @param uri input text
* @return text with resolved property placeholders
- * @throws Exception is thrown if error during parsing
+ * @throws IllegalArgumentException is thrown if error during parsing
*/
- String parseUri(String uri) throws Exception;
+ String parseUri(String uri);
/**
* Parses the input text and resolve all property placeholders.
@@ -56,26 +57,26 @@ public interface PropertiesComponent extends Component {
* @param uri input text
* @param locations locations to load as properties (will not use the default locations)
* @return text with resolved property placeholders
- * @throws Exception is thrown if error during parsing
+ * @throws IllegalArgumentException is thrown if error during parsing
*/
- String parseUri(String uri, String... locations) throws Exception;
+ String parseUri(String uri, String... locations);
/**
* Loads the properties from the default locations.
*
* @return the properties loaded.
- * @throws Exception is thrown if error loading properties
+ * @throws IOError is thrown if error loading properties
*/
- Properties loadProperties() throws Exception;
+ Properties loadProperties();
/**
* Loads the properties from the given locations
*
* @param locations locations to load as properties (will not use the default locations)
* @return the properties loaded.
- * @throws Exception is thrown if error loading properties
+ * @throws IOError is thrown if error loading properties
*/
- Properties loadProperties(String... locations) throws Exception;
+ Properties loadProperties(String... locations);
/**
* A list of locations to load properties. You can use comma to separate multiple locations.
diff --git a/core/camel-base/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java b/core/camel-base/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
index de81632..d41c5d7 100644
--- a/core/camel-base/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
+++ b/core/camel-base/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
@@ -1546,7 +1546,7 @@ public abstract class AbstractCamelContext extends ServiceSupport implements Cam
}
}
- public String resolvePropertyPlaceholders(String text) throws Exception {
+ public String resolvePropertyPlaceholders(String text) {
// While it is more efficient to only do the lookup if we are sure we
// need the component,
// with custom tokens, we cannot know if the URI contains a property or
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentDefaultTest.java b/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentDefaultTest.java
index 3354890..fe68a36 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentDefaultTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentDefaultTest.java
@@ -17,10 +17,12 @@
package org.apache.camel.component.properties;
import java.io.FileNotFoundException;
+import java.io.IOError;
import org.apache.camel.ContextTestSupport;
import org.apache.camel.FailedToCreateRouteException;
import org.apache.camel.ResolveEndpointFailedException;
+import org.apache.camel.RuntimeCamelException;
import org.apache.camel.builder.RouteBuilder;
import org.junit.Test;
@@ -56,7 +58,8 @@ public class PropertiesComponentDefaultTest extends ContextTestSupport {
fail("Should throw exception");
} catch (FailedToCreateRouteException e) {
ResolveEndpointFailedException cause = assertIsInstanceOf(ResolveEndpointFailedException.class, e.getCause());
- FileNotFoundException fnfe = assertIsInstanceOf(FileNotFoundException.class, cause.getCause());
+ RuntimeCamelException rce = assertIsInstanceOf(RuntimeCamelException.class, cause.getCause());
+ FileNotFoundException fnfe = assertIsInstanceOf(FileNotFoundException.class, rce.getCause());
assertEquals("Properties file org/apache/camel/component/properties/unknown.properties not found in classpath", fnfe.getMessage());
}
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentRestartTest.java b/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentRestartTest.java
index 8a2545e..1f398d5 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentRestartTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentRestartTest.java
@@ -51,7 +51,7 @@ public class PropertiesComponentRestartTest extends ContextTestSupport {
protected CamelContext createCamelContext() throws Exception {
final PropertiesComponent pc = new PropertiesComponent("classpath:org/apache/camel/component/properties/myproperties.properties");
pc.setPropertiesResolver(new PropertiesResolver() {
- public Properties resolveProperties(CamelContext context, boolean ignoreMissingLocation, List<PropertiesLocation> locations) throws Exception {
+ public Properties resolveProperties(CamelContext context, boolean ignoreMissingLocation, List<PropertiesLocation> locations) {
resolvedCount++;
return new DefaultPropertiesResolver(pc).resolveProperties(context, ignoreMissingLocation, locations);
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesResolverTest.java b/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesResolverTest.java
index 6c01ef6..a71db59 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesResolverTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesResolverTest.java
@@ -63,7 +63,7 @@ public class PropertiesResolverTest extends ContextTestSupport {
public static class MyCustomResolver implements PropertiesResolver {
- public Properties resolveProperties(CamelContext context, boolean ignoreMissingLocation, List<PropertiesLocation> locations) throws Exception {
+ public Properties resolveProperties(CamelContext context, boolean ignoreMissingLocation, List<PropertiesLocation> locations) {
Properties answer = new Properties();
answer.put("foo", "mock:result");
return answer;
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/CamelContextHelper.java b/core/camel-support/src/main/java/org/apache/camel/support/CamelContextHelper.java
index a2aaa09..d63da74 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/CamelContextHelper.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/CamelContextHelper.java
@@ -460,9 +460,9 @@ public final class CamelContextHelper {
* @param camelContext the camel context
* @param text the text
* @return the boolean vale, or <tt>null</tt> if the text was <tt>null</tt>
- * @throws Exception is thrown if illegal argument or type conversion not possible
+ * @throws IllegalArgumentException is thrown if illegal argument or type conversion not possible
*/
- public static Boolean parseBoolean(CamelContext camelContext, String text) throws Exception {
+ public static Boolean parseBoolean(CamelContext camelContext, String text) {
// ensure we support property placeholders
String s = camelContext.resolvePropertyPlaceholders(text);
if (s != null) {