KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > examples > tools > jython > ScriptExample


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8 package mx4j.examples.tools.jython;
9
10 import java.net.MalformedURLException JavaDoc;
11 import javax.management.Attribute JavaDoc;
12 import javax.management.JMException JavaDoc;
13 import javax.management.MBeanServer JavaDoc;
14 import javax.management.MBeanServerFactory JavaDoc;
15 import javax.management.ObjectName JavaDoc;
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.3 $
21  */

22 public class ScriptExample
23 {
24    public ScriptExample()
25    {
26    }
27
28    /**
29     * Executes the script
30     */

31    public void start() throws JMException JavaDoc, MalformedURLException JavaDoc
32    {
33       // creates new server
34
MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer("Script");
35       ObjectName JavaDoc scriptingName = new ObjectName JavaDoc("Test:name=script");
36       server.createMBean("mx4j.tools.jython.JythonRunner", scriptingName, null);
37
38       // Sample. Starts all monitors
39
server.setAttribute(scriptingName, new Attribute JavaDoc("Script", "[proxy(name).start() for name in server.queryNames(None, None) if server.isInstanceOf(name, 'javax.management.monitor.Monitor')]"));
40       server.invoke(scriptingName, "runScript", null, null);
41
42       // Sample. Stops all timers
43
server.setAttribute(scriptingName, new Attribute JavaDoc("Script", "[proxy(name).start() for name in server.queryNames(None, None) if server.isInstanceOf(name, 'javax.management.timer.Timer')]"));
44       server.invoke(scriptingName, "runScript", null, null);
45
46       // Sample. prints all MBeans which description is not null
47
server.setAttribute(scriptingName, new Attribute JavaDoc("Script", "desc = [server.getMBeanInfo(name).description for name in server.queryNames(None, None)]\nprint filter(lambda x:x, desc)"));
48       server.invoke(scriptingName, "runScript", null, null);
49    }
50
51    public static void main(String JavaDoc[] str) throws JMException JavaDoc, MalformedURLException JavaDoc
52    {
53       ScriptExample example = new ScriptExample();
54       example.start();
55    }
56 }
57
58
Popular Tags