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 *      https://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     *
074     * @return the object exposed by this context
075     */
076    protected T getObject() {
077        return object;
078    }
079
080    @Override
081    public boolean has(final String name) {
082        return jexl.getUberspect().getPropertyGet(object, name) != null;
083    }
084
085    @Override
086    public Object resolveNamespace(final String name) {
087        if (name == null || name.isEmpty()) {
088            return object;
089        }
090        return null;
091    }
092
093    @Override
094    public void set(final String name, final Object value) {
095        final JexlPropertySet jset = jexl.getUberspect().getPropertySet(object, name, value);
096        if (jset != null) {
097            try {
098                jset.invoke(object, value);
099            } catch (final Exception xany) {
100                // ignore
101                if (jexl.isStrict()) {
102                    throw new JexlException.Property(null, name, true, xany);
103                }
104            }
105        }
106    }
107}