01: /*
02: * Copyright (C) The MX4J Contributors.
03: * All rights reserved.
04: *
05: * This software is distributed under the terms of the MX4J License version 1.0.
06: * See the terms of the MX4J License in the documentation provided with this software.
07: */
08: package mx4j.examples.tools.jython;
09:
10: import java.net.MalformedURLException;
11: import javax.management.Attribute;
12: import javax.management.JMException;
13: import javax.management.MBeanServer;
14: import javax.management.MBeanServerFactory;
15: import javax.management.ObjectName;
16:
17: /**
18: * Example as how to use the Jython MBean. An MBean will be created and some scripts executed
19: *
20: * @version $Revision: 1.1 $
21: */
22: public class ScriptExample {
23: public ScriptExample() {
24: }
25:
26: /**
27: * Executes the script
28: */
29: public void start() throws JMException, MalformedURLException {
30: // creates new server
31: MBeanServer server = MBeanServerFactory
32: .createMBeanServer("Script");
33: ObjectName scriptingName = new ObjectName("Test:name=script");
34: server.createMBean("mx4j.tools.jython.JythonRunner",
35: scriptingName, null);
36:
37: // Sample. Starts all monitors
38: server
39: .setAttribute(
40: scriptingName,
41: new Attribute(
42: "Script",
43: "[proxy(name).start() for name in server.queryNames(None, None) if server.isInstanceOf(name, 'javax.management.monitor.Monitor')]"));
44: server.invoke(scriptingName, "runScript", null, null);
45:
46: // Sample. Stops all timers
47: server
48: .setAttribute(
49: scriptingName,
50: new Attribute(
51: "Script",
52: "[proxy(name).start() for name in server.queryNames(None, None) if server.isInstanceOf(name, 'javax.management.timer.Timer')]"));
53: server.invoke(scriptingName, "runScript", null, null);
54:
55: // Sample. prints all MBeans which description is not null
56: server
57: .setAttribute(
58: scriptingName,
59: new Attribute(
60: "Script",
61: "desc = [server.getMBeanInfo(name).description for name in server.queryNames(None, None)]\nprint filter(lambda x:x, desc)"));
62: server.invoke(scriptingName, "runScript", null, null);
63: }
64:
65: public static void main(String[] str) throws JMException,
66: MalformedURLException {
67: ScriptExample example = new ScriptExample();
68: example.start();
69: }
70: }
|