001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2014 ForgeRock AS.
015 */
016
017package org.forgerock.openig.resolver;
018
019import javax.el.BeanELResolver;
020import javax.el.ELContext;
021import javax.el.ELResolver;
022import javax.el.FunctionMapper;
023import javax.el.VariableMapper;
024
025/**
026 * Resolves Java Beans objects.
027 */
028public class BeanResolver implements Resolver {
029
030    private final BeanELResolver delegate;
031    private final ELContext context;
032
033    /**
034     * Builds a new BeanResolver around an EL {@link BeanELResolver} instance.
035     */
036    public BeanResolver() {
037        delegate = new BeanELResolver();
038        context = new BasicELContext();
039    }
040
041    @Override
042    public Class<?> getKey() {
043        return Object.class;
044    }
045
046    @Override
047    public Object get(final Object object, final Object element) {
048        try {
049            final Object value = delegate.getValue(context, object, element);
050            if (context.isPropertyResolved()) {
051                return value;
052            }
053        } catch (Exception e) {
054            // Ignored, considered as un-resolved
055        }
056        return UNRESOLVED;
057    }
058
059    @Override
060    public Object put(final Object object, final Object element, final Object value) {
061        try {
062            delegate.setValue(context, object, element, value);
063        } catch (Exception e) {
064            // Ignored, let other resolvers take over
065        }
066        return UNRESOLVED;
067    }
068
069    private class BasicELContext extends ELContext {
070        @Override
071        public ELResolver getELResolver() {
072            return delegate;
073        }
074
075        @Override
076        public FunctionMapper getFunctionMapper() {
077            return null;
078        }
079
080        @Override
081        public VariableMapper getVariableMapper() {
082            return null;
083        }
084    }
085}