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.openmbean;
009:
010: import java.io.IOException;
011:
012: import javax.management.openmbean.OpenMBeanParameterInfoSupport;
013: import javax.management.openmbean.OpenMBeanOperationInfoSupport;
014: import javax.management.openmbean.OpenMBeanConstructorInfoSupport;
015: import javax.management.openmbean.OpenMBeanInfoSupport;
016: import javax.management.openmbean.OpenMBeanInfo;
017: import javax.management.openmbean.OpenMBeanAttributeInfoSupport;
018: import javax.management.openmbean.OpenDataException;
019: import javax.management.openmbean.CompositeData;
020: import javax.management.MBeanParameterInfo;
021: import javax.management.MBeanOperationInfo;
022: import javax.management.MBeanNotificationInfo;
023: import javax.management.MBeanConstructorInfo;
024: import javax.management.ObjectName;
025: import javax.management.MBeanAttributeInfo;
026: import javax.management.MBeanServerFactory;
027: import javax.management.MBeanServer;
028:
029: //import javax.management.ObjectInstance;
030:
031: /**
032: *
033: * @author <a href="mailto:young_yy@hotmail.org">Young Yang</a>
034: */
035:
036: public class OpenAgent {
037:
038: private static MBeanServer server = MBeanServerFactory
039: .createMBeanServer();
040:
041: public static void main(String[] args) {
042:
043: SimpleOpenMBean openMBean = null;
044: ObjectName openMBeanObjectName = null;
045:
046: try {
047: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
048: echo("\nCreated MBeanServer");
049: // Create and doStart in the MBean server an HTML protocol adaptor
050: // TraceManager.parseTraceProperties();
051: // echo("\nCreate and doStart an HTML protocol adaptor");
052: // ObjectInstance html = server.createMBean("org.sun.jdmk.comm.HtmlAdaptorServer", null);
053: // server.reflectInvoke(html.getObjectName(), "doStart", new Object[0], new String[0]);
054:
055: // Instantiate a SimpleOpenMBean
056: echo("\nInstantiate a SimpleOpenMBean");
057: openMBean = new SimpleOpenMBean();
058:
059: // Register in the MBean server the SimpleOpenMBean
060: echo("\nRegister in the MBeanServer the SimpleOpenMBean");
061: openMBeanObjectName = new ObjectName(
062: "TestDomain:className=SimpleOpenMBean");
063: echo("ObjectName:" + openMBeanObjectName);
064: server.registerMBean(openMBean, openMBeanObjectName);
065: waitForEnterPressed();
066:
067: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
068: printOpenMBeanInfo(openMBeanObjectName);
069: waitForEnterPressed();
070:
071: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
072: echo("\nBuild tShirt1: let default applies for first 3 parameters: (null, null,null, new Float(10.0f))");
073: CompositeData tShirt1 = openMBean.buildTShirt(null, null,
074: null, new Float(10.0f));
075: echo("BUILT tShirt1 = [" + tShirt1);
076: echo("\nBuild tShirt2: specify all parameters: (\"JDMK\", \"red\", \"L\", new Float(15.0f))");
077: CompositeData tShirt2 = openMBean.buildTShirt("JDMK",
078: "red", "L", new Float(15.0f));
079: echo("BUILT tShirt2 = [" + tShirt2);
080: echo("\nAttempt to build tShirt with illegal model: (\"BADBAD\", \"red\", \"L\", new Float(15.0f))");
081: try {
082: openMBean.buildTShirt("BADBAD", "red", "L", new Float(
083: 15.0f));
084: } catch (OpenDataException ode) {
085: ode.printStackTrace();
086: }
087: echo("\nAttempt to build tShirt with illegal price: (\"JAVA\", \"blue\", \"XXL\", new Float(999.0f))");
088: try {
089: openMBean.buildTShirt("JAVA", "blue", "XXL", new Float(
090: 999.0f));
091: } catch (OpenDataException ode) {
092: ode.printStackTrace();
093: }
094: waitForEnterPressed();
095:
096: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
097: echo("\nAdd tShirt1 to TabularData");
098: echo("RETURN: " + openMBean.addTShirt(tShirt1));
099: echo("\nBuild tShirt3: differs from tShirt1 with its price only: (null, null,null, new Float(15.0f))");
100: CompositeData tShirt3 = openMBean.buildTShirt(null, null,
101: null, new Float(15.0f));
102: echo("BUILT tShirt3 = [" + tShirt3);
103: echo("\nAttempt to add tShirt3 to TabularData: will fail as price is not part of the index");
104: echo("(see SimpleOpenMBean.java code)");
105: echo("RETURN: " + openMBean.addTShirt(tShirt3));
106: waitForEnterPressed();
107:
108: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
109:
110: } catch (Exception e) {
111: e.printStackTrace();
112: return;
113: }
114:
115: // System.out.println("\nNow, you can point your browser to http://localhost:8082/");
116: // System.out.println("or doStart your client application to connect to this agent.\n");
117: }
118:
119: /**
120: * Displays all OpenMBean information for the specified MBean.
121: */
122: private static void printOpenMBeanInfo(
123: ObjectName openMBeanObjectName) {
124:
125: echo("\n>>> Getting the OpenMBeanInfo for MBean:\n\t"
126: + openMBeanObjectName);
127: sleep(1000);
128: OpenMBeanInfo info = null;
129: try {
130: info = (OpenMBeanInfoSupport) server
131: .getMBeanInfo(openMBeanObjectName);
132: } catch (Exception e) {
133: echo("\t!!! Could not get OpenMBeanInfo object for "
134: + openMBeanObjectName + " !!!");
135: e.printStackTrace();
136: return;
137: }
138: echo("\nCLASSNAME : " + info.getClassName());
139: echo("\nDESCRIPTION : " + info.getDescription());
140:
141: // Attributes
142: //
143: echo("\n\n----ATTRIBUTES----\n");
144: MBeanAttributeInfo[] attrInfos = info.getAttributes();
145: if (attrInfos.length > 0) {
146: OpenMBeanAttributeInfoSupport attrInfo;
147: for (int i = 0; i < attrInfos.length; i++) {
148: attrInfo = (OpenMBeanAttributeInfoSupport) attrInfos[i];
149: echo(" -- ATTRIBUTE NAME : " + attrInfo.getName()
150: + "\tREAD: " + attrInfo.isReadable()
151: + "\tWRITE: " + attrInfo.isWritable());
152: echo(" DESCRIPTION : "
153: + attrInfo.getDescription());
154: echo(" JAVA TYPE : " + attrInfo.getType());
155: echo(" OPEN TYPE : "
156: + attrInfo.getOpenType().getClass().getName());
157: echo(" DEFAULT VALUE : "
158: + attrInfo.getDefaultValue());
159: echo(" LEGAL VALUES : "
160: + attrInfo.getLegalValues());
161: echo(" MIN VALUE : " + attrInfo.getMinValue());
162: echo(" MAX VALUE : " + attrInfo.getMaxValue());
163: echo("");
164: }
165: } else
166: echo(" ** No attributes **\n");
167:
168: // Constructors
169: //
170: echo("\n\n----CONSTRUCTORS----\n");
171: MBeanConstructorInfo[] constrInfos = info.getConstructors();
172: if (constrInfos.length > 0) {
173: OpenMBeanConstructorInfoSupport constrInfo;
174: for (int i = 0; i < constrInfos.length; i++) {
175: constrInfo = (OpenMBeanConstructorInfoSupport) constrInfos[i];
176: echo(" -- CONSTRUCTOR NAME : " + constrInfo.getName());
177: echo(" DESCRIPTION : "
178: + constrInfo.getDescription());
179: MBeanParameterInfo[] paramInfos = constrInfo
180: .getSignature();
181: echo(" PARAMETERS : " + paramInfos.length
182: + " parameter"
183: + (paramInfos.length > 1 ? "s\n" : ""));
184: if (paramInfos.length > 0) {
185: OpenMBeanParameterInfoSupport paramInfo;
186: for (int j = 0; j < paramInfos.length; j++) {
187: paramInfo = (OpenMBeanParameterInfoSupport) paramInfos[j];
188: echo(" ." + (j + 1) + ". PARAMETER NAME : "
189: + paramInfo.getName());
190: echo(" DESCRIPTION : "
191: + paramInfo.getDescription());
192: echo(" JAVA TYPE : "
193: + paramInfo.getType());
194: echo(" OPEN TYPE : "
195: + paramInfo.getOpenType().getClass()
196: .getName());
197: echo(" DEFAULT VALUE : "
198: + paramInfo.getDefaultValue());
199: echo(" LEGAL VALUES : "
200: + paramInfo.getLegalValues());
201: echo(" MIN VALUE : "
202: + paramInfo.getMinValue());
203: echo(" MAX VALUE : "
204: + paramInfo.getMaxValue());
205: echo("");
206: }
207: }
208: echo("");
209: }
210: } else
211: echo(" ** No constructors **\n");
212:
213: // Operations
214: //
215: echo("\n\n----OPERATIONS----\n");
216: MBeanOperationInfo[] opInfos = info.getOperations();
217: if (opInfos.length > 0) {
218: OpenMBeanOperationInfoSupport opInfo;
219: for (int i = 0; i < opInfos.length; i++) {
220: opInfo = (OpenMBeanOperationInfoSupport) opInfos[i];
221: echo(" -- OPERATION NAME : " + opInfo.getName());
222: echo(" DESCRIPTION : "
223: + opInfo.getDescription());
224: echo(" RETURN JAVA TYPE : " + opInfo.getReturnType());
225: echo(" RETURN OPEN TYPE : "
226: + opInfo.getReturnOpenType().getClass()
227: .getName());
228: MBeanParameterInfo[] paramInfos = opInfo.getSignature();
229: echo(" PARAMETERS : " + paramInfos.length
230: + " parameter"
231: + (paramInfos.length > 1 ? "s\n" : ""));
232: if (paramInfos.length > 0) {
233: OpenMBeanParameterInfoSupport paramInfo;
234: for (int j = 0; j < paramInfos.length; j++) {
235: paramInfo = (OpenMBeanParameterInfoSupport) paramInfos[j];
236: echo(" ." + (j + 1) + ". PARAMETER NAME : "
237: + paramInfo.getName());
238: echo(" DESCRIPTION : "
239: + paramInfo.getDescription());
240: echo(" JAVA TYPE : "
241: + paramInfo.getType());
242: echo(" OPEN TYPE : "
243: + paramInfo.getOpenType().getClass()
244: .getName());
245: echo(" DEFAULT VALUE : "
246: + paramInfo.getDefaultValue());
247: echo(" LEGAL VALUES : "
248: + paramInfo.getLegalValues());
249: echo(" MIN VALUE : "
250: + paramInfo.getMinValue());
251: echo(" MAX VALUE : "
252: + paramInfo.getMaxValue());
253: echo("");
254: }
255: }
256: echo("");
257: }
258: } else
259: echo(" ** No operations **\n");
260:
261: // Notifications
262: //
263: echo("\n\n----NOTIFICATIONS----\n");
264: MBeanNotificationInfo[] notifInfos = info.getNotifications();
265: if (notifInfos.length > 0) {
266: for (int i = 0; i < notifInfos.length; i++) {
267: echo(" -- NOTIFICATION NAME : "
268: + notifInfos[i].getName());
269: echo(" DESCRIPTION : "
270: + notifInfos[i].getDescription());
271: echo("");
272: }
273: } else
274: echo(" ** No notifications **\n");
275: }
276:
277: private static void echo(String msg) {
278: System.out.println(msg);
279: }
280:
281: private static void sleep(int millis) {
282: try {
283: Thread.sleep(millis);
284: } catch (InterruptedException e) {
285: return;
286: }
287: }
288:
289: private static void waitForEnterPressed() {
290: try {
291: echo("\nPress Enter to continue...");
292: boolean done = false;
293: while (!done) {
294: char ch = (char) System.in.read();
295: if (ch < 0 || ch == '\n') {
296: done = true;
297: }
298: }
299: echo("");
300: } catch (IOException e) {
301: e.printStackTrace();
302: }
303: }
304:
305: }
|