001: /*
002: * Copyright 2004,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.bsf.engines.jacl;
018:
019: import java.util.Vector;
020:
021: import org.apache.bsf.BSFDeclaredBean;
022: import org.apache.bsf.BSFException;
023: import org.apache.bsf.BSFManager;
024: import org.apache.bsf.util.BSFEngineImpl;
025:
026: import tcl.lang.Interp;
027: import tcl.lang.ReflectObject;
028: import tcl.lang.TclDouble;
029: import tcl.lang.TclException;
030: import tcl.lang.TclInteger;
031: import tcl.lang.TclObject;
032: import tcl.lang.TclString;
033:
034: /**
035: * This is the interface to Scriptics's Jacl (Tcl) from the
036: * Bean Scripting Framework.
037: * <p>
038: *
039: * @author Sanjiva Weerawarana
040: */
041:
042: public class JaclEngine extends BSFEngineImpl {
043: /* the Jacl interpretor object */
044: private Interp interp;
045:
046: /**
047: *
048: * @param method The name of the method to call.
049: * @param args an array of arguments to be
050: * passed to the extension, which may be either
051: * Vectors of Nodes, or Strings.
052: */
053: public Object call(Object obj, String method, Object[] args)
054: throws BSFException {
055: StringBuffer tclScript = new StringBuffer(method);
056: if (args != null) {
057: for (int i = 0; i < args.length; i++) {
058: tclScript.append(" ");
059: tclScript.append(args[i].toString());
060: }
061: }
062: return eval("<function call>", 0, 0, tclScript.toString());
063: }
064:
065: /**
066: * Declare a bean
067: */
068: public void declareBean(BSFDeclaredBean bean) throws BSFException {
069: String expr = "set " + bean.name + " [bsf lookupBean \""
070: + bean.name + "\"]";
071: eval("<declare bean>", 0, 0, expr);
072: }
073:
074: /**
075: * This is used by an application to evaluate a string containing
076: * some expression.
077: */
078: public Object eval(String source, int lineNo, int columnNo,
079: Object oscript) throws BSFException {
080: String script = oscript.toString();
081: try {
082: interp.eval(script);
083: TclObject result = interp.getResult();
084: Object internalRep = result.getInternalRep();
085:
086: // if the object has a corresponding Java type, unwrap it
087: if (internalRep instanceof ReflectObject)
088: return ReflectObject.get(interp, result);
089: if (internalRep instanceof TclString)
090: return result.toString();
091: if (internalRep instanceof TclDouble)
092: return new Double(TclDouble.get(interp, result));
093: if (internalRep instanceof TclInteger)
094: return new Integer(TclInteger.get(interp, result));
095:
096: return result;
097: } catch (TclException e) {
098: throw new BSFException(BSFException.REASON_EXECUTION_ERROR,
099: "error while eval'ing Jacl expression: "
100: + interp.getResult(), e);
101: }
102: }
103:
104: /**
105: * Initialize the engine.
106: */
107: public void initialize(BSFManager mgr, String lang,
108: Vector declaredBeans) throws BSFException {
109: super .initialize(mgr, lang, declaredBeans);
110:
111: // create interpreter
112: interp = new Interp();
113:
114: // register the extension that user's can use to get at objects
115: // registered by the app
116: interp.createCommand("bsf", new BSFCommand(mgr, this ));
117:
118: // Make java functions be available to Jacl
119: try {
120: interp.eval("jaclloadjava");
121: } catch (TclException e) {
122: throw new BSFException(BSFException.REASON_OTHER_ERROR,
123: "error while loading java package: "
124: + interp.getResult(), e);
125: }
126:
127: int size = declaredBeans.size();
128: for (int i = 0; i < size; i++) {
129: declareBean((BSFDeclaredBean) declaredBeans.elementAt(i));
130: }
131: }
132:
133: /**
134: * Undeclare a previously declared bean.
135: */
136: public void undeclareBean(BSFDeclaredBean bean) throws BSFException {
137: eval("<undeclare bean>", 0, 0, "set " + bean.name + " \"\"");
138: }
139: }
|