KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > conf > MagicNumberReader


1 // $Id: MagicNumberReader.java,v 1.8 2005/04/23 12:44:05 belaban Exp $
2

3 package org.jgroups.conf;
4
5 /**
6  * Reads and maintains mapping between magic numbers and classes
7  * @author Filip Hanik (<a HREF="mailto:filip@filip.net">filip@filip.net)
8  * @version 1.0
9  */

10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.jgroups.util.Util;
14 import org.w3c.dom.Document JavaDoc;
15 import org.w3c.dom.Node JavaDoc;
16 import org.w3c.dom.NodeList JavaDoc;
17
18 import javax.xml.parsers.DocumentBuilder JavaDoc;
19 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
20 import java.io.FileInputStream JavaDoc;
21 import java.io.FileNotFoundException JavaDoc;
22 import java.io.InputStream JavaDoc;
23
24 public class MagicNumberReader {
25     private static final boolean xml_debug=false;
26     public static final String JavaDoc MAGIC_NUMBER_FILE="jg-magic-map.xml";
27
28     public String JavaDoc mMagicNumberFile=MAGIC_NUMBER_FILE;
29
30     protected static final Log log=LogFactory.getLog(MagicNumberReader.class);
31
32     public void setFilename(String JavaDoc file) {
33         mMagicNumberFile=file;
34     }
35
36     /**
37      * try to read the magic number configuration file as a Resource form the classpath using getResourceAsStream
38      * 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)
39      *
40      * @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
41      */

42     public ClassMap[] readMagicNumberMapping() {
43         try {
44             // InputStream stream=getClass().getClassLoader().getResourceAsStream(mMagicNumberFile);
45
InputStream JavaDoc stream=Thread.currentThread().getContextClassLoader().getResourceAsStream(mMagicNumberFile);
46             // try to load the map from file even if it is not a Resource in the class path
47
if(stream == null) {
48                 try {
49                     if(log.isTraceEnabled())
50                         log.trace("Could not read " + mMagicNumberFile + " as Resource from the CLASSPATH, will try to read it from file.");
51                     stream=new FileInputStream JavaDoc(mMagicNumberFile);
52                     if(stream != null && log.isTraceEnabled())
53                         log.trace("Magic number File found at '" + mMagicNumberFile + '\'');
54                 }
55                 catch(FileNotFoundException JavaDoc fnfe) {
56                     if(log.isWarnEnabled())
57                         log.warn("Failed reading - '" + mMagicNumberFile + "' is not found, got error '" +
58                                  fnfe.getLocalizedMessage() + "'. Please make sure it is in the CLASSPATH or in the " +
59                                  "specified location. Will continue, but marshalling will be slower");
60                 }
61             }
62
63             if(stream == null) {
64                 return new ClassMap[0];
65             }
66             return parse(stream);
67         }
68         catch(Exception JavaDoc x) {
69             if(xml_debug) x.printStackTrace();
70             String JavaDoc error=Util.getStackTrace(x);
71             if(log.isErrorEnabled()) log.error(error);
72         }
73         return new ClassMap[0];
74     }
75
76     protected static ClassMap[] parse(InputStream JavaDoc stream) throws Exception JavaDoc {
77         DocumentBuilderFactory JavaDoc factory=DocumentBuilderFactory.newInstance();
78         factory.setValidating(false); //for now
79
DocumentBuilder JavaDoc builder=factory.newDocumentBuilder();
80         builder.setEntityResolver(new ClassPathEntityResolver());
81         Document JavaDoc document=builder.parse(stream);
82         NodeList JavaDoc class_list=document.getElementsByTagName("class");
83         java.util.Vector JavaDoc v=new java.util.Vector JavaDoc();
84         for(int i=0; i < class_list.getLength(); i++) {
85             if(class_list.item(i).getNodeType() == Node.ELEMENT_NODE) {
86                 v.addElement(parseClassData(class_list.item(i)));
87             }
88         }
89         ClassMap[] data=new ClassMap[v.size()];
90         v.copyInto(data);
91         return data;
92     }//parse
93

94     protected static ClassMap parseClassData(Node JavaDoc protocol)
95             throws java.io.IOException JavaDoc {
96         try {
97             protocol.normalize();
98             int pos=0;
99             NodeList JavaDoc children=protocol.getChildNodes();
100             /**
101              * there should be 4 Element Nodes if we are not overriding
102              * 1. description
103              * 2. class-name
104              * 3. preload
105              * 4. magic-number
106              */

107
108             //
109

110
111             String JavaDoc clazzname=null;
112             String JavaDoc desc=null;
113             String JavaDoc preload=null;
114             String JavaDoc magicnumber=null;
115
116             for(int i=0; i < children.getLength(); i++) {
117                 if(children.item(i).getNodeType() == Node.ELEMENT_NODE) {
118                     pos++;
119                     switch(pos) {
120                         case 1:
121                             desc=children.item(i).getFirstChild().getNodeValue();
122                             break;
123                         case 2:
124                             clazzname=children.item(i).getFirstChild().getNodeValue();
125                             break;
126                         case 3:
127                             preload=children.item(i).getFirstChild().getNodeValue();
128                             break;
129                         case 4:
130                             magicnumber=children.item(i).getFirstChild().getNodeValue();
131                             break;
132                     }//switch
133
}//end if
134
}//for
135

136             return new ClassMap(clazzname, desc, Boolean.valueOf(preload).booleanValue(), Integer.valueOf(magicnumber).intValue());
137         }
138         catch(Exception JavaDoc x) {
139             if(x instanceof java.io.IOException JavaDoc)
140                 throw (java.io.IOException JavaDoc)x;
141             else {
142
143                 if(xml_debug) x.printStackTrace();
144                 String JavaDoc error=Util.getStackTrace(x);
145                 if(log.isErrorEnabled()) log.error(error);
146                 throw new java.io.IOException JavaDoc(x.getMessage());
147             }//end if
148
}//catch
149
}
150
151
152 }
153
Popular Tags