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.jexl2.introspection;
18  
19  import org.apache.commons.jexl2.JexlInfo;
20  import org.apache.commons.logging.Log;
21  
22  /**
23   * An uberspect that controls usage of properties, methods and contructors through a sandbox.
24   * @since 2.1
25   */
26  public class SandboxUberspectImpl extends UberspectImpl {
27      /**  The sandbox. */
28      protected final Sandbox sandbox;
29  
30      /**
31       * A constructor for Sandbox uberspect.
32       * @param runtimeLogger the logger to use or null to use default
33       * @param theSandbox the sandbox instance to use
34       */
35      public SandboxUberspectImpl(Log runtimeLogger, Sandbox theSandbox) {
36          super(runtimeLogger);
37          if (theSandbox == null) {
38              throw new NullPointerException("sandbox can not be null");
39          }
40          this.sandbox = theSandbox;
41      }
42  
43      /**
44       * {@inheritDoc}
45       */
46      @Override
47      public void setLoader(ClassLoader cloader) {
48          base().setLoader(cloader);
49      }
50  
51      /**
52       * {@inheritDoc}
53       */
54      @Override
55      public JexlMethod getConstructorMethod(Object ctorHandle, Object[] args, JexlInfo info) {
56          final String className;
57          if (ctorHandle instanceof Class<?>) {
58              Class<?> clazz = (Class<?>) ctorHandle;
59              className = clazz.getName();
60          } else if (ctorHandle != null) {
61              className = ctorHandle.toString();
62          } else {
63              return null;
64          }
65          if (sandbox.execute(className, "") != null) {
66              return super.getConstructorMethod(className, args, info);
67          }
68          return null;
69      }
70  
71      /**
72       * {@inheritDoc}
73       */
74      @Override
75      public JexlMethod getMethod(Object obj, String method, Object[] args, JexlInfo info) {
76          if (obj != null && method != null) {
77              String actual = sandbox.execute(obj.getClass().getName(), method);
78              if (actual != null) {
79                  return getMethodExecutor(obj, actual, args);
80              }
81          }
82          return null;
83      }
84  
85      /**
86       * {@inheritDoc}
87       */
88      @Override
89      public JexlPropertyGet getPropertyGet(Object obj, Object identifier, JexlInfo info) {
90          if (obj != null && identifier != null) {
91              String actual = sandbox.read(obj.getClass().getName(), identifier.toString());
92              if (actual != null) {
93                  return super.getPropertyGet(obj, actual, info);
94              }
95          }
96          return null;
97      }
98  
99      /**
100      * {@inheritDoc}
101      */
102     @Override
103     public JexlPropertySet getPropertySet(final Object obj, final Object identifier, Object arg, JexlInfo info) {
104         if (obj != null && identifier != null) {
105             String actual = sandbox.write(obj.getClass().getName(), identifier.toString());
106             if (actual != null) {
107                 return super.getPropertySet(obj, actual, arg, info);
108             }
109         }
110         return null;
111 
112     }
113 }