001: /*
002: * This software consists of voluntary contributions made by many individuals
003: * on behalf of the Apache Software Foundation and was originally created by
004: * Sanjiva Weerawarana and others at International Business Machines
005: * Corporation. For more information on the Apache Software Foundation,
006: * please see <http://www.apache.org/>.
007: */
008:
009: package org.apache.bsf.util;
010:
011: import java.util.Vector;
012:
013: import org.apache.bsf.BSFEngine;
014: import org.apache.bsf.BSFException;
015: import org.apache.bsf.BSFManager;
016: import org.apache.bsf.util.event.EventProcessor;
017:
018: /*
019: * Copyright (C) 2001-2006 Rony G. Flatscher
020: *
021: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
022: * use this file except in compliance with the License. You may obtain a copy of
023: * the License at
024: *
025: * <a
026: * href="http://www.apache.org/licenses/LICENSE-2.0">http://www.apache.org/licenses/LICENSE-2.0</a>
027: *
028: * Unless required by applicable law or agreed to in writing, software
029: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
030: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
031: * License for the specific language governing permissions and limitations under
032: * the License.
033: *
034: */
035:
036: /**
037: *
038: * This is used to support binding scripts to be run when an event occurs,
039: * forwarding the arguments supplied to the event listener. It is an adapted
040: * version of org.apache.bsf.util.BSFEventProcessor.
041: *
042: * @author Rony G. Flatscher, but most of the code copied from
043: * org.apache.bsf.util.BSFEventProcessor by Sanjiva Weerawarana
044: */
045: public class BSFEventProcessorReturningEventInfos implements
046: EventProcessor {
047: BSFEngine engine;
048:
049: BSFManager manager;
050:
051: String filter;
052:
053: String source;
054:
055: int lineNo;
056:
057: int columnNo;
058:
059: Object script;
060:
061: Object dataFromScriptingEngine; // ---rgf, 2006-02-24: data coming from the
062:
063: // script engine, could be
064:
065: // e.g. an object reference to forward event with received arguments to
066:
067: /**
068: * Package-protected constructor makes this class unavailable for public
069: * use.
070: *
071: * @param dataFromScriptingEngine
072: * this contains any object supplied by the scripting engine and
073: * gets sent back with the supplied script. This could be used
074: * e.g. for indicating which scripting engine object should be
075: * ultimately informed of the event occurrence.
076: */
077: BSFEventProcessorReturningEventInfos(BSFEngine engine,
078: BSFManager manager, String filter, String source,
079: int lineNo, int columnNo, Object script,
080: Object dataFromScriptingEngine) throws BSFException {
081: this .engine = engine;
082: this .manager = manager;
083: this .filter = filter;
084: this .source = source;
085: this .lineNo = lineNo;
086: this .columnNo = columnNo;
087: this .script = script;
088: this .dataFromScriptingEngine = dataFromScriptingEngine;
089: }
090:
091: // ////////////////////////////////////////////////////////////////////////
092: //
093: // event is delegated to me by the adapters using this. inFilter is
094: // in general the name of the method via which the event was received
095: // at the adapter. For prop/veto change events, inFilter is the name
096: // of the property. In any case, in the event processor, I only forward
097: // those events if for which the filters match (if one is specified).
098:
099: public void processEvent(String inFilter, Object[] evtInfo) {
100: try {
101: processExceptionableEvent(inFilter, evtInfo);
102: } catch (RuntimeException re) {
103: // rethrow this .. I don't want to intercept run-time stuff
104: // that can in fact occur legit
105: throw re;
106: } catch (Exception e) {
107: // should not occur
108: System.err
109: .println("BSFError: non-exceptionable event delivery "
110: + "threw exception (that's not nice): " + e);
111: e.printStackTrace();
112: }
113: }
114:
115: // ////////////////////////////////////////////////////////////////////////
116: //
117: // same as above, but used when the method event method may generate
118: // an exception which must go all the way back to the source (as in
119: // the vetoableChange case)
120:
121: public void processExceptionableEvent(String inFilter,
122: Object[] evtInfo) throws Exception {
123:
124: // System.err.println(this+": inFilter=["+inFilter+"],
125: // filter=["+filter+"]");
126: if ((filter != null) && !filter.equals(inFilter)) {
127: // ignore this event
128: return;
129: }
130:
131: // run the script
132: // engine.exec (source, lineNo, columnNo, script);
133:
134: // create the parameter vectors for engine.apply()
135: Vector paramNames = new Vector(), paramValues = new Vector();
136:
137: // parameter # 1
138: // supply the parameters as an array object as sent to the event object
139: // listener
140: // (usually the first entry is the sent event object)
141: paramNames.add("eventParameters");
142: paramValues.add(evtInfo);
143:
144: // parameter # 2
145: // supply the data object received from the scripting engine to be sent
146: // with the event
147: paramNames.add("dataFromScriptingEngine");
148: paramValues.add(this .dataFromScriptingEngine); // can be null as well
149:
150: // parameter # 3
151: // event filter in place
152: paramNames.add("inFilter");
153: paramValues.add(inFilter); // event name that has occurred
154:
155: // parameter # 4
156: // event filter in place
157: paramNames.add("eventFilter");
158: paramValues.add(this .filter); // can be null as well
159:
160: // parameter # 5
161: // BSF manager instance (e.g. allows access to its registry)
162: paramNames.add("BSFManager");
163: paramValues.add(this.manager);
164:
165: engine.apply(source, lineNo, columnNo, this.script, paramNames,
166: paramValues);
167: }
168: }
|