Source Code Cross Referenced for ComponentConfigurationHelper.java in  » ESB » open-esb » com » sun » jbi » util » 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 » ESB » open esb » com.sun.jbi.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * BEGIN_HEADER - DO NOT EDIT
003:         *
004:         * The contents of this file are subject to the terms
005:         * of the Common Development and Distribution License
006:         * (the "License").  You may not use this file except
007:         * in compliance with the License.
008:         *
009:         * You can obtain a copy of the license at
010:         * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011:         * See the License for the specific language governing
012:         * permissions and limitations under the License.
013:         *
014:         * When distributing Covered Code, include this CDDL
015:         * HEADER in each file and include the License file at
016:         * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017:         * If applicable add the following below this CDDL HEADER,
018:         * with the fields enclosed by brackets "[]" replaced with
019:         * your own identifying information: Portions Copyright
020:         * [year] [name of copyright owner]
021:         */
022:
023:        /*
024:         * @(#)ComponentConfigurationHelper.java
025:         * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026:         *
027:         * END_HEADER - DO NOT EDIT
028:         */
029:        package com.sun.jbi.util;
030:
031:        import org.w3c.dom.Document;
032:        import org.w3c.dom.Element;
033:
034:        import java.util.ArrayList;
035:        import java.util.List;
036:        import java.util.Properties;
037:        import java.util.Set;
038:        import java.util.Vector;
039:        import java.util.logging.Logger;
040:        import java.util.regex.Pattern;
041:
042:        import javax.management.openmbean.ArrayType;
043:        import javax.management.openmbean.CompositeData;
044:        import javax.management.openmbean.CompositeDataSupport;
045:        import javax.management.openmbean.CompositeType;
046:        import javax.management.openmbean.TabularData;
047:        import javax.management.openmbean.TabularDataSupport;
048:        import javax.management.openmbean.TabularType;
049:        import javax.management.openmbean.OpenType;
050:        import javax.management.openmbean.SimpleType;
051:
052:        /**
053:         * This is a utility class to convert a set of properties into 
054:         * application variables or application configuration and vice versa.
055:         *
056:         */
057:        public class ComponentConfigurationHelper {
058:            /** The String Translator */
059:            private StringTranslator mTranslator;
060:
061:            /** Application Variable Composite Type */
062:            private static CompositeType sAppVarComposite;
063:
064:            /** Application Variable Tabular Type */
065:            private static TabularType sAppVarTable;
066:
067:            /** Application Variable Composite Item Names */
068:            private static String[] sAppVarItemNames;
069:
070:            /** The index of the Application Variables **/
071:            private static String[] sappVarRowIndex;
072:
073:            /** Logger */
074:            private static Logger mLog;
075:
076:            /** Application Configuration element name in component config meta-data */
077:            private static final String APP_CONFIG_ELEMENT_NAME = "ApplicationConfiguration";
078:
079:            /** Application Variables element name in component config meta-data */
080:            private static final String APP_VAR_ELEMENT_NAME = "ApplicationVariable";
081:
082:            /** isPasswordField attribute in component config meta-data */
083:            private static final String IS_PASSWD_ATTRIB_NAME = "isPasswordField";
084:
085:            /** encrypted attribute in component config meta-data */
086:            private static final String ENCRYPTED_ATTRIB_NAME = "encrypted";
087:
088:            private static final String NAME_ATTRIB_NAME = "name";
089:
090:            private static final String PROPERTY_ELEMENT_NAME = "Property";
091:
092:            private static final String IS_REQUIRED_ATTRIB_NAME = "isRequiredField";
093:
094:            private static final String REQUIRED_ATTRIB_NAME = "required";
095:
096:            public static final String DEFAULT_APP_VAR_TYPE = "STRING";
097:
098:            /** Configuration NS */
099:            public static final String CONFIGURATION_NS = "http://www.sun.com/jbi/Configuration/V1.0";
100:
101:            private static final Pattern NUMBER_PATTERN = Pattern
102:                    .compile("\\d");
103:
104:            private void initOpenTypes()
105:                    throws javax.management.openmbean.OpenDataException {
106:                // Define the Application Variables CompositeType
107:                sAppVarItemNames = new String[] { "name", "value", "type" };
108:
109:                String[] appVarItemDesc = { "Application variable name",
110:                        "Application variable value",
111:                        "Application variable type" };
112:
113:                OpenType[] appVarRowAttrTypes = { SimpleType.STRING,
114:                        SimpleType.STRING, SimpleType.STRING };
115:
116:                sappVarRowIndex = new String[] { "name" };
117:
118:                sAppVarComposite = new CompositeType(
119:                        "ApplicationVariableComposite",
120:                        "Application variable name and value pair",
121:                        sAppVarItemNames, appVarItemDesc, appVarRowAttrTypes);
122:
123:                sAppVarTable = new TabularType("ApplicationVariableList",
124:                        "List of application variables", sAppVarComposite,
125:                        sappVarRowIndex);
126:            }
127:
128:            /** Constructor */
129:            public ComponentConfigurationHelper() {
130:                try {
131:                    mLog = Logger.getLogger("com.sun.jbi.util");
132:                    mTranslator = new StringTranslator("com.sun.jbi.util", null);
133:                    initOpenTypes();
134:                } catch (javax.management.openmbean.OpenDataException opex) {
135:                    throw new RuntimeException(opex);
136:                }
137:            }
138:
139:            /**
140:             * @return the CompositeType for Application Variables.
141:             */
142:            public CompositeType getApplicationVariablesType() {
143:                return sAppVarComposite;
144:            }
145:
146:            /**
147:             * @return the Application Variables Tabular Type
148:             */
149:            public TabularType getApplicationVariableTabularType() {
150:                return sAppVarTable;
151:            }
152:
153:            /**
154:             *  @return the application variables item names
155:             */
156:            public String[] getApplicationVariableItemNames() {
157:                return sAppVarItemNames;
158:            }
159:
160:            /**
161:             * Convert a Properties Object to a Application Variable TabularData.
162:             * The property name is the name of the Property and the value is 
163:             * of the Pattern "[type]value"
164:             *
165:             * @param props - the properties to convert to TabularData.
166:             * @return the TabularData constructed from the Properties
167:             */
168:            public TabularData convertToApplicationVariablesTable(
169:                    Properties props) throws Exception {
170:                TabularData tabularData = new TabularDataSupport(sAppVarTable);
171:
172:                Set names = props.keySet();
173:                for (Object name : names) {
174:                    String appVarName = (String) name;
175:                    String value = props.getProperty(appVarName);
176:                    String appVarType = getAppVarType(value,
177:                            DEFAULT_APP_VAR_TYPE);
178:                    String appVarValue = getAppVarValue(value);
179:
180:                    if (appVarType != null) {
181:                        appVarType = convertTypeCase(appVarType);
182:                    }
183:
184:                    CompositeData cd = new CompositeDataSupport(
185:                            getApplicationVariablesType(),
186:                            sAppVarItemNames,
187:                            new String[] { appVarName, appVarValue, appVarType });
188:                    tabularData.put(cd);
189:                }
190:
191:                return tabularData;
192:            }
193:
194:            /**
195:             * Convert Apllication Variables TabularData to a set of Properties.
196:             */
197:            public Properties convertToApplicationVariablesProperties(
198:                    TabularData appVars) throws Exception {
199:                Properties props = new Properties();
200:
201:                Set names = appVars.keySet();
202:
203:                for (Object name : names) {
204:                    Object[] key = ((java.util.List) name).toArray();
205:
206:                    CompositeData cd = appVars.get(key);
207:
208:                    if (cd != null) {
209:                        String appVarName = (String) cd
210:                                .get(sAppVarItemNames[0]);
211:                        String appVarValue = (String) cd
212:                                .get(sAppVarItemNames[1]);
213:                        String appVarType = (String) cd
214:                                .get(sAppVarItemNames[2]);
215:
216:                        if (appVarType == null) {
217:                            appVarType = DEFAULT_APP_VAR_TYPE;
218:                        }
219:
220:                        if (appVarValue == null) {
221:                            throw new Exception("Illegal CompositeData Values");
222:                        }
223:
224:                        String value = "[" + convertTypeCase(appVarType) + "]"
225:                                + appVarValue;
226:                        props.put(appVarName, value);
227:                    }
228:
229:                }
230:                return props;
231:            }
232:
233:            /**
234:             * Convert Apllication Variables CompositeData to a set of Properties.
235:             *
236:             * @param appVar - application variable composite
237:             */
238:            public Properties convertToApplicationVariableProperty(
239:                    CompositeData appVar) throws Exception {
240:                Properties props = new Properties();
241:
242:                if (appVar != null) {
243:                    String appVarName = (String) appVar
244:                            .get(sAppVarItemNames[0]);
245:                    String appVarValue = (String) appVar
246:                            .get(sAppVarItemNames[1]);
247:                    String appVarType = (String) appVar
248:                            .get(sAppVarItemNames[2]);
249:
250:                    if (appVarType == null) {
251:                        appVarType = DEFAULT_APP_VAR_TYPE;
252:                    }
253:
254:                    if (appVarValue == null) {
255:                        throw new Exception("Illegal CompositeData Value");
256:                    }
257:
258:                    String value = "[" + convertTypeCase(appVarType) + "]"
259:                            + appVarValue;
260:                    props.put(appVarName, value);
261:                }
262:                return props;
263:            }
264:
265:            /**
266:             * Create a Application Variable Composite from the passed in name, value and type
267:             *
268:             * @param name - the name
269:             * @param type - the type string
270:             * @param value - the value
271:             * @return the CompositeData constructed from the parameters
272:             */
273:            public CompositeData createApplicationVariableComposite(
274:                    String name, String value, String type) throws Exception {
275:                if (type == null) {
276:                    type = DEFAULT_APP_VAR_TYPE;
277:                }
278:                return new CompositeDataSupport(getApplicationVariablesType(),
279:                        sAppVarItemNames, new String[] { name, value, type });
280:            }
281:
282:            /**
283:             * Create a Application Variable Composite from the passed in name, and the property 
284:             * value string which is of format : [type]value
285:             *
286:             * @param name - the name
287:             * @param typeAndValueStr - string which is of format : [type]value
288:             * @return the CompositeData constructed from the parameters
289:             */
290:            public CompositeData createApplicationVariableComposite(
291:                    String name, String typeAndValueStr) throws Exception {
292:                return new CompositeDataSupport(getApplicationVariablesType(),
293:                        sAppVarItemNames, new String[] {
294:                                name,
295:                                getAppVarValue(typeAndValueStr),
296:                                getAppVarType(typeAndValueStr,
297:                                        DEFAULT_APP_VAR_TYPE) });
298:            }
299:
300:            /**
301:             * @return the "type" from the "[type]value" string.
302:             */
303:            public String getAppVarType(String str, String defStr)
304:                    throws Exception {
305:                str = str.trim();
306:                int indexOfRP = str.indexOf('[');
307:                int indexOfLP = str.indexOf(']');
308:
309:                if (indexOfRP != 0 || indexOfLP == -1) {
310:                    return defStr;
311:                }
312:
313:                String type = str.substring(indexOfRP + 1, indexOfLP);
314:
315:                if (type == null) {
316:                    type = defStr;
317:                } else if (type.trim().equals("")) {
318:                    type = defStr;
319:                }
320:
321:                String rsltStr = type;
322:
323:                if (type != null) {
324:                    rsltStr = convertTypeCase(type);
325:                }
326:                return rsltStr;
327:            }
328:
329:            /**
330:             * @return the "value" from the "[type]value" string.
331:             */
332:            public String getAppVarValue(String str) throws Exception {
333:                int indexOfRP = str.indexOf('[');
334:                int indexOfLP = str.indexOf(']');
335:
336:                if (indexOfRP != 0 || indexOfLP == -1) {
337:                    return str;
338:                }
339:
340:                return str.substring(indexOfLP + 1, str.length());
341:            }
342:
343:            /**
344:             * Convert a CompositeData to Properties.
345:             *
346:             * @param cd
347:             *      the composite data to convert to Properties
348:             * @return 
349:             *      the properties representation of the CompositeData
350:             *
351:             */
352:            public Properties convertCompositeDataToProperties(CompositeData cd) {
353:                Properties appConfigProps = new Properties();
354:                CompositeType ct = cd.getCompositeType();
355:                Set<String> keys = ct.keySet();
356:
357:                for (String key : keys) {
358:                    Object obj = cd.get(key);
359:
360:                    if (obj != null) {
361:                        Properties props = convertToString(ct.getType(key),
362:                                obj, key);
363:                        appConfigProps.putAll(props);
364:                    }
365:                }
366:                return appConfigProps;
367:            }
368:
369:            /**
370:             *
371:             * @param props 
372:             *         the properties to convert to a CompositeType
373:             * @param appConfigType 
374:             *         application configuration CompositeType
375:             * @throws Exception if the properties cannot be completely mapped into the 
376:             *         CompositeType
377:             */
378:            public CompositeData convertPropertiesToCompositeData(
379:                    Properties props, CompositeType appConfigType)
380:                    throws Exception {
381:                /**
382:                 * For each Composite item, look for a matching property
383:                 * If one exists then convert the value to the expected OpenType as defined 
384:                 * in the CompositeType.  If the value cannot be converted to the expected
385:                 * type an exception is thrown
386:                 */
387:                Set<String> keySet = appConfigType.keySet();
388:                propertyKeysValidCheck(props.keySet(), keySet);
389:                Vector<String> itemNames = new Vector();
390:
391:                Vector<Object> itemValues = new Vector();
392:
393:                int i = 0;
394:                for (String itemName : keySet) {
395:                    OpenType itemType = appConfigType.getType(itemName);
396:
397:                    if (itemType.isArray()) {
398:                        // get all the properties that match the pattern itemName.n
399:                        // into a String[] array
400:                        int indexSuffix = 0;
401:                        boolean done = false;
402:                        List itemValueArray = new ArrayList();
403:                        do {
404:                            String suffixedItemName = itemName + "."
405:                                    + indexSuffix;
406:                            indexSuffix += 1;
407:                            if (props.containsKey(suffixedItemName)) {
408:                                itemValueArray.add(props
409:                                        .getProperty(suffixedItemName));
410:                            } else {
411:                                done = true;
412:                            }
413:
414:                        } while (!done);
415:
416:                        if (!itemValueArray.isEmpty()) {
417:                            itemNames.add(itemName);
418:                            itemValues.add(convertToOpenType(itemType,
419:                                    itemValueArray));
420:                        } else {
421:                            itemNames.add(itemName);
422:                            itemValues.add(null);
423:                        }
424:                    } else {
425:                        if (props.containsKey(itemName)) {
426:                            String itemStrValue = (String) props
427:                                    .getProperty(itemName);
428:                            itemNames.add(itemName);
429:                            itemValues.add(convertToOpenType(itemType,
430:                                    itemStrValue));
431:                        } else {
432:                            itemNames.add(itemName);
433:                            itemValues.add(null);
434:                        }
435:                    }
436:
437:                }
438:
439:                CompositeData cd = new CompositeDataSupport(appConfigType,
440:                        itemNames.toArray(new String[itemNames.size()]),
441:                        itemValues.toArray(new Object[itemValues.size()]));
442:
443:                return cd;
444:            }
445:
446:            /*------------------------------------------------------------------------*\
447:             *                  Utilities to help password obfuscation                *
448:            \*------------------------------------------------------------------------*/
449:
450:            /**
451:             * @param attributeName - name of the property
452:             * @param configDoc  - configuration xml document 
453:             * @return true if a component configuration attribute is a passwordfiled
454:             *  
455:             */
456:            public boolean isPassword(String attributeName, Document configDoc)
457:                    throws Exception {
458:                boolean isPasswordField = false;
459:
460:                if (configDoc != null) {
461:                    if (isOldConfigNS(configDoc)) {
462:                        attributeName = getActualAttributeName(attributeName);
463:                        Element attribElement = (Element) configDoc
464:                                .getElementsByTagNameNS("*", attributeName)
465:                                .item(0);
466:                        if (attribElement != null) {
467:                            String isPwdStr = attribElement
468:                                    .getAttribute(IS_PASSWD_ATTRIB_NAME);
469:                            isPasswordField = Boolean.parseBoolean(isPwdStr);
470:                        } else {
471:                            String errMsg = mTranslator
472:                                    .getString(
473:                                            LocalStringKeys.CCFG_ATTR_DEF_MISSING_IN_XML,
474:                                            attributeName);
475:                            mLog.fine(errMsg);
476:                        }
477:                    } else {
478:                        /**
479:                         * Get all the Property elements and look for the one with
480:                         * name=attributeName
481:                         */
482:                        org.w3c.dom.NodeList properties = configDoc
483:                                .getElementsByTagNameNS("*",
484:                                        PROPERTY_ELEMENT_NAME);
485:
486:                        int numNodes = properties.getLength();
487:
488:                        for (int i = 0; i < numNodes; i++) {
489:                            Element propertyElement = (Element) properties
490:                                    .item(i);
491:                            if (attributeName.equals(propertyElement
492:                                    .getAttribute(NAME_ATTRIB_NAME))) {
493:                                isPasswordField = Boolean
494:                                        .parseBoolean(propertyElement
495:                                                .getAttribute(ENCRYPTED_ATTRIB_NAME));
496:                            }
497:                        }
498:
499:                    }
500:                }
501:                return isPasswordField;
502:            }
503:
504:            /**
505:             * @param attributeName - name of the property
506:             * @param configDoc  - configuration xml document 
507:             * @return true if a component configuration attribute is a required field
508:             *  
509:             */
510:            public boolean isRequired(String attributeName, Document configDoc)
511:                    throws Exception {
512:                boolean isRequiredField = false;
513:
514:                if (configDoc != null) {
515:                    if (isOldConfigNS(configDoc)) {
516:                        attributeName = getActualAttributeName(attributeName);
517:                        Element attribElement = (Element) configDoc
518:                                .getElementsByTagNameNS("*", attributeName)
519:                                .item(0);
520:                        if (attribElement != null) {
521:                            String isRqdStr = attribElement
522:                                    .getAttribute(IS_REQUIRED_ATTRIB_NAME);
523:                            isRequiredField = Boolean.parseBoolean(isRqdStr);
524:                        } else {
525:                            String errMsg = mTranslator
526:                                    .getString(
527:                                            LocalStringKeys.CCFG_ATTR_DEF_MISSING_IN_XML,
528:                                            attributeName);
529:                            mLog.fine(errMsg);
530:                        }
531:                    } else {
532:                        /**
533:                         * Get all the Property elements and look for the one with
534:                         * name=attributeName
535:                         */
536:                        org.w3c.dom.NodeList properties = configDoc
537:                                .getElementsByTagNameNS("*",
538:                                        PROPERTY_ELEMENT_NAME);
539:
540:                        int numNodes = properties.getLength();
541:
542:                        for (int i = 0; i < numNodes; i++) {
543:                            Element propertyElement = (Element) properties
544:                                    .item(i);
545:                            if (attributeName.equals(propertyElement
546:                                    .getAttribute(NAME_ATTRIB_NAME))) {
547:                                isRequiredField = Boolean
548:                                        .parseBoolean(propertyElement
549:                                                .getAttribute(REQUIRED_ATTRIB_NAME));
550:                            }
551:                        }
552:
553:                    }
554:                }
555:                return isRequiredField;
556:            }
557:
558:            /**
559:             *
560:             * @param configDoc  - configuration xml document 
561:             * @return true if the components configuration document declares
562:             *         support for component configuration attributes
563:             *  
564:             */
565:            public boolean isComponentConfigSupported(Document configDoc) {
566:                boolean isConfigSupported = false;
567:
568:                if (configDoc != null) {
569:                    if (!isOldConfigNS(configDoc)) {
570:
571:                        Element root = configDoc.getDocumentElement();
572:                        org.w3c.dom.NodeList children = root.getChildNodes();
573:
574:                        int numNodes = children.getLength();
575:
576:                        for (int i = 0; i < numNodes; i++) {
577:                            org.w3c.dom.Node child = children.item(i);
578:                            if (PROPERTY_ELEMENT_NAME.equals(child
579:                                    .getLocalName())) {
580:                                isConfigSupported = true;
581:                                break;
582:                            }
583:                        }
584:
585:                    }
586:                }
587:                return isConfigSupported;
588:            }
589:
590:            /**
591:             *
592:             * @param configDoc  - configuration xml document 
593:             * @return true if the components configuration document declares
594:             *         support for application variables
595:             *  
596:             */
597:            public boolean isAppVarsSupported(Document configDoc) {
598:                boolean isAppVarsSupported = false;
599:
600:                if (configDoc != null) {
601:                    if (!isOldConfigNS(configDoc)) {
602:                        Element root = configDoc.getDocumentElement();
603:                        org.w3c.dom.NodeList children = root.getChildNodes();
604:
605:                        int numNodes = children.getLength();
606:
607:                        for (int i = 0; i < numNodes; i++) {
608:                            org.w3c.dom.Node child = children.item(i);
609:                            if (APP_VAR_ELEMENT_NAME.equals(child
610:                                    .getLocalName())) {
611:                                isAppVarsSupported = true;
612:                                break;
613:                            }
614:                        }
615:
616:                    }
617:                }
618:                return isAppVarsSupported;
619:            }
620:
621:            /**
622:             *
623:             * @param configDoc  - configuration xml document 
624:             * @return true if the components configuration document declares
625:             *         support for application configuration
626:             *  
627:             */
628:            public boolean isAppConfigSupported(Document configDoc) {
629:                boolean isAppConfigSupported = false;
630:
631:                if (configDoc != null) {
632:                    if (!isOldConfigNS(configDoc)) {
633:                        Element root = configDoc.getDocumentElement();
634:                        org.w3c.dom.NodeList children = root.getChildNodes();
635:
636:                        int numNodes = children.getLength();
637:
638:                        for (int i = 0; i < numNodes; i++) {
639:                            org.w3c.dom.Node child = children.item(i);
640:                            if (APP_CONFIG_ELEMENT_NAME.equals(child
641:                                    .getLocalName())) {
642:                                isAppConfigSupported = true;
643:                                break;
644:                            }
645:                        }
646:
647:                    }
648:                }
649:                return isAppConfigSupported;
650:            }
651:
652:            /**
653:             *
654:             */
655:            public String getActualAttributeName(String name) {
656:                String attribName = name;
657:                String attribSuffix = "";
658:                int index = name.lastIndexOf('.');
659:                if (index != -1) {
660:                    attribSuffix = name.substring(index + 1, name.length());
661:                    if (attribSuffix != null
662:                            && NUMBER_PATTERN.matcher(attribSuffix).matches()) {
663:                        attribName = name.substring(0, index);
664:                    }
665:                }
666:                return attribName;
667:
668:            }
669:
670:            /**
671:             * Convert a string value to the expected OpenType.
672:             *
673:             * @param itemStrValue 
674:             *            string value of the item
675:             * @param opnenType
676:             *            the open type to convert the String to.
677:             * @return 
678:             *        an instance of the OpenType
679:             * @exception
680:             *       throw an exception if the string cannot be converted to the OpenType
681:             */
682:            private Object convertToOpenType(OpenType openType,
683:                    String itemStrValue) throws Exception {
684:                try {
685:                    String className = openType.getClassName();
686:
687:                    if (className.equals(CompositeData.class.getName())) {
688:                        CompositeType subCompositeType = (CompositeType) openType;
689:
690:                        // convert the properties to composite data
691:                        Properties cdProps = convertToProperties(itemStrValue);
692:                        CompositeData cd = convertPropertiesToCompositeData(
693:                                cdProps, subCompositeType);
694:                        return cd;
695:                    } else {
696:                        Class objClass = Class.forName(className);
697:                        Class[] argClass = new Class[] { String.class };
698:                        java.lang.reflect.Constructor objConstructor = objClass
699:                                .getConstructor(argClass);
700:                        String[] argValue = { itemStrValue };
701:                        return objConstructor.newInstance(argValue);
702:                    }
703:
704:                } catch (Exception ex) {
705:                    String errMsg = mTranslator
706:                            .getString(
707:                                    LocalStringKeys.CCFG_FAILED_CONVERSION_TO_OPEN_TYPE,
708:                                    itemStrValue, openType.getTypeName());
709:                    throw new Exception(errMsg);
710:                }
711:            }
712:
713:            /**
714:             * Convert a string array value to the expected OpenType array. 
715:             *
716:             * @param itemStrValue 
717:             *            string value of the item
718:             * @param opnenType
719:             *            the open type to convert the String to.
720:             * @return 
721:             *        an instance of the OpenType
722:             * @exception
723:             *       throw an exception if the string cannot be converted to the OpenType
724:             */
725:            private Object convertToOpenType(OpenType openType,
726:                    List<String> itemValueArray) throws Exception {
727:                if (openType.isArray()) {
728:                    ArrayType arrayType = (ArrayType) openType;
729:
730:                    Object[] objArray = (Object[]) java.lang.reflect.Array
731:                            .newInstance(Class.forName(arrayType
732:                                    .getElementOpenType().getClassName()),
733:                                    itemValueArray.size());
734:                    int i = 0;
735:                    for (String itemValue : itemValueArray) {
736:                        objArray[i++] = convertToOpenType(arrayType
737:                                .getElementOpenType(), itemValue);
738:                    }
739:                    return objArray;
740:                }
741:
742:                String errMsg = mTranslator.getString(
743:                        LocalStringKeys.CCFG_FAILED_CONVERSION_TO_OPEN_TYPE_AR,
744:                        itemValueArray.toString(), openType.getTypeName());
745:                throw new Exception(errMsg);
746:            }
747:
748:            /**
749:             * 
750:             * - If item is an array convert to a list of keys : itemName.0..n
751:             * - If its a CompositeData convert that to a comma separated n/v pairs
752:             * - If it is a SimpleType convert to string
753:             *
754:             * @return the String representation of the type
755:             */
756:            private Properties convertToString(OpenType type, Object value,
757:                    String itemName) {
758:                Properties props = new Properties();
759:                if (type.isArray()) {
760:                    Object[] itemArray = (Object[]) value;
761:
762:                    for (int i = 0; i < itemArray.length; i++) {
763:                        props.put(itemName + "." + i, itemArray[i].toString());
764:                    }
765:                } else {
766:                    if (type.getClassName().equals(
767:                            CompositeData.class.getName())) {
768:                        if (value != null) {
769:                            Properties cdProps = convertCompositeDataToProperties((CompositeData) value);
770:                            Set keys = cdProps.keySet();
771:                            StringBuffer compositeStrBuf = new StringBuffer(
772:                                    "\"");
773:                            for (java.util.Iterator itr = keys.iterator(); itr
774:                                    .hasNext();) {
775:                                String key = (String) itr.next();
776:                                compositeStrBuf.append(key + "="
777:                                        + cdProps.get(key));
778:                                if (itr.hasNext()) {
779:                                    compositeStrBuf.append(", ");
780:                                } else {
781:                                    compositeStrBuf.append("\"");
782:                                }
783:                            }
784:                            props.put(itemName, compositeStrBuf.toString());
785:                        }
786:                    } else {
787:                        props.put(itemName, value.toString());
788:                    }
789:                }
790:                return props;
791:            }
792:
793:            /**
794:             * @param propsStr
795:             *          comma delimites n/v pair string example \"n1=v1, n2=v1\"
796:             * @return the Properties created from the comma separated n/v pair string.
797:             */
798:            private Properties convertToProperties(String propsStr) {
799:                propsStr.trim();
800:
801:                if (propsStr.startsWith("\"")) {
802:                    propsStr = propsStr.substring(1);
803:                }
804:                if (propsStr.endsWith("\"")) {
805:                    propsStr = propsStr.substring(0, propsStr.length() - 1);
806:                }
807:
808:                String[] nvPairs = propsStr.split(",", -1);
809:                Properties props = new Properties();
810:                for (String nvPair : nvPairs) {
811:                    String[] nv = nvPair.split("=", 2);
812:                    if (nv.length == 2) {
813:                        props.put(nv[0].trim(), nv[1].trim());
814:                    }
815:                }
816:                return props;
817:            }
818:
819:            /**
820:             * @returns true if the configuration element passed in adheres to the old
821:             * schema.
822:             */
823:            private boolean isOldConfigNS(Document configDoc) {
824:                Element configElement = configDoc.getDocumentElement();
825:
826:                return !(configElement.getNamespaceURI()
827:                        .equals(CONFIGURATION_NS));
828:            }
829:
830:            /**
831:             * Check if there are any extra properties not relevant to the app config
832:             *  
833:             * @param propKeys - keys in the properties
834:             * @param itemNames - app config item names
835:             * @throws Exception if there are invalid keys.
836:             */
837:            private void propertyKeysValidCheck(Set propKeys, Set itemNames)
838:                    throws Exception {
839:                Vector<String> invalidKeys = new Vector();
840:
841:                for (Object obj : propKeys) {
842:                    boolean found = false;
843:
844:                    String key = getActualAttributeName((String) obj);
845:
846:                    for (Object itemName : itemNames) {
847:                        if (itemName.equals(key)) {
848:                            found = true;
849:                        }
850:                    }
851:
852:                    if (!found) {
853:                        invalidKeys.add((String) key);
854:
855:                    }
856:                }
857:
858:                if (!invalidKeys.isEmpty()) {
859:                    String errMsg = mTranslator.getString(
860:                            LocalStringKeys.CCFG_CD_EXTRA_PROPS,
861:                            convertToString(invalidKeys));
862:                    throw new Exception(errMsg);
863:                }
864:            }
865:
866:            /**
867:             * Convert the objects in the collection to string.
868:             *
869:             * @param set a non-empty set
870:             */
871:            protected String convertToString(java.util.Collection colxn) {
872:                StringBuffer strBuf = new StringBuffer("[ ");
873:                if (!colxn.isEmpty()) {
874:                    java.util.Iterator itr = colxn.iterator();
875:                    while (itr.hasNext()) {
876:                        strBuf.append(itr.next().toString());
877:                        if (itr.hasNext()) {
878:                            strBuf.append(", ");
879:                        } else {
880:                            strBuf.append(" ");
881:                        }
882:                    }
883:                }
884:                strBuf.append(" ]");
885:                return strBuf.toString();
886:            }
887:
888:            /**
889:             * Convert the type case
890:             *
891:             * @param type the type string
892:             */
893:            private String convertTypeCase(String type) {
894:                if (type != null) {
895:                    return type.toUpperCase();
896:                }
897:
898:                return type;
899:                /**
900:                String firstChar = type.substring(0,1);
901:                String remainingChars = type.substring(1, type.length());
902:                return ( firstChar.toUpperCase() + remainingChars.toLowerCase());
903:                 */
904:            }
905:
906:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.