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;
18  
19  import org.apache.commons.jexl3.introspection.JexlPropertyGet;
20  import org.apache.commons.jexl3.introspection.JexlPropertySet;
21  
22  /**
23   * Wraps an Object as a JEXL context and NamespaceResolver.
24   *
25   * @param <T> the wrapped object type to use
26   * @since 3.0
27   */
28  public class ObjectContext<T> implements JexlContext, JexlContext.NamespaceResolver {
29  
30      /** The property solving jexl engine. */
31      private final JexlEngine jexl;
32  
33      /** The object serving as context provider. */
34      private final T object;
35  
36      /**
37       * Creates a new ObjectContext.
38       *
39       * @param engine  the jexl engine to use to solve properties
40       * @param wrapped the object to wrap in this context
41       */
42      public ObjectContext(final JexlEngine engine, final T wrapped) {
43          this.jexl = engine;
44          this.object = wrapped;
45      }
46  
47      @Override
48      public Object get(final String name) {
49          final JexlPropertyGet jget = jexl.getUberspect().getPropertyGet(object, name);
50          if (jget != null) {
51              try {
52                  return jget.invoke(object);
53              } catch (final Exception xany) {
54                  if (jexl.isStrict()) {
55                      throw new JexlException.Property(null, name, true, xany);
56                  }
57              }
58          }
59          return null;
60      }
61  
62      /**
63       * @return the Jexl engine
64       */
65      protected JexlEngine getJexl() {
66          return jexl;
67      }
68  
69      /**
70       * @return the object exposed by this context
71       */
72      protected T getObject() {
73          return object;
74      }
75  
76      @Override
77      public boolean has(final String name) {
78          return jexl.getUberspect().getPropertyGet(object, name) != null;
79      }
80  
81      @Override
82      public Object resolveNamespace(final String name) {
83          if (name == null || name.isEmpty()) {
84              return object;
85          }
86          return null;
87      }
88  
89      @Override
90      public void set(final String name, final Object value) {
91          final JexlPropertySet jset = jexl.getUberspect().getPropertySet(object, name, value);
92          if (jset != null) {
93              try {
94                  jset.invoke(object, value);
95              } catch (final Exception xany) {
96                  // ignore
97                  if (jexl.isStrict()) {
98                      throw new JexlException.Property(null, name, true, xany);
99                  }
100             }
101         }
102     }
103 }