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