001/*
002 * (C) Copyright 2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id: MethodValueExpression.java 28491 2008-01-04 19:04:30Z sfermigier $
018 */
019
020package org.nuxeo.ecm.platform.ui.web.binding;
021
022import java.io.Externalizable;
023import java.io.IOException;
024import java.io.ObjectInput;
025import java.io.ObjectOutput;
026
027import javax.el.ELContext;
028import javax.el.ELException;
029import javax.el.FunctionMapper;
030import javax.el.MethodExpression;
031import javax.el.ValueExpression;
032import javax.el.VariableMapper;
033
034/**
035 * Method value expression encapsulates a method expression so that it invokes it when evaluated as a standard value
036 * expression.
037 *
038 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
039 * @deprecated method resolution is now supported by jboss-el
040 */
041@Deprecated
042public class MethodValueExpression extends ValueExpression implements Externalizable {
043
044    private static final long serialVersionUID = 1228707110702282837L;
045
046    private FunctionMapper functionMapper;
047
048    private VariableMapper variableMapper;
049
050    private MethodExpression methodExpression;
051
052    private Class[] paramTypesClasses;
053
054    public MethodValueExpression() {
055    }
056
057    public MethodValueExpression(FunctionMapper functionMapper, VariableMapper variableMapper,
058            MethodExpression methodExpression, Class[] paramTypesClasses) {
059        this.functionMapper = functionMapper;
060        this.variableMapper = variableMapper;
061        this.methodExpression = methodExpression;
062        this.paramTypesClasses = paramTypesClasses;
063    }
064
065    // Expression interface
066
067    @Override
068    public boolean equals(Object obj) {
069        if (this == obj) {
070            return true;
071        }
072        if (!(obj instanceof MethodValueExpression)) {
073            return false;
074        }
075        MethodValueExpression other = (MethodValueExpression) obj;
076        return methodExpression.equals(other.methodExpression);
077    }
078
079    @Override
080    public int hashCode() {
081        return methodExpression.hashCode();
082    }
083
084    @Override
085    public String getExpressionString() {
086        return methodExpression.getExpressionString();
087    }
088
089    @Override
090    public boolean isLiteralText() {
091        return methodExpression.isLiteralText();
092    }
093
094    // ValueExpression interface
095
096    @Override
097    public Class<?> getExpectedType() {
098        return Object.class;
099    }
100
101    @Override
102    public Class<?> getType(ELContext arg0) {
103        return MethodExpression.class;
104    }
105
106    @Override
107    public Object getValue(ELContext arg0) {
108        // invoke method instead of resolving value
109        try {
110            EvaluationContext evalCtx = new EvaluationContext(arg0, functionMapper, variableMapper);
111            return methodExpression.invoke(evalCtx, paramTypesClasses);
112        } catch (RuntimeException e) {
113            throw new ELException("Error while evaluation MethodValueExpression "
114                    + methodExpression.getExpressionString(), e);
115        }
116    }
117
118    @Override
119    public boolean isReadOnly(ELContext arg0) {
120        return true;
121    }
122
123    @Override
124    public void setValue(ELContext arg0, Object arg1) {
125        // do nothing
126    }
127
128    // Externalizable interface
129
130    @Override
131    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
132        methodExpression = (MethodExpression) in.readObject();
133        paramTypesClasses = (Class[]) in.readObject();
134    }
135
136    @Override
137    public void writeExternal(ObjectOutput out) throws IOException {
138        out.writeObject(methodExpression);
139        out.writeObject(paramTypesClasses);
140    }
141
142}