View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.jexl3.internal.introspection;
18  
19  import org.apache.commons.jexl3.JexlArithmetic;
20  import org.apache.commons.jexl3.JexlOperator;
21  import org.apache.commons.jexl3.introspection.JexlMethod;
22  import org.apache.commons.jexl3.introspection.JexlPropertyGet;
23  import org.apache.commons.jexl3.introspection.JexlPropertySet;
24  import org.apache.commons.jexl3.introspection.JexlSandbox;
25  import org.apache.commons.jexl3.introspection.JexlUberspect;
26  
27  import java.util.Iterator;
28  import java.util.List;
29  
30  /**
31   * An uberspect that controls usage of properties, methods and constructors through a sandbox.
32   * @since 3.0
33   */
34  public final class SandboxUberspect implements JexlUberspect {
35      /** The base uberspect. */
36      private final JexlUberspect uberspect;
37      /**  The sandbox. */
38      private final JexlSandbox sandbox;
39  
40      /**
41       * A constructor for JexlSandbox uberspect.
42       * @param theUberspect the JexlUberspect to sandbox
43       * @param theSandbox the sandbox which is copied to avoid changes at runtime
44       */
45      public SandboxUberspect(final JexlUberspect theUberspect, final JexlSandbox theSandbox) {
46          if (theSandbox == null) {
47              throw new NullPointerException("sandbox can not be null");
48          }
49          if (theUberspect == null) {
50              throw new NullPointerException("uberspect can not be null");
51          }
52          this.uberspect = theUberspect;
53          this.sandbox = theSandbox.copy();
54      }
55  
56      @Override
57      public void setClassLoader(final ClassLoader loader) {
58          uberspect.setClassLoader(loader);
59      }
60  
61      @Override
62      public ClassLoader getClassLoader() {
63          return uberspect.getClassLoader();
64      }
65  
66      @Override
67      public int getVersion() {
68          return uberspect.getVersion();
69      }
70  
71      @Override
72      public Class<?> getClassByName(final String className) {
73          return uberspect.getClassByName(className);
74      }
75  
76      @Override
77      public JexlMethod getConstructor(final Object ctorHandle, final Object... args) {
78          final String className;
79          if (ctorHandle instanceof Class<?>) {
80              className = sandbox.execute((Class<?>) ctorHandle, "");
81          } else if (ctorHandle != null) {
82              className = sandbox.execute(ctorHandle.toString(), "");
83          } else {
84              className = null;
85          }
86          return className != null && className != JexlSandbox.NULL ? uberspect.getConstructor(className, args) : null;
87      }
88  
89      @Override
90      public JexlMethod getMethod(final Object obj, final String method, final Object... args) {
91          if (obj != null && method != null) {
92              final Class<?> clazz = (obj instanceof Class) ? (Class<?>) obj : obj.getClass();
93              final String actual = sandbox.execute(clazz, method);
94              if (actual != null && actual != JexlSandbox.NULL) {
95                  return uberspect.getMethod(obj, actual, args);
96              }
97          }
98          return null;
99      }
100 
101     @Override
102     public List<PropertyResolver> getResolvers(final JexlOperator op, final Object obj) {
103         return uberspect.getResolvers(op, obj);
104     }
105 
106     @Override
107     public JexlPropertyGet getPropertyGet(final Object obj, final Object identifier) {
108         return getPropertyGet(null, obj, identifier);
109     }
110 
111     /**
112      * Identity equality.
113      * <p>Spotbugs just <em>hates</em> string identity...</p>
114      * @param lhs left hand side
115      * @param rhs right hand side
116      * @return true if left is identical to right
117      */
118     private static boolean eq(final Object lhs, final Object rhs) {
119         return lhs == rhs;
120     }
121 
122     @Override
123     public JexlPropertyGet getPropertyGet(final List<PropertyResolver> resolvers,
124                                           final Object obj,
125                                           final Object identifier) {
126         if (obj != null) {
127             final Class<?> clazz = obj instanceof Class<?>? (Class<?>) obj : obj.getClass();
128             if (identifier != null) {
129                 final String property = identifier.toString();
130                 final String actual = sandbox.read(clazz, property);
131                 if (actual != null) {
132                     // no transformation, strict equality: use identifier before string conversion
133                     final Object pty = eq(actual, property) ? identifier : actual;
134                     return uberspect.getPropertyGet(resolvers, obj, pty);
135                 }
136             } else {
137                 final String actual = sandbox.read(clazz, null);
138                 if (actual != JexlSandbox.NULL) {
139                      return uberspect.getPropertyGet(resolvers, obj, null);
140                 }
141             }
142         }
143         return null;
144     }
145 
146     @Override
147     public JexlPropertySet getPropertySet(final Object obj,final Object identifier,final Object arg) {
148         return getPropertySet(null, obj, identifier, arg);
149     }
150 
151     @Override
152     public JexlPropertySet getPropertySet(final List<PropertyResolver> resolvers,
153                                           final Object obj,
154                                           final Object identifier,
155                                           final Object arg) {
156         if (obj != null) {
157             final Class<?> clazz = obj instanceof Class<?>? (Class<?>) obj : obj.getClass();
158             if (identifier != null) {
159                 final String property = identifier.toString();
160                 final String actual = sandbox.write(clazz, property);
161                 if (actual != null) {
162                     // no transformation, strict equality: use identifier before string conversion
163                     final Object pty = eq(actual, property) ? identifier : actual;
164                     return uberspect.getPropertySet(resolvers, obj, pty, arg);
165                 }
166             } else {
167                 final String actual = sandbox.write(clazz, null);
168                 if (actual != JexlSandbox.NULL) {
169                     return uberspect.getPropertySet(resolvers, obj, null, arg);
170                 }
171             }
172         }
173         return null;
174     }
175 
176     @Override
177     public Iterator<?> getIterator(final Object obj) {
178         return uberspect.getIterator(obj);
179     }
180 
181     @Override
182     public JexlArithmetic.Uberspect getArithmetic(final JexlArithmetic arithmetic) {
183         return uberspect.getArithmetic(arithmetic);
184     }
185 }