Source Code Cross Referenced for ModelMBeanInfoSupport.java in  » JMX » mx4j » javax » management » modelmbean » 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 » JMX » mx4j » javax.management.modelmbean 
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:         */
008:
009:        package javax.management.modelmbean;
010:
011:        import java.io.Serializable;
012:        import java.util.Arrays;
013:        import java.util.List;
014:        import javax.management.Descriptor;
015:        import javax.management.DescriptorAccess;
016:        import javax.management.MBeanAttributeInfo;
017:        import javax.management.MBeanConstructorInfo;
018:        import javax.management.MBeanException;
019:        import javax.management.MBeanFeatureInfo;
020:        import javax.management.MBeanInfo;
021:        import javax.management.MBeanNotificationInfo;
022:        import javax.management.MBeanOperationInfo;
023:        import javax.management.RuntimeOperationsException;
024:
025:        /**
026:         * @version $Revision: 1.18 $
027:         */
028:        // Change not needed, workaround to a TCK bug only to achieve TCK compliance
029:        // public class ModelMBeanInfoSupport extends MBeanInfo implements ModelMBeanInfo
030:        public class ModelMBeanInfoSupport extends MBeanInfo implements 
031:                ModelMBeanInfo, Serializable {
032:            private static final MBeanAttributeInfo[] EMPTY_ATTRS = {};
033:            private static final MBeanConstructorInfo[] EMPTY_CTRS = {};
034:            private static final MBeanNotificationInfo[] EMPTY_NOTIFICATIONS = {};
035:            private static final MBeanOperationInfo[] EMPTY_OPS = {};
036:
037:            private static final long serialVersionUID = -1935722590756516193L;
038:
039:            private Descriptor modelMBeanDescriptor;
040:
041:            // The following data members are duplicated from parent class, since there is no way for
042:            // the serialization mechanism to set the deserialized objects to the parent object.
043:            private MBeanAttributeInfo[] modelMBeanAttributes;
044:            private MBeanConstructorInfo[] modelMBeanConstructors;
045:            private MBeanNotificationInfo[] modelMBeanNotifications;
046:            private MBeanOperationInfo[] modelMBeanOperations;
047:
048:            public ModelMBeanInfoSupport(String className, String description,
049:                    ModelMBeanAttributeInfo[] attributes,
050:                    ModelMBeanConstructorInfo[] constructors,
051:                    ModelMBeanOperationInfo[] operations,
052:                    ModelMBeanNotificationInfo[] notifications) {
053:                this (className, description, attributes, constructors,
054:                        operations, notifications, null);
055:            }
056:
057:            public ModelMBeanInfoSupport(String className, String description,
058:                    ModelMBeanAttributeInfo[] attributes,
059:                    ModelMBeanConstructorInfo[] constructors,
060:                    ModelMBeanOperationInfo[] operations,
061:                    ModelMBeanNotificationInfo[] notifications,
062:                    Descriptor mbeanDescriptor) {
063:                super (className, description, attributes, constructors,
064:                        operations, notifications);
065:                modelMBeanAttributes = attributes != null ? attributes
066:                        : EMPTY_ATTRS;
067:                modelMBeanConstructors = constructors != null ? constructors
068:                        : EMPTY_CTRS;
069:                modelMBeanNotifications = notifications != null ? notifications
070:                        : EMPTY_NOTIFICATIONS;
071:                modelMBeanOperations = operations != null ? operations
072:                        : EMPTY_OPS;
073:                checkAndSetDescriptor(mbeanDescriptor);
074:            }
075:
076:            public ModelMBeanInfoSupport(ModelMBeanInfo model) {
077:                super (model.getClassName(), model.getDescription(), model
078:                        .getAttributes(), model.getConstructors(), model
079:                        .getOperations(), model.getNotifications());
080:                if (model.getAttributes() != null) {
081:                    // cannot assume they are already ModelMBeanAttributeInfo
082:                    MBeanAttributeInfo attributes[] = model.getAttributes();
083:                    modelMBeanAttributes = new ModelMBeanAttributeInfo[attributes.length];
084:                    for (int i = 0; i < attributes.length; i++) {
085:                        MBeanAttributeInfo attribute = attributes[i];
086:                        if (attribute instanceof  ModelMBeanAttributeInfo)
087:                            modelMBeanAttributes[i] = new ModelMBeanAttributeInfo(
088:                                    (ModelMBeanAttributeInfo) attribute);
089:                        else
090:                            modelMBeanAttributes[i] = new ModelMBeanAttributeInfo(
091:                                    attribute.getName(), attribute.getType(),
092:                                    attribute.getDescription(), attribute
093:                                            .isReadable(), attribute
094:                                            .isWritable(), attribute.isIs());
095:                    }
096:                }
097:                if (model.getConstructors() != null) {
098:                    // cannot assume they are already ModelMBeanConstructorInfo
099:                    MBeanConstructorInfo constructors[] = model
100:                            .getConstructors();
101:                    modelMBeanConstructors = new ModelMBeanConstructorInfo[constructors.length];
102:                    for (int i = 0; i < constructors.length; i++) {
103:                        MBeanConstructorInfo constructor = constructors[i];
104:                        if (constructor instanceof  ModelMBeanConstructorInfo)
105:                            modelMBeanConstructors[i] = new ModelMBeanConstructorInfo(
106:                                    (ModelMBeanConstructorInfo) constructor);
107:                        else
108:                            modelMBeanConstructors[i] = new ModelMBeanConstructorInfo(
109:                                    constructor.getName(), constructor
110:                                            .getDescription(), constructor
111:                                            .getSignature());
112:                    }
113:                }
114:                if (model.getOperations() != null) {
115:                    // cannot assume they are already ModelMBeanOperationInfo
116:                    MBeanOperationInfo operations[] = model.getOperations();
117:                    modelMBeanOperations = new ModelMBeanOperationInfo[operations.length];
118:                    for (int i = 0; i < operations.length; i++) {
119:                        MBeanOperationInfo operation = operations[i];
120:                        if (operation instanceof  ModelMBeanOperationInfo)
121:                            modelMBeanOperations[i] = new ModelMBeanOperationInfo(
122:                                    (ModelMBeanOperationInfo) operation);
123:                        else
124:                            modelMBeanOperations[i] = new ModelMBeanOperationInfo(
125:                                    operation.getName(), operation
126:                                            .getDescription(), operation
127:                                            .getSignature(), operation
128:                                            .getReturnType(), operation
129:                                            .getImpact());
130:                    }
131:                }
132:                if (model.getNotifications() != null) {
133:                    // cannot assume they are already ModelMBeanNotificationInfo
134:                    MBeanNotificationInfo notifications[] = model
135:                            .getNotifications();
136:                    modelMBeanNotifications = new ModelMBeanNotificationInfo[notifications.length];
137:                    for (int i = 0; i < notifications.length; i++) {
138:                        MBeanNotificationInfo notification = notifications[i];
139:                        if (notification instanceof  ModelMBeanNotificationInfo)
140:                            modelMBeanNotifications[i] = new ModelMBeanNotificationInfo(
141:                                    (ModelMBeanNotificationInfo) notification);
142:                        else
143:                            modelMBeanNotifications[i] = new ModelMBeanNotificationInfo(
144:                                    notification.getNotifTypes(), notification
145:                                            .getName(), notification
146:                                            .getDescription());
147:                    }
148:                }
149:                Descriptor mBeanDescriptor = null;
150:                try {
151:                    mBeanDescriptor = model.getMBeanDescriptor();
152:                } catch (Exception e) {
153:                    // if there is an exception we use null
154:                }
155:                checkAndSetDescriptor(mBeanDescriptor);
156:            }
157:
158:            public Object clone() {
159:                return new ModelMBeanInfoSupport(this );
160:            }
161:
162:            public Descriptor[] getDescriptors(String type)
163:                    throws MBeanException, RuntimeOperationsException {
164:                // On the type the 'role' is not used, so for constructor and operation there
165:                // will be type=constructor and type=operation respectively
166:                // If type == null, means all descriptors
167:                if (type == null) {
168:                    Descriptor[] attrs = getDescriptors("attribute");
169:                    Descriptor[] opers = getDescriptors("operation");
170:                    Descriptor[] ctors = getDescriptors("constructor");
171:                    Descriptor[] notifs = getDescriptors("notification");
172:                    Descriptor[] all = new Descriptor[attrs.length
173:                            + opers.length + ctors.length + notifs.length + 1];
174:                    int i = 0;
175:                    all[i] = getMBeanDescriptor();
176:                    ++i;
177:                    System.arraycopy(attrs, 0, all, i, attrs.length);
178:                    i += attrs.length;
179:                    System.arraycopy(opers, 0, all, i, opers.length);
180:                    i += opers.length;
181:                    System.arraycopy(ctors, 0, all, i, ctors.length);
182:                    i += ctors.length;
183:                    System.arraycopy(notifs, 0, all, i, notifs.length);
184:
185:                    return all;
186:                } else if (type.equals("mbean")) {
187:                    return new Descriptor[] { getMBeanDescriptor() };
188:                } else if (type.equals("attribute")) {
189:                    MBeanAttributeInfo[] attrs = modelMBeanAttributes;
190:                    if (attrs == null) {
191:                        return new Descriptor[0];
192:                    }
193:                    Descriptor[] attributes = new Descriptor[attrs.length];
194:                    for (int i = 0; i < attrs.length; ++i) {
195:                        ModelMBeanAttributeInfo attr = (ModelMBeanAttributeInfo) attrs[i];
196:                        // It's already cloned
197:                        attributes[i] = attr.getDescriptor();
198:                    }
199:                    return attributes;
200:                } else if (type.equals("operation")) {
201:                    MBeanOperationInfo[] opers = modelMBeanOperations;
202:                    if (opers == null) {
203:                        return new Descriptor[0];
204:                    }
205:                    Descriptor[] operations = new Descriptor[opers.length];
206:                    for (int i = 0; i < opers.length; ++i) {
207:                        ModelMBeanOperationInfo oper = (ModelMBeanOperationInfo) opers[i];
208:                        // It's already cloned
209:                        operations[i] = oper.getDescriptor();
210:                    }
211:                    return operations;
212:                } else if (type.equals("constructor")) {
213:                    MBeanConstructorInfo[] ctors = modelMBeanConstructors;
214:                    if (ctors == null) {
215:                        return new Descriptor[0];
216:                    }
217:                    Descriptor[] constructors = new Descriptor[ctors.length];
218:                    for (int i = 0; i < ctors.length; ++i) {
219:                        ModelMBeanConstructorInfo ctor = (ModelMBeanConstructorInfo) ctors[i];
220:                        // It's already cloned
221:                        constructors[i] = ctor.getDescriptor();
222:                    }
223:                    return constructors;
224:                } else if (type.equals("notification")) {
225:                    MBeanNotificationInfo[] notifs = modelMBeanNotifications;
226:                    if (notifs == null) {
227:                        return new Descriptor[0];
228:                    }
229:                    Descriptor[] notifications = new Descriptor[notifs.length];
230:                    for (int i = 0; i < notifs.length; ++i) {
231:                        ModelMBeanNotificationInfo notif = (ModelMBeanNotificationInfo) notifs[i];
232:                        // It's already cloned
233:                        notifications[i] = notif.getDescriptor();
234:                    }
235:                    return notifications;
236:                } else {
237:                    throw new RuntimeOperationsException(
238:                            new IllegalArgumentException(
239:                                    "Invalid descriptor type"));
240:                }
241:            }
242:
243:            public void setDescriptors(Descriptor[] descriptors)
244:                    throws MBeanException, RuntimeOperationsException {
245:                if (descriptors == null) {
246:                    throw new RuntimeOperationsException(
247:                            new IllegalArgumentException(
248:                                    "Descriptors cannot be null"));
249:                }
250:                RuntimeOperationsException x = null;
251:                for (int i = 0; i < descriptors.length; ++i) {
252:                    // PENDING: what should I do in case of exception setting one descriptor ?
253:                    // Going on with the other descriptors or let the exception out ?
254:                    try {
255:                        setDescriptor(descriptors[i], null);
256:                    } catch (RuntimeOperationsException ignored) {
257:                        x = ignored;
258:                    }
259:                }
260:                // PENDING: don't know if this is a suitable solution, anyhow...
261:                if (x != null) {
262:                    throw x;
263:                }
264:            }
265:
266:            public Descriptor getDescriptor(String name) throws MBeanException,
267:                    RuntimeOperationsException {
268:                return getDescriptor(name, null);
269:            }
270:
271:            public Descriptor getDescriptor(String name, String type)
272:                    throws MBeanException, RuntimeOperationsException {
273:                if (name == null) {
274:                    throw new RuntimeOperationsException(
275:                            new IllegalArgumentException(
276:                                    "Descriptor name cannot be null"));
277:                }
278:                //if (type == null) {throw new RuntimeOperationsException(new IllegalArgumentException("Descriptor type cannot be null"));}
279:
280:                if ("MBean".equals(type)) {
281:                    return getMBeanDescriptor();
282:                } else if (type != null) {
283:                    Descriptor[] descrs = getDescriptors(type);
284:                    for (int i = 0; i < descrs.length; ++i) {
285:                        Descriptor descr = descrs[i];
286:                        if (name.equals(descr.getFieldValue("name"))) {
287:                            // Found, no need to clone it.
288:                            return descr;
289:                        }
290:                    }
291:                } else {
292:                    // will have to check them all
293:                    Descriptor result = findDescriptorByName(
294:                            modelMBeanAttributes, name);
295:                    if (result != null) {
296:                        return result;
297:                    }
298:                    result = findDescriptorByName(modelMBeanConstructors, name);
299:                    if (result != null) {
300:                        return result;
301:                    }
302:                    result = findDescriptorByName(modelMBeanNotifications, name);
303:                    if (result != null) {
304:                        return result;
305:                    }
306:                    result = findDescriptorByName(modelMBeanOperations, name);
307:                    if (result != null) {
308:                        return result;
309:                    }
310:                }
311:                return null;
312:            }
313:
314:            public void setDescriptor(Descriptor descriptor,
315:                    String descriptorType) throws MBeanException,
316:                    RuntimeOperationsException {
317:                // PENDING: should throw instead of returning ?
318:                if (descriptor == null) {
319:                    return;
320:                }
321:                if (descriptorType == null) {
322:                    descriptorType = (String) descriptor
323:                            .getFieldValue("descriptorType");
324:                    // Still null ?
325:                    if (descriptorType == null) {
326:                        throw new RuntimeOperationsException(
327:                                new IllegalArgumentException(
328:                                        "Field descriptorType in the given descriptor is not valid"));
329:                    }
330:
331:                    if (descriptorType.equals("operation")) {
332:                        // Take the role to distinguish between operation and constructor
333:                        String role = (String) descriptor.getFieldValue("role");
334:                        if (role == null) {
335:                            throw new RuntimeOperationsException(
336:                                    new IllegalArgumentException(
337:                                            "Field role in the given descriptor is not valid"));
338:                        }
339:
340:                        descriptorType = role;
341:                    }
342:                }
343:
344:                String name = (String) descriptor.getFieldValue("name");
345:                if (name == null) {
346:                    throw new RuntimeOperationsException(
347:                            new IllegalArgumentException(
348:                                    "Field name in the given descriptor is not valid"));
349:                }
350:
351:                // Now decide from the descriptor type where to find the right descriptor
352:                if (descriptorType.equals("MBean")) {
353:                    setMBeanDescriptor(descriptor);
354:                } else if (descriptorType.equals("attribute")) {
355:                    MBeanAttributeInfo[] attrs = modelMBeanAttributes;
356:                    if (attrs != null) {
357:                        for (int i = 0; i < attrs.length; ++i) {
358:                            ModelMBeanAttributeInfo attr = (ModelMBeanAttributeInfo) attrs[i];
359:                            if (name.equals(attr.getName())) {
360:                                // Found the right one
361:                                attr.setDescriptor(descriptor);
362:                                break;
363:                            }
364:                        }
365:                    }
366:                } else if (descriptorType.equals("notification")) {
367:                    MBeanNotificationInfo[] notifs = modelMBeanNotifications;
368:                    if (notifs != null) {
369:                        for (int i = 0; i < notifs.length; ++i) {
370:                            ModelMBeanNotificationInfo notif = (ModelMBeanNotificationInfo) notifs[i];
371:                            if (name.equals(notif.getName())) {
372:                                // Found the right one
373:                                notif.setDescriptor(descriptor);
374:                                break;
375:                            }
376:                        }
377:                    }
378:                } else if (descriptorType.equals("constructor")) {
379:                    MBeanConstructorInfo[] ctors = modelMBeanConstructors;
380:                    if (ctors != null) {
381:                        for (int i = 0; i < ctors.length; ++i) {
382:                            ModelMBeanConstructorInfo ctor = (ModelMBeanConstructorInfo) ctors[i];
383:                            if (name.equals(ctor.getName())) {
384:                                // Found the right one
385:                                ctor.setDescriptor(descriptor);
386:                                break;
387:                            }
388:                        }
389:                    }
390:                } else if (descriptorType.equals("operation")/* || descriptorType.equals("getter") || descriptorType.equals("setter")*/) {
391:                    MBeanOperationInfo[] opers = modelMBeanOperations;
392:                    if (opers != null) {
393:                        for (int i = 0; i < opers.length; ++i) {
394:                            ModelMBeanOperationInfo oper = (ModelMBeanOperationInfo) opers[i];
395:                            if (name.equals(oper.getName())) {
396:                                // Found the right one
397:                                oper.setDescriptor(descriptor);
398:                                break;
399:                            }
400:                        }
401:                    }
402:                }
403:            }
404:
405:            public ModelMBeanAttributeInfo getAttribute(String name)
406:                    throws MBeanException, RuntimeOperationsException {
407:                if (name == null) {
408:                    throw new RuntimeOperationsException(
409:                            new IllegalArgumentException(
410:                                    "Name argument cannot be null"));
411:                }
412:                MBeanAttributeInfo[] attrs = modelMBeanAttributes;
413:                if (attrs != null) {
414:                    for (int i = 0; i < attrs.length; ++i) {
415:                        ModelMBeanAttributeInfo attr = (ModelMBeanAttributeInfo) attrs[i];
416:                        if (name.equals(attr.getName())) {
417:                            // Clone, since the returned attribute is modifiable
418:                            return (ModelMBeanAttributeInfo) attr.clone();
419:                        }
420:                    }
421:                }
422:                // Not found, return null
423:                return null;
424:            }
425:
426:            public ModelMBeanOperationInfo getOperation(String name)
427:                    throws MBeanException, RuntimeOperationsException {
428:                if (name == null) {
429:                    throw new RuntimeOperationsException(
430:                            new IllegalArgumentException(
431:                                    "Name argument cannot be null"));
432:                }
433:                MBeanOperationInfo[] opers = modelMBeanOperations;
434:                if (opers != null) {
435:                    for (int i = 0; i < opers.length; ++i) {
436:                        ModelMBeanOperationInfo oper = (ModelMBeanOperationInfo) opers[i];
437:                        if (name.equals(oper.getName())) {
438:                            // Clone, since the returned operation is modifiable
439:                            return (ModelMBeanOperationInfo) oper.clone();
440:                        }
441:                    }
442:                }
443:                // Not found, return null
444:                return null;
445:            }
446:
447:            public ModelMBeanConstructorInfo getConstructor(String name)
448:                    throws MBeanException, RuntimeOperationsException {
449:                if (name == null) {
450:                    throw new RuntimeOperationsException(
451:                            new IllegalArgumentException(
452:                                    "Name argument cannot be null"));
453:                }
454:                MBeanConstructorInfo[] ctors = modelMBeanConstructors;
455:                if (ctors != null) {
456:                    for (int i = 0; i < ctors.length; ++i) {
457:                        ModelMBeanConstructorInfo ctor = (ModelMBeanConstructorInfo) ctors[i];
458:                        if (name.equals(ctor.getName())) {
459:                            // Clone, since the returned operation is modifiable
460:                            return (ModelMBeanConstructorInfo) ctor.clone();
461:                        }
462:                    }
463:                }
464:                // Not found, return null
465:                return null;
466:            }
467:
468:            public ModelMBeanNotificationInfo getNotification(String name)
469:                    throws MBeanException, RuntimeOperationsException {
470:                if (name == null) {
471:                    throw new RuntimeOperationsException(
472:                            new IllegalArgumentException(
473:                                    "Name argument cannot be null"));
474:                }
475:                MBeanNotificationInfo[] notifs = modelMBeanNotifications;
476:                if (notifs != null) {
477:                    for (int i = 0; i < notifs.length; ++i) {
478:                        ModelMBeanNotificationInfo notif = (ModelMBeanNotificationInfo) notifs[i];
479:                        if (name.equals(notif.getName())) {
480:                            // Clone, since the returned operation is modifiable
481:                            return (ModelMBeanNotificationInfo) notif.clone();
482:                        }
483:                    }
484:                }
485:                // Not found, return null
486:                return null;
487:            }
488:
489:            public Descriptor getMBeanDescriptor() throws MBeanException,
490:                    RuntimeOperationsException {
491:                return (Descriptor) modelMBeanDescriptor.clone();
492:            }
493:
494:            public void setMBeanDescriptor(Descriptor descriptor)
495:                    throws MBeanException, RuntimeOperationsException {
496:                if (descriptor == null) {
497:                    // Replace with default descriptor
498:                    modelMBeanDescriptor = createDefaultMBeanDescriptor();
499:                } else {
500:                    if (isDescriptorValid(descriptor)) {
501:                        modelMBeanDescriptor = (Descriptor) descriptor.clone();
502:                    } else {
503:                        throw new RuntimeOperationsException(
504:                                new IllegalArgumentException(
505:                                        "Invalid descriptor"));
506:                    }
507:                }
508:            }
509:
510:            public MBeanConstructorInfo[] getConstructors() {
511:                // I should clone, since MBeanConstructorInfo is immutable, but ModelMBeanConstructorInfo it isn't
512:                MBeanConstructorInfo[] ctors = modelMBeanConstructors;
513:                if (ctors == null) {
514:                    return null;
515:                }
516:                ModelMBeanConstructorInfo[] constructors = new ModelMBeanConstructorInfo[ctors.length];
517:                for (int i = 0; i < ctors.length; ++i) {
518:                    ModelMBeanConstructorInfo ctor = (ModelMBeanConstructorInfo) ctors[i];
519:                    constructors[i] = (ModelMBeanConstructorInfo) ctor.clone();
520:                }
521:                return constructors;
522:            }
523:
524:            public MBeanAttributeInfo[] getAttributes() {
525:                // I should clone, since MBeanAttributeInfo is immutable, but ModelMBeanAttributeInfo it isn't
526:                MBeanAttributeInfo[] attrs = modelMBeanAttributes;
527:                if (attrs == null) {
528:                    return null;
529:                }
530:                ModelMBeanAttributeInfo[] attributes = new ModelMBeanAttributeInfo[attrs.length];
531:                for (int i = 0; i < attrs.length; ++i) {
532:                    ModelMBeanAttributeInfo attr = (ModelMBeanAttributeInfo) attrs[i];
533:                    attributes[i] = (ModelMBeanAttributeInfo) attr.clone();
534:                }
535:                return attributes;
536:            }
537:
538:            public MBeanOperationInfo[] getOperations() {
539:                // I should clone, since MBeanOperationInfo is immutable, but ModelMBeanOperationInfo it isn't
540:                MBeanOperationInfo[] opers = modelMBeanOperations;
541:                if (opers == null) {
542:                    return null;
543:                }
544:                ModelMBeanOperationInfo[] operations = new ModelMBeanOperationInfo[opers.length];
545:                for (int i = 0; i < opers.length; ++i) {
546:                    ModelMBeanOperationInfo oper = (ModelMBeanOperationInfo) opers[i];
547:                    operations[i] = (ModelMBeanOperationInfo) oper.clone();
548:                }
549:                return operations;
550:            }
551:
552:            public MBeanNotificationInfo[] getNotifications() {
553:                // I should clone, since MBeanNotificationInfo is immutable, but ModelMBeanNotificationInfo it isn't
554:                MBeanNotificationInfo[] notifs = modelMBeanNotifications;
555:                if (notifs == null) {
556:                    return null;
557:                }
558:                ModelMBeanNotificationInfo[] notifications = new ModelMBeanNotificationInfo[notifs.length];
559:                for (int i = 0; i < notifs.length; ++i) {
560:                    ModelMBeanNotificationInfo notif = (ModelMBeanNotificationInfo) notifs[i];
561:                    notifications[i] = (ModelMBeanNotificationInfo) notif
562:                            .clone();
563:                }
564:                return notifications;
565:            }
566:
567:            private void checkAndSetDescriptor(Descriptor descriptor) {
568:                if (descriptor == null) {
569:                    modelMBeanDescriptor = createDefaultMBeanDescriptor();
570:                } else if (isDescriptorValid(descriptor)) {
571:                    modelMBeanDescriptor = addRequiredFields(descriptor);
572:                } else {
573:                    throw new RuntimeOperationsException(
574:                            new IllegalArgumentException("Invalid Descriptor"));
575:                }
576:            }
577:
578:            private boolean isDescriptorValid(Descriptor descriptor) {
579:                if (!descriptor.isValid()) {
580:                    return false;
581:                }
582:
583:                // Spec compliance checks
584:
585:                // Mandatory fields are: name, descriptorType, persistLocation(?), persistName(?), log(?), logFile(?)
586:                String[] names = descriptor.getFieldNames();
587:
588:                if (!containsIgnoreCase(names, "name")
589:                        || !containsIgnoreCase(names, "descriptortype")/* ||
590:                                 !containsIgnoreCase(names, "persistlocation") ||
591:                                 !containsIgnoreCase(names, "persistname") ||
592:                                 !containsIgnoreCase(names, "log") ||
593:                                 !containsIgnoreCase(names, "logfile")*/) {
594:                    return false;
595:                }
596:
597:                // The spec is unclear on what the field name should contain: the name of the MBean or its class name ?
598:                // For now I stay loose, but since it is a dynamic MBean the className cannot be null; this check will be done
599:                // by RequiredModelMBean
600:                //		String name = getClassName();
601:                //		if (name == null) {return false;}
602:                //		if (!name.equals(descriptor.getFieldValue("name"))) {return false;}
603:
604:                // Descriptor type must be 'MBean'
605:                String desctype = (String) descriptor
606:                        .getFieldValue("descriptortype");
607:                if (desctype.compareToIgnoreCase("mbean") != 0)
608:                    return false;
609:
610:                return true;
611:            }
612:
613:            private Descriptor createDefaultMBeanDescriptor() {
614:                // The spec and the javadoc are misaligned WRT the default mbean descriptor:
615:                // Spec says the values of fields are case sensitive, javadoc does not care...
616:                // For field 'export', spec says that a value of null means not visible to other Agent and that any other value means
617:                // that is visible, while javadoc says 'F' means not visible...
618:                // Go with the Javadoc to mimic the RI (WkH)
619:                String[] names = new String[] { "name", "descriptorType",
620:                        "displayName", "persistPolicy", "log", "export",
621:                        "visibility" };
622:                int index = getClassName().lastIndexOf('.') + 1;
623:                Object[] values = new Object[] {
624:                        getClassName().substring(index), "MBean",
625:                        getClassName(), "Never", "F", "F", "1" };
626:                return new DescriptorSupport(names, values);
627:            }
628:
629:            private Descriptor findDescriptorByName(
630:                    MBeanFeatureInfo[] features, String name) {
631:                if (features != null) {
632:                    for (int i = 0; i < features.length; ++i) {
633:                        MBeanFeatureInfo feature = features[i];
634:                        if (feature != null && feature.getName().equals(name)
635:                                && feature instanceof  DescriptorAccess) {
636:                            return ((DescriptorAccess) feature).getDescriptor();
637:                        }
638:                    }
639:                }
640:                return null;
641:            }
642:
643:            private Descriptor addRequiredFields(Descriptor d) {
644:                Descriptor result = (Descriptor) d.clone();
645:                String[] reqfields = { "displayname", "persistpolicy", "log",
646:                        "export", "visibility" };
647:                String[] defvalues = { (String) d.getFieldValue("name"),
648:                        "never", "F", "F", "1" };
649:                List fields = Arrays.asList(d.getFieldNames());
650:                for (int i = 0; i < reqfields.length; i++) {
651:                    if (fields.contains(reqfields[i]) == false) {
652:                        result.setField(reqfields[i], defvalues[i]);
653:                    }
654:                }
655:                return result;
656:            }
657:
658:            static boolean containsIgnoreCase(String[] fields, String field) {
659:                for (int i = 0; i < fields.length; ++i) {
660:                    if (fields[i].equalsIgnoreCase(field))
661:                        return true;
662:                }
663:                return false;
664:            }
665:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.