ossiejhmoore

JoExperiment. runSingleThreaded

Feb 23rd, 2017
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.20 KB | None | 0 0
  1. package com.capspire.ecm.ot.experimental;
  2.  
  3. import com.opentext.livelink.oml.OScriptObject;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6.  
  7. public class JOExperiments {
  8.  
  9.     private static final Object Undefined = null;
  10.     private static final String OK = "ok";
  11.     private static final String ErrMsg = "ErrMsg";
  12.     private static final String ApiError = "ApiError";
  13.     private static final String Result = "Result";
  14.  
  15.     public static synchronized Map<String, Object> runSingleThreaded(String scriptPath) {
  16.         Map<String, Object> retVal = new HashMap<String, Object>();
  17.         retVal.put(OK, Boolean.FALSE);
  18.         retVal.put(ErrMsg, Undefined);
  19.         retVal.put(ApiError, Undefined);
  20.         retVal.put(Result, Undefined);
  21.  
  22.         try {
  23.             if (isNullOrEmpty(scriptPath)) {
  24.                 throw new RuntimeException("The scriptPath parameter cannot be null or empty.");
  25.             }
  26.             Object result = OScriptObject.runScript(scriptPath);
  27.             retVal.put(Result, result);
  28.             retVal.put(OK, Boolean.TRUE);
  29.         } catch (Exception e) {
  30.             retVal.put(ErrMsg, e.getLocalizedMessage());
  31.             retVal.put(ApiError, e);
  32.         }
  33.         if (IsNotOk(retVal)) {
  34.             if (IsUndefined(retVal.get(ErrMsg))) {
  35.                 retVal.put(ErrMsg, "Unexpected error executing runSingleThreaded. Perhaps you forgot to set OK to true or to provide an ErrMsg value.");
  36.             }
  37.         }
  38.  
  39.         return retVal;
  40.     }
  41.  
  42.     private static boolean isNullOrEmpty(Object o) {
  43.         return ((o == null) || (o.toString() == null) || (o.toString().isEmpty()));
  44.     }
  45.  
  46.     private static boolean IsOk(Object o) {
  47.         Object test;
  48.         if (o instanceof Map) {
  49.             test = ((Map) o).get(OK);
  50.         } else if (o instanceof Boolean) {
  51.             test = o;
  52.         } else {
  53.             throw new RuntimeException(String.format("Expected Boolean or Map, got %s", o.getClass().getName()));
  54.         }
  55.         return (Boolean.TRUE.equals(test));
  56.     }
  57.  
  58.     private static boolean IsNotOk(Object o) {
  59.         return !IsOk(o);
  60.     }
  61.  
  62.     private static boolean IsUndefined(Object o) {
  63.         return (o == null);
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment