1 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 ; 16 import java.io.ObjectInput ; 17 import java.io.ObjectOutput ; 18 import java.io.Serializable ; 19 20 21 22 23 32 public class BSH extends Protocol { 33 static final String name="BSH"; 34 Interpreter interpreter=null; 35 36 public BSH() { 37 ; 38 } 39 40 public String getName() { 41 return name; 42 } 43 44 45 46 public void init() throws Exception { 47 ; 48 } 49 50 public void start() throws Exception { 51 ; 52 } 53 54 public void stop() { 55 if(interpreter != null) 56 destroyInterpreter(); 57 } 58 59 public void destroy() { 60 ; 61 } 62 63 64 public void startUpHandler() { 65 ; 66 } 67 68 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 retval; 107 String code; 108 109 110 if(buf == null) { 111 if(log.isErrorEnabled()) log.error("buffer was null"); 112 return; 113 } 114 115 code=new String (buf); 116 117 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 if(retval != null) { 150 if(retval instanceof Serializable ) 151 rsp.setObject((Serializable )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 166 public void destroyInterpreter() { 188 interpreter=null; 190 if(log.isInfoEnabled()) log.info("beanshell interpreter was destroyed"); 191 } 192 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 ; } 205 206 public BshHeader(int type) { 207 this.type=type; 208 } 209 210 public long size() { 211 return 10; 212 } 213 214 public String toString() { 215 StringBuffer sb=new StringBuffer (); 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 out) throws IOException { 230 out.writeInt(type); 231 } 232 233 public void readExternal(ObjectInput in) throws IOException , ClassNotFoundException { 234 type=in.readInt(); 235 } 236 237 } 238 239 } 240 | Popular Tags |