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 */ 017package org.apache.commons.jexl3; 018 019import org.apache.commons.jexl3.introspection.JexlPropertyGet; 020import org.apache.commons.jexl3.introspection.JexlPropertySet; 021 022/** 023 * Wraps an Object as a JEXL context and NamespaceResolver. 024 * 025 * @param <T> the wrapped object type to use 026 * @since 3.0 027 */ 028public class ObjectContext<T> implements JexlContext, JexlContext.NamespaceResolver { 029 030 /** The property solving jexl engine. */ 031 private final JexlEngine jexl; 032 033 /** The object serving as context provider. */ 034 private final T object; 035 036 /** 037 * Creates a new ObjectContext. 038 * 039 * @param engine the jexl engine to use to solve properties 040 * @param wrapped the object to wrap in this context 041 */ 042 public ObjectContext(final JexlEngine engine, final T wrapped) { 043 this.jexl = engine; 044 this.object = wrapped; 045 } 046 047 @Override 048 public Object get(final String name) { 049 final JexlPropertyGet jget = jexl.getUberspect().getPropertyGet(object, name); 050 if (jget != null) { 051 try { 052 return jget.invoke(object); 053 } catch (final Exception xany) { 054 if (jexl.isStrict()) { 055 throw new JexlException.Property(null, name, true, xany); 056 } 057 } 058 } 059 return null; 060 } 061 062 /** 063 * Gets the Jexl engine 064 * 065 * @return the Jexl engine 066 */ 067 protected JexlEngine getJexl() { 068 return jexl; 069 } 070 071 /** 072 * Gets the object exposed by this context 073 * @return the object exposed by this context 074 */ 075 protected T getObject() { 076 return object; 077 } 078 079 @Override 080 public boolean has(final String name) { 081 return jexl.getUberspect().getPropertyGet(object, name) != null; 082 } 083 084 @Override 085 public Object resolveNamespace(final String name) { 086 if (name == null || name.isEmpty()) { 087 return object; 088 } 089 return null; 090 } 091 092 @Override 093 public void set(final String name, final Object value) { 094 final JexlPropertySet jset = jexl.getUberspect().getPropertySet(object, name, value); 095 if (jset != null) { 096 try { 097 jset.invoke(object, value); 098 } catch (final Exception xany) { 099 // ignore 100 if (jexl.isStrict()) { 101 throw new JexlException.Property(null, name, true, xany); 102 } 103 } 104 } 105 } 106}