001: /*
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */
008: package mx4j.examples.tools.adaptor.http;
009:
010: import java.io.IOException;
011: import java.util.ArrayList;
012: import java.util.Iterator;
013: import java.util.List;
014: import java.util.Set;
015: import javax.management.Attribute;
016: import javax.management.MBeanServer;
017: import javax.management.MBeanServerFactory;
018: import javax.management.MBeanServerInvocationHandler;
019: import javax.management.ObjectInstance;
020: import javax.management.ObjectName;
021: import javax.management.Query;
022: import javax.management.relation.RelationServiceMBean;
023: import javax.management.relation.Role;
024: import javax.management.relation.RoleInfo;
025: import javax.management.relation.RoleList;
026: import javax.management.relation.RoleResult;
027:
028: import mx4j.tools.adaptor.http.DefaultProcessor;
029: import mx4j.tools.adaptor.http.XSLTProcessor;
030:
031: /**
032: * Example as how to use the HttpAdaptor and the XSLTProcessor and the RelationProcessor
033: * To use this example please add as arguments to the command line in this order <host><port><path to xsl files>
034: *
035: * @version $Revision: 1.1 $
036: */
037: public class RelationServiceAdaptor {
038: private MBeanServer m_server = null;
039: private RelationServiceMBean m_proxy = null;
040: private mx4j.tools.adaptor.http.HttpAdaptor m_adaptor = null;
041: private DefaultProcessor m_processor = null;
042: private XSLTProcessor m_xsltProcessor = null;
043: private ObjectName httpAdaptorObjectName = null;
044: private ObjectName processorName = null;
045: private ObjectName m_relationServiceObjectName = null;
046:
047: public RelationServiceAdaptor() {
048: m_server = MBeanServerFactory
049: .createMBeanServer("MyAdaptorTests");
050: m_adaptor = new mx4j.tools.adaptor.http.HttpAdaptor();
051: m_processor = new DefaultProcessor();
052: m_xsltProcessor = new XSLTProcessor();
053: }
054:
055: public void startTests(String[] args) {
056: int defaultPort = 1999;
057: String defaultHost = "localhost";
058: String defaultPath = ".";
059: if (args.length > 0) {
060: defaultHost = args[0];
061: }
062: if (args.length > 1) {
063: defaultPort = Integer.parseInt(args[1]);
064: }
065: if (args.length > 2) {
066: defaultPath = args[2];
067: }
068:
069: try {
070: System.out
071: .println("Building the objectNames and registering the HttpAdaptor, and XSLTProcessor");
072: // build object names
073: httpAdaptorObjectName = new ObjectName(
074: "Server:name=HttpAdaptor");
075: processorName = new ObjectName(
076: "processor:name=XSLTProcessor");
077:
078: // register adaptor in server
079: m_server.registerMBean(m_adaptor, httpAdaptorObjectName);
080: m_server.registerMBean(m_xsltProcessor, processorName);
081:
082: m_server.setAttribute(processorName, new Attribute("File",
083: defaultPath));
084:
085: m_adaptor.setPort(defaultPort);
086: m_adaptor.setHost(defaultHost);
087: m_adaptor.setProcessor(m_xsltProcessor);
088: m_adaptor.setAuthenticationMethod("none");
089:
090: System.out
091: .println("------------------------------------------- done --------------------------------------------");
092:
093: System.out
094: .println("starting the adpator and then checking all is active");
095: m_adaptor.start();
096:
097: if (m_adaptor.isActive()) {
098: System.out.println("Adaptor is active");
099: System.out.println("The name of the processor: "
100: + m_adaptor.getProcessor().getName());
101: }
102:
103: System.out
104: .println("------------------------------------------- done --------------------------------------------");
105: System.out
106: .println("Press enter to register relationService");
107: waitForEnterPressed();
108: } catch (Exception ex) {
109: ex.printStackTrace();
110: }
111: }
112:
113: public void buildMBeans() {
114: try {
115: System.out
116: .println("Building and registering the relationService and 6 MBeans");
117: String m_relationServiceClassName = "javax.management.relation.RelationService";
118: m_relationServiceObjectName = new ObjectName(
119: "relations:type=" + m_relationServiceClassName
120: + "_1");
121: Object[] params = { new Boolean(true) };
122: String[] signature = { "boolean" };
123: m_server.createMBean(m_relationServiceClassName,
124: m_relationServiceObjectName, null, params,
125: signature);
126:
127: // create a proxy
128: m_proxy = (RelationServiceMBean) MBeanServerInvocationHandler
129: .newProxyInstance(m_server,
130: m_relationServiceObjectName,
131: RelationServiceMBean.class, false);
132:
133: System.out.println("Press ENTER to register 6 MBeans");
134: waitForEnterPressed();
135: String mbeanClassName = "mx4j.examples.tools.adaptor.http.SimpleStandard";
136: ObjectName mbeanObjectName1 = new ObjectName(
137: "domain:type=SimpleStandard_1");
138: ObjectName mbeanObjectName2 = new ObjectName(
139: "domain:type=SimpleStandard_2");
140: ObjectName mbeanObjectName3 = new ObjectName(
141: "domain:type=SimpleStandard_3");
142: ObjectName mbeanObjectName4 = new ObjectName(
143: "domain:type=SimpleStandard_4");
144: ObjectName mbeanObjectName5 = new ObjectName(
145: "domain:type=SimpleStandard_5");
146: ObjectName mbeanObjectName6 = new ObjectName(
147: "domain:type=SimpleStandard_6");
148:
149: m_server
150: .createMBean(mbeanClassName, mbeanObjectName1, null);
151: m_server
152: .createMBean(mbeanClassName, mbeanObjectName2, null);
153: m_server
154: .createMBean(mbeanClassName, mbeanObjectName3, null);
155: m_server
156: .createMBean(mbeanClassName, mbeanObjectName4, null);
157: m_server
158: .createMBean(mbeanClassName, mbeanObjectName5, null);
159: m_server
160: .createMBean(mbeanClassName, mbeanObjectName6, null);
161: System.out
162: .println("------------------------------------------- done --------------------------------------------");
163:
164: System.out.println("Creating RoleInfos for RelationType");
165: RoleInfo[] roleInfos = new RoleInfo[2];
166: String roleName1 = "primary";
167: roleInfos[0] = new RoleInfo(roleName1,
168: "mx4j.examples.tools.adaptor.http.SimpleStandard",
169: true, true, 1, 1, null);
170:
171: String roleName2 = "secondary";
172: roleInfos[1] = new RoleInfo(roleName2,
173: "mx4j.examples.tools.adaptor.http.SimpleStandard",
174: true, true, 0, -1, null);
175:
176: // create a relation type with those role infos
177: String relationTypeName = "Building_relation_view1";
178: m_proxy.createRelationType(relationTypeName, roleInfos);
179:
180: // creating more relationTypes to test
181: String relationTypeName2 = "Testing_2";
182: m_proxy.createRelationType(relationTypeName2, roleInfos);
183:
184: System.out
185: .println("Creating relationIds for relationTypeName: "
186: + relationTypeName);
187: System.out.println("First create the roles...");
188:
189: ArrayList roleValue1 = new ArrayList();
190: roleValue1.add(mbeanObjectName1);
191:
192: Role role1 = new Role(roleName1, roleValue1);
193:
194: ArrayList roleValue2 = new ArrayList();
195: roleValue2.add(mbeanObjectName2);
196: roleValue2.add(mbeanObjectName3);
197: roleValue2.add(mbeanObjectName4);
198:
199: Role role2 = new Role(roleName2, roleValue2);
200: RoleList roleList1 = new RoleList();
201: roleList1.add(role1);
202: roleList1.add(role2);
203:
204: /// testing form here
205: ArrayList role5Value = new ArrayList();
206: role5Value.add(mbeanObjectName2);
207:
208: Role role5 = new Role(roleName1, role5Value);
209: ArrayList roleValue5 = new ArrayList();
210: roleValue5.add(mbeanObjectName4);
211:
212: Role role6 = new Role(roleName2, roleValue5);
213: RoleList roleList5 = new RoleList();
214: roleList5.add(role5);
215: roleList5.add(role6);
216:
217: System.out
218: .println("------------------------------------------- done --------------------------------------------");
219: System.out.println("Now create relations with ids:::");
220:
221: String relationId1 = "relationId_1";
222: m_proxy.createRelation(relationId1, relationTypeName,
223: roleList1);
224:
225: String relationId2 = "relationId_2";
226: m_proxy.createRelation(relationId2, relationTypeName,
227: roleList5);
228:
229: String relationId3 = "relationId_3";
230: m_proxy.createRelation(relationId3, relationTypeName,
231: roleList1);
232: System.out
233: .println("------------------------------------------- done --------------------------------------------");
234:
235: System.out
236: .println("creating relationIds for relationtypeName: "
237: + relationTypeName2);
238:
239: String relationId4 = "relationId_number2_1";
240: m_proxy.createRelation(relationId4, relationTypeName2,
241: roleList1);
242:
243: String relationId5 = "relationId_number2_2";
244: m_proxy.createRelation(relationId5, relationTypeName2,
245: roleList1);
246:
247: String relationId6 = "relationId_number2_3";
248: m_proxy.createRelation(relationId6, relationTypeName2,
249: roleList1);
250: System.out
251: .println("------------------------------------------- done --------------------------------------------");
252: waitForEnterPressed();
253:
254: System.out
255: .println("create a relation MBean and add it in the Relation Service");
256: String relMBeanClassName = "mx4j.examples.tools.adaptor.http.SimpleRelationTestSupport";
257: String relationId7 = "relationId_relationMBean_1";
258: ObjectName relMBeanObjName1 = new ObjectName(
259: "relationType:name=RelationTypeSupportInstance");
260: m_server.createMBean(relMBeanClassName, relMBeanObjName1,
261: null, new Object[] { relationId7,
262: m_relationServiceObjectName,
263: relationTypeName2, roleList1 },
264: new String[] { "java.lang.String",
265: "javax.management.ObjectName",
266: "java.lang.String",
267: "javax.management.relation.RoleList" });
268: m_proxy.addRelation(relMBeanObjName1);
269: System.out
270: .println("------------------------------------------- done --------------------------------------------");
271: waitForEnterPressed();
272: RoleResult result = m_proxy.getAllRoles(relationId1);
273: Iterator i = result.getRoles().iterator();
274: while (i.hasNext()) {
275: Role r = (Role) i.next();
276: List l = r.getRoleValue();
277: Iterator j = l.iterator();
278: while (j.hasNext()) {
279: ObjectName objName = (ObjectName) j.next();
280: System.out.println("ObjectName: "
281: + objName.getCanonicalName()
282: + " for relationId: " + relationId1);
283: }
284: }
285:
286: RoleResult result2 = m_proxy.getAllRoles(relationId2);
287: Iterator i2 = result2.getRoles().iterator();
288: while (i2.hasNext()) {
289: Role r = (Role) i2.next();
290: List l = r.getRoleValue();
291: Iterator j = l.iterator();
292: while (j.hasNext()) {
293: ObjectName objName = (ObjectName) j.next();
294: System.out.println("ObjectName: "
295: + objName.getCanonicalName()
296: + " for relationId: " + relationId2);
297: }
298: }
299:
300: System.out.println("getting all relationIds");
301: List li = m_proxy.getAllRelationIds();
302: System.out.println("allrelationIds list: " + li.toString());
303:
304: System.out
305: .println("You can view the adaptor at url http://......");
306: System.out
307: .println(">>>>>>>>>>>>>>>> PRESS ENTER TO END THE DEMO <<<<<<<<<<<<<<<<<<<<");
308:
309: waitForEnterPressed();
310: } catch (Exception ex) {
311: ex.printStackTrace();
312: }
313: }
314:
315: public void endTests() {
316: try {
317: m_adaptor.stop();
318: Set mbeanSet = m_server.queryMBeans(null, Query
319: .initialSubString(Query.classattr(), Query
320: .value("test*")));
321: for (Iterator i = mbeanSet.iterator(); i.hasNext();) {
322: m_server.unregisterMBean(((ObjectInstance) i.next())
323: .getObjectName());
324: }
325: // release the relationService
326: m_server.unregisterMBean(m_relationServiceObjectName);
327: m_server.unregisterMBean(processorName);
328: m_server.unregisterMBean(httpAdaptorObjectName);
329: // release the MBeanServer
330: MBeanServerFactory.releaseMBeanServer(m_server);
331: System.exit(0);
332: } catch (Exception ex) {
333: ex.printStackTrace();
334: System.exit(1);
335: }
336: }
337:
338: public static void main(String[] args) {
339: RelationServiceAdaptor bnb = new RelationServiceAdaptor();
340: bnb.startTests(args);
341: bnb.buildMBeans();
342: bnb.endTests();
343: }
344:
345: private static void waitForEnterPressed() {
346: try {
347: boolean done = false;
348: while (!done) {
349: char ch = (char) System.in.read();
350: if (ch < 0 || ch == '\n') {
351: done = true;
352: }
353: }
354: } catch (IOException ex) {
355: ex.printStackTrace();
356: }
357: return;
358: }
359: }
|