001 /****************************************************************************** 002 * Copyright (C) MActor Developers. All rights reserved. * 003 * ---------------------------------------------------------------------------* 004 * This file is part of MActor. * 005 * * 006 * MActor is free software; you can redistribute it and/or modify * 007 * it under the terms of the GNU General Public License as published by * 008 * the Free Software Foundation; either version 2 of the License, or * 009 * (at your option) any later version. * 010 * * 011 * MActor is distributed in the hope that it will be useful, * 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 014 * GNU General Public License for more details. * 015 * * 016 * You should have received a copy of the GNU General Public License * 017 * along with MActor; if not, write to the Free Software * 018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * 019 ******************************************************************************/ 020 package org.mactor.framework.commandexecutors.bsh; 021 022 import java.io.File; 023 import java.util.Iterator; 024 import java.util.Map; 025 import java.util.Map.Entry; 026 027 import org.mactor.framework.MactorException; 028 import org.mactor.framework.TestContextImpl; 029 import org.mactor.framework.spec.ProjectContext; 030 031 import bsh.EvalError; 032 import bsh.Interpreter; 033 import bsh.ParseException; 034 import bsh.TargetError; 035 036 public class BshUtil { 037 public static Object executeCommand(Map<String, Object> variables, String scriptFilename, TestContextImpl context) throws MactorException { 038 File path = ProjectContext.getGlobalInstance().getAbsolutePath(scriptFilename); 039 if (!path.exists()) 040 throw new MactorException("The specifed BeanShell script '" + path.getAbsolutePath() + "' does not exist"); 041 String script = ProjectContext.getGlobalInstance().readStringFromFile(scriptFilename, false); 042 if (context != null) { 043 script = context.substitute(script); 044 } 045 try { 046 Interpreter i = new Interpreter(); 047 Iterator<Entry<String, Object>> it = variables.entrySet().iterator(); 048 while (it.hasNext()) { 049 Entry<String, Object> e = it.next(); 050 i.set(e.getKey(), e.getValue()); 051 } 052 return i.eval(script); 053 } catch (TargetError e) { 054 throw new MactorException("Failed to execute the BeanShell script '" + path.getAbsolutePath() + "'. The script valid, but an exception was thrown while exceuting the script. Line:" 055 + e.getErrorLineNumber() + ". Description: " + e.getErrorText() + ". Cause:" + e.getTarget()); 056 } catch (ParseException e) { 057 throw new MactorException("Failed to execute the BeanShell script '" + path.getAbsolutePath() + "'. The script has a syntactic error:" + e); 058 } catch (EvalError e) { 059 throw new MactorException("Failed to execute the BeanShell script '" + path.getAbsolutePath() + "'. Error in scrip:" + e); 060 } 061 } 062 }