/* 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.
 */

package org.activiti.engine.impl.scripting;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.script.Bindings;
import javax.script.SimpleScriptContext;

import org.activiti.engine.delegate.VariableScope;

/**
 * @author Tom Baeyens
 * @author Joram Barrez
 */
public class ScriptBindings implements Bindings {

  /**
   * The script engine implementations put some key/value pairs into the binding. This list contains those keys, such that they wouldn't be stored as process variable.
   * 
   * This list contains the keywords for JUEL, Javascript and Groovy.
   */
  protected static final Set<String> UNSTORED_KEYS = new HashSet<String>(Arrays.asList("out", "out:print", "lang:import", "context", "elcontext", "print", "println"));

  protected List<Resolver> scriptResolvers;
  protected VariableScope variableScope;
  protected Bindings defaultBindings;
  protected boolean storeScriptVariables = true; // By default everything is
                                                 // stored (backwards
                                                 // compatibility)

  public ScriptBindings(List<Resolver> scriptResolvers, VariableScope variableScope) {
    this.scriptResolvers = scriptResolvers;
    this.variableScope = variableScope;
    this.defaultBindings = new SimpleScriptContext().getBindings(SimpleScriptContext.ENGINE_SCOPE);
  }

  public ScriptBindings(List<Resolver> scriptResolvers, VariableScope variableScope, boolean storeScriptVariables) {
    this(scriptResolvers, variableScope);
    this.storeScriptVariables = storeScriptVariables;
  }

  public boolean containsKey(Object key) {
    for (Resolver scriptResolver : scriptResolvers) {
      if (scriptResolver.containsKey(key)) {
        return true;
      }
    }
    return defaultBindings.containsKey(key);
  }

  public Object get(Object key) {
    for (Resolver scriptResolver : scriptResolvers) {
      if (scriptResolver.containsKey(key)) {
        return scriptResolver.get(key);
      }
    }
    return defaultBindings.get(key);
  }

  public Object put(String name, Object value) {
    if (storeScriptVariables) {
      Object oldValue = null;
      if (!UNSTORED_KEYS.contains(name)) {
        oldValue = variableScope.getVariable(name);
        variableScope.setVariable(name, value);
        return oldValue;
      }
    }
    return defaultBindings.put(name, value);
  }

  public Set<Map.Entry<String, Object>> entrySet() {
    return variableScope.getVariables().entrySet();
  }

  public Set<String> keySet() {
    return variableScope.getVariables().keySet();
  }

  public int size() {
    return variableScope.getVariables().size();
  }

  public Collection<Object> values() {
    return variableScope.getVariables().values();
  }

  public void putAll(Map<? extends String, ? extends Object> toMerge) {
    throw new UnsupportedOperationException();
  }

  public Object remove(Object key) {
    if (UNSTORED_KEYS.contains(key)) {
      return null;
    }
    return defaultBindings.remove(key);
  }

  public void clear() {
    throw new UnsupportedOperationException();
  }

  public boolean containsValue(Object value) {
    throw new UnsupportedOperationException();
  }

  public boolean isEmpty() {
    throw new UnsupportedOperationException();
  }
}

@@ -1067,16 +1067,22 @@
 inding. 
+%0A   * 
 This lis
@@ -1294,16 +1294,21 @@
 _KEYS = 
+%0A    
 new Hash
@@ -1410,16 +1410,34 @@
 println%22
+, %22nashorn.global%22
 ));%0A%0A  p
@@ -1636,130 +1636,26 @@
 g is
-%0A                                                 // stored (backwards%0A                                                 //
+ stored (backwards
  com
@@ -3851,10 +3851,106 @@
 ();%0A  %7D%0A
+  %0A  public void addUnstoredKey(String unstoredKey) %7B%0A  %09UNSTORED_KEYS.add(unstoredKey);%0A  %7D%0A  %0A
 %7D%0A

Add 'nashorn.global' object to unstored keys. Also add a method to add new keys at runtime.