001/*
002 * (C) Copyright 2007 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id: MethodValueExpression.java 28491 2008-01-04 19:04:30Z sfermigier $
020 */
021
022package org.nuxeo.ecm.platform.ui.web.binding;
023
024import java.io.Externalizable;
025import java.io.IOException;
026import java.io.ObjectInput;
027import java.io.ObjectOutput;
028
029import javax.el.ELContext;
030import javax.el.ELException;
031import javax.el.FunctionMapper;
032import javax.el.MethodExpression;
033import javax.el.ValueExpression;
034import javax.el.VariableMapper;
035
036/**
037 * Method value expression encapsulates a method expression so that it invokes it when evaluated as a standard value
038 * expression.
039 *
040 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
041 * @deprecated method resolution is now supported by jboss-el
042 */
043@Deprecated
044public class MethodValueExpression extends ValueExpression implements Externalizable {
045
046    private static final long serialVersionUID = 1228707110702282837L;
047
048    private FunctionMapper functionMapper;
049
050    private VariableMapper variableMapper;
051
052    private MethodExpression methodExpression;
053
054    private Class[] paramTypesClasses;
055
056    public MethodValueExpression() {
057    }
058
059    public MethodValueExpression(FunctionMapper functionMapper, VariableMapper variableMapper,
060            MethodExpression methodExpression, Class[] paramTypesClasses) {
061        this.functionMapper = functionMapper;
062        this.variableMapper = variableMapper;
063        this.methodExpression = methodExpression;
064        this.paramTypesClasses = paramTypesClasses;
065    }
066
067    // Expression interface
068
069    @Override
070    public boolean equals(Object obj) {
071        if (this == obj) {
072            return true;
073        }
074        if (!(obj instanceof MethodValueExpression)) {
075            return false;
076        }
077        MethodValueExpression other = (MethodValueExpression) obj;
078        return methodExpression.equals(other.methodExpression);
079    }
080
081    @Override
082    public int hashCode() {
083        return methodExpression.hashCode();
084    }
085
086    @Override
087    public String getExpressionString() {
088        return methodExpression.getExpressionString();
089    }
090
091    @Override
092    public boolean isLiteralText() {
093        return methodExpression.isLiteralText();
094    }
095
096    // ValueExpression interface
097
098    @Override
099    public Class<?> getExpectedType() {
100        return Object.class;
101    }
102
103    @Override
104    public Class<?> getType(ELContext arg0) {
105        return MethodExpression.class;
106    }
107
108    @Override
109    public Object getValue(ELContext arg0) {
110        // invoke method instead of resolving value
111        try {
112            EvaluationContext evalCtx = new EvaluationContext(arg0, functionMapper, variableMapper);
113            return methodExpression.invoke(evalCtx, paramTypesClasses);
114        } catch (RuntimeException e) {
115            throw new ELException("Error while evaluation MethodValueExpression "
116                    + methodExpression.getExpressionString(), e);
117        }
118    }
119
120    @Override
121    public boolean isReadOnly(ELContext arg0) {
122        return true;
123    }
124
125    @Override
126    public void setValue(ELContext arg0, Object arg1) {
127        // do nothing
128    }
129
130    // Externalizable interface
131
132    @Override
133    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
134        methodExpression = (MethodExpression) in.readObject();
135        paramTypesClasses = (Class[]) in.readObject();
136    }
137
138    @Override
139    public void writeExternal(ObjectOutput out) throws IOException {
140        out.writeObject(methodExpression);
141        out.writeObject(paramTypesClasses);
142    }
143
144}