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 * @return the Jexl engine 038 */ 039 protected JexlEngine getJexl() { 040 return jexl; 041 } 042 043 /** 044 * @return the object exposed by this context 045 */ 046 protected T getObject() { 047 return object; 048 } 049 050 /** 051 * Creates a new ObjectContext. 052 * 053 * @param engine the jexl engine to use to solve properties 054 * @param wrapped the object to wrap in this context 055 */ 056 public ObjectContext(final JexlEngine engine, final T wrapped) { 057 this.jexl = engine; 058 this.object = wrapped; 059 } 060 061 @Override 062 public Object get(final String name) { 063 final JexlPropertyGet jget = jexl.getUberspect().getPropertyGet(object, name); 064 if (jget != null) { 065 try { 066 return jget.invoke(object); 067 } catch (final Exception xany) { 068 if (jexl.isStrict()) { 069 throw new JexlException.Property(null, name, true, xany); 070 } 071 } 072 } 073 return null; 074 } 075 076 @Override 077 public void set(final String name, final Object value) { 078 final JexlPropertySet jset = jexl.getUberspect().getPropertySet(object, name, value); 079 if (jset != null) { 080 try { 081 jset.invoke(object, value); 082 } catch (final Exception xany) { 083 // ignore 084 if (jexl.isStrict()) { 085 throw new JexlException.Property(null, name, true, xany); 086 } 087 } 088 } 089 } 090 091 @Override 092 public boolean has(final String name) { 093 return jexl.getUberspect().getPropertyGet(object, name) != null; 094 } 095 096 @Override 097 public Object resolveNamespace(final String name) { 098 if (name == null || name.isEmpty()) { 099 return object; 100 } 101 return null; 102 } 103}