Guest User

Untitled

a guest
Jun 10th, 2014
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.75 KB | None | 0 0
  1. package com.ais.ifidsadminutility;
  2.  
  3. import android.os.Environment;
  4. import android.util.Log;
  5. import clojure.lang.IFn;
  6. import clojure.lang.RT;
  7. import clojure.java.api.Clojure;
  8. import clojure.lang.Compiler;
  9.  
  10. import java.io.File;
  11. import java.io.IOException;
  12.  
  13. public class ScriptManager {
  14.     private static ScriptManager instance = null;
  15.    
  16.    
  17.    
  18.     public static ScriptManager getInstance() {
  19.            if(instance == null) {
  20.               instance = new ScriptManager();
  21.            }
  22.            return instance;
  23.     }
  24.    
  25.     public void runTest() {    
  26.         // Load the Clojure script -- as a side effect this initializes the runtime.
  27.         //IFn read_string = Clojure.var("clojure.core", "read-string");
  28.         //IFn eval = Clojure.var("clojure.core", "eval");
  29.        
  30.         //String str = "(ns user) (defn foo [a b]   (str a \" \" b))";
  31.         RT.init();
  32.        
  33.         try {
  34.             FileUtilities.writeAssetToFile("scripts/main.clj", "main.clj", this.getScriptFolder());
  35.         } catch (IOException e) {
  36.             // TODO Auto-generated catch block
  37.             Log.e("ScriptManager", "Failed to load copy to external storage: ");
  38.             Log.e("ScriptManager", e.getMessage());
  39.             e.printStackTrace();
  40.         }
  41.        
  42.         //IFn map = Clojure.var("clojure.core", "map");
  43.         //IFn inc = Clojure.var("clojure.core", "inc");
  44.         //Object resMap = map.invoke(inc, Clojure.read("[1 2 3]"));
  45.         //Log.v("ScriptManager", resMap.toString());
  46.        
  47.         //Grabbing the load-file clojure function
  48.         IFn load_file = Clojure.var("clojure.core", "load-file");
  49.         IFn load_string = Clojure.var("clojure.core", "load-string");
  50.         IFn slurp = Clojure.var("clojure.core", "slurp");
  51.        
  52.         if (load_file == null) {
  53.             Log.e("ScriptManager", "load_file is null");
  54.         }
  55.        
  56.         String scriptPath = new File(this.getScriptFolder(), "main.clj").getAbsolutePath();
  57.        
  58.         //try and read print the output of our file
  59.         String text = (String) slurp.invoke(scriptPath);
  60.         Log.v("ScriptManager", text);
  61.        
  62.         try {
  63.             load_string.invoke(text);
  64.         }
  65.         catch (Exception e) {
  66.             Log.e("ScriptManager", "Failed to load-string init file");
  67.             Log.e("ScriptManager", e.getMessage());
  68.             return;
  69.         }
  70.        
  71.         try {
  72.             load_file.invoke(scriptPath);
  73.         }
  74.         catch (Exception e) {
  75.             Log.e("ScriptManager", "Failed to load-file init file");
  76.             Log.e("ScriptManager", e.getMessage());
  77.             return;
  78.         }
  79.        
  80.         // Get a reference to the foo function.
  81.         IFn foo = Clojure.var("user", "foo");
  82.  
  83.         // Call it!
  84.         Object result = foo.invoke("Hi there");
  85.         Log.v("ScriptManager", result.toString()); 
  86.     }
  87.    
  88.     public String getScriptFolder() {
  89.         return new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "/iFIDS.com/scripts/").getAbsolutePath();
  90.     }
  91.    
  92. }
Advertisement
Add Comment
Please, Sign In to add comment