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: }
|