/*
 * #%L
 * fujion
 * %%
 * Copyright (C) 2008 - 2018 Regenstrief Institute, Inc.
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * #L%
 */
package org.fujion.script.jruby;

import java.util.Map;
import java.util.Map.Entry;

import org.fujion.script.IScriptLanguage;
import org.jruby.embed.ScriptingContainer;
import org.jruby.javasupport.JavaEmbedUtils.EvalUnit;

/**
 * Support for embedding Ruby scripts.
 */
public class JRubyScript implements IScriptLanguage {
    
    /**
     * Wrapper for a parsed Ruby script
     */
    public static class ParsedScript implements IParsedScript {
        
        private final ScriptingContainer container;
        
        private final EvalUnit unit;
        
        public ParsedScript(String source) {
            container = new ScriptingContainer();
            unit = container.parse(source);
        }
        
        @Override
        public Object run(Map<String, Object> variables) {
            container.clear();
            
            if (variables != null) {
                for (Entry<String, Object> entry : variables.entrySet()) {
                    container.put(entry.getKey(), entry.getValue());
                }
            }
            
            return unit.run();
        }
        
        @Override
        public void destroy() {
            container.terminate();
        }
    }
    
    /**
     * @see org.fujion.script.IScriptLanguage#getType()
     */
    @Override
    public String getType() {
        return "jruby";
    }
    
    /**
     * Global variables in JRuby must start with a '$' character.
     *
     * @see org.fujion.script.IScriptLanguage#getSelf()
     */
    @Override
    public String getSelf() {
        return "$self";
    }
    
    /**
     * @see org.fujion.script.IScriptLanguage#parse(java.lang.String)
     */
    @Override
    public IParsedScript parse(String source) {
        return new ParsedScript(source);
    }
    
}

@@ -779,16 +779,58 @@
 nguage;%0A
+import org.jruby.embed.LocalContextScope;%0A
 import o
@@ -864,16 +864,16 @@
 tainer;%0A
-
 import o
@@ -1020,21 +1020,17 @@
 guage %7B%0A
-    
 %0A
+
     /**%0A
@@ -1141,25 +1141,17 @@
 cript %7B%0A
-        
 %0A
+
         
@@ -1194,25 +1194,17 @@
 tainer;%0A
-        
 %0A
+
         
@@ -1232,25 +1232,17 @@
 t unit;%0A
-        
 %0A
+
         
@@ -1325,16 +1325,46 @@
 ntainer(
+LocalContextScope.SINGLETHREAD
 );%0A     
@@ -1408,33 +1408,25 @@
 ;%0A        %7D%0A
-        
 %0A
+
         @Ove
@@ -1521,29 +1521,17 @@
 lear();%0A
-            
 %0A
+
         
@@ -1739,21 +1739,9 @@
   %7D%0A
-            
 %0A
+
     
@@ -1781,17 +1781,9 @@
   %7D%0A
-        
 %0A
+
     
@@ -1875,29 +1875,25 @@
     %7D%0A    %7D%0A
-    
 %0A
+
     /**%0A    
@@ -2018,37 +2018,33 @@
  %22jruby%22;%0A    %7D%0A
-    
 %0A
+
     /**%0A     * G
@@ -2246,21 +2246,17 @@
 ;%0A    %7D%0A
-    
 %0A
+
     /**%0A
@@ -2442,15 +2442,11 @@
 ;%0A    %7D%0A
-    
 %0A%7D%0A

Fix sporadic JRuby ArrayIndexOutOfBoundsException.