1 8 package mx4j.examples.tools.adaptor.http; 9 10 import java.io.IOException ; 11 import java.util.ArrayList ; 12 import java.util.Iterator ; 13 import java.util.List ; 14 import java.util.Set ; 15 import javax.management.Attribute ; 16 import javax.management.MBeanServer ; 17 import javax.management.MBeanServerFactory ; 18 import javax.management.MBeanServerInvocationHandler ; 19 import javax.management.ObjectInstance ; 20 import javax.management.ObjectName ; 21 import javax.management.Query ; 22 import javax.management.relation.RelationServiceMBean ; 23 import javax.management.relation.Role ; 24 import javax.management.relation.RoleInfo ; 25 import javax.management.relation.RoleList ; 26 import javax.management.relation.RoleResult ; 27 28 import mx4j.tools.adaptor.http.DefaultProcessor; 29 import mx4j.tools.adaptor.http.XSLTProcessor; 30 31 37 public class RelationServiceAdaptor 38 { 39 private MBeanServer m_server = null; 40 private RelationServiceMBean m_proxy = null; 41 private mx4j.tools.adaptor.http.HttpAdaptor m_adaptor = null; 42 private DefaultProcessor m_processor = null; 43 private XSLTProcessor m_xsltProcessor = null; 44 private ObjectName httpAdaptorObjectName = null; 45 private ObjectName processorName = null; 46 private ObjectName m_relationServiceObjectName = null; 47 48 public RelationServiceAdaptor() 49 { 50 m_server = MBeanServerFactory.createMBeanServer("MyAdaptorTests"); 51 m_adaptor = new mx4j.tools.adaptor.http.HttpAdaptor(); 52 m_processor = new DefaultProcessor(); 53 m_xsltProcessor = new XSLTProcessor(); 54 } 55 56 public void startTests(String [] args) 57 { 58 int defaultPort = 1999; 59 String defaultHost = "localhost"; 60 String defaultPath = "."; 61 if (args.length > 0) 62 { 63 defaultHost = args[0]; 64 } 65 if (args.length > 1) 66 { 67 defaultPort = Integer.parseInt(args[1]); 68 } 69 if (args.length > 2) 70 { 71 defaultPath = args[2]; 72 } 73 74 try 75 { 76 System.out.println("Building the objectNames and registering the HttpAdaptor, and XSLTProcessor"); 77 httpAdaptorObjectName = new ObjectName ("Server:name=HttpAdaptor"); 79 processorName = new ObjectName ("processor:name=XSLTProcessor"); 80 81 m_server.registerMBean(m_adaptor, httpAdaptorObjectName); 83 m_server.registerMBean(m_xsltProcessor, processorName); 84 85 m_server.setAttribute(processorName, new Attribute ("File", defaultPath)); 86 87 m_adaptor.setPort(defaultPort); 88 m_adaptor.setHost(defaultHost); 89 m_adaptor.setProcessor(m_xsltProcessor); 90 m_adaptor.setAuthenticationMethod("none"); 91 92 System.out.println("------------------------------------------- done --------------------------------------------"); 93 94 System.out.println("starting the adpator and then checking all is active"); 95 m_adaptor.start(); 96 97 if (m_adaptor.isActive()) 98 { 99 System.out.println("Adaptor is active"); 100 System.out.println("The name of the processor: " + m_adaptor.getProcessor().getName()); 101 } 102 103 System.out.println("------------------------------------------- done --------------------------------------------"); 104 System.out.println("Press enter to register relationService"); 105 waitForEnterPressed(); 106 } 107 catch (Exception ex) 108 { 109 ex.printStackTrace(); 110 } 111 } 112 113 public void buildMBeans() 114 { 115 try 116 { 117 System.out.println("Building and registering the relationService and 6 MBeans"); 118 String m_relationServiceClassName = "javax.management.relation.RelationService"; 119 m_relationServiceObjectName = new ObjectName ("relations:type=" + m_relationServiceClassName + "_1"); 120 Object [] params = {new Boolean (true)}; 121 String [] signature = {"boolean"}; 122 m_server.createMBean(m_relationServiceClassName, m_relationServiceObjectName, null, params, signature); 123 124 m_proxy = (RelationServiceMBean )MBeanServerInvocationHandler.newProxyInstance(m_server, m_relationServiceObjectName, RelationServiceMBean .class, false); 126 127 System.out.println("Press ENTER to register 6 MBeans"); 128 waitForEnterPressed(); 129 String mbeanClassName = "mx4j.examples.tools.adaptor.http.SimpleStandard"; 130 ObjectName mbeanObjectName1 = new ObjectName ("domain:type=SimpleStandard_1"); 131 ObjectName mbeanObjectName2 = new ObjectName ("domain:type=SimpleStandard_2"); 132 ObjectName mbeanObjectName3 = new ObjectName ("domain:type=SimpleStandard_3"); 133 ObjectName mbeanObjectName4 = new ObjectName ("domain:type=SimpleStandard_4"); 134 ObjectName mbeanObjectName5 = new ObjectName ("domain:type=SimpleStandard_5"); 135 ObjectName mbeanObjectName6 = new ObjectName ("domain:type=SimpleStandard_6"); 136 137 m_server.createMBean(mbeanClassName, mbeanObjectName1, null); 138 m_server.createMBean(mbeanClassName, mbeanObjectName2, null); 139 m_server.createMBean(mbeanClassName, mbeanObjectName3, null); 140 m_server.createMBean(mbeanClassName, mbeanObjectName4, null); 141 m_server.createMBean(mbeanClassName, mbeanObjectName5, null); 142 m_server.createMBean(mbeanClassName, mbeanObjectName6, null); 143 System.out.println("------------------------------------------- done --------------------------------------------"); 144 145 System.out.println("Creating RoleInfos for RelationType"); 146 RoleInfo [] roleInfos = new RoleInfo [2]; 147 String roleName1 = "primary"; 148 roleInfos[0] = new RoleInfo (roleName1, "mx4j.examples.tools.adaptor.http.SimpleStandard", true, true, 1, 1, null); 149 150 String roleName2 = "secondary"; 151 roleInfos[1] = new RoleInfo (roleName2, "mx4j.examples.tools.adaptor.http.SimpleStandard", true, true, 0, -1, null); 152 153 String relationTypeName = "Building_relation_view1"; 155 m_proxy.createRelationType(relationTypeName, roleInfos); 156 157 String relationTypeName2 = "Testing_2"; 159 m_proxy.createRelationType(relationTypeName2, roleInfos); 160 161 System.out.println("Creating relationIds for relationTypeName: " + relationTypeName); 162 System.out.println("First create the roles..."); 163 164 ArrayList roleValue1 = new ArrayList (); 165 roleValue1.add(mbeanObjectName1); 166 167 Role role1 = new Role (roleName1, roleValue1); 168 169 ArrayList roleValue2 = new ArrayList (); 170 roleValue2.add(mbeanObjectName2); 171 roleValue2.add(mbeanObjectName3); 172 roleValue2.add(mbeanObjectName4); 173 174 Role role2 = new Role (roleName2, roleValue2); 175 RoleList roleList1 = new RoleList (); 176 roleList1.add(role1); 177 roleList1.add(role2); 178 179 ArrayList role5Value = new ArrayList (); 181 role5Value.add(mbeanObjectName2); 182 183 Role role5 = new Role (roleName1, role5Value); 184 ArrayList roleValue5 = new ArrayList (); 185 roleValue5.add(mbeanObjectName4); 186 187 Role role6 = new Role (roleName2, roleValue5); 188 RoleList roleList5 = new RoleList (); 189 roleList5.add(role5); 190 roleList5.add(role6); 191 192 System.out.println("------------------------------------------- done --------------------------------------------"); 193 System.out.println("Now create relations with ids:::"); 194 195 String relationId1 = "relationId_1"; 196 m_proxy.createRelation(relationId1, relationTypeName, roleList1); 197 198 String relationId2 = "relationId_2"; 199 m_proxy.createRelation(relationId2, relationTypeName, roleList5); 200 201 String relationId3 = "relationId_3"; 202 m_proxy.createRelation(relationId3, relationTypeName, roleList1); 203 System.out.println("------------------------------------------- done --------------------------------------------"); 204 205 System.out.println("creating relationIds for relationtypeName: " + relationTypeName2); 206 207 String relationId4 = "relationId_number2_1"; 208 m_proxy.createRelation(relationId4, relationTypeName2, roleList1); 209 210 String relationId5 = "relationId_number2_2"; 211 m_proxy.createRelation(relationId5, relationTypeName2, roleList1); 212 213 String relationId6 = "relationId_number2_3"; 214 m_proxy.createRelation(relationId6, relationTypeName2, roleList1); 215 System.out.println("------------------------------------------- done --------------------------------------------"); 216 waitForEnterPressed(); 217 218 System.out.println("create a relation MBean and add it in the Relation Service"); 219 String relMBeanClassName = "mx4j.examples.tools.adaptor.http.SimpleRelationTestSupport"; 220 String relationId7 = "relationId_relationMBean_1"; 221 ObjectName relMBeanObjName1 = new ObjectName ("relationType:name=RelationTypeSupportInstance"); 222 m_server.createMBean(relMBeanClassName, relMBeanObjName1, null, 223 new Object []{relationId7, m_relationServiceObjectName, relationTypeName2, roleList1}, 224 new String []{"java.lang.String", "javax.management.ObjectName", "java.lang.String", "javax.management.relation.RoleList"}); 225 m_proxy.addRelation(relMBeanObjName1); 226 System.out.println("------------------------------------------- done --------------------------------------------"); 227 waitForEnterPressed(); 228 RoleResult result = m_proxy.getAllRoles(relationId1); 229 Iterator i = result.getRoles().iterator(); 230 while (i.hasNext()) 231 { 232 Role r = (Role )i.next(); 233 List l = r.getRoleValue(); 234 Iterator j = l.iterator(); 235 while (j.hasNext()) 236 { 237 ObjectName objName = (ObjectName )j.next(); 238 System.out.println("ObjectName: " + objName.getCanonicalName() + " for relationId: " + relationId1); 239 } 240 } 241 242 RoleResult result2 = m_proxy.getAllRoles(relationId2); 243 Iterator i2 = result2.getRoles().iterator(); 244 while (i2.hasNext()) 245 { 246 Role r = (Role )i2.next(); 247 List l = r.getRoleValue(); 248 Iterator j = l.iterator(); 249 while (j.hasNext()) 250 { 251 ObjectName objName = (ObjectName )j.next(); 252 System.out.println("ObjectName: " + objName.getCanonicalName() + " for relationId: " + relationId2); 253 } 254 } 255 256 System.out.println("getting all relationIds"); 257 List li = m_proxy.getAllRelationIds(); 258 System.out.println("allrelationIds list: " + li.toString()); 259 260 System.out.println("You can view the adaptor at url http://......"); 261 System.out.println(">>>>>>>>>>>>>>>> PRESS ENTER TO END THE DEMO <<<<<<<<<<<<<<<<<<<<"); 262 263 waitForEnterPressed(); 264 } 265 catch (Exception ex) 266 { 267 ex.printStackTrace(); 268 } 269 } 270 271 public void endTests() 272 { 273 try 274 { 275 m_adaptor.stop(); 276 Set mbeanSet = m_server.queryMBeans(null, Query.initialSubString(Query.classattr(), Query.value("test*"))); 277 for (Iterator i = mbeanSet.iterator(); i.hasNext();) 278 { 279 m_server.unregisterMBean(((ObjectInstance )i.next()).getObjectName()); 280 } 281 m_server.unregisterMBean(m_relationServiceObjectName); 283 m_server.unregisterMBean(processorName); 284 m_server.unregisterMBean(httpAdaptorObjectName); 285 MBeanServerFactory.releaseMBeanServer(m_server); 287 System.exit(0); 288 } 289 catch (Exception ex) 290 { 291 ex.printStackTrace(); 292 System.exit(1); 293 } 294 } 295 296 public static void main(String [] args) 297 { 298 RelationServiceAdaptor bnb = new RelationServiceAdaptor(); 299 bnb.startTests(args); 300 bnb.buildMBeans(); 301 bnb.endTests(); 302 } 303 304 private static void waitForEnterPressed() 305 { 306 try 307 { 308 boolean done = false; 309 while (!done) 310 { 311 char ch = (char)System.in.read(); 312 if (ch < 0 || ch == '\n') 313 { 314 done = true; 315 } 316 } 317 } 318 catch (IOException ex) 319 { 320 ex.printStackTrace(); 321 } 322 return; 323 } 324 } | Popular Tags |