1 8 package mx4j.examples.services.relation; 9 10 import java.util.ArrayList ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 import java.util.Set ; 14 import javax.management.MBeanServer ; 15 import javax.management.MBeanServerFactory ; 16 import javax.management.MBeanServerInvocationHandler ; 17 import javax.management.ObjectInstance ; 18 import javax.management.ObjectName ; 19 import javax.management.Query ; 20 import javax.management.relation.RelationServiceMBean ; 21 import javax.management.relation.Role ; 22 import javax.management.relation.RoleList ; 23 import javax.management.relation.RoleResult ; 24 25 import mx4j.log.Log; 26 import mx4j.log.Logger; 27 28 31 32 36 public class RelationServiceExample 37 { 38 private MBeanServer m_server = null; 39 private RelationServiceMBean m_proxy = null; 40 private String m_relationServiceClass = "javax.management.relation.RelationService"; 41 private String m_libraryClassName = "mx4j.examples.services.relation.SimplePersonalLibrary"; 42 private ObjectName m_libraryObjectName = null; 43 private ObjectName m_relationObjectName = null; 44 private SimplePersonalLibrary m_library = null; 45 46 public RelationServiceExample() 47 { 48 m_server = MBeanServerFactory.createMBeanServer("RelationExample"); 49 } 50 51 public void setUpRelations() 52 { 53 try 55 { 56 System.out.println("Creating RelationService in the MBeanServer"); 57 Object [] params = {new Boolean (true)}; 58 String [] signature = {"boolean"}; 59 m_relationObjectName = new ObjectName ("relations:class=" + m_relationServiceClass); 60 m_server.createMBean(m_relationServiceClass, m_relationObjectName, null, params, signature); 61 62 m_proxy = (RelationServiceMBean )MBeanServerInvocationHandler.newProxyInstance(m_server, m_relationObjectName, RelationServiceMBean .class, false); 64 System.out.println("----------------------- done ----------------------------"); 65 66 System.out.println("create the relationType"); 67 String libraryTypeName = "personal_library"; 68 m_library = new SimplePersonalLibrary(libraryTypeName); 69 addRelationType(); 71 printRelationTypeInfo(); 72 System.out.println("----------------------- done ----------------------------"); 73 74 System.out.println("create RelationId for the relationType"); 75 String personalLibraryId = libraryTypeName + "_internal"; 76 System.out.println("----------------------- done ----------------------------"); 78 79 String ownerClassName = "mx4j.examples.services.relation.SimpleOwner"; String bookClassName = "mx4j.examples.services.relation.SimpleBooks"; 83 System.out.println("Creating MBeans to represent our relations"); 84 ObjectName ownerName1 = new ObjectName ("library:name=" + ownerClassName + "1"); 85 ObjectName ownerName2 = new ObjectName ("library:name=" + ownerClassName + "2"); 86 ObjectName bookName1 = new ObjectName ("library:name=" + bookClassName + "1"); 87 ObjectName bookName2 = new ObjectName ("library:name=" + bookClassName + "2"); 88 ObjectName bookName3 = new ObjectName ("library:name=" + bookClassName + "3"); 89 ObjectName bookName4 = new ObjectName ("library:name=" + bookClassName + "4"); 90 ObjectName bookName5 = new ObjectName ("library:name=" + bookClassName + "5"); 91 92 m_server.createMBean(bookClassName, bookName1, null, new Object []{"Lord of the rings"}, new String []{"java.lang.String"}); 93 m_server.createMBean(bookClassName, bookName2, null, new Object []{"The Hobbit"}, new String []{"java.lang.String"}); 94 m_server.createMBean(bookClassName, bookName3, null, new Object []{"Harry Potter"}, new String []{"java.lang.String"}); 95 m_server.createMBean(bookClassName, bookName4, null, new Object []{"UML Distilled"}, new String []{"java.lang.String"}); 96 m_server.createMBean(bookClassName, bookName5, null, new Object []{"Applying UML"}, new String []{"java.lang.String"}); 97 98 m_server.createMBean(ownerClassName, ownerName1, null, new Object []{"Fred"}, new String []{"java.lang.String"}); 99 m_server.createMBean(ownerClassName, ownerName2, null, new Object []{"Humpty Dumpty"}, new String []{"java.lang.String"}); 100 System.out.println("----------------------- done ----------------------------"); 101 102 System.out.println("Build the roles"); 103 ArrayList ownerList = new ArrayList (); 105 ownerList.add(ownerName1); Role ownerRole = new Role ("owner", ownerList); 107 108 System.out.println("created owner Role"); 109 110 ArrayList bookList = new ArrayList (); 111 bookList.add(bookName1); 113 bookList.add(bookName2); 114 bookList.add(bookName3); 115 Role bookRole = new Role ("books", bookList); 116 117 System.out.println("Created book role"); 118 System.out.println("----------------------- done ----------------------------"); 119 120 System.out.println("Creating the relation"); 121 RoleList libraryList = new RoleList (); 123 libraryList.add(ownerRole); 124 libraryList.add(bookRole); 125 createLibraryRelation(personalLibraryId, libraryTypeName, libraryList); 127 System.out.println("Getting all the related info"); 128 printAllRelationInfo(); 129 System.out.println("----------------------- done ----------------------------"); 130 131 System.out.println("borrow a book we have 3 one more does not invalidate our relation"); 133 borrowBooks(personalLibraryId, "books", bookName4); 134 ArrayList newBookList4 = getRoleValue(personalLibraryId, "books"); 135 System.out.println("we now have 4 books: " + newBookList4.toString()); 136 System.out.println("----------------------- done ----------------------------"); 137 138 System.out.println("2 MBeans removed from the MBeanServer - no problem we still have a valid relation."); 140 m_server.unregisterMBean(bookName1); 141 m_server.unregisterMBean(bookName2); 142 143 ArrayList newBookList = getRoleValue(personalLibraryId, "books"); 144 System.out.println("After removing the 2 MBeans we have only 2 Book MBeans left " + newBookList.toString()); 145 System.out.println("----------------------- done ----------------------------"); 146 147 System.out.println("Deregistering the last of our books from the MBeanServer"); 152 m_server.unregisterMBean(bookName3); 153 m_server.unregisterMBean(bookName4); 154 System.out.println("----------------------- done ----------------------------"); 155 156 System.out.println("Testing access by running queries: "); 157 System.out.println("The relation should have been removed and an exception of RelationNotFoundException returned"); 158 testAllAccessQueries(personalLibraryId); 159 System.out.println("----------------------- done ----------------------------"); 160 161 } 162 catch (Exception ex) 163 { 164 System.out.println("Could Not create the RelationService: " + ex); 165 ex.printStackTrace(); 166 } 167 } 168 169 public void borrowBooks(String relationId, String roleName, ObjectName bookToAdd) 170 { 171 Logger logger = getLogger(); 172 try 173 { 174 ArrayList oldRoleValue = getRoleValue(relationId, roleName); 176 ArrayList newRoleValue = (ArrayList )oldRoleValue.clone(); 177 newRoleValue.add(bookToAdd); 178 Role role = new Role (roleName, newRoleValue); 180 Object [] params1 = {relationId, role}; 181 String [] signature1 = {"java.lang.String", "javax.management.relation.Role"}; 182 m_server.invoke(m_relationObjectName, "setRole", params1, signature1); 183 } 184 catch (Exception ex) 185 { 186 logger.error("Unable to add a book"); 187 ex.printStackTrace(); 188 } 189 } 190 191 private void printList(List list) 192 { 193 for (Iterator i = list.iterator(); i.hasNext();) 194 { 195 System.out.println(">>>> Names representing roles: " + i.next()); 196 } 197 } 198 199 private ArrayList getRoleValue(String relationId, String roleName) 200 { 201 Logger logger = getLogger(); 202 try 203 { 204 Object [] params = {relationId, roleName}; 205 String [] signature = {"java.lang.String", "java.lang.String"}; 206 return ((ArrayList )(m_server.invoke(m_relationObjectName, "getRole", params, signature))); 207 } 208 catch (Exception ex) 209 { 210 logger.error("Unable to get the list of roles for ID: " + relationId); 211 return null; 212 } 213 } 214 215 public void endExample() 216 { 217 try 218 { 219 System.out.println("Cleaning up......"); 220 Set mbeanSet = m_server.queryMBeans(null, Query.initialSubString(Query.classattr(), Query.value("management*"))); 222 for (Iterator i = mbeanSet.iterator(); i.hasNext();) 223 { 224 m_server.unregisterMBean(((ObjectInstance )i.next()).getObjectName()); 225 } 226 m_server.unregisterMBean(m_relationObjectName); 228 MBeanServerFactory.releaseMBeanServer(m_server); 230 System.exit(0); 231 } 232 catch (Exception ex) 233 { 234 ex.printStackTrace(); 235 System.exit(1); 236 } 237 } 238 239 private void addRelationType() 240 { 241 try 242 { 243 Object [] params = {m_library}; 244 String [] signature = {"javax.management.relation.RelationType"}; 245 m_server.invoke(m_relationObjectName, "addRelationType", params, signature); 246 } 247 catch (Exception ex) 248 { 249 ex.printStackTrace(); 250 } 251 } 252 253 private void printRelationTypeInfo() 254 { 255 try 256 { 257 ArrayList relTypeNameList = (ArrayList )(m_server.getAttribute(m_relationObjectName, "AllRelationTypeNames")); 258 System.out.println("The RelationType Names found in the RelationService: " + relTypeNameList.toString()); 259 } 260 catch (Exception ex) 261 { 262 ex.printStackTrace(); 263 } 264 } 265 266 private void createLibraryRelation(String personalLibraryId, String libraryTypeName, RoleList libraryList) 267 { 268 Logger logger = getLogger(); 269 try 270 { 271 Object [] params = {personalLibraryId, libraryTypeName, libraryList}; 272 String [] signature = {"java.lang.String", "java.lang.String", "javax.management.relation.RoleList"}; 273 m_server.invoke(m_relationObjectName, "createRelation", params, signature); 274 } 275 catch (Exception ex) 276 { 277 logger.error("Exception creating Library Relation: " + ex.getMessage()); 278 ex.printStackTrace(); 279 } 280 } 281 282 private void printAllRelationInfo() 283 { 284 Logger logger = getLogger(); 285 try 286 { 287 ArrayList allRelationIds = (ArrayList )m_server.getAttribute(m_relationObjectName, "AllRelationIds"); 288 for (Iterator i = allRelationIds.iterator(); i.hasNext();) 289 { 290 String currentRelationId = (String )i.next(); 291 System.out.println("All RelationIds: " + currentRelationId); 292 testAllAccessQueries(currentRelationId); 293 } 294 } 295 catch (Exception ex) 296 { 297 logger.error("Unable to print the relations"); 298 ex.printStackTrace(); 299 } 300 } 301 302 private void testAllAccessQueries(String relationId) 303 { 304 Logger logger = getLogger(); 305 try 307 { 308 Object [] params = {relationId}; 309 String [] signature = {"java.lang.String"}; 310 RoleResult roleResult = (RoleResult )(m_server.invoke(m_relationObjectName, "getAllRoles", params, signature)); 311 RoleList roleList = roleResult.getRoles(); 312 for (Iterator i = roleList.iterator(); i.hasNext();) 313 { 314 Role currentRole = (Role )i.next(); 315 System.out.println(">>>> role name: " + currentRole.getRoleName()); 316 System.out.println(">>>> role values: " + currentRole.getRoleValue().toString()); 317 } 318 System.out.println("No unresolved Roles roleUnresolved size: " + roleResult.getRolesUnresolved().size()); 319 } 320 catch (Exception ex) 321 { 322 logger.error("Exception printing the results from relationId: " + relationId); 323 System.out.println("Printing the Exception message to validate exception: " + ex.getMessage()); 324 } 325 326 } 327 328 private Logger getLogger() 329 { 330 return Log.getLogger(getClass().getName()); 331 } 332 333 public static void main(String [] args) 334 { 335 RelationServiceExample example = new RelationServiceExample(); 336 example.setUpRelations(); 337 example.endExample(); 338 } 339 } | Popular Tags |