This is an automated email from the ASF dual-hosted git repository.
orpiske pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 09ff2c9 (chores) camel-djl: cleanup exceptions (#5870)
09ff2c9 is described below
commit 09ff2c9bed08c4d6b5d1c47329832f33f61aba0a
Author: Otavio Rodolfo Piske <[email protected]>
AuthorDate: Tue Jul 27 19:02:06 2021 +0200
(chores) camel-djl: cleanup exceptions (#5870)
- Use specific exceptions instead of generic ones
- Remove unuse exceptions for method signatures
---
.../model/CustomImageClassificationPredictor.java | 15 ++++++++-------
.../djl/model/CustomObjectDetectionPredictor.java | 17 +++++++++--------
.../djl/model/ModelPredictorProducer.java | 13 ++++++++++---
.../djl/model/ZooImageClassificationPredictor.java | 22 +++++++++++++---------
.../djl/model/ZooObjectDetectionPredictor.java | 21 ++++++++++++---------
5 files changed, 52 insertions(+), 36 deletions(-)
diff --git
a/components/camel-djl/src/main/java/org/apache/camel/component/djl/model/CustomImageClassificationPredictor.java
b/components/camel-djl/src/main/java/org/apache/camel/component/djl/model/CustomImageClassificationPredictor.java
index 63b01e2..cfc7548 100644
---
a/components/camel-djl/src/main/java/org/apache/camel/component/djl/model/CustomImageClassificationPredictor.java
+++
b/components/camel-djl/src/main/java/org/apache/camel/component/djl/model/CustomImageClassificationPredictor.java
@@ -29,6 +29,7 @@ import ai.djl.modality.cv.ImageFactory;
import ai.djl.translate.TranslateException;
import ai.djl.translate.Translator;
import org.apache.camel.Exchange;
+import org.apache.camel.RuntimeCamelException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -59,31 +60,31 @@ public class CustomImageClassificationPredictor extends
AbstractPredictor {
Map<String, Float> result = classify(model, translator,
exchange.getIn().getBody(InputStream.class));
exchange.getIn().setBody(result);
} else {
- throw new RuntimeException("Data type is not supported. Body
should be byte[], InputStream or File");
+ throw new RuntimeCamelException("Data type is not supported. Body
should be byte[], InputStream or File");
}
}
- private Map<String, Float> classify(Model model, Translator translator,
File input) throws Exception {
+ private Map<String, Float> classify(Model model, Translator translator,
File input) {
try (InputStream fileInputStream = new FileInputStream(input)) {
Image image =
ImageFactory.getInstance().fromInputStream(fileInputStream);
return classify(model, translator, image);
} catch (IOException e) {
LOG.error("Couldn't transform input into a BufferedImage");
- throw new RuntimeException("Couldn't transform input into a
BufferedImage", e);
+ throw new RuntimeCamelException("Couldn't transform input into a
BufferedImage", e);
}
}
- private Map<String, Float> classify(Model model, Translator translator,
InputStream input) throws Exception {
+ private Map<String, Float> classify(Model model, Translator translator,
InputStream input) {
try {
Image image = ImageFactory.getInstance().fromInputStream(input);
return classify(model, translator, image);
} catch (IOException e) {
LOG.error("Couldn't transform input into a BufferedImage");
- throw new RuntimeException("Couldn't transform input into a
BufferedImage", e);
+ throw new RuntimeCamelException("Couldn't transform input into a
BufferedImage", e);
}
}
- private Map<String, Float> classify(Model model, Translator translator,
Image image) throws Exception {
+ private Map<String, Float> classify(Model model, Translator translator,
Image image) {
try (Predictor<Image, Classifications> predictor =
model.newPredictor(translator)) {
Classifications classifications = predictor.predict(image);
List<Classifications.Classification> list =
classifications.items();
@@ -91,7 +92,7 @@ public class CustomImageClassificationPredictor extends
AbstractPredictor {
.collect(Collectors.toMap(Classifications.Classification::getClassName, x ->
(float) x.getProbability()));
} catch (TranslateException e) {
LOG.error("Could not process input or output", e);
- throw new RuntimeException("Could not process input or output", e);
+ throw new RuntimeCamelException("Could not process input or
output", e);
}
}
}
diff --git
a/components/camel-djl/src/main/java/org/apache/camel/component/djl/model/CustomObjectDetectionPredictor.java
b/components/camel-djl/src/main/java/org/apache/camel/component/djl/model/CustomObjectDetectionPredictor.java
index b683af9..e990b92 100644
---
a/components/camel-djl/src/main/java/org/apache/camel/component/djl/model/CustomObjectDetectionPredictor.java
+++
b/components/camel-djl/src/main/java/org/apache/camel/component/djl/model/CustomObjectDetectionPredictor.java
@@ -26,6 +26,7 @@ import ai.djl.modality.cv.output.DetectedObjects;
import ai.djl.translate.TranslateException;
import ai.djl.translate.Translator;
import org.apache.camel.Exchange;
+import org.apache.camel.RuntimeCamelException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -42,7 +43,7 @@ public class CustomObjectDetectionPredictor extends
AbstractPredictor {
}
@Override
- public void process(Exchange exchange) throws Exception {
+ public void process(Exchange exchange) {
Model model =
exchange.getContext().getRegistry().lookupByNameAndType(modelName, Model.class);
Translator translator =
exchange.getContext().getRegistry().lookupByNameAndType(translatorName,
Translator.class);
@@ -57,37 +58,37 @@ public class CustomObjectDetectionPredictor extends
AbstractPredictor {
DetectedObjects result = classify(model, translator,
exchange.getIn().getBody(InputStream.class));
exchange.getIn().setBody(result);
} else {
- throw new RuntimeException("Data type is not supported. Body
should be byte[], InputStream or File");
+ throw new RuntimeCamelException("Data type is not supported. Body
should be byte[], InputStream or File");
}
}
- public DetectedObjects classify(Model model, Translator translator, Image
image) throws Exception {
+ public DetectedObjects classify(Model model, Translator translator, Image
image) {
try (Predictor<Image, DetectedObjects> predictor =
model.newPredictor(translator)) {
DetectedObjects detectedObjects = predictor.predict(image);
return detectedObjects;
} catch (TranslateException e) {
LOG.error("Could not process input or output", e);
- throw new RuntimeException("Could not process input or output", e);
+ throw new RuntimeCamelException("Could not process input or
output", e);
}
}
- public DetectedObjects classify(Model model, Translator translator, File
input) throws Exception {
+ public DetectedObjects classify(Model model, Translator translator, File
input) {
try (InputStream fileInputStream = new FileInputStream(input)) {
Image image =
ImageFactory.getInstance().fromInputStream(fileInputStream);
return classify(model, translator, image);
} catch (IOException e) {
LOG.error("Couldn't transform input into a BufferedImage");
- throw new RuntimeException("Couldn't transform input into a
BufferedImage", e);
+ throw new RuntimeCamelException("Couldn't transform input into a
BufferedImage", e);
}
}
- public DetectedObjects classify(Model model, Translator translator,
InputStream input) throws Exception {
+ public DetectedObjects classify(Model model, Translator translator,
InputStream input) {
try {
Image image = ImageFactory.getInstance().fromInputStream(input);
return classify(model, translator, image);
} catch (IOException e) {
LOG.error("Couldn't transform input into a BufferedImage");
- throw new RuntimeException("Couldn't transform input into a
BufferedImage", e);
+ throw new RuntimeCamelException("Couldn't transform input into a
BufferedImage", e);
}
}
}
diff --git
a/components/camel-djl/src/main/java/org/apache/camel/component/djl/model/ModelPredictorProducer.java
b/components/camel-djl/src/main/java/org/apache/camel/component/djl/model/ModelPredictorProducer.java
index 88cbd47..7283ddf 100644
---
a/components/camel-djl/src/main/java/org/apache/camel/component/djl/model/ModelPredictorProducer.java
+++
b/components/camel-djl/src/main/java/org/apache/camel/component/djl/model/ModelPredictorProducer.java
@@ -16,6 +16,12 @@
*/
package org.apache.camel.component.djl.model;
+import java.io.IOException;
+
+import ai.djl.MalformedModelException;
+import ai.djl.repository.zoo.ModelNotFoundException;
+import org.apache.camel.RuntimeCamelException;
+
import static ai.djl.Application.CV.IMAGE_CLASSIFICATION;
import static ai.djl.Application.CV.OBJECT_DETECTION;
@@ -25,13 +31,14 @@ public final class ModelPredictorProducer {
// No-op; won't be called
}
- public static AbstractPredictor getZooPredictor(String applicationPath,
String artifactId) throws Exception {
+ public static AbstractPredictor getZooPredictor(String applicationPath,
String artifactId)
+ throws ModelNotFoundException, MalformedModelException,
IOException {
if (applicationPath.equals(IMAGE_CLASSIFICATION.getPath())) {
return new ZooImageClassificationPredictor(artifactId);
} else if (applicationPath.equals(OBJECT_DETECTION.getPath())) {
return new ZooObjectDetectionPredictor(artifactId);
} else {
- throw new RuntimeException("Application not supported ");
+ throw new RuntimeCamelException("Application not supported ");
}
}
@@ -41,7 +48,7 @@ public final class ModelPredictorProducer {
} else if (applicationPath.equals(OBJECT_DETECTION.getPath())) {
return new CustomObjectDetectionPredictor(model, translator);
} else {
- throw new RuntimeException("Application not supported ");
+ throw new RuntimeCamelException("Application not supported ");
}
}
}
diff --git
a/components/camel-djl/src/main/java/org/apache/camel/component/djl/model/ZooImageClassificationPredictor.java
b/components/camel-djl/src/main/java/org/apache/camel/component/djl/model/ZooImageClassificationPredictor.java
index 19cf386..c335a32 100644
---
a/components/camel-djl/src/main/java/org/apache/camel/component/djl/model/ZooImageClassificationPredictor.java
+++
b/components/camel-djl/src/main/java/org/apache/camel/component/djl/model/ZooImageClassificationPredictor.java
@@ -22,16 +22,19 @@ import java.util.Map;
import java.util.stream.Collectors;
import ai.djl.Application;
+import ai.djl.MalformedModelException;
import ai.djl.inference.Predictor;
import ai.djl.modality.Classifications;
import ai.djl.modality.cv.Image;
import ai.djl.modality.cv.ImageFactory;
import ai.djl.repository.zoo.Criteria;
+import ai.djl.repository.zoo.ModelNotFoundException;
import ai.djl.repository.zoo.ModelZoo;
import ai.djl.repository.zoo.ZooModel;
import ai.djl.training.util.ProgressBar;
import ai.djl.translate.TranslateException;
import org.apache.camel.Exchange;
+import org.apache.camel.RuntimeCamelException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -40,7 +43,8 @@ public class ZooImageClassificationPredictor extends
AbstractPredictor {
private final ZooModel<Image, Classifications> model;
- public ZooImageClassificationPredictor(String artifactId) throws Exception
{
+ public ZooImageClassificationPredictor(String artifactId) throws
ModelNotFoundException, MalformedModelException,
+ IOException {
Criteria<Image, Classifications> criteria = Criteria.builder()
.optApplication(Application.CV.IMAGE_CLASSIFICATION)
.setTypes(Image.class, Classifications.class)
@@ -52,7 +56,7 @@ public class ZooImageClassificationPredictor extends
AbstractPredictor {
}
@Override
- public void process(Exchange exchange) throws Exception {
+ public void process(Exchange exchange) {
if (exchange.getIn().getBody() instanceof byte[]) {
byte[] bytes = exchange.getIn().getBody(byte[].class);
Map<String, Float> result = classify(new
ByteArrayInputStream(bytes));
@@ -64,31 +68,31 @@ public class ZooImageClassificationPredictor extends
AbstractPredictor {
Map<String, Float> result =
classify(exchange.getIn().getBody(InputStream.class));
exchange.getIn().setBody(result);
} else {
- throw new RuntimeException("Data type is not supported. Body
should be byte[], InputStream or File");
+ throw new RuntimeCamelException("Data type is not supported. Body
should be byte[], InputStream or File");
}
}
- public Map<String, Float> classify(File input) throws Exception {
+ public Map<String, Float> classify(File input) {
try (InputStream fileInputStream = new FileInputStream(input)) {
Image image =
ImageFactory.getInstance().fromInputStream(fileInputStream);
return classify(image);
} catch (IOException e) {
LOG.error("Couldn't transform input into a BufferedImage");
- throw new RuntimeException("Couldn't transform input into a
BufferedImage", e);
+ throw new RuntimeCamelException("Couldn't transform input into a
BufferedImage", e);
}
}
- public Map<String, Float> classify(InputStream input) throws Exception {
+ public Map<String, Float> classify(InputStream input) {
try {
Image image = ImageFactory.getInstance().fromInputStream(input);
return classify(image);
} catch (IOException e) {
LOG.error("Couldn't transform input into a BufferedImage");
- throw new RuntimeException("Couldn't transform input into a
BufferedImage", e);
+ throw new RuntimeCamelException("Couldn't transform input into a
BufferedImage", e);
}
}
- public Map<String, Float> classify(Image image) throws Exception {
+ public Map<String, Float> classify(Image image) {
try (Predictor<Image, Classifications> predictor =
model.newPredictor()) {
Classifications classifications = predictor.predict(image);
List<Classifications.Classification> list =
classifications.items();
@@ -96,7 +100,7 @@ public class ZooImageClassificationPredictor extends
AbstractPredictor {
.collect(Collectors.toMap(Classifications.Classification::getClassName, x ->
(float) x.getProbability()));
} catch (TranslateException e) {
LOG.error("Could not process input or output", e);
- throw new RuntimeException("Could not process input or output", e);
+ throw new RuntimeCamelException("Could not process input or
output", e);
}
}
}
diff --git
a/components/camel-djl/src/main/java/org/apache/camel/component/djl/model/ZooObjectDetectionPredictor.java
b/components/camel-djl/src/main/java/org/apache/camel/component/djl/model/ZooObjectDetectionPredictor.java
index 661e60c..c18ebe3 100644
---
a/components/camel-djl/src/main/java/org/apache/camel/component/djl/model/ZooObjectDetectionPredictor.java
+++
b/components/camel-djl/src/main/java/org/apache/camel/component/djl/model/ZooObjectDetectionPredictor.java
@@ -19,16 +19,19 @@ package org.apache.camel.component.djl.model;
import java.io.*;
import ai.djl.Application;
+import ai.djl.MalformedModelException;
import ai.djl.inference.Predictor;
import ai.djl.modality.cv.Image;
import ai.djl.modality.cv.ImageFactory;
import ai.djl.modality.cv.output.DetectedObjects;
import ai.djl.repository.zoo.Criteria;
+import ai.djl.repository.zoo.ModelNotFoundException;
import ai.djl.repository.zoo.ModelZoo;
import ai.djl.repository.zoo.ZooModel;
import ai.djl.training.util.ProgressBar;
import ai.djl.translate.TranslateException;
import org.apache.camel.Exchange;
+import org.apache.camel.RuntimeCamelException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -38,7 +41,7 @@ public class ZooObjectDetectionPredictor extends
AbstractPredictor {
private final ZooModel<Image, DetectedObjects> model;
- public ZooObjectDetectionPredictor(String artifactId) throws Exception {
+ public ZooObjectDetectionPredictor(String artifactId) throws
ModelNotFoundException, MalformedModelException, IOException {
Criteria<Image, DetectedObjects> criteria = Criteria.builder()
.optApplication(Application.CV.OBJECT_DETECTION)
.setTypes(Image.class, DetectedObjects.class)
@@ -49,7 +52,7 @@ public class ZooObjectDetectionPredictor extends
AbstractPredictor {
}
@Override
- public void process(Exchange exchange) throws Exception {
+ public void process(Exchange exchange) {
if (exchange.getIn().getBody() instanceof byte[]) {
byte[] bytes = exchange.getIn().getBody(byte[].class);
DetectedObjects result = classify(new ByteArrayInputStream(bytes));
@@ -61,36 +64,36 @@ public class ZooObjectDetectionPredictor extends
AbstractPredictor {
DetectedObjects result =
classify(exchange.getIn().getBody(InputStream.class));
exchange.getIn().setBody(result);
} else {
- throw new RuntimeException("Data type is not supported. Body
should be byte[], InputStream or File");
+ throw new RuntimeCamelException("Data type is not supported. Body
should be byte[], InputStream or File");
}
}
- public DetectedObjects classify(Image image) throws Exception {
+ public DetectedObjects classify(Image image) {
try (Predictor<Image, DetectedObjects> predictor =
model.newPredictor()) {
DetectedObjects detectedObjects = predictor.predict(image);
return detectedObjects;
} catch (TranslateException e) {
- throw new RuntimeException("Could not process input or output", e);
+ throw new RuntimeCamelException("Could not process input or
output", e);
}
}
- public DetectedObjects classify(File input) throws Exception {
+ public DetectedObjects classify(File input) {
try (InputStream fileInputStream = new FileInputStream(input)) {
Image image =
ImageFactory.getInstance().fromInputStream(fileInputStream);
return classify(image);
} catch (IOException e) {
LOG.error("Couldn't transform input into a BufferedImage");
- throw new RuntimeException("Couldn't transform input into a
BufferedImage", e);
+ throw new RuntimeCamelException("Couldn't transform input into a
BufferedImage", e);
}
}
- public DetectedObjects classify(InputStream input) throws Exception {
+ public DetectedObjects classify(InputStream input) {
try {
Image image = ImageFactory.getInstance().fromInputStream(input);
return classify(image);
} catch (IOException e) {
LOG.error("Couldn't transform input into a BufferedImage");
- throw new RuntimeException("Couldn't transform input into a
BufferedImage", e);
+ throw new RuntimeCamelException("Couldn't transform input into a
BufferedImage", e);
}
}
}