@@ -180,7 +180,7 @@ |
/** |
* This is an error. (either an exception or a string). |
*/ |
- Object serviceError = null; |
+ Throwable serviceError = null; |
|
|
// report |
@@ -442,7 +442,7 @@ |
|
//////////////////////////////////////////////////////////////////// |
|
- private Object preProcess0() throws Exception { |
+ private Throwable preProcess0() throws Exception { |
// handle static data |
for (Resource r : Binaries.getMergedResources(getClass())) { |
if (r.type() == ResourceType.JDBC || r.type() == ResourceType.REFERENCE || r.type() == ResourceType.OUTPUT) { |
@@ -457,7 +457,7 @@ |
} |
} |
} |
- return doPrePostProcessWrapper(true); |
+ return doProcessWrapper(pre); |
} |
|
|
@@ -470,7 +470,7 @@ |
// * @throws Exception if an error occurred during execution. |
// */ |
@Deprecated |
- public Callable<Object> createCallable() throws Exception { |
+ public Callable<Throwable> createCallable() throws Exception { |
return () -> { |
return process0(); |
}; |
@@ -489,34 +489,35 @@ |
return EXEC_OK; |
} |
|
+ private static final int pre = 0; |
+ private static final int proc = 1; |
+ private static final int post = 2; |
+ |
|
/** |
* This replaces the process() call from process0 |
*/ |
- private Object doPrePostProcessWrapper(boolean pre) { |
+ private Throwable doProcessWrapper(int phase) { |
try { |
- if (pre) { |
- preProcess(); |
- } else { |
- postProcess(); |
+ switch (phase) { |
+ case pre: |
+ preProcess(); |
+ break; |
+ case proc: |
+ doProcess(); // preferred |
+ String ret = process(); // deprecated |
+ if (ret != null) { |
+ return new ServiceException(ret); |
+ } |
+ break; |
+ case post: |
+ postProcess(); |
+ break; |
} |
- return null; |
} catch (Throwable E) { |
return E; |
} |
- } |
- |
- |
- /** |
- * This replaces the process() call from process0 |
- */ |
- private Object doProcessWrapper() { |
- try { |
- doProcess(); |
- return process(); // remove call later |
- } catch (Throwable E) { |
- return E; |
- } |
+ return null; |
} |
|
|
@@ -535,7 +536,7 @@ |
} |
|
|
- private Object process0() throws Exception { |
+ private Throwable process0() throws Exception { |
Resource[] resources = Binaries.getResourcesByType(getClass(), ResourceType.OMS_COMP); |
if (resources.length > 0) { |
Class cl = getClass(); |
@@ -543,17 +544,20 @@ |
String classname = resources[0].file(); |
cl = Thread.currentThread().getContextClassLoader().loadClass(classname); |
} |
- Object comp = cl.newInstance(); |
- OMSComponentMapper cm = new OMSComponentMapper(comp); |
- cm.setInputs(this); |
- cm.process(); |
- cm.getOutputs(this); |
- return EXEC_OK; |
+ try { |
+ OMSComponentMapper cm = new OMSComponentMapper(cl.newInstance()); |
+ cm.setInputs(this); |
+ cm.process(); |
+ cm.getOutputs(this); |
+ } catch (Throwable T) { |
+ return T; |
+ } |
+ return null; |
} |
|
Resource resource = Binaries.getResourceById(getClass(), AUTO_RUN); |
if (resource == null) { |
- return doProcessWrapper(); |
+ return doProcessWrapper(proc); |
} |
|
Executable e = null; |
@@ -578,24 +582,24 @@ |
LOG.info("done with exit value: " + ret); |
|
if (ret == 0) { |
- return EXEC_OK; |
+ return null; |
} |
FilenameFilter ff = new WildcardFileFilter("*" + Binaries.STDERR, IOCase.INSENSITIVE); |
File[] f = getWorkspaceDir().listFiles(ff); |
if (f != null && f.length > 0) { |
- return FileUtils.readFileToString(f[0]); |
+ return new ServiceException(FileUtils.readFileToString(f[0])); |
} |
- return EXEC_FAILED + " return code " + ret; |
+ return new ServiceException(EXEC_FAILED + " return code " + ret); |
} |
|
|
- private Object postProcess0() throws Exception { |
+ private Throwable postProcess0() throws Exception { |
for (Resource r : Binaries.getResourcesByType(getClass(), ResourceType.OUTPUT)) { |
String[] files = r.file().split("\\s+"); |
FilenameFilter ff = new WildcardFileFilter(files, IOCase.INSENSITIVE); |
putResult(getWorkspaceDir().listFiles(ff)); |
} |
- return doPrePostProcessWrapper(false); |
+ return doProcessWrapper(post); |
} |
|
|
@@ -2291,11 +2295,11 @@ |
// preprocess(); |
serviceError = preProcess0(); |
if (serviceError != null) { |
- throw (Throwable) serviceError; |
+ throw serviceError; |
} |
|
// step 2 create the model |
- Callable<Object> model = createCallable(); |
+ Callable<Throwable> model = createCallable(); |
// Callable<Object> model = () -> { |
// return process0(); |
// }; |
@@ -2452,13 +2456,13 @@ |
|
static final long ONE_DAY = 60 * 60 * 24 * 1000; // 1 day in milli seconds, max time for a running job to be accessible. |
// |
- FutureTask<Object> task; |
+ FutureTask<Throwable> task; |
String status = UNKNOWN; |
// |
long keepResults = Dates.getDurationSec("csip.session.ttl"); |
|
|
- public Task(Callable<Object> call) { |
+ public Task(Callable<Throwable> call) { |
task = new FutureTask<>(call); |
} |
|
@@ -2702,7 +2706,7 @@ |
} |
|
|
- private void setFailedStatus(Object err) { |
+ private void setFailedStatus(Throwable err) { |
String message = JSONUtils.getErrorMessage(err); |
try { |
JSONObject metainfo = updateMetadata(FAILED); |
@@ -462,44 +462,29 @@ |
} |
|
|
- public static String getErrorMessage(Object e) { |
- String message = null; |
- if (e instanceof String) { |
- message = (String) e; |
- } else if (e instanceof Throwable) { |
- message = ((Throwable) e).getMessage(); |
- if (message == null) { |
- message = e.getClass().toString(); |
- } |
- } else { |
- message = "unknown error."; |
+ public static String getErrorMessage(Throwable e) { |
+ String message = e.getMessage(); |
+ if (message == null) { |
+ message = e.getClass().toString(); |
} |
return message; |
} |
|
|
- public static String getStringStackTrace(Object e) { |
- if (!(e instanceof Throwable)) { |
- return null; |
- } |
+ public static String getStringStackTrace(Throwable e) { |
StringWriter sw = new StringWriter(); |
PrintWriter pw = new PrintWriter(sw); |
- ((Throwable) e).printStackTrace(pw); |
+ e.printStackTrace(pw); |
pw.close(); |
return sw.toString(); |
} |
|
|
- public static JSONArray getJSONStackTrace(Object e) { |
- if (!(e instanceof Throwable)) { |
- return null; |
- } |
- Throwable E = (Throwable) e; |
- String s = getStringStackTrace(E); |
+ public static JSONArray getJSONStackTrace(Throwable e) { |
+ String s = getStringStackTrace(e); |
JSONArray arr = new JSONArray(); |
- String[] st = s.split("\n"); |
- for (int i = 0; i < st.length; i++) { |
- arr.put(st[i].replace('\t', ' ')); |
+ for (String st : s.split("\n")) { |
+ arr.put(st.replace('\t', ' ')); |
} |
return arr; |
} |
@@ -526,12 +511,12 @@ |
} |
|
|
- public static JSONObject newError(JSONObject meta, JSONArray parameter, Throwable T) { |
+ public static JSONObject newError(JSONObject meta, JSONArray parameter, Throwable err) { |
JSONObject resp = new JSONObject(); |
try { |
- meta.put(ModelDataService.ERROR, getErrorMessage(T)); |
+ meta.put(ModelDataService.ERROR, getErrorMessage(err)); |
if (Config.getBoolean("csip.response.stacktrace")) { |
- JSONArray trace = getJSONStackTrace(T); |
+ JSONArray trace = getJSONStackTrace(err); |
if (trace != null) { |
meta.put("stacktrace", trace); |
} |
@@ -547,7 +532,6 @@ |
} |
|
|
- |
public static JSONObject data(String name, Object value) throws JSONException { |
return data(name, value, null); |
} |