1 8 9 package mx4j.tools.jython; 10 11 import java.io.BufferedReader ; 12 import java.io.IOException ; 13 import java.io.InputStream ; 14 import java.io.InputStreamReader ; 15 import java.net.URL ; 16 import javax.management.InstanceNotFoundException ; 17 import javax.management.ListenerNotFoundException ; 18 import javax.management.MBeanRegistration ; 19 import javax.management.MBeanServer ; 20 import javax.management.Notification ; 21 import javax.management.NotificationFilter ; 22 import javax.management.NotificationListener ; 23 import javax.management.ObjectName ; 24 25 import mx4j.log.Log; 26 import mx4j.log.Logger; 27 import org.python.core.Py; 28 import org.python.core.PyCode; 29 import org.python.core.PySystemState; 30 import org.python.util.PythonInterpreter; 31 32 51 public class JythonRunner implements JythonRunnerMBean, NotificationListener , MBeanRegistration 52 { 53 private MBeanServer server = null; 54 55 private ObjectName targetMBeanName, objectName; 56 57 private String notificationName; 58 59 private boolean useText = true; 60 61 private boolean useCache = false; 62 63 private String scriptText; 64 65 private URL scriptFile; 66 67 private PyCode cache = null; 68 69 private static PythonInterpreter interpreter; 70 71 public void handleNotification(Notification notification, Object handback) 72 { 73 if (notificationName != null && !notification.getType().equals(notificationName)) return; 74 75 Logger logger = getLogger(); 76 if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Notification " + notification + " hit, sending message"); 77 runScript(); 78 } 79 80 private Logger getLogger() 81 { 82 return Log.getLogger(getClass().getName()); 83 } 84 85 89 public void runScript() 90 { 91 PythonInterpreter interp = getPythonInterpreter(); 92 interp.set("server", server); 93 String script = null; 94 if (useText) 95 { 96 script = scriptText; 97 } 98 else 99 { 100 try 101 { 102 script = loadStream(scriptFile.openStream()); 103 } 104 catch (IOException e) 105 { 106 Logger log = getLogger(); 107 log.error("Exception during url opening", e); 108 } 109 } 110 interp.exec(script); 111 } 112 113 public static PythonInterpreter getPythonInterpreter() 114 { 115 if (interpreter == null) 116 { 117 interpreter = new PythonInterpreter(); 118 PySystemState sys = Py.getSystemState(); 119 sys.add_package("javax.management"); 120 sys.add_package("javax.management.loading"); 121 sys.add_package("javax.management.modelmbean"); 122 sys.add_package("javax.management.monitor"); 123 sys.add_package("javax.management.openmbean"); 124 sys.add_package("javax.management.remote"); 125 sys.add_package("javax.management.remote.rmi"); 126 sys.add_package("javax.management.relation"); 127 sys.add_package("javax.management.timer"); 128 try 129 { 130 String script = loadStream(JythonRunner.class.getClassLoader().getResourceAsStream("mx4j/tools/jython/jmxUtils.py")); 131 interpreter.exec(script); 132 } 133 catch (IOException e) 134 { 135 e.printStackTrace(); 136 } 137 } 138 return interpreter; 139 } 140 141 protected static String loadStream(InputStream in) throws IOException 142 { 143 BufferedReader reader = new BufferedReader (new InputStreamReader (in)); 144 String line = null; 145 StringBuffer buffer = new StringBuffer (); 146 while ((line = reader.readLine()) != null) 147 { 148 buffer.append(line); 149 buffer.append("\n"); 150 } 151 return buffer.toString(); 152 } 153 154 157 public String getNotificationType() 158 { 159 return notificationName; 160 } 161 162 167 public void setNotificationType(String notificationName) 168 { 169 this.notificationName = notificationName; 170 } 171 172 176 public void setObservedObject(ObjectName targetMBeanName) 177 { 178 this.targetMBeanName = targetMBeanName; 179 registerListener(); 180 } 181 182 185 public ObjectName getObservedObject() 186 { 187 return targetMBeanName; 188 } 189 190 194 public boolean getUseText() 195 { 196 return this.useText; 197 } 198 199 203 public void setScript(String text) 204 { 205 this.scriptText = text; 206 this.useText = true; 207 } 208 209 212 public String getScript() 213 { 214 return this.scriptText; 215 } 216 217 220 public URL getScriptURL() 221 { 222 return scriptFile; 223 } 224 225 230 public void setScriptURL(URL file) 231 { 232 this.scriptFile = file; 233 this.useText = false; 234 } 235 236 240 public boolean getCacheScript() 241 { 242 return useCache; 243 } 244 245 249 public void setCacheScript(boolean useCache) 250 { 251 this.useCache = useCache; 252 } 253 254 257 public ObjectName preRegister(MBeanServer server, ObjectName name) 258 throws java.lang.Exception 259 { 260 this.server = server; 261 this.objectName = name; 262 return name; 263 } 264 265 266 public void postRegister(Boolean registrationDone) 267 { 268 } 269 270 271 public void preDeregister() throws java.lang.Exception 272 { 273 unregisterListener(); 274 } 275 276 277 public void postDeregister() 278 { 279 } 280 281 protected void registerListener() 282 { 283 try 284 { 285 if (targetMBeanName != null && server.isInstanceOf(targetMBeanName, "javax.management.NotificationBroadcaster")) 286 { 287 server.addNotificationListener(targetMBeanName, this, new MessageFilter(), null); 288 } 289 } 290 catch (InstanceNotFoundException e) 291 { 292 Logger log = getLogger(); 293 log.error("Exception during notification registration", e); 294 } 295 } 296 297 protected void unregisterListener() 298 { 299 try 300 { 301 if (targetMBeanName != null && server.isInstanceOf(targetMBeanName, "javax.management.NotificationBroadcaster")) 302 { 303 server.removeNotificationListener(targetMBeanName, this); 304 } 305 } 306 catch (InstanceNotFoundException e) 307 { 308 Logger log = getLogger(); 309 log.error("Exception during notification unregistration", e); 310 } 311 catch (ListenerNotFoundException e) 312 { 313 } 314 } 315 316 private class MessageFilter implements NotificationFilter 317 { 318 public boolean isNotificationEnabled(Notification notification) 319 { 320 return notificationName == null || (notification.getType() != null && notification.getType().equals(notificationName)); 321 } 322 } 323 } 324 | Popular Tags |