0001: /* JFox, the OpenSource J2EE Application Server
0002: *
0003: * Copyright (C) 2002 huihoo.org
0004: * Distributable under GNU LGPL license
0005: * See the GNU Lesser General Public License for more details.
0006: */
0007:
0008: package example.jmx.relation;
0009:
0010: import java.util.ArrayList;
0011: import java.util.Iterator;
0012: import java.util.HashMap;
0013: import java.io.IOException;
0014: import java.net.UnknownHostException;
0015: import javax.management.MBeanServer;
0016: import javax.management.ObjectName;
0017: import javax.management.MBeanException;
0018: import javax.management.MBeanServerFactory;
0019: import javax.management.MBeanOperationInfo;
0020: import javax.management.MBeanNotificationInfo;
0021: import javax.management.MBeanInfo;
0022: import javax.management.MBeanAttributeInfo;
0023: import javax.management.MBeanConstructorInfo;
0024: import javax.management.Attribute;
0025: import javax.management.relation.RelationType;
0026: import javax.management.relation.RoleResult;
0027: import javax.management.relation.RoleList;
0028: import javax.management.relation.RoleInfo;
0029: import javax.management.relation.Role;
0030: import javax.management.relation.RoleUnresolvedList;
0031:
0032: import org.huihoo.jfox.jmx.adaptor.http.HtmlAdaptorServer;
0033:
0034: /**
0035: *
0036: * @author <a href="mailto:young_yy@hotmail.org">Young Yang</a>
0037: */
0038:
0039: public class RelationAgent {
0040: /*
0041: * ------------------------------------------
0042: * PRIVATE VARIABLES
0043: * ------------------------------------------
0044: */
0045:
0046: private MBeanServer server = null;
0047:
0048: public RelationAgent() {
0049:
0050: // CREATE the MBeanServer
0051: //
0052: echo("\n\tCREATE the MBeanServer.");
0053: server = MBeanServerFactory.createMBeanServer();
0054: }
0055:
0056: /*
0057: * ------------------------------------------
0058: * PUBLIC METHODS
0059: * ------------------------------------------
0060: */
0061:
0062: public static void main(String[] args) {
0063:
0064: // START
0065: //
0066: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0067: echo("\n>>> START of Relation Service example");
0068: echo("\n>>> CREATE the agent...");
0069: RelationAgent agent = new RelationAgent();
0070: echo("\npress <Enter> to continue...\n");
0071: waitForEnterPressed();
0072:
0073: echo("\n>>> CREATE the HtmlAdaptor...");
0074: agent.createHtmlAdaptor();
0075:
0076: // DO THE DEMO
0077: //
0078: agent.doSimpleDemo();
0079:
0080: // END
0081: //
0082: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0083: echo("\n>>> END of the Relation Service example:\n");
0084: String localHost = null;
0085: try {
0086: localHost = java.net.InetAddress.getLocalHost()
0087: .getHostName();
0088: } catch (UnknownHostException e) {
0089: localHost = "localhost";
0090: }
0091: echo("\n\tpress <Enter> to doStop the agent...\n");
0092: waitForEnterPressed();
0093: System.exit(0);
0094: }
0095:
0096: /*
0097: * ------------------------------------------
0098: * PRIVATE METHODS
0099: * ------------------------------------------
0100: */
0101:
0102: private void doSimpleDemo() {
0103:
0104: // build ObjectName of RelationService
0105: String relServClassName = "javax.management.relation.RelationService";
0106: ObjectName relServObjName = createMBeanName(relServClassName, 1);
0107:
0108: // doCreate and register the Relation Service
0109: // Flag set for immediate purge
0110: //
0111: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0112: createRelationService(relServObjName, relServClassName);
0113: echo("\npress <Enter> to continue...\n");
0114: waitForEnterPressed();
0115:
0116: // get and display the management information exposed by the relation
0117: // service
0118: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0119: printMBeanInfo(relServObjName, relServClassName);
0120: echo("\npress <Enter> to continue...\n");
0121: waitForEnterPressed();
0122:
0123: // doCreate two role infos
0124: // - container:
0125: // - element class: example.jmx.standard.SimpleStandard
0126: // - read: y, write: y
0127: // - multiplicity: 1,1
0128: // - contained:
0129: // - element class: example.jmx.standard.SimpleStandard
0130: // - read: y, write: y
0131: // - multiplicity: 0,n
0132: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0133: RoleInfo[] roleInfoArray = new RoleInfo[2];
0134: String role1Name = "container";
0135: roleInfoArray[0] = createRoleInfo(role1Name,
0136: "example.jmx.standard.SimpleStandard", true, true, 1,
0137: 1, null);
0138: String role2Name = "contained";
0139: roleInfoArray[1] = createRoleInfo(role2Name,
0140: "example.jmx.standard.SimpleStandard", true, true, 0,
0141: -1, null);
0142: echo("\npress <Enter> to continue...\n");
0143: waitForEnterPressed();
0144:
0145: // doCreate invalid relation type: no name
0146: // echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0147: // createRelationType(null, roleInfoArray, relServObjName);
0148: // echo("\npress <Enter> to continue...\n");
0149: // waitForEnterPressed();
0150:
0151: // doCreate a relation type with those role infos
0152: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0153: String relTypeName = "myRelationType";
0154: createRelationType(relTypeName, roleInfoArray, relServObjName);
0155: echo("\npress <Enter> to continue...\n");
0156: waitForEnterPressed();
0157:
0158: // print info about the known relation types
0159: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0160: printRelationTypeInfo(relServObjName);
0161: echo("\npress <Enter> to continue...\n");
0162: waitForEnterPressed();
0163:
0164: // build six simple MBean ObjectNames
0165: //
0166: String mbeanClassName = "example.jmx.standard.SimpleStandard";
0167: ObjectName mbeanObjectName1 = createMBeanName(mbeanClassName, 1);
0168: ObjectName mbeanObjectName2 = createMBeanName(mbeanClassName, 2);
0169: ObjectName mbeanObjectName3 = createMBeanName(mbeanClassName, 3);
0170: ObjectName mbeanObjectName4 = createMBeanName(mbeanClassName, 4);
0171: ObjectName mbeanObjectName5 = createMBeanName(mbeanClassName, 5);
0172: ObjectName mbeanObjectName6 = createMBeanName(mbeanClassName, 6);
0173:
0174: // doCreate and register six simple MBeans with given ObjectNames
0175: //
0176: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0177: createSimpleBean(mbeanObjectName1, mbeanClassName);
0178: createSimpleBean(mbeanObjectName2, mbeanClassName);
0179: createSimpleBean(mbeanObjectName3, mbeanClassName);
0180: createSimpleBean(mbeanObjectName4, mbeanClassName);
0181: createSimpleBean(mbeanObjectName5, mbeanClassName);
0182: createSimpleBean(mbeanObjectName6, mbeanClassName);
0183: echo("\npress <Enter> to continue...\n");
0184: waitForEnterPressed();
0185:
0186: // doCreate roles
0187: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0188: ArrayList role1Value = new ArrayList();
0189: role1Value.add(mbeanObjectName1);
0190: //
0191: // Invalid case: empty role value, where role info expects at least one
0192: // reference.
0193: // Will throw a MBeanException->InvalidRoleValueException
0194: // role1Value = new ArrayList();
0195: //
0196: Role role1 = createRole(role1Name, role1Value);
0197: ArrayList role2Value = new ArrayList();
0198: role2Value.add(mbeanObjectName2);
0199: //
0200: // Another invalid case: reference a MBean not registered
0201: // Will throw a MBeanException->InvalidRoleValueException
0202: // ObjectName mbeanObjectName7 = createMBeanName(mbeanClassName, 7);
0203: // role2Value.add(mbeanObjectName7);
0204: //
0205: Role role2 = createRole(role2Name, role2Value);
0206: RoleList roleList1 = new RoleList();
0207: roleList1.add(role1);
0208: roleList1.add(role2);
0209: echo("\npress <Enter> to continue...\n");
0210: waitForEnterPressed();
0211:
0212: // doCreate an internal relation
0213: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0214: String relId1 = relTypeName + "_internal_1";
0215: //
0216: // Invalid relation, as container role is expected to have at least one
0217: // reference
0218: // Will throw a MBeanException wrapping an InvalidRoleValueException
0219: // roleList1 = null;
0220: //
0221: createRelation(relId1, relTypeName, roleList1, relServObjName);
0222: echo("\npress <Enter> to continue...\n");
0223: waitForEnterPressed();
0224:
0225: // print info about all relations known by the Relation Service
0226: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0227: printAllRelationInfo(relServObjName);
0228:
0229: // do queries
0230: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0231: //
0232: // Invalid case: query for a MBean not referenced anywhere (here it
0233: // does not even exist)
0234: // will return an empty HashMap
0235: // ObjectName mbeanObjectName7 = createMBeanName(mbeanClassName, 7);
0236: // doQueries(mbeanObjectName7,
0237: // relTypeName,
0238: // "container",
0239: // relServObjName);
0240: //
0241: doQueries(mbeanObjectName2, relTypeName, "contained",
0242: relServObjName);
0243: echo("\npress <Enter> to continue...\n");
0244: waitForEnterPressed();
0245:
0246: // add a reference in a role
0247: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0248: //
0249: // Invalid case: add a reference to exceed maximum degree
0250: // Will throw a MBeanException wrapping an InvalidRoleValueException
0251: // singleRoleIndirectAdd(relId1,
0252: // "container",
0253: // mbeanObjectName3,
0254: // relServObjName);
0255: //
0256: // Invalid case: try to update a role whch does not exist
0257: // Will throw a RoleNotFoundException
0258: // singleRoleIndirectAdd(relId1,
0259: // "foo",
0260: // mbeanObjectName3,
0261: // relServObjName);
0262: //
0263: singleRoleIndirectAdd(relId1, "contained", mbeanObjectName3,
0264: relServObjName);
0265: echo("\npress <Enter> to continue...\n");
0266: waitForEnterPressed();
0267:
0268: // update several roles
0269: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0270: ArrayList role1List = new ArrayList();
0271: role1List.add(mbeanObjectName4);
0272: //
0273: // Invalid case: role exceeds maximum cardinality
0274: // A RoleUnresolved element is returned in the RoleResult
0275: // role1List.add(mbeanObjectName5);
0276: //
0277: Role role1_1 = new Role(role1Name, role1List);
0278: ArrayList role2List = new ArrayList();
0279: role2List.add(mbeanObjectName5);
0280: role2List.add(mbeanObjectName6);
0281: Role role2_1 = new Role(role2Name, role2List);
0282: RoleList roleList2 = new RoleList();
0283: roleList2.add(role1_1);
0284: roleList2.add(role2_1);
0285: //
0286: // Invalid case: try to update a role which does not exist
0287: // the returned RoleResult will include a RoleUnresolved part
0288: // Role dummyRole = new Role("dummy", role2List);
0289: // roleList2.add(dummyRole);
0290: //
0291: multiRoleIndirectUpdate(relId1, roleList2, relServObjName);
0292: echo("\npress <Enter> to continue...\n");
0293: waitForEnterPressed();
0294:
0295: // print info about all relations known by the Relation Service
0296: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0297: printAllRelationInfo(relServObjName);
0298:
0299: // doCreate a relation MBean and add it in the Relation Service
0300: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0301: String relMBeanClassName = "example.jmx.relation.SimpleRelation";
0302: String relId2 = relTypeName + "_relMBean_2";
0303: //
0304: // Invalid case: add a relation with an id already used
0305: // relId2 = relId1;
0306: //
0307: // Another invalid case: null roleList
0308: // roleList1 = null;
0309: //
0310: // Another invalid case: add a role not described in relation type
0311: // ArrayList role3Value = new ArrayList();
0312: // Role role3 = createRole("dummy", role3Value);
0313: // roleList1.add(role3);
0314: //
0315: ObjectName relMBeanObjName1 = createMBeanName(
0316: relMBeanClassName, 2);
0317: createRelationMBean(relMBeanObjName1, relMBeanClassName,
0318: relId2, relTypeName, roleList1, relServObjName);
0319: echo("\npress <Enter> to continue...\n");
0320: waitForEnterPressed();
0321:
0322: // Invalid case: add as a reference a MBean not of the correct class.
0323: // Will throw a MBeanException->InvalidRoleValueException
0324: // singleRoleIndirectAdd(relId1,
0325: // "contained",
0326: // relMBeanObjName1,
0327: // relServObjName);
0328: //
0329:
0330: // use relation operations on relation MBean, and specific accessors
0331: // as isRelationMBean
0332: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0333: testRelationMBeanOperations(relMBeanObjName1);
0334: echo("\npress <Enter> to continue...\n");
0335: waitForEnterPressed();
0336: testRelationMBeanSpecificOperations(relId2, relServObjName,
0337: relMBeanObjName1);
0338: echo("\npress <Enter> to continue...\n");
0339: waitForEnterPressed();
0340:
0341: // add a reference in a role via the relation service
0342: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0343: //
0344: // Invalid case: add a reference to exceed maximum degree
0345: // Will throw a MBeanException wrapping an InvalidRoleValueException
0346: // singleRoleIndirectUpdate(relId1,
0347: // "container",
0348: // mbeanObjectName3,
0349: // relServObjName);
0350: //
0351: singleRoleIndirectAdd(relId2, "contained", mbeanObjectName3,
0352: relServObjName);
0353: echo("\npress <Enter> to continue...\n");
0354: waitForEnterPressed();
0355:
0356: // update several roles via the relation service
0357: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0358: multiRoleIndirectUpdate(relId2, roleList2, relServObjName);
0359: echo("\npress <Enter> to continue...\n");
0360: waitForEnterPressed();
0361:
0362: // print info about all relations known by the Relation Service
0363: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0364: printAllRelationInfo(relServObjName);
0365:
0366: // add a reference in a role of relation MBean using direct call
0367: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0368: singleRoleDirectAdd(relMBeanObjName1, "contained",
0369: mbeanObjectName3);
0370: echo("\npress <Enter> to continue...\n");
0371: waitForEnterPressed();
0372:
0373: // update several roles in relation MBean via direct call
0374: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0375: multiRoleDirectUpdate(relMBeanObjName1, roleList1);
0376: echo("\npress <Enter> to continue...\n");
0377: waitForEnterPressed();
0378:
0379: // print info about all relations known by the Relation Service
0380: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0381: printAllRelationInfo(relServObjName);
0382:
0383: // do queries
0384: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0385: doQueries(mbeanObjectName2, relTypeName, "contained",
0386: relServObjName);
0387: echo("\npress <Enter> to continue...\n");
0388: waitForEnterPressed();
0389:
0390: // unregistration of a referenced MBean in "contained"
0391: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0392: unregisterMBean(mbeanObjectName2);
0393: echo("The unregistration of this MBean which was referenced in a relation does not invalidate the relation.");
0394: echo("This because the minimum cardinality of the role is 0.");
0395: echo("So the role value has just to be updated");
0396: echo("\npress <Enter> to continue...\n");
0397: waitForEnterPressed();
0398:
0399: // do queries
0400: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0401: doQueries(mbeanObjectName1, relTypeName, "container",
0402: relServObjName);
0403: echo("\npress <Enter> to continue...\n");
0404: waitForEnterPressed();
0405:
0406: // remove the internal relation
0407: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0408: removeRelation(relId1, relServObjName);
0409: echo("\npress <Enter> to continue...\n");
0410: waitForEnterPressed();
0411:
0412: // print info about all relations known by the Relation Service
0413: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0414: printAllRelationInfo(relServObjName);
0415:
0416: // get all relations of given type
0417: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0418: printAllRelationIdsOfType(relTypeName, relServObjName);
0419: echo("\npress <Enter> to continue...\n");
0420: waitForEnterPressed();
0421:
0422: // remove the relation MBean from the relation service
0423: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0424: removeRelation(relId2, relServObjName);
0425: echo("This relation is a relation MBean. Removing it as a relation just removes it from the Relation Service.");
0426: echo("The MBean is not unregistered.");
0427: echo("\npress <Enter> to continue...\n");
0428: waitForEnterPressed();
0429:
0430: // get all relations of given type
0431: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0432: printAllRelationIdsOfType(relTypeName, relServObjName);
0433: echo("\npress <Enter> to continue...\n");
0434: waitForEnterPressed();
0435:
0436: // add again the relation MBean to the relation service
0437: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0438: addRelation(relMBeanObjName1, relServObjName);
0439: echo("\npress <Enter> to continue...\n");
0440: waitForEnterPressed();
0441:
0442: // get all relations of given type
0443: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0444: printAllRelationIdsOfType(relTypeName, relServObjName);
0445: echo("\npress <Enter> to continue...\n");
0446: waitForEnterPressed();
0447:
0448: // unregister relation MBean
0449: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0450: unregisterMBean(relMBeanObjName1);
0451: echo("Unregistering a MBean being a relation will also remove it from the Relation Service, as it cannot be accessed any longer.");
0452: echo("\npress <Enter> to continue...\n");
0453: waitForEnterPressed();
0454:
0455: // get all relations of given type
0456: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0457: printAllRelationIdsOfType(relTypeName, relServObjName);
0458: echo("\npress <Enter> to continue...\n");
0459: waitForEnterPressed();
0460:
0461: // doCreate a new internal relation
0462: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0463: role1Value = new ArrayList();
0464: role1Value.add(mbeanObjectName1);
0465: role1 = createRole(role1Name, role1Value);
0466: role2Value = new ArrayList();
0467: // do not use mbeanObjectName2, as it has been unregistered
0468: role2Value.add(mbeanObjectName3);
0469: role2 = createRole(role2Name, role2Value);
0470: roleList1 = new RoleList();
0471: roleList1.add(role1);
0472: roleList1.add(role2);
0473: String relId3 = relTypeName + "_internal_3";
0474: createRelation(relId3, relTypeName, roleList1, relServObjName);
0475: echo("\npress <Enter> to continue...\n");
0476: waitForEnterPressed();
0477:
0478: // unregistration of a referenced MBean in "container"
0479: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0480: unregisterMBean(mbeanObjectName1);
0481: echo("Unregistering this MBean will invalidate the relation as it was referenced in a role with a cardinality of 1,1.");
0482: echo("\npress <Enter> to continue...\n");
0483: waitForEnterPressed();
0484:
0485: // print info about all relations known by the Relation Service
0486: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0487: printAllRelationInfo(relServObjName);
0488: echo("\npress <Enter> to continue...\n");
0489: waitForEnterPressed();
0490:
0491: // re-doCreate internal relation
0492: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0493: role1Value = new ArrayList();
0494: // do not use mbeanObjectName1, unregistered
0495: role1Value.add(mbeanObjectName3);
0496: role1 = createRole(role1Name, role1Value);
0497: roleList1 = new RoleList();
0498: roleList1.add(role1);
0499: // role "contained" to be defaulted
0500: String relId4 = relTypeName + "_internal_4";
0501: createRelation(relId4, relTypeName, roleList1, relServObjName);
0502: echo("\npress <Enter> to continue...\n");
0503: waitForEnterPressed();
0504:
0505: // print info about all relations known by the Relation Service
0506: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0507: printAllRelationInfo(relServObjName);
0508:
0509: // remove a relation type
0510: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0511: removeRelationType(relTypeName, relServObjName);
0512: echo("\npress <Enter> to continue...\n");
0513: waitForEnterPressed();
0514:
0515: // print info about the known relation types
0516: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0517: printRelationTypeInfo(relServObjName);
0518: echo("\npress <Enter> to continue...\n");
0519: waitForEnterPressed();
0520:
0521: // print info about all relations known by the Relation Service
0522: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0523: printAllRelationInfo(relServObjName);
0524: echo("\npress <Enter> to continue...\n");
0525: waitForEnterPressed();
0526:
0527: // doCreate a user relation type
0528: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0529: String usrRelTypeName = "SimpleRelationType";
0530: SimpleRelationType usrRelType = new SimpleRelationType(
0531: "SimpleRelationType");
0532: addRelationType(usrRelType, relServObjName);
0533: echo("\npress <Enter> to continue...\n");
0534: waitForEnterPressed();
0535:
0536: // print info about the known relation types
0537: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0538: printRelationTypeInfo(relServObjName);
0539: echo("\npress <Enter> to continue...\n");
0540: waitForEnterPressed();
0541:
0542: // doCreate roles
0543: // mbeanObjectName1 and mbeanObjectName2 unregistered
0544: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0545: ArrayList primaryValue = new ArrayList();
0546: primaryValue.add(mbeanObjectName3);
0547: primaryValue.add(mbeanObjectName4);
0548: Role primaryRole = createRole("primary", primaryValue);
0549: ArrayList secondaryValue = new ArrayList();
0550: secondaryValue.add(mbeanObjectName5);
0551: secondaryValue.add(mbeanObjectName6);
0552: Role secondaryRole = createRole("secondary", secondaryValue);
0553: RoleList otherRoleList = new RoleList();
0554: otherRoleList.add(primaryRole);
0555: otherRoleList.add(secondaryRole);
0556: echo("\npress <Enter> to continue...\n");
0557: waitForEnterPressed();
0558:
0559: // doCreate an internal relation
0560: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0561: String relId5 = usrRelTypeName + "_internal_1";
0562: createRelation(relId5, usrRelTypeName, otherRoleList,
0563: relServObjName);
0564: echo("\npress <Enter> to continue...\n");
0565: waitForEnterPressed();
0566:
0567: // print info about all relations known by the Relation Service
0568: echo("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
0569: printAllRelationInfo(relServObjName);
0570: }
0571:
0572: private void createSimpleBean(ObjectName mbeanObjectName,
0573: String mbeanName) {
0574:
0575: echo("\n>>> CREATE THE MBEAN " + mbeanName
0576: + " within the MBeanServer:");
0577: String mbeanClassName = mbeanName;
0578: echo("\tOBJECT NAME = " + mbeanObjectName);
0579: try {
0580: server.createMBean(mbeanClassName, mbeanObjectName);
0581: } catch (Exception e) {
0582: echo("\t!!! Could not doCreate the " + mbeanName
0583: + " MBean !!!");
0584: printException(e);
0585: }
0586: }
0587:
0588: private void printMBeanInfo(ObjectName mbeanObjectName,
0589: String mbeanName) {
0590:
0591: echo("\n>>> GETTING THE MANAGEMENT INFORMATION for the "
0592: + mbeanName + " MBean");
0593: echo("\tusing the getMBeanInfo method of the MBeanServer");
0594: sleep(1000);
0595: MBeanInfo info = null;
0596: try {
0597: info = server.getMBeanInfo(mbeanObjectName);
0598: } catch (Exception e) {
0599: echo("\t!!! Could not get MBeanInfo object for "
0600: + mbeanName + " !!!");
0601: printException(e);
0602: }
0603: echo("\nCLASSNAME: \t" + info.getClassName());
0604: echo("\nDESCRIPTION: \t" + info.getDescription());
0605: echo("\nATTRIBUTES");
0606: MBeanAttributeInfo[] attrInfo = info.getAttributes();
0607: if (attrInfo.length > 0) {
0608: for (int i = 0; i < attrInfo.length; i++) {
0609: echo(" ** NAME: \t" + attrInfo[i].getName());
0610: echo(" DESCR: \t" + attrInfo[i].getDescription());
0611: echo(" TYPE: \t" + attrInfo[i].getType()
0612: + "\tREAD: " + attrInfo[i].isReadable()
0613: + "\tWRITE: " + attrInfo[i].isWritable());
0614: }
0615: } else
0616: echo(" ** No attributes **");
0617: echo("\nCONSTRUCTORS");
0618: MBeanConstructorInfo[] constrInfo = info.getConstructors();
0619: for (int i = 0; i < constrInfo.length; i++) {
0620: echo(" ** NAME: \t" + constrInfo[i].getName());
0621: echo(" DESCR: \t" + constrInfo[i].getDescription());
0622: echo(" PARAM: \t" + constrInfo[i].getSignature().length
0623: + " parameter(s)");
0624: }
0625: echo("\nOPERATIONS");
0626: MBeanOperationInfo[] opInfo = info.getOperations();
0627: if (opInfo.length > 0) {
0628: for (int i = 0; i < opInfo.length; i++) {
0629: echo(" ** NAME: \t" + opInfo[i].getName());
0630: echo(" DESCR: \t" + opInfo[i].getDescription());
0631: echo(" PARAM: \t" + opInfo[i].getSignature().length
0632: + " parameter(s)");
0633: }
0634: } else
0635: echo(" ** No operations ** ");
0636: echo("\nNOTIFICATIONS");
0637: MBeanNotificationInfo[] notifInfo = info.getNotifications();
0638: if (notifInfo.length > 0) {
0639: for (int i = 0; i < notifInfo.length; i++) {
0640: echo(" ** NAME: \t" + notifInfo[i].getName());
0641: echo(" DESCR: \t" + notifInfo[i].getDescription());
0642: }
0643: } else
0644: echo(" ** No notifications **");
0645: }
0646:
0647: private static void echo(String msg) {
0648: System.out.println(msg);
0649: }
0650:
0651: private static void sleep(int millis) {
0652: try {
0653: Thread.sleep(millis);
0654: } catch (InterruptedException e) {
0655: return;
0656: }
0657: }
0658:
0659: private static void waitForEnterPressed() {
0660: try {
0661: System.in.read();
0662: } catch (IOException e) {
0663: e.printStackTrace();
0664: }
0665: return;
0666: }
0667:
0668: // doCreate ObjectName
0669: private ObjectName createMBeanName(String theMBeanClassName,
0670: int theMBeanNbr) {
0671: ObjectName mbeanObjectName = null;
0672: try {
0673: String domain = server.getDefaultDomain();
0674: StringBuffer mbeanName = new StringBuffer(theMBeanClassName);
0675: mbeanName.append(theMBeanNbr);
0676: mbeanObjectName = new ObjectName(domain + ":type="
0677: + mbeanName);
0678: } catch (Exception e) {
0679: echo("\t!!! Could not doCreate the MBean ObjectName !!!");
0680: printException(e);
0681: }
0682: return mbeanObjectName;
0683: }
0684:
0685: // doCreate and register the relation service
0686: private void createRelationService(ObjectName theRelServObjName,
0687: String theRelServClassName) {
0688:
0689: echo("\n>>> CREATE THE RELATION SERVICE " + theRelServClassName
0690: + " within the MBeanServer:");
0691: echo("\tOBJECT NAME = " + theRelServObjName);
0692: try {
0693: Object[] params = new Object[1];
0694: params[0] = new Boolean(true);
0695: String[] signature = new String[1];
0696: signature[0] = "boolean";
0697: server.createMBean(theRelServClassName, theRelServObjName,
0698: params, signature);
0699: } catch (Exception e) {
0700: echo("\t!!! Could not doCreate the " + theRelServClassName
0701: + " MBean !!!");
0702: printException(e);
0703: }
0704: }
0705:
0706: // doCreate a role info
0707: private RoleInfo createRoleInfo(String theRoleName,
0708: String theRefMBeanClassName, boolean theIsReadable,
0709: boolean theIsWritable, int theMinDegree, int theMaxDegree,
0710: String theDesc) {
0711: echo("\n>>> CREATE THE ROLE INFO " + theRoleName);
0712: RoleInfo roleInfo = null;
0713: try {
0714: roleInfo = new RoleInfo(theRoleName, theRefMBeanClassName,
0715: theIsReadable, theIsWritable, theMinDegree,
0716: theMaxDegree, theDesc);
0717: echo("\n\t" + roleInfo.toString());
0718:
0719: } catch (Exception e) {
0720: echo("\t!!! Could not doCreate the role info "
0721: + theRoleName + " !!!");
0722: printException(e);
0723: }
0724: return roleInfo;
0725: }
0726:
0727: // doCreate a relation type
0728: private void createRelationType(String theRelTypeName,
0729: RoleInfo[] theRoleInfoArray, ObjectName theRelServObjName) {
0730: echo("\n>>> CREATE THE RELATION TYPE " + theRelTypeName);
0731: try {
0732: Object[] params = new Object[2];
0733: params[0] = theRelTypeName;
0734: params[1] = theRoleInfoArray;
0735: String[] signature = new String[2];
0736: signature[0] = "java.lang.String";
0737: try {
0738: signature[1] = (theRoleInfoArray.getClass()).getName();
0739: } catch (Exception exc) {
0740: throw exc;
0741: }
0742: server.invoke(theRelServObjName, "createRelationType",
0743: params, signature);
0744: } catch (Exception e) {
0745: echo("\t!!! Could not doCreate the relation type "
0746: + theRelTypeName + " !!!");
0747: printException(e);
0748: }
0749: }
0750:
0751: // print info about the relation types
0752: private void printRelationTypeInfo(ObjectName relServObjName) {
0753: echo("\n>>> PRINT RELATION TYPE INFO about the relation types in Relation Service "
0754: + relServObjName);
0755: try {
0756: echo("\n-> Retrieve all relation types");
0757: ArrayList relTypeNameList = (ArrayList) (server
0758: .getAttribute(relServObjName,
0759: "AllRelationTypeNames"));
0760:
0761: for (Iterator relTypeNameIter = relTypeNameList.iterator(); relTypeNameIter
0762: .hasNext();) {
0763: String currRelTypeName = (String) (relTypeNameIter
0764: .next());
0765: echo("\n-> Print role info for relation type "
0766: + currRelTypeName);
0767: Object[] params2 = new Object[1];
0768: params2[0] = currRelTypeName;
0769: String[] signature2 = new String[1];
0770: signature2[0] = "java.lang.String";
0771: ArrayList roleInfoList = (ArrayList) (server.invoke(
0772: relServObjName, "getRoleInfos", params2,
0773: signature2));
0774: printList(roleInfoList);
0775: }
0776: } catch (Exception e) {
0777: echo("\t!!! Could not browse the relation types!!!");
0778: printException(e);
0779: }
0780: }
0781:
0782: // doCreate a role
0783: private Role createRole(String theRoleName, ArrayList theRoleValue) {
0784: echo("\n>>> CREATING ROLE " + theRoleName);
0785: Role role = null;
0786: try {
0787: role = new Role(theRoleName, theRoleValue);
0788: echo("\t" + role.toString());
0789: } catch (Exception e) {
0790: echo("\t!!! Could doCreate role " + theRoleName + "!!!");
0791: printException(e);
0792: }
0793: return role;
0794: }
0795:
0796: // doCreate an internal relation
0797: private void createRelation(String theRelId, String theRelTypeName,
0798: RoleList theRoleList, ObjectName theRelServObjName) {
0799: echo("\n>>> CREATE INTERNAL RELATION " + theRelId + " of type "
0800: + theRelTypeName);
0801: try {
0802: Object[] params = new Object[3];
0803: params[0] = theRelId;
0804: params[1] = theRelTypeName;
0805: params[2] = theRoleList;
0806: String[] signature = new String[3];
0807: signature[0] = "java.lang.String";
0808: signature[1] = "java.lang.String";
0809: signature[2] = "javax.management.relation.RoleList";
0810: server.invoke(theRelServObjName, "createRelation", params,
0811: signature);
0812: } catch (Exception e) {
0813: echo("\t!!! Could not doCreate the relation " + theRelId
0814: + "!!!");
0815: printException(e);
0816: }
0817: }
0818:
0819: // print info about all relations handled by given Relation Service
0820: private void printAllRelationInfo(ObjectName theRelServObjName) {
0821: echo("\n>>> PRINT ALL RELATIONS in relation service "
0822: + theRelServObjName);
0823: try {
0824: ArrayList relIdList = (ArrayList) (server.getAttribute(
0825: theRelServObjName, "AllRelationIds"));
0826:
0827: for (Iterator relIdIter = relIdList.iterator(); relIdIter
0828: .hasNext();) {
0829: String currRelId = (String) (relIdIter.next());
0830: printRelationInfo(currRelId, theRelServObjName);
0831:
0832: echo("\npress <Enter> to continue...\n");
0833: waitForEnterPressed();
0834: }
0835: } catch (Exception e) {
0836: echo("\t!!! Could not browse relations of relation service "
0837: + theRelServObjName + "!!!");
0838: printException(e);
0839: }
0840: }
0841:
0842: // print info about given relation
0843: private void printRelationInfo(String theRelId,
0844: ObjectName theRelServObjName) {
0845: echo("\n>>> PRINT RELATION " + theRelId
0846: + " accessed via relation service " + theRelServObjName);
0847:
0848: testRelationIndirectAccess(false, theRelId, theRelServObjName,
0849: null);
0850: }
0851:
0852: private void testRelationIndirectAccess(boolean theRelMBeanFlg,
0853: String theRelId, ObjectName theRelServObjName,
0854: ObjectName theRelMBeanName) {
0855: try {
0856: echo("\n-> Retrieve all roles");
0857: // retrieves all roles
0858: ObjectName theName1 = null;
0859: Object[] params1 = new Object[] { theRelId };
0860: String[] signature1 = new String[] { "java.lang.String" };
0861: RoleResult roleResult = null;
0862: if (!theRelMBeanFlg) {
0863: theName1 = theRelServObjName;
0864: roleResult = (RoleResult) (server.invoke(theName1,
0865: "getAllRoles", params1, signature1));
0866: } else {
0867: theName1 = theRelMBeanName;
0868: roleResult = (RoleResult) (server.getAttribute(
0869: theName1, "AllRoles"));
0870: }
0871: printRoleResult(roleResult);
0872:
0873: RoleList roleList1 = roleResult.getRoles();
0874: String[] roleNameArray = new String[(roleList1.size())];
0875: int i = 0;
0876: for (Iterator roleIter = roleList1.iterator(); roleIter
0877: .hasNext();) {
0878: Role currRole = (Role) (roleIter.next());
0879: String currRoleName = currRole.getRoleName();
0880:
0881: roleNameArray[i] = currRoleName;
0882: i++;
0883:
0884: // Check that the role can be accessed directly
0885: echo("\n-> Checking access via getRole() on role "
0886: + currRoleName);
0887: ObjectName theName2 = null;
0888: Object[] params2 = null;
0889: String[] signature2 = null;
0890: if (!theRelMBeanFlg) {
0891: theName2 = theRelServObjName;
0892: params2 = new Object[2];
0893: params2[0] = theRelId;
0894: params2[1] = currRoleName;
0895: signature2 = new String[2];
0896: signature2[0] = "java.lang.String";
0897: signature2[1] = "java.lang.String";
0898: } else {
0899: theName2 = theRelMBeanName;
0900: params2 = new Object[1];
0901: params2[0] = currRoleName;
0902: signature2 = new String[1];
0903: signature2[0] = "java.lang.String";
0904: }
0905: ArrayList currRoleValue = (ArrayList) (server.invoke(
0906: theName2, "getRole", params2, signature2));
0907: echo("\n\tvalue retrieved via getRole on role "
0908: + currRoleName);
0909: printList(currRoleValue);
0910:
0911: // Print number of MBeans referenced in role
0912: echo("\n-> Print number of MBeans referenced in "
0913: + currRoleName);
0914: Integer currRoleRefNbr = (Integer) (server.invoke(
0915: theName2, "getRoleCardinality", params2,
0916: signature2));
0917: echo("\n\t" + currRoleRefNbr.intValue()
0918: + " referenced MBeans");
0919: }
0920:
0921: // Check getRoles()
0922: echo("\n-> Access via getRoles()");
0923: ObjectName theName3 = null;
0924: Object[] params3 = null;
0925: String[] signature3 = null;
0926: if (!theRelMBeanFlg) {
0927: theName3 = theRelServObjName;
0928: params3 = new Object[2];
0929: params3[0] = theRelId;
0930: params3[1] = roleNameArray;
0931: signature3 = new String[2];
0932: signature3[0] = "java.lang.String";
0933: signature3[1] = (roleNameArray.getClass()).getName();
0934: } else {
0935: theName3 = theRelMBeanName;
0936: params3 = new Object[1];
0937: params3[0] = roleNameArray;
0938: signature3 = new String[1];
0939: signature3[0] = (roleNameArray.getClass()).getName();
0940: }
0941: RoleResult roleRes = (RoleResult) (server.invoke(theName3,
0942: "getRoles", params3, signature3));
0943: printRoleResult(roleRes);
0944:
0945: // referenced MBeans
0946: echo("\n-> Referenced MBeans");
0947: HashMap refMBeanMap = null;
0948: if (!theRelMBeanFlg) {
0949: refMBeanMap = (HashMap) (server.invoke(theName1,
0950: "getReferencedMBeans", params1, signature1));
0951: } else {
0952: refMBeanMap = (HashMap) (server.getAttribute(theName1,
0953: "ReferencedMBeans"));
0954: }
0955: printMap(refMBeanMap);
0956:
0957: // relation type name
0958: echo("\n-> Relation type");
0959: String relTypeName = null;
0960: if (!theRelMBeanFlg) {
0961: relTypeName = (String) (server.invoke(theName1,
0962: "getRelationTypeName", params1, signature1));
0963: } else {
0964: relTypeName = (String) (server.getAttribute(theName1,
0965: "RelationTypeName"));
0966: }
0967: echo("\n\tRELATION TYPE is " + relTypeName);
0968: } catch (Exception e) {
0969: echo("\t!!! Could not print relation " + theRelId + "!!!");
0970: printException(e);
0971: }
0972: }
0973:
0974: // print info on a role
0975: private void printRole(Role theRole) {
0976: try {
0977: echo("\n\t" + theRole.toString());
0978: } catch (Exception e) {
0979: echo("\t!!! Could not print role " + theRole + "!!!");
0980: printException(e);
0981: }
0982: }
0983:
0984: // print a RoleResult
0985: private void printRoleResult(RoleResult theRoleResult) {
0986: echo("\tPrinting RoleResult");
0987: try {
0988: RoleList roleList = theRoleResult.getRoles();
0989: echo("\t-> Printing the RoleList");
0990: printList(roleList);
0991: RoleUnresolvedList roleUnresList = theRoleResult
0992: .getRolesUnresolved();
0993: echo("\t-> Printing the RoleUnresolvedList");
0994: printList(roleUnresList);
0995: } catch (Exception e) {
0996: echo("\t!!! Could not print RoleResult!!!");
0997: printException(e);
0998: }
0999: }
1000:
1001: // execute queries
1002: private void doQueries(ObjectName theMBeanObjName,
1003: String theRelTypeName, String theRoleName,
1004: ObjectName theRelServObjName) {
1005: echo("\n>>> PERFORMING QUERIES to retrieve where the MBean "
1006: + theMBeanObjName
1007: + " is referenced and to which MBeans it is associated.");
1008: try {
1009: echo("\n-> Referencing relations");
1010: echo("\n\tFIRST, only looking with the MBean");
1011: Object[] params1 = new Object[3];
1012: params1[0] = theMBeanObjName;
1013: params1[1] = null;
1014: params1[2] = null;
1015: String[] signature1 = new String[3];
1016: signature1[0] = "javax.management.ObjectName";
1017: signature1[1] = "java.lang.String";
1018: signature1[2] = "java.lang.String";
1019: HashMap refRelMap1 = (HashMap) (server.invoke(
1020: theRelServObjName, "findReferencingRelations",
1021: params1, signature1));
1022: printMap(refRelMap1);
1023:
1024: echo("\n\tSECOND, MBean + relation type");
1025: Object[] params2 = new Object[3];
1026: params2[0] = theMBeanObjName;
1027: params2[1] = theRelTypeName;
1028: params2[2] = null;
1029: HashMap refRelMap2 = (HashMap) (server.invoke(
1030: theRelServObjName, "findReferencingRelations",
1031: params2, signature1));
1032: printMap(refRelMap2);
1033:
1034: echo("\n\tTHIRD, MBean + role name");
1035: Object[] params3 = new Object[3];
1036: params3[0] = theMBeanObjName;
1037: params3[1] = null;
1038: params3[2] = theRoleName;
1039: HashMap refRelMap3 = (HashMap) (server.invoke(
1040: theRelServObjName, "findReferencingRelations",
1041: params3, signature1));
1042: printMap(refRelMap3);
1043:
1044: echo("\n\tTHEN, MBean + relation type + role name");
1045: Object[] params4 = new Object[3];
1046: params4[0] = theMBeanObjName;
1047: params4[1] = theRelTypeName;
1048: params4[2] = theRoleName;
1049: HashMap refRelMap4 = (HashMap) (server.invoke(
1050: theRelServObjName, "findReferencingRelations",
1051: params4, signature1));
1052: printMap(refRelMap4);
1053:
1054: echo("\n");
1055:
1056: echo("\n-> Associated MBeans");
1057: echo("\n\tFIRST, only looking with the MBean");
1058: Object[] params5 = new Object[3];
1059: params5[0] = theMBeanObjName;
1060: params5[1] = null;
1061: params5[2] = null;
1062: HashMap refRelMap5 = (HashMap) (server.invoke(
1063: theRelServObjName, "findAssociatedMBeans", params5,
1064: signature1));
1065: printMap(refRelMap5);
1066:
1067: echo("\n\tSECOND, MBean + relation type");
1068: Object[] params6 = new Object[3];
1069: params6[0] = theMBeanObjName;
1070: params6[1] = theRelTypeName;
1071: params6[2] = null;
1072: HashMap refRelMap6 = (HashMap) (server.invoke(
1073: theRelServObjName, "findAssociatedMBeans", params2,
1074: signature1));
1075: printMap(refRelMap6);
1076:
1077: echo("\n\tTHIRD, MBean + role name");
1078: Object[] params7 = new Object[3];
1079: params7[0] = theMBeanObjName;
1080: params7[1] = null;
1081: params7[2] = theRoleName;
1082: HashMap refRelMap7 = (HashMap) (server.invoke(
1083: theRelServObjName, "findAssociatedMBeans", params3,
1084: signature1));
1085: printMap(refRelMap7);
1086:
1087: echo("\n\tTHEN, MBean + relation type + role name");
1088: Object[] params8 = new Object[3];
1089: params8[0] = theMBeanObjName;
1090: params8[1] = theRelTypeName;
1091: params8[2] = theRoleName;
1092: HashMap refRelMap8 = (HashMap) (server.invoke(
1093: theRelServObjName, "findAssociatedMBeans", params8,
1094: signature1));
1095: printMap(refRelMap8);
1096: } catch (Exception e) {
1097: echo("\t!!! Could not do queries!!!");
1098: printException(e);
1099: }
1100: }
1101:
1102: // print a list
1103: private void printList(ArrayList theList) {
1104: if (theList == null) {
1105: return;
1106: }
1107: StringBuffer listDesc = new StringBuffer();
1108: for (Iterator iter = theList.iterator(); iter.hasNext();) {
1109: listDesc.append("\t");
1110: Object currObj = iter.next();
1111: listDesc.append(currObj.toString());
1112: if (iter.hasNext()) {
1113: listDesc.append(",\n");
1114: }
1115: }
1116: echo(listDesc.toString());
1117: return;
1118: }
1119:
1120: // print a map with keys, and values being ArrayList
1121: private void printMap(HashMap theMap) {
1122: if (theMap == null) {
1123: return;
1124: }
1125: for (Iterator keyIter = (theMap.keySet()).iterator(); keyIter
1126: .hasNext();) {
1127: Object currObj = keyIter.next();
1128: echo("\t" + currObj.toString());
1129: echo("\tvalue:");
1130: ArrayList currVal = (ArrayList) (theMap.get(currObj));
1131: printList(currVal);
1132: }
1133: return;
1134: }
1135:
1136: // Add a reference in a role in a relation via the relation service
1137: private void singleRoleIndirectAdd(String theRelId,
1138: String theRoleName, ObjectName theRefToAdd,
1139: ObjectName theRelServObjName) {
1140: echo("\n>>> UPDATE ROLE VIA RELATION SERVICE: add reference to "
1141: + theRefToAdd
1142: + " in role "
1143: + theRoleName
1144: + " in relation " + theRelId);
1145: try {
1146: // First get the role value
1147: echo("\n-> FIRST, get role value");
1148: Object[] params1 = new Object[2];
1149: params1[0] = theRelId;
1150: params1[1] = theRoleName;
1151: String[] signature1 = new String[2];
1152: signature1[0] = "java.lang.String";
1153: signature1[1] = "java.lang.String";
1154: ArrayList oldRoleValue = (ArrayList) (server.invoke(
1155: theRelServObjName, "getRole", params1, signature1));
1156:
1157: ArrayList newValue = (ArrayList) (oldRoleValue.clone());
1158: newValue.add(theRefToAdd);
1159: echo("\n-> THEN update the value");
1160: Role newRole = new Role(theRoleName, newValue);
1161: Object[] params2 = new Object[2];
1162: params2[0] = theRelId;
1163: params2[1] = newRole;
1164: String[] signature2 = new String[2];
1165: signature2[0] = "java.lang.String";
1166: signature2[1] = "javax.management.relation.Role";
1167: server.invoke(theRelServObjName, "setRole", params2,
1168: signature2);
1169:
1170: echo("\n-> THEN check the update");
1171: ArrayList newRoleValue = (ArrayList) (server.invoke(
1172: theRelServObjName, "getRole", params1, signature1));
1173: echo("\tnew value:");
1174: printList(newRoleValue);
1175: } catch (Exception e) {
1176: echo("\t!!! Could not do update a role in relation "
1177: + theRelId + "!!!");
1178: printException(e);
1179: }
1180: }
1181:
1182: // update several roles in a relation via the relation service
1183: private void multiRoleIndirectUpdate(String theRelId,
1184: RoleList theRoleList, ObjectName theRelServObjName) {
1185: echo("\n>>> UPDATE ROLES VIA RELATION SERVICE: in relation "
1186: + theRelId);
1187: try {
1188: Object[] params = new Object[2];
1189: params[0] = theRelId;
1190: params[1] = theRoleList;
1191: String[] signature = new String[2];
1192: signature[0] = "java.lang.String";
1193: signature[1] = "javax.management.relation.RoleList";
1194: RoleResult result = (RoleResult) (server.invoke(
1195: theRelServObjName, "setRoles", params, signature));
1196: printRoleResult(result);
1197: } catch (Exception e) {
1198: echo("\t!!! Could not update several roles in relation "
1199: + theRelId + "!!!");
1200: printException(e);
1201: }
1202: }
1203:
1204: // Add a reference in a role in a relation MBean via direct calls
1205: private void singleRoleDirectAdd(ObjectName theRelMBeanName,
1206: String theRoleName, ObjectName theRefToAdd) {
1207: echo("\n>>> UPDATE ROLE IN RELATION MBEAN: add reference to "
1208: + theRefToAdd + " in role " + theRoleName);
1209: try {
1210: // First get the role value
1211: echo("\n-> FIRST, get role value");
1212: Object[] params1 = new Object[1];
1213: params1[0] = theRoleName;
1214: String[] signature1 = new String[1];
1215: signature1[0] = "java.lang.String";
1216: ArrayList oldRoleValue = (ArrayList) (server.invoke(
1217: theRelMBeanName, "getRole", params1, signature1));
1218:
1219: ArrayList newValue = (ArrayList) (oldRoleValue.clone());
1220: newValue.add(theRefToAdd);
1221: echo("\n-> THEN update the value");
1222: Role newRole = new Role(theRoleName, newValue);
1223: server.setAttribute(theRelMBeanName, new Attribute("Role",
1224: newRole));
1225:
1226: echo("\n-> THEN check the update");
1227: ArrayList newRoleValue = (ArrayList) (server.invoke(
1228: theRelMBeanName, "getRole", params1, signature1));
1229: echo("\tnew value:");
1230: printList(newRoleValue);
1231: } catch (Exception e) {
1232: echo("\t!!! Could not update a role in relation "
1233: + theRelMBeanName + "!!!");
1234: printException(e);
1235: }
1236: }
1237:
1238: // update several roles in a relation MBean via direct call
1239: private void multiRoleDirectUpdate(ObjectName theRelMBeanObjName,
1240: RoleList theRoleList) {
1241: echo("\n>>> UPDATE ROLES IN RELATION MBEAN");
1242: try {
1243: Object[] params = new Object[1];
1244: params[0] = theRoleList;
1245: String[] signature = new String[1];
1246: signature[0] = "javax.management.relation.RoleList";
1247: RoleResult result = (RoleResult) (server.invoke(
1248: theRelMBeanObjName, "setRoles", params, signature));
1249: printRoleResult(result);
1250: } catch (Exception e) {
1251: echo("\t!!! Could not update several roles in relation MBean "
1252: + theRelMBeanObjName + "!!!");
1253: printException(e);
1254: }
1255: }
1256:
1257: // doCreate a relation MBean and add it in relation service
1258: private void createRelationMBean(ObjectName theRelMBeanObjName,
1259: String theRelMBeanClassName, String theRelId,
1260: String theRelTypeName, RoleList theRoleList,
1261: ObjectName theRelServObjName) {
1262: echo("\n>>> CREATE RELATION MBEAN objectName: "
1263: + theRelMBeanObjName + ", relation id: " + theRelId
1264: + ", relation type name: " + theRelTypeName);
1265: try {
1266: Object[] params1 = new Object[4];
1267: params1[0] = theRelId;
1268: params1[1] = theRelServObjName;
1269: params1[2] = theRelTypeName;
1270: params1[3] = theRoleList;
1271: String[] signature1 = new String[4];
1272: signature1[0] = "java.lang.String";
1273: signature1[1] = "javax.management.ObjectName";
1274: signature1[2] = "java.lang.String";
1275: signature1[3] = "javax.management.relation.RoleList";
1276: server.createMBean(theRelMBeanClassName,
1277: theRelMBeanObjName, params1, signature1);
1278:
1279: addRelation(theRelMBeanObjName, theRelServObjName);
1280:
1281: } catch (Exception e) {
1282: echo("\t!!! Could not doCreate relation MBean for relation "
1283: + theRelId + "!!!");
1284: printException(e);
1285: }
1286: }
1287:
1288: // Adds an existing MBean as a relation
1289: private void addRelation(ObjectName theRelMBeanObjName,
1290: ObjectName theRelServObjName) {
1291: echo("\n>>> ADDING MBEAN AS RELATION, ObjectName: "
1292: + theRelMBeanObjName);
1293: try {
1294: Object[] params2 = new Object[1];
1295: params2[0] = theRelMBeanObjName;
1296: String[] signature2 = new String[1];
1297: signature2[0] = "javax.management.ObjectName";
1298: server.invoke(theRelServObjName, "addRelation", params2,
1299: signature2);
1300: } catch (Exception e) {
1301: echo("\t!!! Could not add relation MBean "
1302: + theRelMBeanObjName + "!!!");
1303: printException(e);
1304: }
1305: }
1306:
1307: // use relation operations on relation MBean, and specific accessors
1308: // as isRelationMBean
1309: private void testRelationMBeanOperations(
1310: ObjectName theRelMBeanObjName) {
1311: echo("\n>>> TEST RELATION MBEAN RELATION OPERATIONS objectName: "
1312: + theRelMBeanObjName);
1313: try {
1314: // Will test getRole(), getRoles(), getAllRoles(),
1315: // getReferencedMBeans() and getRelationTypeName()
1316: testRelationIndirectAccess(true, null, null,
1317: theRelMBeanObjName);
1318:
1319: // test other methods
1320:
1321: echo("\n-> Retrieve Relation Service Name");
1322: ObjectName relServName = (ObjectName) (server.getAttribute(
1323: theRelMBeanObjName, "RelationServiceName"));
1324: echo("\n Relation Service name: " + relServName);
1325:
1326: echo("\n-> Retrieve relation id");
1327: String relId = (String) (server.getAttribute(
1328: theRelMBeanObjName, "RelationId"));
1329: echo("\n Relation id: " + relId);
1330:
1331: } catch (Exception e) {
1332: echo("\t!!! Could not exercise relation management operations on relation "
1333: + theRelMBeanObjName + "!!!");
1334: printException(e);
1335: }
1336: }
1337:
1338: // Test Relation Service specific accessors for relation MBean
1339: private void testRelationMBeanSpecificOperations(String theRelId,
1340: ObjectName theRelServObjName, ObjectName theRelMBeanObjName) {
1341: echo("\n>>> TEST RELATION SERVICE ACCESSORS FOR RELATION MBEAN "
1342: + theRelId + " with name " + theRelMBeanObjName);
1343: try {
1344: // Test accessor to determine if relation id is associated to a
1345: // relation MBean
1346: echo("\n-> test that relation id is associated to right ObjectName");
1347: Object[] params1 = new Object[1];
1348: params1[0] = theRelId;
1349: String[] signature1 = new String[1];
1350: signature1[0] = "java.lang.String";
1351: ObjectName relMBeanName = (ObjectName) (server.invoke(
1352: theRelServObjName, "isRelationMBean", params1,
1353: signature1));
1354: echo("\n\t Retrieved ObjectName: " + relMBeanName);
1355:
1356: // Test accessor to retrieve relation id associated to an
1357: // ObjectName if associated MBean has been added as a relation
1358: echo("\n -> test that ObjectName is associated to relation id");
1359: Object[] params2 = new Object[1];
1360: params2[0] = theRelMBeanObjName;
1361: String[] signature2 = new String[1];
1362: signature2[0] = "javax.management.ObjectName";
1363: String relId = (String) (server.invoke(theRelServObjName,
1364: "isRelation", params2, signature2));
1365: echo("\n\t Retrieved relation id: " + relId);
1366:
1367: } catch (Exception e) {
1368: echo("\t!!! Could not exercise relation service specific operations for relation MBean "
1369: + theRelId + "!!!");
1370: printException(e);
1371: }
1372: }
1373:
1374: // Unregister a referenced MBean
1375: private void unregisterMBean(ObjectName theMBeanObjName) {
1376: echo("\n>>> UNREGISTER " + theMBeanObjName);
1377: try {
1378: server.unregisterMBean(theMBeanObjName);
1379: } catch (Exception e) {
1380: echo("\t!!! Cannot unregister MBean " + theMBeanObjName
1381: + "!!!");
1382: printException(e);
1383: }
1384: }
1385:
1386: // Remove a relation
1387: private void removeRelation(String theRelId,
1388: ObjectName theRelServObjName) {
1389: echo("\n>>> REMOVE RELATION " + theRelId);
1390: try {
1391: Object[] params = new Object[1];
1392: params[0] = theRelId;
1393: String[] signature = new String[1];
1394: signature[0] = "java.lang.String";
1395: server.invoke(theRelServObjName, "removeRelation", params,
1396: signature);
1397: } catch (Exception e) {
1398: echo("\t!!! Cannot remove relation " + theRelId + "!!!");
1399: printException(e);
1400: }
1401: }
1402:
1403: // Print the relation ids for all relations of given type
1404: private void printAllRelationIdsOfType(String theRelTypeName,
1405: ObjectName theRelServObjName) {
1406: echo("\n>>> PRINTING ALL RELATION IDS OF TYPE "
1407: + theRelTypeName);
1408: try {
1409: Object[] params = new Object[1];
1410: params[0] = theRelTypeName;
1411: String[] signature = new String[1];
1412: signature[0] = "java.lang.String";
1413: ArrayList relIdList = (ArrayList) (server.invoke(
1414: theRelServObjName, "findRelationsOfType", params,
1415: signature));
1416: printList(relIdList);
1417: } catch (Exception e) {
1418: echo("\t!!! Cannot retrieve relations of type "
1419: + theRelTypeName + "!!!");
1420: printException(e);
1421: }
1422: }
1423:
1424: // Removes relation type
1425: private void removeRelationType(String theRelTypeName,
1426: ObjectName theRelServObjName) {
1427: echo("\n>>> REMOVING RELATION TYPE " + theRelTypeName);
1428: echo("This will remove all the relations of that type.");
1429: try {
1430: Object[] params = new Object[1];
1431: params[0] = theRelTypeName;
1432: String[] signature = new String[1];
1433: signature[0] = "java.lang.String";
1434: ArrayList relIdList = (ArrayList) (server.invoke(
1435: theRelServObjName, "removeRelationType", params,
1436: signature));
1437: printList(relIdList);
1438: } catch (Exception e) {
1439: echo("\t!!! Cannot remove relation type " + theRelTypeName
1440: + "!!!");
1441: printException(e);
1442: }
1443: }
1444:
1445: private void printException(Exception theExc) {
1446: theExc.printStackTrace();
1447: StringBuffer excName = new StringBuffer();
1448: Exception exc = null;
1449: if (theExc instanceof MBeanException) {
1450: boolean isMBeanExcFlg = true;
1451: excName.append("MBeanException->");
1452: Exception exc1 = theExc;
1453: while (isMBeanExcFlg) {
1454: exc = ((MBeanException) exc1).getTargetException();
1455: if (!(exc instanceof MBeanException)) {
1456: isMBeanExcFlg = false;
1457: } else {
1458: exc1 = exc;
1459: excName.append("MBeanException->");
1460: }
1461: }
1462: } else {
1463: exc = theExc;
1464: }
1465: excName.append((exc.getClass()).getName());
1466: echo("Exception: " + excName.toString());
1467: echo(exc.getMessage());
1468: echo("\nEXITING...\n");
1469: System.exit(1);
1470: }
1471:
1472: // Adds user relation type to the Relation Service
1473: private void addRelationType(RelationType theRelTypeObj,
1474: ObjectName theRelServObjName) {
1475: echo("\n>>> ADDING USER RELATION TYPE");
1476: try {
1477: Object[] params = new Object[1];
1478: params[0] = theRelTypeObj;
1479: String[] signature = new String[1];
1480: signature[0] = "javax.management.relation.RelationType";
1481: server.invoke(theRelServObjName, "addRelationType", params,
1482: signature);
1483: } catch (Exception e) {
1484: echo("\t!!! Cannot add user relation type!!!");
1485: printException(e);
1486: }
1487: }
1488:
1489: private void createHtmlAdaptor() {
1490: // START HtmlAdaptor
1491: try {
1492: ObjectName haName = new ObjectName(
1493: "Adaptor:type=HtmlAdaptor,port=8082");
1494: HtmlAdaptorServer ha = new HtmlAdaptorServer();
1495: server.registerMBean(ha, haName);
1496: ha.start();
1497: } catch (Exception e) {
1498: e.printStackTrace();
1499: }
1500: }
1501:
1502: }
|