/*
 * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
 *
 * This file is part of Resin(R) Open Source
 *
 * Each copy or derived work must preserve the copyright notice and this
 * notice unmodified.
 *
 * Resin Open Source is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Resin Open Source is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
 * of NON-INFRINGEMENT.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Resin Open Source; if not, write to the
 *   Free SoftwareFoundation, Inc.
 *   59 Temple Place, Suite 330
 *   Boston, MA 02111-1307  USA
 *
 * @author Scott Ferguson
 */

package com.caucho.el;

import java.util.HashMap;

import java.util.logging.*;

import javax.servlet.jsp.el.VariableResolver;
import javax.servlet.jsp.el.ELException;

import com.caucho.util.L10N;

import com.caucho.log.Log;

import com.caucho.vfs.WriteStream;

import com.caucho.loader.EnvironmentLocal;

/**
 * Abstract implementation class for an expression.
 */
public class EL {
  private static final Logger log = Log.open(EL.class);
  private static final L10N L = new L10N(EL.class);
  
  private static EnvironmentLocal<VariableResolver> _elEnvironment =
    new EnvironmentLocal<VariableResolver>();
  
  private static EnvironmentLocal<HashMap<String,Object>> _envVar =
    new EnvironmentLocal<HashMap<String,Object>>();

  public final static Object NULL = new Object();

  public static VariableResolver getEnvironment()
  {
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    
    return getEnvironment(loader);
  }

  public static VariableResolver getEnvironment(ClassLoader loader)
  {
    VariableResolver resolver = _elEnvironment.get(loader);

    if (resolver == null) {
      VariableResolver parent = new SystemPropertiesResolver();
      
      resolver = new EnvironmentResolver(loader, parent);
      _elEnvironment.set(resolver, loader);
    }

    return resolver;
  }
  
  public static void setEnvironment(VariableResolver env)
  {
    _elEnvironment.set(env);
  }
  
  public static void setEnvironment(VariableResolver env,
                                    ClassLoader loader)
  {
    _elEnvironment.set(env, loader);
  }

  /**
   * Sets the environment map.
   */
  public static void setVariableMap(HashMap<String,Object> map,
				    ClassLoader loader)
  {
    _envVar.set(map, loader);
  }

  /**
   * Gets a var from the specified level.
   */
  public static Object getLevelVar(String name, ClassLoader loader)
  {
    HashMap<String,Object> varMap = _envVar.getLevel(loader);

    if (varMap == null)
      return null;
    else
      return varMap.get(name);
  }

  /**
   * Puts an environment value.
   */
  public static Object putVar(String name, Object value)
  {
    return putVar(name, value, Thread.currentThread().getContextClassLoader());
  }

  /**
   * Puts an environment value.
   */
  public static Object putVar(String name, Object value, ClassLoader loader)
  {
    HashMap<String,Object> varMap = _envVar.getLevel(loader);

    if (varMap == null) {
      varMap = new HashMap<String,Object>();
      _envVar.set(varMap, loader);
    }

    return varMap.put(name, value);
  }

  public static Object evalObject(String value)
    throws ELParseException, ELException
  {
    ELParser parser = new ELParser(value);

    Expr expr = parser.parse();

    return expr.evalObject(getEnvironment());
  }

  public static Object evalObject(String value, VariableResolver env)
    throws ELParseException, ELException
  {
    ELParser parser = new ELParser(value);

    Expr expr = parser.parse();

    return expr.evalObject(env);
  }

  public static String evalString(String value, VariableResolver env)
    throws ELParseException, ELException
  {
    ELParser parser = new ELParser(value);

    Expr expr = parser.parse();

    return expr.evalString(env);
  }
}
