Source Code Cross Referenced for MagicNumberReader.java in  » Net » JGroups-2.4.1-sp3 » org » jgroups » conf » 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 » Net » JGroups 2.4.1 sp3 » org.jgroups.conf 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        // $Id: MagicNumberReader.java,v 1.11 2006/10/09 13:35:23 belaban Exp $
002:
003:        package org.jgroups.conf;
004:
005:        /**
006:         * Reads and maintains mapping between magic numbers and classes
007:         * @author Filip Hanik (<a href="mailto:filip@filip.net">filip@filip.net)
008:         * @version 1.0
009:         */
010:
011:        import org.apache.commons.logging.Log;
012:        import org.apache.commons.logging.LogFactory;
013:        import org.jgroups.util.Util;
014:        import org.w3c.dom.Document;
015:        import org.w3c.dom.Node;
016:        import org.w3c.dom.NodeList;
017:
018:        import javax.xml.parsers.DocumentBuilder;
019:        import javax.xml.parsers.DocumentBuilderFactory;
020:        import java.io.FileInputStream;
021:        import java.io.FileNotFoundException;
022:        import java.io.InputStream;
023:        import java.io.IOException;
024:
025:        public class MagicNumberReader {
026:            public static final String MAGIC_NUMBER_FILE = "jg-magic-map.xml";
027:
028:            public String mMagicNumberFile = MAGIC_NUMBER_FILE;
029:
030:            protected static final Log log = LogFactory
031:                    .getLog(MagicNumberReader.class);
032:
033:            public void setFilename(String file) {
034:                mMagicNumberFile = file;
035:            }
036:
037:            /**
038:             * try to read the magic number configuration file as a Resource form the classpath using getResourceAsStream
039:             * if this fails this method tries to read the configuration file from mMagicNumberFile using a FileInputStream (not in classpath but somewhere else in the disk)
040:             *
041:             * @return an array of ClassMap objects that where parsed from the file (if found) or an empty array if file not found or had en exception
042:             */
043:            public ClassMap[] readMagicNumberMapping() {
044:                try {
045:                    InputStream stream = Util.getResourceAsStream(
046:                            mMagicNumberFile, this .getClass());
047:                    // try to load the map from file even if it is not a Resource in the class path
048:                    if (stream == null) {
049:                        try {
050:                            if (log.isTraceEnabled())
051:                                log
052:                                        .trace("Could not read "
053:                                                + mMagicNumberFile
054:                                                + " as Resource from the CLASSPATH, will try to read it from file.");
055:                            stream = new FileInputStream(mMagicNumberFile);
056:                            if (stream != null && log.isTraceEnabled())
057:                                log.trace("Magic number File found at '"
058:                                        + mMagicNumberFile + '\'');
059:                        } catch (FileNotFoundException fnfe) {
060:                            if (log.isWarnEnabled())
061:                                log
062:                                        .warn("Failed reading - '"
063:                                                + mMagicNumberFile
064:                                                + "' is not found, got error '"
065:                                                + fnfe.getLocalizedMessage()
066:                                                + "'. Please make sure it is in the CLASSPATH or in the "
067:                                                + "specified location. Will continue, but marshalling will be slower");
068:                        }
069:                    }
070:
071:                    if (stream == null) {
072:                        return new ClassMap[0];
073:                    }
074:                    return parse(stream);
075:                } catch (Exception x) {
076:                    if (log.isErrorEnabled())
077:                        log.error("failed reading mapig map", x);
078:                }
079:                return new ClassMap[0];
080:            }
081:
082:            protected static ClassMap[] parse(InputStream stream)
083:                    throws Exception {
084:                DocumentBuilderFactory factory = DocumentBuilderFactory
085:                        .newInstance();
086:                factory.setValidating(false); //for now
087:                DocumentBuilder builder = factory.newDocumentBuilder();
088:                Document document = builder.parse(stream);
089:                NodeList class_list = document.getElementsByTagName("class");
090:                java.util.Vector v = new java.util.Vector();
091:                for (int i = 0; i < class_list.getLength(); i++) {
092:                    if (class_list.item(i).getNodeType() == Node.ELEMENT_NODE) {
093:                        v.addElement(parseClassData(class_list.item(i)));
094:                    }
095:                }
096:                ClassMap[] data = new ClassMap[v.size()];
097:                v.copyInto(data);
098:                return data;
099:            }//parse
100:
101:            protected static ClassMap parseClassData(Node protocol)
102:                    throws java.io.IOException {
103:                try {
104:                    protocol.normalize();
105:                    int pos = 0;
106:                    NodeList children = protocol.getChildNodes();
107:                    /**
108:                     * there should be 4 Element Nodes if we are not overriding
109:                     * 1. description
110:                     * 2. class-name
111:                     * 3. preload
112:                     * 4. magic-number
113:                     */
114:
115:                    String clazzname = null;
116:                    String desc = null;
117:                    String preload = null;
118:                    String magicnumber = null;
119:
120:                    for (int i = 0; i < children.getLength(); i++) {
121:                        if (children.item(i).getNodeType() == Node.ELEMENT_NODE) {
122:                            pos++;
123:                            switch (pos) {
124:                            case 1:
125:                                desc = children.item(i).getFirstChild()
126:                                        .getNodeValue();
127:                                break;
128:                            case 2:
129:                                clazzname = children.item(i).getFirstChild()
130:                                        .getNodeValue();
131:                                break;
132:                            case 3:
133:                                preload = children.item(i).getFirstChild()
134:                                        .getNodeValue();
135:                                break;
136:                            case 4:
137:                                magicnumber = children.item(i).getFirstChild()
138:                                        .getNodeValue();
139:                                break;
140:                            }
141:                        }
142:                    }
143:
144:                    return new ClassMap(clazzname, desc, Boolean.valueOf(
145:                            preload).booleanValue(), Integer.valueOf(
146:                            magicnumber).intValue());
147:                } catch (Exception x) {
148:                    if (x instanceof  java.io.IOException)
149:                        throw (java.io.IOException) x;
150:                    else {
151:                        IOException tmp = new IOException();
152:                        tmp.initCause(x);
153:                        throw tmp;
154:                    }
155:                }
156:            }
157:
158:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.