001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.el;
031:
032: import com.caucho.loader.EnvironmentLocal;
033: import com.caucho.util.L10N;
034:
035: import javax.el.ELContext;
036: import javax.el.ELException;
037: import java.util.HashMap;
038: import java.util.logging.Logger;
039:
040: /**
041: * Abstract implementation class for an expression.
042: */
043: public class EL {
044: private static final Logger log = Logger.getLogger(EL.class
045: .getName());
046: private static final L10N L = new L10N(EL.class);
047:
048: private static EnvironmentLocal<ELContext> _elEnvironment = new EnvironmentLocal<ELContext>();
049:
050: private static EnvironmentLocal<HashMap<String, Object>> _envVar = new EnvironmentLocal<HashMap<String, Object>>();
051:
052: public final static Object NULL = new Object();
053:
054: public static ELContext getEnvironment() {
055: ClassLoader loader = Thread.currentThread()
056: .getContextClassLoader();
057:
058: return getEnvironment(loader);
059: }
060:
061: public static ELContext getEnvironment(ClassLoader loader) {
062: ELContext context = _elEnvironment.getLevel(loader);
063:
064: if (context == null) {
065: context = new EnvironmentContext(loader);
066:
067: _elEnvironment.set(context, loader);
068: }
069:
070: return context;
071: }
072:
073: public static void setEnvironment(ELContext env) {
074: _elEnvironment.set(env);
075: }
076:
077: public static void setEnvironment(ELContext env, ClassLoader loader) {
078: _elEnvironment.set(env, loader);
079: }
080:
081: /**
082: * Sets the environment map.
083: */
084: public static void setVariableMap(HashMap<String, Object> map,
085: ClassLoader loader) {
086: _envVar.set(map, loader);
087: }
088:
089: /**
090: * Gets a var from the specified level.
091: */
092: public static Object getLevelVar(String name, ClassLoader loader) {
093: HashMap<String, Object> varMap = _envVar.getLevel(loader);
094:
095: if (varMap == null)
096: return null;
097: else
098: return varMap.get(name);
099: }
100:
101: /**
102: * Puts an environment value.
103: */
104: public static Object putVar(String name, Object value) {
105: return putVar(name, value, Thread.currentThread()
106: .getContextClassLoader());
107: }
108:
109: /**
110: * Puts an environment value.
111: */
112: public static Object putVar(String name, Object value,
113: ClassLoader loader) {
114: HashMap<String, Object> varMap = _envVar.getLevel(loader);
115:
116: if (varMap == null) {
117: varMap = new HashMap<String, Object>();
118: _envVar.set(varMap, loader);
119: }
120:
121: return varMap.put(name, value);
122: }
123:
124: public static Object evalObject(String value)
125: throws ELParseException, ELException {
126: ELParser parser = new ELParser(getEnvironment(), value);
127:
128: Expr expr = parser.parse();
129:
130: return expr.getValue(getEnvironment());
131: }
132:
133: public static String evalString(String value, ELContext env)
134: throws ELParseException, ELException {
135: ELParser parser = new ELParser(getEnvironment(), value);
136:
137: Expr expr = parser.parse();
138:
139: return expr.evalString(env);
140: }
141:
142: public static boolean evalBoolean(String value, ELContext env)
143: throws ELParseException, ELException {
144: ELParser parser = new ELParser(getEnvironment(), value);
145:
146: Expr expr = parser.parse();
147:
148: return expr.evalBoolean(env);
149: }
150: }
|