001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 */
012package org.nuxeo.ecm.automation.core.operations;
013
014import org.mvel2.CompileException;
015import org.nuxeo.ecm.automation.OperationContext;
016import org.nuxeo.ecm.automation.core.Constants;
017import org.nuxeo.ecm.automation.core.annotations.Context;
018import org.nuxeo.ecm.automation.core.annotations.Operation;
019import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
020import org.nuxeo.ecm.automation.core.annotations.Param;
021import org.nuxeo.ecm.automation.core.scripting.Expression;
022import org.nuxeo.ecm.automation.core.scripting.Scripting;
023
024/**
025 * Save the session - TODO remove this?
026 *
027 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
028 */
029@Operation(id = RunScript.ID, category = Constants.CAT_SCRIPTING, label = "Run Script", description = "Run a script which content is specified as text in the 'script' parameter", aliases = { "Context.RunScript" })
030public class RunScript {
031
032    public static final String ID = "RunScript";
033
034    @Context
035    protected OperationContext ctx;
036
037    @Param(name = "script", widget = Constants.W_MULTILINE_TEXT)
038    protected String script;
039
040    private volatile Expression expr;
041
042    @OperationMethod
043    public void run() throws CompileException, RuntimeException {
044        if (expr == null) {
045            String text = script.replaceAll("&lt;", "<");
046            text = text.replaceAll("&gt;", ">");
047            text = text.replaceAll("&amp;", "&");
048            expr = Scripting.newExpression(text);
049        }
050        expr.eval(ctx);
051    }
052
053}