001: /* JFox, the OpenSource J2EE Application Server
002: *
003: * Copyright (C) 2002 huihoo.org
004: * Distributable under GNU LGPL license
005: * See the GNU Lesser General Public License for more details.
006: */
007:
008: package example.jmx.standard;
009:
010: import java.io.IOException; //import java.net.UnknownHostException;
011: import javax.management.MBeanServer;
012: import javax.management.MBeanServerFactory;
013: import javax.management.ObjectName;
014: import javax.management.MalformedObjectNameException;
015: import javax.management.Attribute;
016: import javax.management.MBeanInfo;
017: import javax.management.MBeanAttributeInfo;
018: import javax.management.MBeanConstructorInfo;
019: import javax.management.MBeanOperationInfo;
020: import javax.management.MBeanNotificationInfo;
021:
022: /**
023: *
024: * @author <a href="mailto:young_yy@hotmail.org">Young Yang</a>
025: */
026:
027: public class StandardAgent {
028:
029: private MBeanServer server = null;
030:
031: public StandardAgent() {
032: echo("\n\tCREATE the MBeanServer.");
033: server = MBeanServerFactory.createMBeanServer();
034: }
035:
036: public static void main(String[] args) {
037:
038: // START
039: //
040: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
041: echo("\n>>> CREATE the agent...");
042: StandardAgent agent = new StandardAgent();
043: echo("\npress <Enter> to continue...\n");
044: waitForEnterPressed();
045:
046: // DO THE DEMO
047: //
048: agent.doSimpleDemo();
049:
050: // END
051: //
052: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
053: echo("\n>>> END of the SimpleStandard example:\n");
054: // String localHost = "localhost";
055: // try {
056: // localHost = java.net.InetAddress.getLocalHost().getHostName();
057: // } catch (UnknownHostException e) {
058: // localHost = "localhost" ;
059: // }
060: echo("\n\tpress <Enter> to doStop the agent...\n");
061: waitForEnterPressed();
062: System.exit(0);
063: }
064:
065: /*
066: * ------------------------------------------
067: * PRIVATE METHODS
068: * ------------------------------------------
069: */
070:
071: private void doSimpleDemo() {
072:
073: // build the simple MBean ObjectName
074: //
075: ObjectName mbeanObjectName = null;
076: String domain = server.getDefaultDomain();
077: String mbeanName = "example.jmx.standard.SimpleStandard";
078: try {
079: mbeanObjectName = new ObjectName(domain + ":type="
080: + mbeanName);
081: } catch (MalformedObjectNameException e) {
082: echo("\t!!! Could not doCreate the MBean ObjectName !!!");
083: e.printStackTrace();
084: echo("\nEXITING...\n");
085: System.exit(1);
086: }
087: // doCreate and register the MBean
088: //
089: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
090: createSimpleBean(mbeanObjectName, mbeanName);
091: echo("\npress <Enter> to continue...\n");
092: waitForEnterPressed();
093:
094: // get and display the management information exposed by the MBean
095: //
096: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
097: printMBeanInfo(mbeanObjectName, mbeanName);
098: echo("\npress <Enter> to continue...\n");
099: waitForEnterPressed();
100:
101: // manage the MBean
102: //
103: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
104: manageSimpleBean(mbeanObjectName, mbeanName);
105: echo("\npress <Enter> to continue...\n");
106: waitForEnterPressed();
107:
108: // trying to do illegal management actions...
109: //
110: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
111: goTooFar(mbeanObjectName);
112: echo("\npress <Enter> to continue...\n");
113: waitForEnterPressed();
114: }
115:
116: private void createSimpleBean(ObjectName mbeanObjectName,
117: String mbeanName) {
118:
119: echo("\n>>> CREATE the " + mbeanName
120: + " MBean within the MBeanServer:");
121: //String mbeanClassName = this.getClass().getPackage().getNameSpace() + mbeanName;
122: String mbeanClassName = mbeanName;
123: echo("\tOBJECT NAME = " + mbeanObjectName);
124: try {
125: server.createMBean(mbeanClassName, mbeanObjectName);
126: } catch (Exception e) {
127: echo("\t!!! Could not doCreate the " + mbeanName
128: + " MBean !!!");
129: e.printStackTrace();
130: echo("\nEXITING...\n");
131: System.exit(1);
132: }
133: }
134:
135: private void manageSimpleBean(ObjectName mbeanObjectName,
136: String mbeanName) {
137:
138: echo("\n>>> MANAGING the " + mbeanName + " MBean");
139: echo(" using its attributes and operations exposed for management");
140:
141: try {
142: // Get attribute values
143: sleep(1000);
144: printSimpleAttributes(mbeanObjectName);
145:
146: // Change State attribute
147: sleep(1000);
148: echo("\n Setting State attribute to value \"new state\"...");
149: Attribute stateAttribute = new Attribute("State",
150: "new state");
151: server.setAttribute(mbeanObjectName, stateAttribute);
152:
153: // Get attribute values
154: sleep(1000);
155: printSimpleAttributes(mbeanObjectName);
156:
157: // Invoking reset operation
158: sleep(1000);
159: echo("\n Invoking reset operation...");
160: server.invoke(mbeanObjectName, "reset", null, null);
161:
162: // Get attribute values
163: sleep(1000);
164: printSimpleAttributes(mbeanObjectName);
165:
166: } catch (Exception e) {
167: e.printStackTrace();
168: return;
169: }
170: }
171:
172: private void goTooFar(ObjectName mbeanObjectName) {
173:
174: echo("\n>>> Trying to set the NbChanges attribute (read-only)!");
175: echo("\n... We should get an AttributeNotFoundException:\n");
176: sleep(1000);
177: // Try to set the NbChanges attribute
178: Attribute nbChangesAttribute = new Attribute("NbChanges",
179: new Integer(1));
180: try {
181: server.setAttribute(mbeanObjectName, nbChangesAttribute);
182: } catch (Exception e) {
183: e.printStackTrace();
184: }
185: echo("\n\n>>> Trying to access the NbResets property (not exposed for management)!");
186: echo("\n... We should get an AttributeNotFoundException:\n");
187: sleep(1000);
188: // Try to access the NbResets property
189: try {
190: server.getAttribute(mbeanObjectName, "NbResets");
191: } catch (Exception e) {
192: e.printStackTrace();
193: }
194: return;
195: }
196:
197: private void printMBeanInfo(ObjectName mbeanObjectName,
198: String mbeanName) {
199:
200: echo("\n>>> Getting the management information for the "
201: + mbeanName + " MBean");
202: echo(" using the getMBeanInfo method of the MBeanServer");
203: sleep(1000);
204: MBeanInfo info = null;
205: try {
206: info = server.getMBeanInfo(mbeanObjectName);
207: } catch (Exception e) {
208: echo("\t!!! Could not get MBeanInfo object for "
209: + mbeanName + " !!!");
210: e.printStackTrace();
211: return;
212: }
213: echo("\nCLASSNAME: \t" + info.getClassName());
214: echo("\nDESCRIPTION: \t" + info.getDescription());
215: echo("\nATTRIBUTES");
216: MBeanAttributeInfo[] attrInfo = info.getAttributes();
217: if (attrInfo.length > 0) {
218: for (int i = 0; i < attrInfo.length; i++) {
219: echo(" ** NAME: \t" + attrInfo[i].getName());
220: echo(" DESCR: \t" + attrInfo[i].getDescription());
221: echo(" TYPE: \t" + attrInfo[i].getType()
222: + "\tREAD: " + attrInfo[i].isReadable()
223: + "\tWRITE: " + attrInfo[i].isWritable());
224: }
225: } else
226: echo(" ** No attributes **");
227: echo("\nCONSTRUCTORS");
228: MBeanConstructorInfo[] constrInfo = info.getConstructors();
229: for (int i = 0; i < constrInfo.length; i++) {
230: echo(" ** NAME: \t" + constrInfo[i].getName());
231: echo(" DESCR: \t" + constrInfo[i].getDescription());
232: echo(" PARAM: \t" + constrInfo[i].getSignature().length
233: + " parameter(s)");
234: }
235: echo("\nOPERATIONS");
236: MBeanOperationInfo[] opInfo = info.getOperations();
237: if (opInfo.length > 0) {
238: for (int i = 0; i < opInfo.length; i++) {
239: echo(" ** NAME: \t" + opInfo[i].getName());
240: echo(" DESCR: \t" + opInfo[i].getDescription());
241: echo(" PARAM: \t" + opInfo[i].getSignature().length
242: + " parameter(s)");
243: }
244: } else
245: echo(" ** No operations ** ");
246: echo("\nNOTIFICATIONS");
247: MBeanNotificationInfo[] notifInfo = info.getNotifications();
248: if (notifInfo.length > 0) {
249: for (int i = 0; i < notifInfo.length; i++) {
250: echo(" ** NAME: \t" + notifInfo[i].getName());
251: echo(" DESCR: \t" + notifInfo[i].getDescription());
252: }
253: } else
254: echo(" ** No notifications **");
255: }
256:
257: private void printSimpleAttributes(ObjectName mbeanObjectName) {
258:
259: try {
260: echo("\n Getting attribute values:");
261: String State = (String) server.getAttribute(
262: mbeanObjectName, "State");
263: Integer NbChanges = (Integer) server.getAttribute(
264: mbeanObjectName, "NbChanges");
265: echo("\tState = \"" + State + "\"");
266: echo("\tNbChanges = " + NbChanges);
267: } catch (Exception e) {
268: echo("\t!!! Could not read attributes !!!");
269: e.printStackTrace();
270: return;
271: }
272: }
273:
274: private static void echo(String msg) {
275: System.out.println(msg);
276: }
277:
278: private static void sleep(int millis) {
279: try {
280: Thread.sleep(millis);
281: } catch (InterruptedException e) {
282: return;
283: }
284: }
285:
286: private static void waitForEnterPressed() {
287: try {
288: System.in.read();
289: } catch (IOException e) {
290: e.printStackTrace();
291: }
292: }
293:
294: }
|