1 55 56 package org.apache.bsf.engines.jacl; 57 58 import java.util.*; 59 import java.io.*; 60 61 import tcl.lang.*; 62 63 import org.apache.bsf.*; 64 import org.apache.bsf.util.*; 65 66 73 74 public class JaclEngine extends BSFEngineImpl { 75 76 private Interp interp; 77 78 85 public Object call (Object obj, String method, Object [] args) 86 throws BSFException { 87 StringBuffer tclScript = new StringBuffer (method); 88 if (args != null) { 89 for( int i = 0 ; i < args.length ; i++ ) { 90 tclScript.append (" "); 91 tclScript.append (args[i].toString ()); 92 } 93 } 94 return eval ("<function call>", 0, 0, tclScript.toString ()); 95 } 96 99 public void declareBean (BSFDeclaredBean bean) throws BSFException { 100 String expr = "set " + bean.name + " [bsf lookupBean \"" + bean.name + 101 "\"]"; 102 eval ("<declare bean>", 0, 0, expr); 103 } 104 108 public Object eval (String source, int lineNo, int columnNo, 109 Object oscript) throws BSFException { 110 String script = oscript.toString (); 111 try { 112 interp.eval (script); 113 TclObject result = interp.getResult(); 114 Object internalRep = result.getInternalRep(); 115 116 if (internalRep instanceof ReflectObject) 118 return ReflectObject.get(interp,result); 119 if (internalRep instanceof TclString) 120 return result.toString(); 121 if (internalRep instanceof TclDouble) 122 return new Double (TclDouble.get(interp,result)); 123 if (internalRep instanceof TclInteger) 124 return new Integer (TclInteger.get(interp,result)); 125 126 return result; 127 } catch (TclException e) { 128 throw new BSFException (BSFException.REASON_EXECUTION_ERROR, 129 "error while eval'ing Jacl expression: " + 130 interp.getResult (), e); 131 } 132 } 133 136 public void initialize (BSFManager mgr, String lang, 137 Vector declaredBeans) throws BSFException { 138 super.initialize (mgr, lang, declaredBeans); 139 140 interp = new Interp(); 142 143 interp.createCommand ("bsf", new BSFCommand (mgr, this)); 146 147 try { 149 interp.eval("jaclloadjava"); 150 } catch (TclException e) { 151 throw new BSFException (BSFException.REASON_OTHER_ERROR, 152 "error while loading java package: " + 153 interp.getResult (), e); 154 } 155 156 int size = declaredBeans.size (); 157 for (int i = 0; i < size; i++) { 158 declareBean ((BSFDeclaredBean) declaredBeans.elementAt (i)); 159 } 160 } 161 162 165 public void undeclareBean (BSFDeclaredBean bean) throws BSFException { 166 eval ("<undeclare bean>", 0, 0, "set " + bean.name + " \"\""); 167 } 168 } 169 | Popular Tags |