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 org.apache.bsf.BSFEngine;
020: import org.apache.bsf.BSFException;
021: import org.apache.bsf.BSFManager;
022: import org.apache.bsf.util.EngineUtils;
023:
024: import tcl.lang.Command;
025: import tcl.lang.Interp;
026: import tcl.lang.ReflectObject;
027: import tcl.lang.TCL;
028: import tcl.lang.TclException;
029: import tcl.lang.TclObject;
030:
031: // class used to add "bsf" command to the Jacl runtime
032: class BSFCommand implements Command {
033: BSFManager mgr;
034: BSFEngine jengine;
035:
036: BSFCommand(BSFManager mgr, BSFEngine jengine) {
037: this .mgr = mgr;
038: this .jengine = jengine;
039: }
040:
041: public void cmdProc(Interp interp, TclObject argv[])
042: throws TclException {
043: if (argv.length < 2) {
044: interp
045: .setResult("invalid # of args; usage: bsf "
046: + "lookupBean|registerBean|unregisterBean|addEventListener args");
047: throw new TclException(TCL.ERROR);
048: }
049:
050: String op = argv[1].toString();
051:
052: if (op.equals("lookupBean")) {
053: if (argv.length != 3) {
054: interp.setResult("invalid # of args; usage: bsf "
055: + "lookupBean name-of-bean");
056: throw new TclException(TCL.ERROR);
057: }
058:
059: String beanName = argv[2].toString();
060: Object bean = mgr.lookupBean(beanName);
061: if (bean == null) {
062: interp.setResult("unknown object: " + beanName);
063: throw new TclException(TCL.ERROR);
064: }
065: interp.setResult(ReflectObject.newInstance(interp, bean
066: .getClass(), bean));
067:
068: } else if (op.equals("registerBean")) {
069: if (argv.length != 4) {
070: interp.setResult("invalid # of args; usage: bsf "
071: + "registerBean name-of-bean bean");
072: throw new TclException(TCL.ERROR);
073: }
074: mgr.registerBean(argv[2].toString(), ReflectObject.get(
075: interp, argv[3]));
076: interp.setResult("");
077:
078: } else if (op.equals("unregisterBean")) {
079: if (argv.length != 3) {
080: interp.setResult("invalid # of args; usage: bsf "
081: + "unregisterBean name-of-bean");
082: throw new TclException(TCL.ERROR);
083: }
084: mgr.unregisterBean(argv[2].toString());
085: interp.setResult("");
086:
087: } else if (op.equals("addEventListener")) {
088: if (argv.length != 6) {
089: interp
090: .setResult("invalid # of args; usage: bsf "
091: + "addEventListener object event-set-name filter "
092: + "script-to-run");
093: throw new TclException(TCL.ERROR);
094: }
095: try {
096: // usage: bsf addEventListener object event-set filter script
097: String filter = argv[4].toString();
098: filter = filter.equals("") ? null : filter;
099: EngineUtils
100: .addEventListener(ReflectObject.get(interp,
101: argv[2]), argv[3].toString(), filter,
102: jengine, mgr, "<event-script>", 0, 0,
103: argv[5].toString());
104: } catch (BSFException e) {
105: e.printStackTrace();
106: interp
107: .setResult("got BSF exception: "
108: + e.getMessage());
109: throw new TclException(TCL.ERROR);
110: }
111: }
112: }
113: }
|