Skip to content

Commit c559523

Browse files
committed
Make Exceptions Handling better
1 parent ecf9927 commit c559523

File tree

7 files changed

+99
-22
lines changed

7 files changed

+99
-22
lines changed

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name := "pdal-jni"
22

33
lazy val commonSettings = Seq(
4-
version := "1.8.3" + Environment.versionSuffix,
4+
version := "1.8.4" + Environment.versionSuffix,
55
scalaVersion := "2.11.12",
66
crossScalaVersions := Seq("2.12.8", "2.11.12"),
77
organization := "io.pdal",

core/src/main/scala/io/pdal/Pipeline.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import ch.jodersky.jni.nativeLoader
3838
class Pipeline(val json: String) extends Native {
3939
Pipeline // reference the object so that the nativeLoader will load the JNI native libraries
4040

41-
@native def initialise(): Unit
41+
@native def initialize(): Unit
4242
@native def execute(): Unit
4343
@native def getPointViews(): PointViewIterator
4444
@native def dispose(): Unit
@@ -52,5 +52,5 @@ class Pipeline(val json: String) extends Native {
5252

5353
@nativeLoader("pdaljni.1.4")
5454
object Pipeline {
55-
def apply(json: String): Pipeline = { val p = new Pipeline(json); p.initialise(); p }
55+
def apply(json: String): Pipeline = { val p = new Pipeline(json); p.initialize(); p }
5656
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package io.pdal
2+
3+
abstract class PipelineException(msg: String) extends Exception(msg)
4+
5+
case class InitializationException(msg: String) extends PipelineException(msg)
6+
case class ExecutionException(msg: String) extends PipelineException(msg)

core/src/test/scala/io/pdal/PipelineSpec.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,5 +173,19 @@ class PipelineSpec extends TestEnvironmentSpec {
173173
pv.dispose()
174174
pvi.dispose()
175175
}
176+
177+
it("should fail with InitializationException") {
178+
intercept[InitializationException] { Pipeline(null) }
179+
}
180+
181+
it("should fail with ExecutionException") {
182+
val pipeline = Pipeline("{")
183+
intercept[ExecutionException] { pipeline.execute() }
184+
intercept[ExecutionException] { pipeline.getPointViews() }
185+
intercept[ExecutionException] { pipeline.getMetadata() }
186+
intercept[ExecutionException] { pipeline.getSchema() }
187+
188+
pipeline.dispose()
189+
}
176190
}
177191
}

native/src/include/io_pdal_Pipeline.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

native/src/io_pdal_Pipeline.cpp

Lines changed: 72 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,42 @@ using pdal::PointViewLess;
4949
using pdal::PointViewPtr;
5050
using pdal::pdal_error;
5151

52-
JNIEXPORT void JNICALL Java_io_pdal_Pipeline_initialise
52+
jstring throwInitializationException(JNIEnv *env, const char *message)
53+
{
54+
jclass Exception = env->FindClass("io/pdal/InitializationException");
55+
env->ThrowNew(Exception, message);
56+
return env->NewStringUTF(message);
57+
}
58+
59+
jstring throwExecutionException(JNIEnv *env, const char *message)
60+
{
61+
jclass Exception = env->FindClass("io/pdal/ExecutionException");
62+
env->ThrowNew(Exception, message);
63+
return env->NewStringUTF(message);
64+
}
65+
66+
JNIEXPORT void JNICALL Java_io_pdal_Pipeline_initialize
5367
(JNIEnv *env, jobject obj)
5468
{
5569
jclass c = env->GetObjectClass(obj);
5670
jfieldID fid = env->GetFieldID(c, "json", "Ljava/lang/String;");
5771
jstring jstr = reinterpret_cast<jstring>(env->GetObjectField(obj, fid));
58-
setHandle(env, obj, new Pipeline(std::string(env->GetStringUTFChars(jstr, 0))));
72+
73+
if(jstr == NULL)
74+
{
75+
throwInitializationException(env, "Null string passed into the Pipeline constructor.");
76+
}
77+
else
78+
{
79+
try
80+
{
81+
setHandle(env, obj, new Pipeline(std::string(env->GetStringUTFChars(jstr, 0))));
82+
}
83+
catch (const pdal_error &pe)
84+
{
85+
throwInitializationException(env, pe.what());
86+
}
87+
}
5988
}
6089

6190
JNIEXPORT void JNICALL Java_io_pdal_Pipeline_dispose
@@ -70,21 +99,42 @@ JNIEXPORT void JNICALL Java_io_pdal_Pipeline_execute
7099
(JNIEnv *env, jobject obj)
71100
{
72101
Pipeline *p = getHandle<Pipeline>(env, obj);
73-
p->execute();
102+
try
103+
{
104+
p->execute();
105+
}
106+
catch(const pdal_error &pe)
107+
{
108+
throwExecutionException(env, pe.what());
109+
}
74110
}
75111

76112
JNIEXPORT jstring JNICALL Java_io_pdal_Pipeline_getMetadata
77113
(JNIEnv *env, jobject obj)
78114
{
79-
Pipeline *p = getHandle<Pipeline>(env, obj);
80-
return env->NewStringUTF(p->getMetadata().c_str());
115+
try
116+
{
117+
Pipeline *p = getHandle<Pipeline>(env, obj);
118+
return env->NewStringUTF(p->getMetadata().c_str());
119+
}
120+
catch(const pdal_error &pe)
121+
{
122+
return throwExecutionException(env, pe.what());
123+
}
81124
}
82125

83126
JNIEXPORT jstring JNICALL Java_io_pdal_Pipeline_getSchema
84127
(JNIEnv *env, jobject obj)
85128
{
86-
Pipeline *p = getHandle<Pipeline>(env, obj);
87-
return env->NewStringUTF(p->getSchema().c_str());
129+
try
130+
{
131+
Pipeline *p = getHandle<Pipeline>(env, obj);
132+
return env->NewStringUTF(p->getSchema().c_str());
133+
}
134+
catch(const pdal_error &pe)
135+
{
136+
return throwExecutionException(env, pe.what());
137+
}
88138
}
89139

90140
JNIEXPORT jboolean JNICALL Java_io_pdal_Pipeline_validate
@@ -129,16 +179,23 @@ JNIEXPORT jstring JNICALL Java_io_pdal_Pipeline_getLog
129179
JNIEXPORT jobject JNICALL Java_io_pdal_Pipeline_getPointViews
130180
(JNIEnv *env, jobject obj)
131181
{
132-
Pipeline *p = getHandle<Pipeline>(env, obj);
133-
PointViewSet pvset = p->getPointViews();
182+
try
183+
{
184+
Pipeline *p = getHandle<Pipeline>(env, obj);
185+
PointViewSet pvset = p->getPointViews();
134186

135-
jclass pviClass = env->FindClass("io/pdal/PointViewIterator");
136-
jmethodID pviCtor = env->GetMethodID(pviClass, "<init>", "()V");
137-
jobject pvi = env->NewObject(pviClass, pviCtor);
187+
jclass pviClass = env->FindClass("io/pdal/PointViewIterator");
188+
jmethodID pviCtor = env->GetMethodID(pviClass, "<init>", "()V");
189+
jobject pvi = env->NewObject(pviClass, pviCtor);
138190

139-
PointViewIterator *it = new PointViewIterator(pvset);
191+
PointViewIterator *it = new PointViewIterator(pvset);
140192

141-
setHandle(env, pvi, it);
193+
setHandle(env, pvi, it);
142194

143-
return pvi;
195+
return pvi;
196+
}
197+
catch(const libpdaljava::java_error &je)
198+
{
199+
return throwExecutionException(env, je.what());
200+
}
144201
}

scripts/merge-native.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ done
1717
export PDAL_VERSION_SUFFIX=${PDAL_VERSION_SUFFIX-"-SNAPSHOT"}
1818

1919
cd ./native/target
20-
rm -f ./pdal-native-1.8.3${PDAL_VERSION_SUFFIX}.jar
20+
rm -f ./pdal-native-1.8.4${PDAL_VERSION_SUFFIX}.jar
2121
rm -rf ./tmp; mkdir -p ./tmp
2222

23-
cd tmp; jar -xf ../pdal-native-x86_64-darwin-1.8.3${PDAL_VERSION_SUFFIX}.jar; cd ~-
24-
cd tmp; jar -xf ../pdal-native-x86_64-linux-1.8.3${PDAL_VERSION_SUFFIX}.jar; cd ~-
23+
cd tmp; jar -xf ../pdal-native-x86_64-darwin-1.8.4${PDAL_VERSION_SUFFIX}.jar; cd ~-
24+
cd tmp; jar -xf ../pdal-native-x86_64-linux-1.8.4${PDAL_VERSION_SUFFIX}.jar; cd ~-
2525

2626
jar -cvf pdal-native-1.8.0${PDAL_VERSION_SUFFIX}.jar -C tmp .
2727

0 commit comments

Comments
 (0)