| 1 |
/* |
|---|
| 2 |
* (C) Copyright 2007 Nuxeo SAS (http://nuxeo.com/) and contributors. |
|---|
| 3 |
* |
|---|
| 4 |
* All rights reserved. This program and the accompanying materials |
|---|
| 5 |
* are made available under the terms of the GNU Lesser General Public License |
|---|
| 6 |
* (LGPL) version 2.1 which accompanies this distribution, and is available at |
|---|
| 7 |
* http://www.gnu.org/licenses/lgpl.html |
|---|
| 8 |
* |
|---|
| 9 |
* This library is distributed in the hope that it will be useful, |
|---|
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 12 |
* Lesser General Public License for more details. |
|---|
| 13 |
* |
|---|
| 14 |
* Contributors: |
|---|
| 15 |
* Nuxeo - initial API and implementation |
|---|
| 16 |
* |
|---|
| 17 |
* $Id$ |
|---|
| 18 |
*/ |
|---|
| 19 |
|
|---|
| 20 |
package org.nuxeo.ecm.platform.ui.web.binding; |
|---|
| 21 |
|
|---|
| 22 |
import java.io.Externalizable; |
|---|
| 23 |
import java.io.IOException; |
|---|
| 24 |
import java.io.ObjectInput; |
|---|
| 25 |
import java.io.ObjectOutput; |
|---|
| 26 |
|
|---|
| 27 |
import javax.el.ELContext; |
|---|
| 28 |
import javax.el.MethodExpression; |
|---|
| 29 |
import javax.el.ValueExpression; |
|---|
| 30 |
|
|---|
| 31 |
import org.apache.commons.logging.Log; |
|---|
| 32 |
import org.apache.commons.logging.LogFactory; |
|---|
| 33 |
|
|---|
| 34 |
/** |
|---|
| 35 |
* Method value expression encapsulates a method expression so that it invokes |
|---|
| 36 |
* it when evaluated as a standard value expression. |
|---|
| 37 |
* |
|---|
| 38 |
* @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a> |
|---|
| 39 |
* |
|---|
| 40 |
*/ |
|---|
| 41 |
public class MethodValueExpression extends ValueExpression implements |
|---|
| 42 |
Externalizable { |
|---|
| 43 |
|
|---|
| 44 |
private static final Log log = LogFactory.getLog(MethodValueExpression.class); |
|---|
| 45 |
|
|---|
| 46 |
private static final long serialVersionUID = 1228707110702282837L; |
|---|
| 47 |
|
|---|
| 48 |
private MethodExpression methodExpression; |
|---|
| 49 |
|
|---|
| 50 |
private Class[] paramTypesClasses; |
|---|
| 51 |
|
|---|
| 52 |
public MethodValueExpression() { |
|---|
| 53 |
} |
|---|
| 54 |
|
|---|
| 55 |
public MethodValueExpression(MethodExpression methodExpression, |
|---|
| 56 |
Class[] paramTypesClasses) { |
|---|
| 57 |
this.methodExpression = methodExpression; |
|---|
| 58 |
this.paramTypesClasses = paramTypesClasses; |
|---|
| 59 |
} |
|---|
| 60 |
|
|---|
| 61 |
// Expression interface |
|---|
| 62 |
|
|---|
| 63 |
@Override |
|---|
| 64 |
public boolean equals(Object obj) { |
|---|
| 65 |
if (this == obj) { |
|---|
| 66 |
return true; |
|---|
| 67 |
} |
|---|
| 68 |
if (!(obj instanceof MethodValueExpression)) { |
|---|
| 69 |
return false; |
|---|
| 70 |
} |
|---|
| 71 |
MethodValueExpression other = (MethodValueExpression) obj; |
|---|
| 72 |
return methodExpression.equals(other.methodExpression); |
|---|
| 73 |
} |
|---|
| 74 |
|
|---|
| 75 |
@Override |
|---|
| 76 |
public int hashCode() { |
|---|
| 77 |
return methodExpression.hashCode(); |
|---|
| 78 |
} |
|---|
| 79 |
|
|---|
| 80 |
@Override |
|---|
| 81 |
public String getExpressionString() { |
|---|
| 82 |
return methodExpression.getExpressionString(); |
|---|
| 83 |
} |
|---|
| 84 |
|
|---|
| 85 |
@Override |
|---|
| 86 |
public boolean isLiteralText() { |
|---|
| 87 |
return methodExpression.isLiteralText(); |
|---|
| 88 |
} |
|---|
| 89 |
|
|---|
| 90 |
// ValueExpression interface |
|---|
| 91 |
|
|---|
| 92 |
@Override |
|---|
| 93 |
public Class<?> getExpectedType() { |
|---|
| 94 |
return Object.class; |
|---|
| 95 |
} |
|---|
| 96 |
|
|---|
| 97 |
@Override |
|---|
| 98 |
public Class<?> getType(ELContext arg0) { |
|---|
| 99 |
return MethodExpression.class; |
|---|
| 100 |
} |
|---|
| 101 |
|
|---|
| 102 |
@Override |
|---|
| 103 |
public Object getValue(ELContext arg0) { |
|---|
| 104 |
// invoke method instead of resolving value |
|---|
| 105 |
Object res; |
|---|
| 106 |
try { |
|---|
| 107 |
return methodExpression.invoke(arg0, paramTypesClasses); |
|---|
| 108 |
} |
|---|
| 109 |
catch (Throwable t) { |
|---|
| 110 |
log.error( |
|---|
| 111 |
"Error while evaluation MethodValueExpression " + methodExpression.getExpressionString()); |
|---|
| 112 |
log.error("parameters are : " + paramTypesClasses.toString()); |
|---|
| 113 |
return null; |
|---|
| 114 |
} |
|---|
| 115 |
} |
|---|
| 116 |
|
|---|
| 117 |
@Override |
|---|
| 118 |
public boolean isReadOnly(ELContext arg0) { |
|---|
| 119 |
return true; |
|---|
| 120 |
} |
|---|
| 121 |
|
|---|
| 122 |
@Override |
|---|
| 123 |
public void setValue(ELContext arg0, Object arg1) { |
|---|
| 124 |
// do nothing |
|---|
| 125 |
} |
|---|
| 126 |
|
|---|
| 127 |
// Externalizable interface |
|---|
| 128 |
|
|---|
| 129 |
public void readExternal(ObjectInput in) throws IOException, |
|---|
| 130 |
ClassNotFoundException { |
|---|
| 131 |
methodExpression = (MethodExpression) in.readObject(); |
|---|
| 132 |
paramTypesClasses = (Class[]) in.readObject(); |
|---|
| 133 |
} |
|---|
| 134 |
|
|---|
| 135 |
public void writeExternal(ObjectOutput out) throws IOException { |
|---|
| 136 |
out.writeObject(methodExpression); |
|---|
| 137 |
out.writeObject(paramTypesClasses); |
|---|
| 138 |
} |
|---|
| 139 |
|
|---|
| 140 |
} |
|---|