Source Code Cross Referenced for RelationServiceExample.java in  » J2EE » enhydra » mx4j » examples » services » relation » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » enhydra » mx4j.examples.services.relation 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:         */package mx4j.examples.services.relation;
008:
009:        import java.util.ArrayList;
010:        import java.util.Iterator;
011:        import java.util.List;
012:        import java.util.Set;
013:        import javax.management.MBeanServer;
014:        import javax.management.MBeanServerFactory;
015:        import javax.management.MBeanServerInvocationHandler;
016:        import javax.management.ObjectInstance;
017:        import javax.management.ObjectName;
018:        import javax.management.Query;
019:        import javax.management.relation.RelationServiceMBean;
020:        import javax.management.relation.Role;
021:        import javax.management.relation.RoleList;
022:        import javax.management.relation.RoleResult;
023:
024:        import mx4j.log.Log;
025:        import mx4j.log.Logger;
026:
027:        /**
028:         * @version $Revision: 1.1 $
029:         */
030:
031:        /**
032:         * This class will demonstrate the use-case scenarios described in the docs, under chapter "examples" and sub-section RelationService
033:         * Some methods will also use the MBeanProxy.
034:         */
035:        public class RelationServiceExample {
036:            private MBeanServer m_server = null;
037:            private RelationServiceMBean m_proxy = null;
038:            private String m_relationServiceClass = "javax.management.relation.RelationService";
039:            private String m_libraryClassName = "mx4j.examples.services.relation.SimplePersonalLibrary";
040:            private ObjectName m_libraryObjectName = null;
041:            private ObjectName m_relationObjectName = null;
042:            private SimplePersonalLibrary m_library = null;
043:
044:            public RelationServiceExample() {
045:                m_server = MBeanServerFactory
046:                        .createMBeanServer("RelationExample");
047:            }
048:
049:            public void setUpRelations() {
050:                // build the object name and register the relationService
051:                try {
052:                    System.out
053:                            .println("Creating RelationService in the MBeanServer");
054:                    Object[] params = { new Boolean(true) };
055:                    String[] signature = { "boolean" };
056:                    m_relationObjectName = new ObjectName("relations:class="
057:                            + m_relationServiceClass);
058:                    m_server.createMBean(m_relationServiceClass,
059:                            m_relationObjectName, null, params, signature);
060:
061:                    // we will create the MBeanProxy now so we can make some simple calls through the proxy
062:                    m_proxy = (RelationServiceMBean) MBeanServerInvocationHandler
063:                            .newProxyInstance(m_server, m_relationObjectName,
064:                                    RelationServiceMBean.class, false);
065:                    System.out
066:                            .println("----------------------- done ----------------------------");
067:
068:                    System.out.println("create the relationType");
069:                    String libraryTypeName = "personal_library";
070:                    m_library = new SimplePersonalLibrary(libraryTypeName);
071:                    // add it to the relationService
072:                    addRelationType();
073:                    printRelationTypeInfo();
074:                    System.out
075:                            .println("----------------------- done ----------------------------");
076:
077:                    System.out
078:                            .println("create RelationId for the relationType");
079:                    String personalLibraryId = libraryTypeName + "_internal";
080:                    //....Done....
081:                    System.out
082:                            .println("----------------------- done ----------------------------");
083:
084:                    // we now need to build the Roles and MBeans that will represent those relations
085:                    String ownerClassName = "mx4j.examples.services.relation.SimpleOwner"; // create 2 instance of this
086:                    String bookClassName = "mx4j.examples.services.relation.SimpleBooks"; // create 5 instances of this
087:
088:                    System.out
089:                            .println("Creating MBeans to represent our relations");
090:                    ObjectName ownerName1 = new ObjectName("library:name="
091:                            + ownerClassName + "1");
092:                    ObjectName ownerName2 = new ObjectName("library:name="
093:                            + ownerClassName + "2");
094:                    ObjectName bookName1 = new ObjectName("library:name="
095:                            + bookClassName + "1");
096:                    ObjectName bookName2 = new ObjectName("library:name="
097:                            + bookClassName + "2");
098:                    ObjectName bookName3 = new ObjectName("library:name="
099:                            + bookClassName + "3");
100:                    ObjectName bookName4 = new ObjectName("library:name="
101:                            + bookClassName + "4");
102:                    ObjectName bookName5 = new ObjectName("library:name="
103:                            + bookClassName + "5");
104:
105:                    m_server.createMBean(bookClassName, bookName1, null,
106:                            new Object[] { "Lord of the rings" },
107:                            new String[] { "java.lang.String" });
108:                    m_server.createMBean(bookClassName, bookName2, null,
109:                            new Object[] { "The Hobbit" },
110:                            new String[] { "java.lang.String" });
111:                    m_server.createMBean(bookClassName, bookName3, null,
112:                            new Object[] { "Harry Potter" },
113:                            new String[] { "java.lang.String" });
114:                    m_server.createMBean(bookClassName, bookName4, null,
115:                            new Object[] { "UML Distilled" },
116:                            new String[] { "java.lang.String" });
117:                    m_server.createMBean(bookClassName, bookName5, null,
118:                            new Object[] { "Applying UML" },
119:                            new String[] { "java.lang.String" });
120:
121:                    m_server.createMBean(ownerClassName, ownerName1, null,
122:                            new Object[] { "Fred" },
123:                            new String[] { "java.lang.String" });
124:                    m_server.createMBean(ownerClassName, ownerName2, null,
125:                            new Object[] { "Humpty Dumpty" },
126:                            new String[] { "java.lang.String" });
127:                    System.out
128:                            .println("----------------------- done ----------------------------");
129:
130:                    System.out.println("Build the roles");
131:                    // build our Lists of values for our first use case an owner registers and takes out one book
132:                    ArrayList ownerList = new ArrayList();
133:                    ownerList.add(ownerName1); // can only add owner to an owner role can only be 1
134:                    Role ownerRole = new Role("owner", ownerList);
135:
136:                    System.out.println("created owner Role");
137:
138:                    ArrayList bookList = new ArrayList();
139:                    // we can have between 1 and 4 books more than 4 invalidates out relation and less than 1 invalidates it
140:                    bookList.add(bookName1);
141:                    bookList.add(bookName2);
142:                    bookList.add(bookName3);
143:                    Role bookRole = new Role("books", bookList);
144:
145:                    System.out.println("Created book role");
146:                    System.out
147:                            .println("----------------------- done ----------------------------");
148:
149:                    System.out.println("Creating the relation");
150:                    // add our roles to the RoleList
151:                    RoleList libraryList = new RoleList();
152:                    libraryList.add(ownerRole);
153:                    libraryList.add(bookRole);
154:                    // now to create the relation
155:                    createLibraryRelation(personalLibraryId, libraryTypeName,
156:                            libraryList);
157:                    System.out.println("Getting all the related info");
158:                    printAllRelationInfo();
159:                    System.out
160:                            .println("----------------------- done ----------------------------");
161:
162:                    // borrow one book still within our stated quota
163:                    System.out
164:                            .println("borrow a book we have 3 one more does not invalidate our relation");
165:                    borrowBooks(personalLibraryId, "books", bookName4);
166:                    ArrayList newBookList4 = getRoleValue(personalLibraryId,
167:                            "books");
168:                    System.out.println("we now have 4 books: "
169:                            + newBookList4.toString());
170:                    System.out
171:                            .println("----------------------- done ----------------------------");
172:
173:                    // remove 2 books from the MBeanServer an see if our owner has only 2 left
174:                    System.out
175:                            .println("2 MBeans removed from the MBeanServer - no problem we still have a valid relation.");
176:                    m_server.unregisterMBean(bookName1);
177:                    m_server.unregisterMBean(bookName2);
178:
179:                    ArrayList newBookList = getRoleValue(personalLibraryId,
180:                            "books");
181:                    System.out
182:                            .println("After removing the 2 MBeans we have only 2 Book MBeans left "
183:                                    + newBookList.toString());
184:                    System.out
185:                            .println("----------------------- done ----------------------------");
186:
187:                    // we will now demonstrate the unhappy scenarios.
188:                    //invalidate the relation and borrow too many books throws InvalidRoleValueException
189:                    // note we cannot add bookName1 or bookName2 as they have been unregistered from the MBeanServer
190:                    // register
191:                    System.out
192:                            .println("Deregistering the last of our books from the MBeanServer");
193:                    m_server.unregisterMBean(bookName3);
194:                    m_server.unregisterMBean(bookName4);
195:                    System.out
196:                            .println("----------------------- done ----------------------------");
197:
198:                    System.out.println("Testing access by running queries: ");
199:                    System.out
200:                            .println("The relation should have been removed and an exception of RelationNotFoundException returned");
201:                    testAllAccessQueries(personalLibraryId);
202:                    System.out
203:                            .println("----------------------- done ----------------------------");
204:
205:                } catch (Exception ex) {
206:                    System.out.println("Could Not create the RelationService: "
207:                            + ex);
208:                    ex.printStackTrace();
209:                }
210:            }
211:
212:            public void borrowBooks(String relationId, String roleName,
213:                    ObjectName bookToAdd) {
214:                Logger logger = getLogger();
215:                try {
216:                    // get the old values
217:                    ArrayList oldRoleValue = getRoleValue(relationId, roleName);
218:                    ArrayList newRoleValue = (ArrayList) oldRoleValue.clone();
219:                    newRoleValue.add(bookToAdd);
220:                    // now we update the values
221:                    Role role = new Role(roleName, newRoleValue);
222:                    Object[] params1 = { relationId, role };
223:                    String[] signature1 = { "java.lang.String",
224:                            "javax.management.relation.Role" };
225:                    m_server.invoke(m_relationObjectName, "setRole", params1,
226:                            signature1);
227:                } catch (Exception ex) {
228:                    logger.error("Unable to add a book");
229:                    ex.printStackTrace();
230:                }
231:            }
232:
233:            private void printList(List list) {
234:                for (Iterator i = list.iterator(); i.hasNext();) {
235:                    System.out.println(">>>> Names representing roles: "
236:                            + i.next());
237:                }
238:            }
239:
240:            private ArrayList getRoleValue(String relationId, String roleName) {
241:                Logger logger = getLogger();
242:                try {
243:                    Object[] params = { relationId, roleName };
244:                    String[] signature = { "java.lang.String",
245:                            "java.lang.String" };
246:                    return ((ArrayList) (m_server.invoke(m_relationObjectName,
247:                            "getRole", params, signature)));
248:                } catch (Exception ex) {
249:                    logger.error("Unable to get the list of roles for ID: "
250:                            + relationId);
251:                    return null;
252:                }
253:            }
254:
255:            public void endExample() {
256:                try {
257:                    System.out.println("Cleaning up......");
258:                    // this query will return the set of mbeans which have a class attribute of "management*" which is our MBeans
259:                    Set mbeanSet = m_server.queryMBeans(null, Query
260:                            .initialSubString(Query.classattr(), Query
261:                                    .value("management*")));
262:                    for (Iterator i = mbeanSet.iterator(); i.hasNext();) {
263:                        m_server.unregisterMBean(((ObjectInstance) i.next())
264:                                .getObjectName());
265:                    }
266:                    // release the relationService
267:                    m_server.unregisterMBean(m_relationObjectName);
268:                    // release the MBeanServer
269:                    MBeanServerFactory.releaseMBeanServer(m_server);
270:                    System.exit(0);
271:                } catch (Exception ex) {
272:                    ex.printStackTrace();
273:                    System.exit(1);
274:                }
275:            }
276:
277:            private void addRelationType() {
278:                try {
279:                    Object[] params = { m_library };
280:                    String[] signature = { "javax.management.relation.RelationType" };
281:                    m_server.invoke(m_relationObjectName, "addRelationType",
282:                            params, signature);
283:                } catch (Exception ex) {
284:                    ex.printStackTrace();
285:                }
286:            }
287:
288:            private void printRelationTypeInfo() {
289:                try {
290:                    ArrayList relTypeNameList = (ArrayList) (m_server
291:                            .getAttribute(m_relationObjectName,
292:                                    "AllRelationTypeNames"));
293:                    System.out
294:                            .println("The RelationType Names found in the RelationService: "
295:                                    + relTypeNameList.toString());
296:                } catch (Exception ex) {
297:                    ex.printStackTrace();
298:                }
299:            }
300:
301:            private void createLibraryRelation(String personalLibraryId,
302:                    String libraryTypeName, RoleList libraryList) {
303:                Logger logger = getLogger();
304:                try {
305:                    Object[] params = { personalLibraryId, libraryTypeName,
306:                            libraryList };
307:                    String[] signature = { "java.lang.String",
308:                            "java.lang.String",
309:                            "javax.management.relation.RoleList" };
310:                    m_server.invoke(m_relationObjectName, "createRelation",
311:                            params, signature);
312:                } catch (Exception ex) {
313:                    logger.error("Exception creating Library Relation: "
314:                            + ex.getMessage());
315:                    ex.printStackTrace();
316:                }
317:            }
318:
319:            private void printAllRelationInfo() {
320:                Logger logger = getLogger();
321:                try {
322:                    ArrayList allRelationIds = (ArrayList) m_server
323:                            .getAttribute(m_relationObjectName,
324:                                    "AllRelationIds");
325:                    for (Iterator i = allRelationIds.iterator(); i.hasNext();) {
326:                        String currentRelationId = (String) i.next();
327:                        System.out.println("All RelationIds: "
328:                                + currentRelationId);
329:                        testAllAccessQueries(currentRelationId);
330:                    }
331:                } catch (Exception ex) {
332:                    logger.error("Unable to print the relations");
333:                    ex.printStackTrace();
334:                }
335:            }
336:
337:            private void testAllAccessQueries(String relationId) {
338:                Logger logger = getLogger();
339:                // retrieve all roles
340:                try {
341:                    Object[] params = { relationId };
342:                    String[] signature = { "java.lang.String" };
343:                    RoleResult roleResult = (RoleResult) (m_server.invoke(
344:                            m_relationObjectName, "getAllRoles", params,
345:                            signature));
346:                    RoleList roleList = roleResult.getRoles();
347:                    for (Iterator i = roleList.iterator(); i.hasNext();) {
348:                        Role currentRole = (Role) i.next();
349:                        System.out.println(">>>> role name: "
350:                                + currentRole.getRoleName());
351:                        System.out.println(">>>> role values: "
352:                                + currentRole.getRoleValue().toString());
353:                    }
354:                    System.out
355:                            .println("No unresolved Roles roleUnresolved size: "
356:                                    + roleResult.getRolesUnresolved().size());
357:                } catch (Exception ex) {
358:                    logger
359:                            .error("Exception printing the results from relationId: "
360:                                    + relationId);
361:                    System.out
362:                            .println("Printing the Exception message to validate exception: "
363:                                    + ex.getMessage());
364:                }
365:
366:            }
367:
368:            private Logger getLogger() {
369:                return Log.getLogger(getClass().getName());
370:            }
371:
372:            public static void main(String[] args) {
373:                RelationServiceExample example = new RelationServiceExample();
374:                example.setUpRelations();
375:                example.endExample();
376:            }
377:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.