| 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.IOException; |
|---|
| 23 |
import java.io.ObjectInput; |
|---|
| 24 |
import java.io.ObjectOutput; |
|---|
| 25 |
import java.io.Serializable; |
|---|
| 26 |
|
|---|
| 27 |
import javax.el.ELContext; |
|---|
| 28 |
import javax.el.ExpressionFactory; |
|---|
| 29 |
import javax.el.MethodExpression; |
|---|
| 30 |
import javax.el.MethodInfo; |
|---|
| 31 |
import javax.faces.application.Application; |
|---|
| 32 |
import javax.faces.context.FacesContext; |
|---|
| 33 |
|
|---|
| 34 |
import org.apache.commons.logging.Log; |
|---|
| 35 |
import org.apache.commons.logging.LogFactory; |
|---|
| 36 |
import org.nuxeo.ecm.platform.ui.web.util.ComponentTagUtils; |
|---|
| 37 |
|
|---|
| 38 |
/** |
|---|
| 39 |
* Meta method expression used to invoke the EL expression that is already the |
|---|
| 40 |
* result of a method expression. |
|---|
| 41 |
* |
|---|
| 42 |
* For instance it is useful to use this expression to provide action links |
|---|
| 43 |
* defined in NXActions extensions with links like |
|---|
| 44 |
* #{documentAction.createDocument('Domain')}. |
|---|
| 45 |
* |
|---|
| 46 |
* There is no more than one level of abstraction: |
|---|
| 47 |
* <ul> |
|---|
| 48 |
* <li> the expression method value can be a standard method expression (with |
|---|
| 49 |
* parameters or not) ; |
|---|
| 50 |
* <li> the expression method value can result in another expression method |
|---|
| 51 |
* value after being invoke, in which case it is reinvoked again using the same |
|---|
| 52 |
* context ; |
|---|
| 53 |
* <li> no further method invoking will be performed. |
|---|
| 54 |
* </ul> |
|---|
| 55 |
* |
|---|
| 56 |
* @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a> |
|---|
| 57 |
* |
|---|
| 58 |
*/ |
|---|
| 59 |
public class MetaMethodExpression extends MethodExpression implements |
|---|
| 60 |
Serializable { |
|---|
| 61 |
|
|---|
| 62 |
private static final long serialVersionUID = -2721042412903607760L; |
|---|
| 63 |
|
|---|
| 64 |
private static final Log log = LogFactory.getLog(MetaMethodExpression.class); |
|---|
| 65 |
|
|---|
| 66 |
private MethodExpression originalMethodExpression; |
|---|
| 67 |
|
|---|
| 68 |
public MetaMethodExpression(MethodExpression originalMethodExpression) { |
|---|
| 69 |
this.originalMethodExpression = originalMethodExpression; |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
// Expression interface |
|---|
| 73 |
|
|---|
| 74 |
@Override |
|---|
| 75 |
public boolean equals(Object obj) { |
|---|
| 76 |
if (this == obj) { |
|---|
| 77 |
return true; |
|---|
| 78 |
} |
|---|
| 79 |
if (!(obj instanceof MetaMethodExpression)) { |
|---|
| 80 |
return false; |
|---|
| 81 |
} |
|---|
| 82 |
MetaMethodExpression other = (MetaMethodExpression) obj; |
|---|
| 83 |
return originalMethodExpression.equals(other.originalMethodExpression); |
|---|
| 84 |
} |
|---|
| 85 |
|
|---|
| 86 |
@Override |
|---|
| 87 |
public int hashCode() { |
|---|
| 88 |
return originalMethodExpression.hashCode(); |
|---|
| 89 |
} |
|---|
| 90 |
|
|---|
| 91 |
@Override |
|---|
| 92 |
public String getExpressionString() { |
|---|
| 93 |
return originalMethodExpression.getExpressionString(); |
|---|
| 94 |
} |
|---|
| 95 |
|
|---|
| 96 |
@Override |
|---|
| 97 |
public boolean isLiteralText() { |
|---|
| 98 |
return originalMethodExpression.isLiteralText(); |
|---|
| 99 |
} |
|---|
| 100 |
|
|---|
| 101 |
// MethodExpression interface |
|---|
| 102 |
|
|---|
| 103 |
@Override |
|---|
| 104 |
public MethodInfo getMethodInfo(ELContext context) { |
|---|
| 105 |
// TODO Auto-generated method stub |
|---|
| 106 |
return null; |
|---|
| 107 |
} |
|---|
| 108 |
|
|---|
| 109 |
@Override |
|---|
| 110 |
public Object invoke(ELContext context, Object[] params) { |
|---|
| 111 |
Object res = null; |
|---|
| 112 |
if (originalMethodExpression != null) { |
|---|
| 113 |
res = originalMethodExpression.invoke(context, params); |
|---|
| 114 |
if (res instanceof String) { |
|---|
| 115 |
String expression = (String) res; |
|---|
| 116 |
if (ComponentTagUtils.isValueReference(expression)) { |
|---|
| 117 |
FacesContext faces = FacesContext.getCurrentInstance(); |
|---|
| 118 |
Application app = faces.getApplication(); |
|---|
| 119 |
ExpressionFactory factory = app.getExpressionFactory(); |
|---|
| 120 |
MethodExpression newMeth = factory.createMethodExpression( |
|---|
| 121 |
context, expression, Object.class, new Class[0]); |
|---|
| 122 |
try { |
|---|
| 123 |
res = newMeth.invoke(context, null); |
|---|
| 124 |
} catch (Exception err) { |
|---|
| 125 |
log.error(String.format( |
|---|
| 126 |
"Error processing action expression %s: %s", |
|---|
| 127 |
expression, err)); |
|---|
| 128 |
res = null; |
|---|
| 129 |
} |
|---|
| 130 |
} else { |
|---|
| 131 |
res = expression; |
|---|
| 132 |
} |
|---|
| 133 |
} |
|---|
| 134 |
} |
|---|
| 135 |
return res; |
|---|
| 136 |
} |
|---|
| 137 |
|
|---|
| 138 |
// Externalizable interface |
|---|
| 139 |
|
|---|
| 140 |
public void readExternal(ObjectInput in) throws IOException, |
|---|
| 141 |
ClassNotFoundException { |
|---|
| 142 |
originalMethodExpression = (MethodExpression) in.readObject(); |
|---|
| 143 |
} |
|---|
| 144 |
|
|---|
| 145 |
public void writeExternal(ObjectOutput out) throws IOException { |
|---|
| 146 |
out.writeObject(originalMethodExpression); |
|---|
| 147 |
} |
|---|
| 148 |
|
|---|
| 149 |
} |
|---|