001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.jexl3; 019 020import java.util.concurrent.Callable; 021import java.util.concurrent.atomic.AtomicBoolean; 022 023/** 024 * Manages variables which can be referenced in a JEXL expression. 025 * 026 * <p>JEXL variable names in their simplest form are 'java-like' identifiers. 027 * JEXL also considers 'ant' inspired variables expressions as valid. 028 * For instance, the expression 'x.y.z' is an 'antish' variable and will be resolved as a whole by the context, 029 * i.e. using the key "x.y.z". This proves to be useful to solve "fully qualified class names".</p> 030 * 031 * <p>The interpreter variable resolution algorithm will try the different sequences of identifiers till it finds 032 * one that exists in the context; if "x" is an object known in the context (JexlContext.has("x") returns true), 033 * "x.y" will <em>not</em> be looked up in the context but will most likely refer to "x.getY()".</p> 034 * 035 * <p>Note that JEXL may use '$jexl' and '$ujexl' variables for internal purpose; setting or getting those 036 * variables may lead to unexpected results unless specified otherwise.</p> 037 * 038 * @since 1.0 039 */ 040public interface JexlContext { 041 042 /** 043 * Gets the value of a variable. 044 * 045 * @param name the variable's name 046 * @return the value 047 */ 048 Object get(String name); 049 050 /** 051 * Sets the value of a variable. 052 * 053 * @param name the variable's name 054 * @param value the variable's value 055 */ 056 void set(String name, Object value); 057 058 /** 059 * Checks whether a variable is defined in this context. 060 * 061 * <p>A variable may be defined with a null value; this method checks whether the 062 * value is null or if the variable is undefined.</p> 063 * 064 * @param name the variable's name 065 * @return true if it exists, false otherwise 066 */ 067 boolean has(String name); 068 069 /** 070 * A marker interface of the JexlContext that declares how to resolve a namespace from its name; 071 * it is used by the interpreter during evaluation. 072 * 073 * <p>In JEXL, a namespace is an object that serves the purpose of encapsulating functions; for instance, 074 * the "math" namespace would be the proper object to expose functions like "log(...)", "sinus(...)", etc.</p> 075 * 076 * In expressions like "ns:function(...)", the resolver is called with resolveNamespace("ns"). 077 * 078 * <p>JEXL itself reserves 'jexl' and 'ujexl' as namespaces for internal purpose; resolving those may lead to 079 * unexpected results.</p> 080 * 081 * @since 3.0 082 */ 083 interface NamespaceResolver { 084 085 /** 086 * Resolves a namespace by its name. 087 * @param name the name 088 * @return the namespace object 089 */ 090 Object resolveNamespace(String name); 091 } 092 093 /** 094 * A marker interface of the JexlContext, NamespaceFunctor allows creating an instance 095 * to delegate namespace methods calls to. 096 * 097 * <p>The functor is created once during the lifetime of a script evaluation.</p> 098 */ 099 interface NamespaceFunctor { 100 /** 101 * Creates the functor object that will be used instead of the namespace. 102 * @param context the context 103 * @return the namespace functor instance 104 */ 105 Object createFunctor(JexlContext context); 106 } 107 108 /** 109 * A marker interface of the JexlContext that indicates the interpreter to put this context 110 * in the JexlEngine thread local context instance during evaluation. 111 * This allows user functions or methods to access the context during a call. 112 * Note that the usual caveats wrt using thread local apply (caching/leaking references, etc.); in particular, 113 * keeping a reference to such a context is to be considered with great care and caution. 114 * It should also be noted that sharing such a context between threads should implicate synchronizing variable 115 * accessing the implementation class. 116 * 117 * @see JexlEngine#setThreadContext(JexlContext.ThreadLocal) 118 * @see JexlEngine#getThreadContext() 119 */ 120 interface ThreadLocal extends JexlContext { 121 // no specific method 122 } 123 124 /** 125 * A marker interface of the JexlContext that processes annotations. 126 * It is used by the interpreter during evaluation to execute annotation evaluations. 127 * <p>If the JexlContext is not an instance of an AnnotationProcessor, encountering an annotation will generate 128 * an error or a warning depending on the engine strictness. 129 * @since 3.1 130 */ 131 interface AnnotationProcessor { 132 /** 133 * Processes an annotation. 134 * <p>All annotations are processed through this method; the statement 'call' is to be performed within 135 * the processAnnotation method. The implementation <em>must</em> perform the call explicitly. 136 * <p>The arguments and the statement <em>must not</em> be referenced or cached for longer than the duration 137 * of the processAnnotation call. 138 * 139 * @param name the annotation name 140 * @param args the arguments of the annotation, evaluated as arguments of this call 141 * @param statement the statement that was annotated; the processor should invoke this statement 'call' method 142 * @return the result of statement.call() 143 * @throws Exception if annotation processing fails 144 */ 145 Object processAnnotation(String name, Object[] args, Callable<Object> statement) throws Exception; 146 } 147 148 /** 149 * A marker interface of the JexlContext that exposes runtime evaluation options. 150 * @since 3.2 151 */ 152 interface OptionsHandle { 153 /** 154 * Retrieves the current set of options though the context. 155 * <p> 156 * This method will be called once at beginning of evaluation and an interpreter private copy 157 * of the context handled JexlOptions instance used for the duration of the execution; 158 * the context handled JexlOptions instance being only used as the source of that copy, 159 * it can safely alter its boolean flags during execution with no effect, avoiding any behavior ambiguity. 160 * @return the engine options 161 */ 162 JexlOptions getEngineOptions(); 163 } 164 165 /** 166 * A marker interface of the JexlContext that processes pragmas. 167 * It is called by the engine before interpreter creation; as a marker of 168 * JexlContext, it is expected to have access and interact with the context 169 * instance. 170 * @since 3.2 171 */ 172 interface PragmaProcessor { 173 /** 174 * Process one pragma. 175 * @param key the key 176 * @param value the value 177 */ 178 void processPragma(String key, Object value); 179 } 180 181 /** 182 * A marker interface of the JexlContext sharing a cancelling flag. 183 * <p>A script running in a thread can thus be notified through this reference 184 * of its cancellation through the context. It uses the same interpreter logic 185 * that reacts to cancellation and is an alternative to using callable() and/or 186 * interrupting script interpreter threads. 187 * @since 3.2 188 */ 189 interface CancellationHandle { 190 /** 191 * @return a cancelable boolean used by the interpreter 192 */ 193 AtomicBoolean getCancellation(); 194 } 195}