Source Code Cross Referenced for DescriptorSupport.java in  » JMX » XMOJO » 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 » XMOJO » javax.management.modelmbean 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * The XMOJO Project 5
003:         * Copyright © 2003 XMOJO.org. All rights reserved.
004:
005:         * NO WARRANTY
006:
007:         * BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR
008:         * THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
009:         * OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
010:         * PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
011:         * OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
012:         * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
013:         * TO THE QUALITY AND PERFORMANCE OF THE LIBRARY IS WITH YOU. SHOULD THE
014:         * LIBRARY PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
015:         * REPAIR OR CORRECTION.
016:
017:         * IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL
018:         * ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE
019:         * THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
020:         * GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
021:         * USE OR INABILITY TO USE THE LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF
022:         * DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
023:         * PARTIES OR A FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE),
024:         * EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
025:         * SUCH DAMAGES.
026:         **/package javax.management.modelmbean;
027:
028:        import java.io.ObjectInputStream;
029:        import java.io.ObjectInputStream.GetField;
030:        import java.io.ObjectOutputStream;
031:        import java.io.ObjectOutputStream.PutField;
032:        import java.io.ObjectStreamField;
033:        import java.io.IOException;
034:        import java.util.HashMap;
035:        import java.util.StringTokenizer;
036:
037:        import javax.management.Descriptor;
038:        import javax.management.MBeanException;
039:        import javax.management.RuntimeOperationsException;
040:
041:        import com.adventnet.utils.jmx.Utilities;
042:
043:        /**
044:         * This class represents the metadata set for a ModelMBean element. A
045:         * descriptor is part of the ModelMBeanInfo, ModelMBeanNotificationInfo,
046:         * ModelMBeanAttributeInfo, ModelMBeanConstructorInfo, and ModelMBeanParameterInfo.
047:         * <p>
048:         * A descriptor consists of a collection of fields. Each field is in
049:         * fieldname=fieldvalue format. Field names are not case sensitive, case will
050:         * be preserved on field values.
051:         * <p>
052:         * All field names and values are not predefined. New fields can be defined
053:         * and added by any program. Some fields have been predefined for consistency
054:         * of implmentation and support by the ModelMBeanInfo ModelMBean*Info, and
055:         * ModelMBean classes.
056:         * <p>
057:         * By default the descriptor store size is 20 fields. This is configurable.
058:         */
059:        public class DescriptorSupport implements  Descriptor {
060:            public String currClass = "DescriptorSupport";
061:
062:            static final long serialVersionUID = 8071560848919417985L;
063:
064:            HashMap store = null;
065:
066:            int DEFAULT_NUM_FIELDS = 20;
067:
068:            static String NAME = "name";
069:            static String DESCRIPTOR_TYPE = "descriptorType";
070:            static String VALUE = "value";
071:            static String DEFAULT = "default";
072:            static String DISPLAY_NAME = "displayName";
073:            static String GET_METHOD = "getMethod";
074:            static String SET_METHOD = "setMethod";
075:            static String PROTOCOL_MAP = "protocolMap";
076:            static String PERSIST_POLICY = "persistPolicy";
077:            static String PERSIST_PERIOD = "persistPeriod";
078:            static String CURRENCY_TIME_LIMIT = "currencyTimeLimit";
079:            static String LAST_UPDATED_TIMESTAMP = "lastUpdatedTimeStamp";
080:            static String ITERABLE = "iterable";
081:            static String VISIBILITY = "visibility";
082:            static String PRESENTATION_STRING = "presentationString";
083:
084:            static String ROLE = "role";
085:            static String CLASS = "class";
086:
087:            static String SEVERITY = "severity";
088:            static String MESSAGE_ID = "messageID";
089:            static String MESSAGE_TEXT = "messageText";
090:            static String LOG = "log";
091:            static String LOG_FILE = "logfile";
092:
093:            static String TARGET_OBJECT = "targetObject";
094:            static String TARGET_TYPE = "targetObjectType";
095:            static String LAST_RETURNED_VALUE = "lastReturnedValue";
096:
097:            private static final ObjectStreamField[] serialPersistentFields = {
098:                    new ObjectStreamField("descriptor", java.util.HashMap.class),
099:                    new ObjectStreamField("currClass", java.lang.String.class) };
100:
101:            /**
102:             * Descriptor default constructor.
103:             */
104:            public DescriptorSupport() {
105:                store = new HashMap(DEFAULT_NUM_FIELDS);
106:            }
107:
108:            /**
109:             * Descriptor constructor. Takes as parameter the initial capacity of the
110:             * Map that stores the descriptor fields.
111:             *
112:             * @param initNumFields The initial capacity of the Map that stores
113:             * 				the descriptor fields.
114:             */
115:            public DescriptorSupport(int initNumFields) throws MBeanException,
116:                    RuntimeOperationsException {
117:                if (initNumFields <= 0)
118:                    throw new RuntimeOperationsException(new RuntimeException(
119:                            "Wrong initNumFields to DescriptorSupport"));
120:
121:                store = new HashMap(initNumFields);
122:            }
123:
124:            /**
125:             * Descriptor constructor taking a Descriptor as parameter. Creates a new
126:             * descriptor initialized to the values of the descriptor passed in parameter.
127:             *
128:             * @param inDescr the descriptor to be used to initialize the constructed
129:             * 				descriptor. If it is null or contains no descriptor
130:             * 				fields, an empty Descriptor will be created.
131:             *
132:             * @exception RuntimeOperationsException for illegal value for inDescr.
133:             * 				inDescr cannot be null. If the descriptor fails for any
134:             * 				reason, this exception will be thrown.
135:             */
136:            public DescriptorSupport(DescriptorSupport inDescr) {
137:                if (inDescr == null)
138:                    throw new RuntimeOperationsException(new RuntimeException(
139:                            "invalid DescriptorSupport"));
140:
141:                store = new HashMap(inDescr.store);
142:            }
143:
144:            /**
145:             * Descriptor constructor taking an XML String. This method is part of
146:             * this implementation and not required by the JMX Specification.
147:             *
148:             * @param inStr - An XML-formatted string used to populate this Descriptor
149:             */
150:            public DescriptorSupport(String inStr) throws MBeanException,
151:                    RuntimeOperationsException, XMLParseException {
152:                if (inStr == null) {
153:                    throw new RuntimeOperationsException(
154:                            new IllegalArgumentException(
155:                                    "The String parameter passed should not be null"));
156:                }
157:
158:                try {
159:                    ModelMBeanInfo info = Utilities
160:                            .convertXmlToModelMBeanInfo(inStr);
161:                } catch (Exception e) {
162:                    throw new XMLParseException();
163:                }
164:            }
165:
166:            /**
167:             * Constructor taking field names and field values. The array and array
168:             * elements cannot be null.
169:             *
170:             * @param fieldNames String array of field names. No elements of this
171:             * 				array can be null.
172:             *
173:             * @param fieldValues Object array of the corresponding field values.
174:             * 				Elements of the array can be null. The fieldValue must be
175:             * 				valid for the fieldName.
176:             * 				Note:
177:             * 				array sizes of parameters should match. If both arrays
178:             * 				are null or empty, then an empty descriptor is created.
179:             * 				No exception is thrown.
180:             *
181:             * @exception RuntimeOperationsException for illegal value for field
182:             * 				Names or field Values. The array lengths must be equal.
183:             * 				If the descriptor construction fails for any reason,
184:             * 				this exception will be thrown.
185:             */
186:            public DescriptorSupport(String[] fieldNames, Object[] fieldValues)
187:                    throws RuntimeOperationsException {
188:                this ();
189:
190:                if (fieldNames == null || fieldValues == null
191:                        || (fieldNames.length != fieldValues.length))
192:                    throw new RuntimeOperationsException(new RuntimeException(
193:                            "Invalid fieldNames|fieldValues as arguments"));
194:
195:                for (int i = 0; i < fieldNames.length; i++) {
196:                    if (fieldNames[i] == null)
197:                        throw new RuntimeOperationsException(
198:                                new RuntimeException("FieldName cannot be null"));
199:                }
200:
201:                for (int i = 0; i < fieldValues.length; i++) {
202:                    if (fieldValues[i] == null)
203:                        throw new RuntimeOperationsException(
204:                                new RuntimeException(
205:                                        "FieldValue cannot be null"));
206:                }
207:
208:                for (int i = 0; i < fieldNames.length; i++)
209:                    store.put(fieldNames[i].toLowerCase(), fieldValues[i]);
210:            }
211:
212:            /**
213:             * Constructor taking fields in the fieldName=fieldValue format.
214:             *
215:             * @param fields String array of with each element containing a field name
216:             * 				and value. If this array is null or empty, then the
217:             * 				default constructor will be executed. Null strings or
218:             * 				empty strings will be ignored.
219:             * 				Note:
220:             * 				each string should be of the form fieldName=fieldValue,
221:             * 				with no imbedded blanks or other punctuation.
222:             *
223:             * @exception RuntimeOperationsException for illegal value for field Names
224:             * 				or field Values. The field must contain an "=".
225:             * 				"=fieldValue", "fieldName", and "fieldValue" are illegal.
226:             * 				FieldName cannot be null. "fieldName=" will cause the
227:             * 				value to be null. If the descriptor construction fails
228:             * 				for any reason, this exception will be thrown.
229:             */
230:            public DescriptorSupport(String[] fields) {
231:                this ();
232:
233:                if (fields == null)
234:                    throw new RuntimeOperationsException(new RuntimeException(
235:                            "Invalid fields as arguments"));
236:
237:                String[] fieldNames = new String[fields.length];
238:                String[] fieldValues = new String[fields.length];
239:
240:                try {
241:                    for (int i = 0; i < fields.length; i++) {
242:                        StringTokenizer strTok = new StringTokenizer(fields[i],
243:                                "=");
244:                        fieldNames[i] = strTok.nextToken();
245:                        fieldValues[i] = strTok.nextToken();
246:                    }
247:                } catch (Exception e) {
248:                    throw new RuntimeOperationsException(new RuntimeException(
249:                            "Invalid fields as arguments"));
250:                }
251:
252:                for (int i = 0; i < fieldNames.length; i++)
253:                    store.put(fieldNames[i].toLowerCase(), fieldValues[i]);
254:            }
255:
256:            /**
257:             * Returns the value for a specific fieldname.
258:             *
259:             * @param fieldName The field name in question; if not found, null is returned.
260:             *
261:             * @return This returns the field name of type Object
262:             *
263:             * @exception RuntimeOperationsException for illegal value for field name.
264:             */
265:            public Object getFieldValue(String fieldName)
266:                    throws RuntimeOperationsException {
267:                if (fieldName == null)
268:                    throw new RuntimeOperationsException(new RuntimeException(
269:                            "Invalid fieldName"));
270:
271:                return store.get(fieldName.toLowerCase());
272:            }
273:
274:            /**
275:             * Sets the value for a specific fieldname. The field value will be
276:             * validated before it is set.  If it is not valid, then an exception will
277:             * be thrown. This will modify an existing field or add a new field.
278:             *
279:             * @param  fieldName: The field name to be set. Cannot be null or empty.
280:             *
281:             * @param  fieldValue: The field value to be set for the field name. Can be null.
282:             *
283:             * @exception RuntimeOperationsException for illegal value for
284:             * 				field name or field value.
285:             */
286:            public void setField(String fieldName, Object fieldValue)
287:                    throws RuntimeOperationsException {
288:                if (fieldName == null)
289:                    throw new RuntimeOperationsException(new RuntimeException(
290:                            "Invalid fieldName|fieldValue"));
291:
292:                store.put(fieldName.toLowerCase(), fieldValue);
293:            }
294:
295:            /**
296:             * Returns all of the  fields contained in this descriptor as a string array.
297:             *
298:             * @return String array of fields in the format fieldName=fieldValue.
299:             * 				If the value of a field is not a String, then the toString
300:             * 				method will be called on it and the returned value used
301:             * 				as the value for the field in the returned array. Object
302:             * 				values which are not Strings will be enclosed in parens.
303:             * 				If the descriptor is empty, you will get an empty array.
304:             */
305:            public String[] getFields() {
306:                Object[] fieldNames = store.keySet().toArray();
307:                String[] toRet = new String[fieldNames.length];
308:
309:                for (int i = 0; i < fieldNames.length; i++) {
310:                    String field = (String) fieldNames[i];
311:
312:                    Object fieldValue = store.get(field.toLowerCase());
313:                    if (fieldValue != null) {
314:                        if (fieldValue instanceof  String)
315:                            field = field + "=" + (String) fieldValue;
316:                        else
317:                            field = field + "=(" + fieldValue.toString() + ")";
318:                    } else {
319:                        field = field + "=" + "null";
320:                    }
321:
322:                    toRet[i] = field;
323:                }
324:
325:                return toRet;
326:            }
327:
328:            /**
329:             * Returns all the fields names in the descriptor.
330:             *
331:             * @return String array of fields names. If the descriptor is empty,
332:             * 				you will get an empty array.
333:             */
334:            public String[] getFieldNames() {
335:                Object[] objArr = store.keySet().toArray();
336:                String[] toRet = new String[objArr.length];
337:
338:                for (int i = 0; i < objArr.length; i++)
339:                    toRet[i] = (String) objArr[i];
340:
341:                return toRet;
342:            }
343:
344:            /**
345:             * Returns all the field values in the descriptor as an array of Objects.
346:             * The retuned values are in the same order as the fieldNames String array
347:             * parameter.
348:             *
349:             * @param fieldNames String array of the names of the fields that the
350:             * 				values should be returned for.  If the array is empty then
351:             * 				an empty array will be returned.  If the array is 'null'
352:             * 				then all values will be returned.  If a field name in the
353:             * 				array does not exist, then null is returned for the
354:             * 				matching array element being returned.
355:             *
356:             * @return Object array of field values. If the descriptor is empty,
357:             * 				you will get an empty array.
358:             */
359:            public Object[] getFieldValues(String[] fieldNames) {
360:                if (fieldNames == null)
361:                    return store.values().toArray();
362:
363:                Object[] fieldValues = new Object[fieldNames.length];
364:
365:                for (int i = 0; i < fieldNames.length; i++)
366:                    fieldValues[i] = store.get(fieldNames[i].toLowerCase());
367:
368:                return fieldValues;
369:            }
370:
371:            /**
372:             * Removes a field from the descriptor
373:             *
374:             * @param fieldName String name of the field to be removed. If the field
375:             * 				is not found no exception is thrown.
376:             */
377:            public void removeField(String fieldName) {
378:                store.remove(fieldName);
379:            }
380:
381:            /**
382:             * Sets all Fields in the list to the new value in with the same index in
383:             * the fieldValue array.  Array sizes must match. The field value will be
384:             * validated before it is set. If it is not valid, then an exception will
385:             * be thrown. If the arrays are empty, then no change will take effect.
386:             *
387:             * @param fieldNames String array of field names. The array and array
388:             * 				elements cannot be null.
389:             *
390:             * @param fieldValues Object array of the corresponding field values.
391:             * 				The array cannot be null. Elements of the array can be null.
392:             *
393:             * @exception RuntimeOperationsException for illegal value for field Names
394:             * 				or field Values. Neither can be null.  The array lengths
395:             * 				must be equal. If the descriptor construction fails for
396:             * 				any reason, this exception will be thrown.
397:             */
398:            public void setFields(String[] fieldNames, Object[] fieldValues)
399:                    throws RuntimeOperationsException {
400:                if (fieldNames == null || fieldValues == null
401:                        || (fieldNames.length != fieldValues.length))
402:                    throw new RuntimeOperationsException(new RuntimeException(
403:                            "Invalid fieldNames|fieldValues as arguments"));
404:
405:                for (int i = 0; i < fieldNames.length; i++) {
406:                    if (fieldNames[i] == null)
407:                        throw new RuntimeOperationsException(
408:                                new RuntimeException("FieldName cannot be null"));
409:                }
410:
411:                for (int i = 0; i < fieldValues.length; i++) {
412:                    if (fieldValues[i] == null)
413:                        throw new RuntimeOperationsException(
414:                                new RuntimeException(
415:                                        "FieldValue cannot be null"));
416:                }
417:
418:                for (int i = 0; i < fieldNames.length; i++)
419:                    store.put(fieldNames[i].toLowerCase(), fieldValues[i]);
420:            }
421:
422:            /**
423:             * Returns a new Descriptor which is a duplicate of the Descriptor.
424:             *
425:             * @return This returns a duplicate copy of the Descriptor
426:             *
427:             * @exception RuntimeOperationsException for illegal value for field Names
428:             * 				or field Values. If the descriptor construction fails for
429:             * 				any reason, this exception will be thrown.
430:             */
431:            public Object clone() throws RuntimeOperationsException {
432:                Descriptor clonedDescr = new DescriptorSupport();
433:
434:                String[] fieldNames = getFieldNames();
435:
436:                for (int i = 0; i < fieldNames.length; i++) {
437:                    String fieldName = fieldNames[i];
438:                    Object fieldValue = getFieldValue(fieldName);
439:
440:                    if (fieldValue instanceof  Descriptor)
441:                        fieldValue = ((Descriptor) fieldValue).clone();
442:
443:                    clonedDescr.setField(fieldName, fieldValue);
444:                }
445:
446:                return clonedDescr;
447:            }
448:
449:            /**
450:             * Returns true if fieldValues are checked to be sure they are legal for
451:             * the fieldNames.
452:             *
453:             * @return This returns true if the fieldValues checked are valid otherwise false
454:             *
455:             * @exception RuntimeOperationsException If the validity checking fails
456:             * 				for any reason, this exception will be thrown.
457:             */
458:            public boolean isValid() throws RuntimeOperationsException {
459:                Object[] fieldNames = store.keySet().toArray();
460:
461:                for (int i = 0; i < fieldNames.length; i++) {
462:                    String field = (String) fieldNames[i];
463:                    Object fieldValue = store.get(field.toLowerCase());
464:
465:                    if (field.equalsIgnoreCase(DESCRIPTOR_TYPE))
466:                        checkDescriptorType(DESCRIPTOR_TYPE, fieldValue);
467:
468:                    else if (field.equalsIgnoreCase(DISPLAY_NAME))
469:                        checkIsString(DISPLAY_NAME, fieldValue);
470:
471:                    else if (field.equalsIgnoreCase(GET_METHOD))
472:                        checkIsString(GET_METHOD, fieldValue);
473:
474:                    else if (field.equalsIgnoreCase(SET_METHOD))
475:                        checkIsString(SET_METHOD, fieldValue);
476:
477:                    else if (field.equalsIgnoreCase(PROTOCOL_MAP))
478:                        checkIsDescriptor(PROTOCOL_MAP, fieldValue);
479:
480:                    else if (field.equalsIgnoreCase(PERSIST_POLICY))
481:                        checkPersistPolicy(PERSIST_POLICY, fieldValue);
482:
483:                    else if (field.equalsIgnoreCase(CURRENCY_TIME_LIMIT))
484:                        checkIsInt(CURRENCY_TIME_LIMIT, fieldValue);
485:
486:                    else if (field.equalsIgnoreCase(LAST_UPDATED_TIMESTAMP))
487:                        checkIsInt(LAST_UPDATED_TIMESTAMP, fieldValue);
488:
489:                    else if (field.equalsIgnoreCase(ITERABLE))
490:                        checkIsBoolean(ITERABLE, fieldValue);
491:
492:                    else if (field.equalsIgnoreCase(VISIBILITY))
493:                        checkVisibility(VISIBILITY, fieldValue);
494:
495:                    else if (field.equalsIgnoreCase(PRESENTATION_STRING))
496:                        checkIsString(PRESENTATION_STRING, fieldValue);
497:
498:                    else if (field.equalsIgnoreCase(ROLE))
499:                        checkRole(ROLE, fieldValue);
500:
501:                    else if (field.equalsIgnoreCase(CLASS))
502:                        checkIsString(CLASS, fieldValue);
503:
504:                    else if (field.equalsIgnoreCase(SEVERITY))
505:                        checkSeverity(SEVERITY, fieldValue);
506:
507:                    else if (field.equalsIgnoreCase(MESSAGE_ID))
508:                        checkIsString(MESSAGE_ID, fieldValue);
509:
510:                    else if (field.equalsIgnoreCase(MESSAGE_TEXT))
511:                        checkIsString(MESSAGE_TEXT, fieldValue);
512:
513:                    else if (field.equalsIgnoreCase(LOG))
514:                        checkIsBoolean(LOG, fieldValue);
515:
516:                    else if (field.equalsIgnoreCase(LOG_FILE))
517:                        checkIsString(LOG_FILE, fieldValue);
518:
519:                    else if (field.equalsIgnoreCase(TARGET_TYPE))
520:                        checkTargetType(TARGET_TYPE, fieldValue);
521:                }
522:
523:                return true;
524:            }
525:
526:            /**
527:             * This method returns the XML which represents the descriptor.
528:             *
529:             * @return an XML String representing the descriptor.The XML string will
530:             * 				be formatted for each field with " NAME=name
531:             * 				VALUE=fieldValue TYPE=String|Object|ClassName>". The format
532:             * 				will be:
533:             * 				<Descriptor><field name="name" value="IfTable"/>
534:             * 				<field name="isReadable" value="T"/></Descriptor>
535:             *
536:             * @exception RuntimeOperationsException thrown for illegal value for field
537:             * 				Names or field Values. If the XML formated string
538:             * 				construction fails for any reason, this will be thrown.
539:             */
540:            public String toXMLString() {
541:                StringBuffer buff = new StringBuffer();
542:                buff.append("<Descriptor>");
543:
544:                Object[] fieldNames = store.keySet().toArray();
545:
546:                for (int i = 0; i < fieldNames.length; i++) {
547:                    String field = (String) fieldNames[i];
548:                    if (field == null)
549:                        throw new RuntimeOperationsException(
550:                                new RuntimeException("FieldName cannot be null"));
551:                    buff.append("<field name=\"" + field + "\"");
552:
553:                    Object fieldValue = store.get(field.toLowerCase());
554:                    if (fieldValue == null)
555:                        throw new RuntimeOperationsException(
556:                                new RuntimeException(
557:                                        "FieldValue cannot be null"));
558:                    buff.append(" value=\"" + fieldValue.toString() + "\" />");
559:                }
560:
561:                buff.append("</Descriptor>");
562:                return buff.toString();
563:            }
564:
565:            /**
566:             * A human readable string representing the descriptor. The string will be
567:             * in the format of "fieldName=fieldValue,fieldName2=fieldValue2,..."
568:             *
569:             * @return The string representation of descriptor
570:             *
571:             * @exception RuntimeOperationsException for illegal value for field Names
572:             * 				or field Values. If the descriptor string fails for any
573:             * 				reason, this exception will be thrown.
574:             */
575:            public String toString() {
576:                String toRet = null;
577:                String[] fields = getFields();
578:
579:                for (int i = 0; i < fields.length; i++) {
580:                    if (i == 0)
581:                        toRet = fields[i];
582:                    else
583:                        toRet = toRet + "," + fields[i];
584:                }
585:                return toRet;
586:            }
587:
588:            //--------------------------- Private methods ---------------------------//
589:
590:            private void checkDescriptorType(String name, Object value)
591:                    throws RuntimeOperationsException {
592:                try {
593:                    String str = (String) value;
594:
595:                    if (!(str.equalsIgnoreCase("attribute")
596:                            || str.equalsIgnoreCase("operation")
597:                            || str.equalsIgnoreCase("notification") || str
598:                            .equalsIgnoreCase("mbean")))
599:                        throw new RuntimeException();
600:                } catch (RuntimeException e) {
601:                    throw new RuntimeOperationsException(e, "Invalid : " + name);
602:                }
603:
604:            }
605:
606:            private void checkIsString(String name, Object value)
607:                    throws RuntimeOperationsException {
608:                try {
609:                    String str = (String) value;
610:                } catch (RuntimeException e) {
611:                    throw new RuntimeOperationsException(e, "Invalid : " + name);
612:                }
613:            }
614:
615:            private void checkIsDescriptor(String name, Object value)
616:                    throws RuntimeOperationsException {
617:                try {
618:                    Descriptor descr = (Descriptor) value;
619:                } catch (RuntimeException e) {
620:                    throw new RuntimeOperationsException(e, "Invalid : " + name);
621:                }
622:            }
623:
624:            private void checkPersistPolicy(String name, Object value)
625:                    throws RuntimeOperationsException {
626:                try {
627:                    String str = (String) value;
628:
629:                    if (!(str.equalsIgnoreCase("OnUpdate")
630:                            || str.equalsIgnoreCase("OnTimer")
631:                            || str.equalsIgnoreCase("NoMoreOftenThan")
632:                            || str.equalsIgnoreCase("Always") || str
633:                            .equalsIgnoreCase("Never")))
634:                        throw new RuntimeException();
635:                } catch (RuntimeException e) {
636:                    throw new RuntimeOperationsException(e, "Invalid : " + name);
637:                }
638:            }
639:
640:            private void checkIsInt(String name, Object value)
641:                    throws RuntimeOperationsException {
642:                try {
643:                    int i = Integer.parseInt((String) value);
644:                } catch (RuntimeException e) {
645:                    throw new RuntimeOperationsException(e, "Invalid : " + name);
646:                }
647:            }
648:
649:            private void checkIsBoolean(String name, Object value)
650:                    throws RuntimeOperationsException {
651:                try {
652:                    String str = (String) value;
653:
654:                    if (str != null && str.equals(""))
655:                        value = "F";
656:
657:                    if (str != null
658:                            && ((str.equals("true") || str.equals("false")
659:                                    || str.equals("T") || str.equals("F") || str
660:                                    .trim().equals("")) == false)) {
661:                        throw new RuntimeException();
662:                    }
663:                } catch (RuntimeException e) {
664:                    throw new RuntimeOperationsException(e, "Invalid : " + name);
665:                }
666:            }
667:
668:            private void checkVisibility(String name, Object value)
669:                    throws RuntimeOperationsException {
670:                try {
671:                    int i = Integer.parseInt((String) value);
672:                    if (i < 1 || i > 4)
673:                        throw new RuntimeException();
674:                } catch (RuntimeException e) {
675:                    throw new RuntimeOperationsException(e, "Invalid : " + name);
676:                }
677:            }
678:
679:            private void checkRole(String name, Object value)
680:                    throws RuntimeOperationsException {
681:                try {
682:                    String str = (String) value;
683:                    if (!(str.equalsIgnoreCase("getter")
684:                            || str.equalsIgnoreCase("setter")
685:                            || str.equalsIgnoreCase("operation") || str
686:                            .equalsIgnoreCase("constructor")))
687:                        throw new RuntimeException();
688:                } catch (RuntimeException e) {
689:                    throw new RuntimeOperationsException(e, "Invalid : " + name);
690:                }
691:            }
692:
693:            private void checkSeverity(String name, Object value)
694:                    throws RuntimeOperationsException {
695:                try {
696:                    int i = Integer.parseInt((String) value);
697:                    if (i < 1 || i > 5)
698:                        throw new RuntimeException();
699:                } catch (RuntimeException e) {
700:                    throw new RuntimeOperationsException(e, "Invalid : " + name);
701:                }
702:            }
703:
704:            private void checkTargetType(String name, Object value)
705:                    throws RuntimeOperationsException {
706:                try {
707:                    String str = (String) value;
708:                    if (!(str.equalsIgnoreCase("ObjectReference")
709:                            || str.equalsIgnoreCase("EJBHandle")
710:                            || str.equalsIgnoreCase("Handle")
711:                            || str.equalsIgnoreCase("IOR") || str
712:                            .equalsIgnoreCase("RMIReference")))
713:                        throw new RuntimeException();
714:                } catch (RuntimeException e) {
715:                    throw new RuntimeOperationsException(e, "Invalid : " + name);
716:                }
717:            }
718:
719:            private void readObject(ObjectInputStream objectinputstream)
720:                    throws IOException, ClassNotFoundException {
721:                ObjectInputStream.GetField getfield = objectinputstream
722:                        .readFields();
723:
724:                try {
725:                    store = (HashMap) getfield.get("descriptor", "");
726:                    currClass = (String) getfield.get("currClass", null);
727:                } catch (Exception exception) {
728:                }
729:            }
730:
731:            private void writeObject(ObjectOutputStream objectoutputstream)
732:                    throws IOException {
733:                ObjectOutputStream.PutField putfield = objectoutputstream
734:                        .putFields();
735:                putfield.put("descriptor", store);
736:                putfield.put("currClass", currClass);
737:
738:                objectoutputstream.writeFields();
739:            }
740:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.