View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2008 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  package ch.elca.el4j.services.debug;
18  
19  import java.io.ByteArrayOutputStream;
20  import java.io.PrintStream;
21  import java.io.StringReader;
22  
23  import org.springframework.context.ApplicationContext;
24  
25  import bsh.EvalError;
26  import bsh.Interpreter;
27  import bsh.InterpreterError;
28  import bsh.NameSpace;
29  import bsh.Primitive;
30  import bsh.TargetError;
31  
32  
33  /**
34   * The evaluator can evaluate script expressions.
35   *
36   * For now it uses bsh. Later it should also be able to support other shells
37   * (via bean scripting or the jdk 1.6 mechanism).
38   *
39   * Features: <br>
40   *   bsh.show=true shows more eval output <br>
41   *   Variables available in the shell script: <ul>
42   *   	<li> {@code ac} the current application context
43   *      <li> {@code $_} the last return result
44   *    </ul>
45   *
46   * @svnLink $Revision: 4010 $;$Date: 2009-12-01 10:59:54 +0100 (Di, 01. Dez 2009) $;$Author: jonasha $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/bshlauncher/src/main/java/ch/elca/el4j/services/debug/ShellExecutorImpl.java $
47   * 
48   * @author Philipp H. Oser (POS)
49   */
50  public class ShellExecutorImpl implements ShellExecutor {
51  
52  	private ByteArrayOutputStream out;
53  	
54  	private ByteArrayOutputStream err;
55  
56  	private PrintStream m_outPStream;
57  	
58  	private PrintStream m_errPStream;
59  
60  	private Interpreter interpreter;
61  	
62  	protected ApplicationContext m_ac;
63  
64  	public ShellExecutorImpl() {
65  		startup();
66  	}
67  
68  	public ShellExecutorImpl(ApplicationContext ac) {
69  		this();
70  		m_ac = ac;
71  	}
72  
73  	
74  	
75  	protected synchronized void startup () {
76  
77  		out = new ByteArrayOutputStream();
78  		err = new ByteArrayOutputStream();
79  		
80  		interpreter = new Interpreter
81  			(new StringReader (""),
82  				m_outPStream = new PrintStream (out),
83  				m_errPStream = new PrintStream (err),
84  				false);
85  	}
86  	
87  	
88  	// inherited comment
89  	public synchronized ResultHolder eval (String stmt)  {
90  		Object retVal = null;
91  		
92  		try {
93  			retVal = interpreter.eval (stmt);
94  			if (retVal != Primitive.VOID) {
95  				interpreter.set ("$_", retVal);
96  			}
97  			if (m_ac != null){
98  				interpreter.set ("ac", m_ac);
99  			}
100 			
101 			Object show = interpreter.get ("bsh.show");
102 			if ( (retVal != null) && show instanceof Boolean &&
103 				((Boolean) show).booleanValue() == true){
104 				m_outPStream.println ("<"+retVal+">");
105 			}
106 		} catch (InterpreterError e) {
107 			m_errPStream.println ("Internal Error: " + e.getMessage());
108 			e.printStackTrace (m_errPStream);
109 		} catch (TargetError te) {
110 			m_errPStream.println ("// Uncaught Exception: " + te);
111 			if (te.inNativeCode()){
112 				te.printStackTrace (m_errPStream);
113 			}
114 		} catch (EvalError ee) {
115 			m_errPStream.println (ee.toString());
116 		} catch (Exception e) {
117 			m_errPStream.println ("Unknown error: " + e);
118 			e.printStackTrace (m_errPStream);
119 		}
120 		
121 		String outStr = out.toString();
122 		out.reset();
123 
124 		String errStr = err.toString();
125 		err.reset();
126 		
127 		return new ResultHolder (retVal,outStr,errStr);
128 	}
129 
130 	// inherited comment
131 	public synchronized NameSpace getNameSpace () {
132 		return interpreter.getNameSpace();
133 	}
134 	
135 	// inherited comment
136 	public synchronized void setNameSpace (NameSpace ns){
137 		interpreter.setNameSpace (ns);
138 	}
139 
140 
141 	public synchronized Interpreter getInterpreter() {
142 		return interpreter;
143 	}
144 
145 
146 	public synchronized void setInterpreter(Interpreter interpreter) {
147 		this.interpreter = interpreter;
148 	}
149 
150 }
151