KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > protocols > BSH


1 // $Id: BSH.java,v 1.10 2004/09/22 10:34:11 belaban Exp $
2

3 package org.jgroups.protocols;
4
5
6 import bsh.EvalError;
7 import bsh.Interpreter;
8 import org.jgroups.Address;
9 import org.jgroups.Event;
10 import org.jgroups.Header;
11 import org.jgroups.Message;
12 import org.jgroups.stack.Protocol;
13 import org.jgroups.util.Util;
14
15 import java.io.IOException JavaDoc;
16 import java.io.ObjectInput JavaDoc;
17 import java.io.ObjectOutput JavaDoc;
18 import java.io.Serializable JavaDoc;
19
20
21
22
23 /**
24  * Beanshell (<a HREF=http://www.beanshell.org>"www.beanshell.org"</a>) interpreter class.
25  * The <tt>eval()</tt> method receives Java code, executes it and returns the
26  * result of the evaluation (or an exception).
27  * User: Bela
28  * Date: Mar 8, 2003
29  * Time: 1:57:07 PM
30  * @author Bela Ban
31  */

32 public class BSH extends Protocol {
33     static final String JavaDoc name="BSH";
34     Interpreter interpreter=null;
35
36     public BSH() {
37         ;
38     }
39
40     public String JavaDoc getName() {
41         return name;
42     }
43
44
45
46     public void init() throws Exception JavaDoc {
47         ;
48     }
49
50     public void start() throws Exception JavaDoc {
51         ;
52     }
53
54     public void stop() {
55         if(interpreter != null)
56             destroyInterpreter();
57     }
58
59     public void destroy() {
60         ;
61     }
62
63     /** We have no up handler thread */
64     public void startUpHandler() {
65         ;
66     }
67
68     /** We have no down handler thread */
69     public void startDownHandler() {
70         ;
71     }
72
73     public void up(Event evt) {
74         Header h;
75         Message msg;
76         int type;
77
78         if(evt.getType() == Event.MSG) {
79             msg=(Message)evt.getArg();
80             h=msg.removeHeader(name);
81             if(h != null && h instanceof BshHeader) {
82                 type=((BshHeader)h).type;
83                 switch(type) {
84                     case BshHeader.REQ:
85                         handleRequest(msg.getSrc(), msg.getBuffer());
86                         return;
87                     case BshHeader.RSP:
88                         msg.putHeader(name, h);
89                         passUp(evt);
90                         return;
91                     case BshHeader.DESTROY_INTERPRETER:
92                         destroyInterpreter();
93                         return;
94                     default:
95                         if(log.isErrorEnabled()) log.error("header type was not REQ as expected" +
96                                     " (was " + type + ')');
97                         return;
98                 }
99             }
100         }
101         passUp(evt);
102     }
103
104
105     void handleRequest(Address sender, byte[] buf) {
106         Object JavaDoc retval;
107         String JavaDoc code;
108
109
110         if(buf == null) {
111             if(log.isErrorEnabled()) log.error("buffer was null");
112             return;
113         }
114
115         code=new String JavaDoc(buf);
116
117         // create interpreter just-in-time
118
if(interpreter == null) {
119             interpreter=new Interpreter();
120
121                 if(log.isInfoEnabled()) log.info("beanshell interpreter was created");
122             try {
123                 interpreter.set("bsh_prot", this);
124
125                     if(log.isInfoEnabled()) log.info("set \"bsh_prot\" to " + this);
126             }
127             catch(EvalError err) {
128                 if(log.isErrorEnabled()) log.error("failed setting \"bsh_prot\": " + err);
129             }
130
131         }
132
133         try {
134             retval=interpreter.eval(code);
135
136                 if(log.isInfoEnabled()) log.info("eval: \"" + code +
137                                                   "\", retval=" + retval);
138         }
139         catch(EvalError ex) {
140             if(log.isErrorEnabled()) log.error("error is " + Util.getStackTrace(ex));
141             retval=ex;
142         }
143
144         if(sender != null) {
145             Message rsp=new Message(sender, null, null);
146
147             // serialize the object if serializable, otherwise just send string
148
// representation
149
if(retval != null) {
150                 if(retval instanceof Serializable JavaDoc)
151                     rsp.setObject((Serializable JavaDoc)retval);
152                 else
153                     rsp.setObject(retval.toString());
154             }
155
156
157                 if(log.isInfoEnabled()) log.info("sending back response " +
158                                                   retval + " to " + rsp.getDest());
159             rsp.putHeader(name, new BshHeader(BshHeader.RSP));
160             passDown(new Event(Event.MSG, rsp));
161         }
162     }
163
164
165 /* --------------------------- Callbacks ---------------------------- */
166 // public Object eval(String code) throws Exception {
167
// Object retval=null;
168
// try {
169
// retval=interpreter.eval(code);
170
//
171
// if(log.isInfoEnabled()) log.info("BSH.eval()", "eval: \"" + code +
172
// "\", retval=" + retval);
173
// if(retval != null && !(retval instanceof Serializable)) {
174
// if(log.isErrorEnabled()) log.error("BSH.eval", "return value " + retval +
175
// " is not serializable, cannot be sent back " +
176
// "(returning null)");
177
// return null;
178
// }
179
// return retval;
180
// }
181
// catch(EvalError ex) {
182
// if(log.isErrorEnabled()) log.error("BSH.eval()", "error is " + Util.getStackTrace(ex));
183
// return ex;
184
// }
185
// }
186
//
187
public void destroyInterpreter() {
188         interpreter=null; // allow it to be garbage collected
189

190             if(log.isInfoEnabled()) log.info("beanshell interpreter was destroyed");
191     }
192     /* ------------------------ End of Callbacks ------------------------ */
193
194
195     public static class BshHeader extends Header {
196         public static final int REQ=1;
197         public static final int RSP=2;
198         public static final int DESTROY_INTERPRETER=3;
199         int type=REQ;
200
201
202         public BshHeader() {
203             ; // used by externalization
204
}
205
206         public BshHeader(int type) {
207             this.type=type;
208         }
209
210         public long size() {
211             return 10;
212         }
213
214         public String JavaDoc toString() {
215             StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
216             if(type == REQ)
217                 sb.append("REQ");
218             else
219                 if(type == RSP)
220                     sb.append("RSP");
221                 else
222                     if(type == DESTROY_INTERPRETER)
223                         sb.append("DESTROY_INTERPRETER");
224                     else
225                         sb.append("<unknown type>");
226             return sb.toString();
227         }
228
229         public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
230             out.writeInt(type);
231         }
232
233         public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
234             type=in.readInt();
235         }
236
237     }
238
239 }
240
Popular Tags